This function determines if a given character is the first byte of a UTF-8 multibyte character. It takes a 1-byte character as input and returns a logical value indicating if it is the first byte of an UTF-8 binary string.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=1), | intent(in) | :: | chara |
pure function is_first_byte_of_character(chara) result(res) use, intrinsic :: iso_fortran_env implicit none character(1), intent(in) :: chara ! Input single byte character logical :: res ! Result indicating if it is the first byte of a multibyte character. integer(int8) :: byte, shift_6 ! Integer representation of the character and shifted value. ! Convert the character to its integer representation byte = int(ichar(chara), kind(byte)) ! Initialize the result to `.true.` (assume it is the first byte). res = .true. ! Shift the byte 6 bits to the right. shift_6 = ishft(byte, -6) ! If the shifted value equals 2 (10_2), it is a continuation byte, not the first byte. if (shift_6 == 2) res = .false. end function is_first_byte_of_character