tokenize_char Function

private pure function tokenize_char(string) result(tokens)

Split a character string into whitespace separated words.

Arguments

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

Return Value type(string_type), allocatable, (:)


Called by

proc~~tokenize_char~~CalledByGraph proc~tokenize_char tokenize_char interface~tokenize tokenize interface~tokenize->proc~tokenize_char proc~tokenize_string tokenize_string interface~tokenize->proc~tokenize_string proc~tokenize_string->proc~tokenize_char

Variables

Type Visibility Attributes Name Initial
integer(kind=default_int), private, allocatable :: first(:)
integer(kind=default_int), private :: i_token
integer(kind=default_int), private, allocatable :: last(:)
integer(kind=default_int), private :: n_chars
integer(kind=default_int), private :: n_tokens
integer(kind=default_int), private :: offset
integer(kind=default_int), private :: pos

Source Code

   pure function tokenize_char(string) result(tokens)
      !! Split a character string into whitespace separated words.
      character(len=*), intent(in) :: string
      type(string_type), allocatable :: tokens(:)

      integer(default_int), allocatable :: first(:), last(:)
      integer(default_int) :: n_chars, n_tokens, pos, offset, i_token

      n_chars = len(string)
      ! A token needs at least one character and one separator, so there can
      ! never be more than (n + 2)/2 of them; that bound also covers n == 0.
      allocate (first((n_chars + 2)/2), last((n_chars + 2)/2))

      n_tokens = 0
      pos = 1
      do
         offset = verify(string(pos:), whitespace)
         if (offset == 0) exit
         n_tokens = n_tokens + 1
         first(n_tokens) = pos + offset - 1
         offset = scan(string(first(n_tokens):), whitespace)
         if (offset == 0) then
            last(n_tokens) = n_chars
         else
            last(n_tokens) = first(n_tokens) + offset - 2
         end if
         pos = last(n_tokens) + 1
      end do

      allocate (tokens(n_tokens))
      do i_token = 1, n_tokens
         tokens(i_token) = string_type(string(first(i_token):last(i_token)))
      end do

   end function tokenize_char