which_segment_symbol_belong Function

public pure function which_segment_symbol_belong(segments, symbol) result(res)

This function takes an array of segments and a character as arguments, and returns the segment as rank=1 array to which symbol belongs (included in the segment interval).

Arguments

Type IntentOptional Attributes Name
type(segment_t), intent(in) :: segments(:)
character(len=*), intent(in) :: symbol

Return Value type(segment_t)


Source Code

   pure function which_segment_symbol_belong (segments, symbol) result(res)
      use :: forgex_utf8_m
      implicit none
      type(segment_t), intent(in) :: segments(:)
      character(*),    intent(in) :: symbol
      type(segment_t)             :: res

      integer         :: i, i_end, j
      type(segment_t) :: target_for_comparison

      ! If `symbol` is a empty character, return SEG_EMPTY
      if (symbol == '') then
         res = SEG_EMPTY
         return
      end if

      ! Initialize indices.
      i = 1
      i_end = idxutf8(symbol, i)

      ! The target to check for inclusion.
      target_for_comparison = symbol_to_segment(symbol(i:i_end))

      ! Scan the segments array.
      do j = 1, size(segments)
         ! Compare segments and return the later element of the segments, which contains the target segment.
         if (target_for_comparison .in. segments(j)) then
            res = segments(j)
            return
         end if
      end do

      ! If not found, returns SEG_EMPTY.
      res = SEG_EMPTY
   end function which_segment_symbol_belong