utf8_reverse Subroutine

public subroutine utf8_reverse(utf8)

reverse the order of code points in place

Arguments

TypeIntentOptionalAttributesName
class(utf8_string), intent(inout) :: utf8

Contents

Source Code


Source Code

    subroutine utf8_reverse(utf8)
        class(utf8_string), intent(inout) :: utf8
        character(len=:, kind=c_char), allocatable :: tmp
        integer :: i, j, l, n

        l = len(utf8%str)
        call move_alloc(from=utf8%str, to=tmp)
        allocate (character(len=l, kind=c_char) :: utf8%str)

        i = 1; j = l
        do
            if (i > l) exit
            n = codepoint_num_bytes(cast_byte(tmp(i:i)))
            utf8%str(j - n + 1:j) = tmp(i:i + n - 1)
            i = i + n
            j = j - n
        end do

    end subroutine utf8_reverse