Created
January 18, 2019 12:44
-
-
Save a2ikm/b11b31aec5ddb840681a1ec8380f9a19 to your computer and use it in GitHub Desktop.
Convert byte index to char index in a String.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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