utf8_at Function

public pure function utf8_at(utf8, idx) result(s)

return the code point at specified position

Arguments

TypeIntentOptionalAttributesName
class(utf8_string), intent(in) :: utf8
integer, intent(in) :: idx

Return Value character(kind=c_char,len=:),allocatable


Contents

Source Code


Source Code

    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