return the code point at specified position
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(utf8_string), | intent(in) | :: | utf8 | |||
integer, | intent(in) | :: | idx |
pure function utf8_at(utf8, idx) result(s)
class(utf8_string), intent(in) :: utf8
integer, intent(in) :: idx
character(len=:, kind=c_char), allocatable :: s
integer :: i, j, n
if (idx < 1) then
allocate (character(len=0, kind=c_char) :: s); return
end if
i = 1; j = 1
do
if (i > len(utf8%str)) then
allocate (character(len=0, kind=c_char) :: s); return
end if
n = codepoint_num_bytes(cast_byte(utf8%str(i:i)))
if (j == idx) then
allocate (s, source=utf8%str(i:i + n - 1)); return
end if
i = i + n
j = j + 1
end do
end function utf8_at