utf8_index Function

public pure function utf8_index(utf8, substring) result(idx)

return the position where substring occurs in utf8_string for the first time

Arguments

TypeIntentOptionalAttributesName
class(utf8_string), intent(in) :: utf8
character(kind=c_char,len=*), intent(in) :: substring

Return Value integer


Contents

Source Code


Source Code

    pure function utf8_index(utf8, substring) result(idx)
        class(utf8_string), intent(in) :: utf8
        character(len=*, kind=c_char), intent(in) :: substring
        integer :: idx
        integer :: bit, cit
        integer :: nt, ls

        idx = 0
        bit = 1; cit = 1
        ls = len(substring)
        do
            if (bit + ls - 1 > len(utf8%str)) exit
            if (utf8%str(bit:bit + ls - 1) == substring(:)) then
                idx = cit; return
            end if
            nt = codepoint_num_bytes(cast_byte(utf8%str(bit:bit)))
            bit = bit + nt
            cit = cit + 1
        end do

    end function utf8_index