Skip to content

Instantly share code, notes, and snippets.

@a2ikm
Created January 18, 2019 12:44
Show Gist options
  • Save a2ikm/b11b31aec5ddb840681a1ec8380f9a19 to your computer and use it in GitHub Desktop.
Save a2ikm/b11b31aec5ddb840681a1ec8380f9a19 to your computer and use it in GitHub Desktop.
Convert byte index to char index in a String.
class String
# byte単位で計算されたインデックスを文字単位に変換する。
# 文字の途中や、範囲外の値が渡されたらnilを返す。
#
def byteindex2charindex(byteindex)
return 0 if byteindex == 0
cur = 0
codepoints.each.with_index(1) do |codepoint, index|
cur += codepoint.chr(Encoding::UTF_8).bytesize
return index if cur == byteindex
end
return nil
end
end
str = "こんにちは赤ちゃん"
assert_equal 0, str.byteindex2charindex("".bytesize)
assert_equal str.index("ち"), str.byteindex2charindex("こんに".bytesize)
assert_equal str.index("赤"), str.byteindex2charindex("こんにちは".bytesize)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment