Bercriber's Blog

プログラミング

2025/06/03 22:03


Vimscriptでマルチバイト文字数を数える

前回見事chatGPT先生の嘘を鵜呑みにして間違えたのをアップしてたので訂正。

function! GetTotalUtf8CharCount()
  let total_chars = 0
  let num_lines = line('$')

  for i in range(1, num_lines)
    let line_content = getline(i)
    let total_chars += len(line_content) " ちゃうぞ
  endfor

  return total_chars
endfunction

というコード片を乗せたのだが、len()はマルチバイト対応してなくて、使うべきはstrchars()でした