return a substring of utf8_string
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(utf8_string), | intent(in) | :: | utf8 | |||
integer, | intent(in) | :: | begin | |||
integer, | intent(in) | :: | end |
pure function utf8_slice(utf8, begin, end) result(slice)
class(utf8_string), intent(in) :: utf8
integer, intent(in) :: begin
integer, intent(in) :: end
character(len=:, kind=c_char), allocatable :: slice
integer :: bi, bj ! byte index for begin (i) and end (j)
integer :: ci, cj ! codepoint index for begin (i) and end (j)
integer :: n
if (begin > end .or. begin > len(utf8%str) .or. end < 1) then
allocate (character(len=0, kind=c_char) :: slice); return
end if
bi = 1; ci = 1
if (begin > 1) then
do
if (ci == begin) exit
if (bi > len(utf8%str)) then
allocate (character(len=0, kind=c_char) :: slice); return
end if
n = codepoint_num_bytes(cast_byte(utf8%str(bi:bi)))
bi = bi + n
ci = ci + 1
end do
end if
bj = bi; cj = ci
do
if (bj > len(utf8%str)) then
allocate (slice, source=utf8%str(bi:)); return
end if
n = codepoint_num_bytes(cast_byte(utf8%str(bi:bi)))
if (cj == end) then
allocate (slice, source=utf8%str(bi:bj + n - 1)); return
end if
bj = bj + n
cj = cj + 1
end do
end function utf8_slice