len_utf8 Function

public pure function len_utf8(str) result(count)

This function calculates the length of a UTF-8 string.

It takes a UTF-8 string as input and returns the number of characters in the string.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: str

Return Value integer


Source Code

   pure function len_utf8(str) result(count)
      implicit none
      character(*), intent(in) :: str
      integer :: i, inext, count

      ! Initialize
      i = 1
      count = 0

      ! Loop through the string until the end of the string is reached.
      do while(i <= len(str))
         inext = idxutf8(str, i) + 1   ! Get the index of the next UTF-8 character.
         count = count + 1             ! Increment the character count.
         i = inext                     ! Move to the next character.
      end do
   end function len_utf8