pos
If the char is invalid UTF-8, pos
is returned as-is
assert "abc".items.find_beginning_of_char_at(2) == 2
assert "か".items.find_beginning_of_char_at(1) == 0
assert [0x41, 233].to_s.items.find_beginning_of_char_at(1) == 1
# Returns the beginning position of the char at position `pos`
#
# If the char is invalid UTF-8, `pos` is returned as-is
#
# ~~~raw
# assert "abc".items.find_beginning_of_char_at(2) == 2
# assert "か".items.find_beginning_of_char_at(1) == 0
# assert [0x41, 233].to_s.items.find_beginning_of_char_at(1) == 1
# ~~~
fun find_beginning_of_char_at(pos: Int): Int do
var endpos = pos
var c = self[pos]
if c & 0x80 == 0x00 then return pos
while c & 0xC0 == 0x80 do
pos -= 1
c = self[pos]
end
var stpos = pos
if length_of_char_at(stpos) >= (endpos - stpos + 1) then return pos
return endpos
end
lib/core/text/native.nit:261,2--281,4