tokenize Subroutine

public pure subroutine tokenize(strin, delimiter, toks, Nt)

Tokenize a string in order to parse it.

Note

The dummy array containing tokens must allocatable and its character elements must have the same length of the input string. If the length of the delimiter is higher than the input string one then the output tokens array is allocated with only one element set to input string.

Arguments

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

String to be tokenized.

character(len=*), intent(in) :: delimiter

Delimiter of tokens.

character(len=len), intent(out), allocatable :: toks(:)

Tokens.

integer(kind=I4P), intent(out), optional :: Nt

Number of tokens.


Called by

proc~~tokenize~~CalledByGraph proc~tokenize flap_utils_m::tokenize proc~check_choices flap_command_line_argument_t::command_line_argument%check_choices proc~check_choices->proc~tokenize proc~get_args_from_string flap_command_line_interface_t::command_line_interface%get_args_from_string proc~get_args_from_string->proc~tokenize proc~get_cla_list flap_command_line_argument_t::command_line_argument%get_cla_list proc~get_cla_list->proc~tokenize proc~get_cla_list_from_buffer flap_command_line_argument_t::command_line_argument%get_cla_list_from_buffer proc~get_cla_list->proc~get_cla_list_from_buffer proc~get_cla_list_from_buffer->proc~tokenize proc~get_cla_list_from_buffer->proc~check_choices proc~get_cla_list_varying_char flap_command_line_argument_t::command_line_argument%get_cla_list_varying_char proc~get_cla_list_varying_char->proc~tokenize proc~get_cla_list_varying_i1p flap_command_line_argument_t::command_line_argument%get_cla_list_varying_I1P proc~get_cla_list_varying_i1p->proc~tokenize proc~get_cla_list_varying_i2p flap_command_line_argument_t::command_line_argument%get_cla_list_varying_I2P proc~get_cla_list_varying_i2p->proc~tokenize proc~get_cla_list_varying_i4p flap_command_line_argument_t::command_line_argument%get_cla_list_varying_I4P proc~get_cla_list_varying_i4p->proc~tokenize proc~get_cla_list_varying_i8p flap_command_line_argument_t::command_line_argument%get_cla_list_varying_I8P proc~get_cla_list_varying_i8p->proc~tokenize proc~get_cla_list_varying_logical flap_command_line_argument_t::command_line_argument%get_cla_list_varying_logical proc~get_cla_list_varying_logical->proc~tokenize proc~get_cla_list_varying_r16p flap_command_line_argument_t::command_line_argument%get_cla_list_varying_R16P proc~get_cla_list_varying_r16p->proc~tokenize proc~get_cla_list_varying_r4p flap_command_line_argument_t::command_line_argument%get_cla_list_varying_R4P proc~get_cla_list_varying_r4p->proc~tokenize proc~get_cla_list_varying_r8p flap_command_line_argument_t::command_line_argument%get_cla_list_varying_R8P proc~get_cla_list_varying_r8p->proc~tokenize none~get flap_command_line_argument_t::command_line_argument%get none~get->proc~get_cla_list proc~get_cla flap_command_line_argument_t::command_line_argument%get_cla none~get->proc~get_cla none~get_args flap_command_line_interface_t::command_line_interface%get_args none~get_args->proc~get_args_from_string none~get_varying flap_command_line_argument_t::command_line_argument%get_varying none~get_varying->proc~get_cla_list_varying_char none~get_varying->proc~get_cla_list_varying_i1p none~get_varying->proc~get_cla_list_varying_i2p none~get_varying->proc~get_cla_list_varying_i4p none~get_varying->proc~get_cla_list_varying_i8p none~get_varying->proc~get_cla_list_varying_logical none~get_varying->proc~get_cla_list_varying_r4p none~get_varying->proc~get_cla_list_varying_r8p proc~get_cla->proc~check_choices proc~get_cla_list_varying_char~2 flap_command_line_interface_t::command_line_interface%get_cla_list_varying_char proc~get_cla_list_varying_char~2->none~get_varying proc~parse~2 flap_command_line_interface_t::command_line_interface%parse proc~get_cla_list_varying_char~2->proc~parse~2 proc~get_cla_list_varying_i1p~2 flap_command_line_interface_t::command_line_interface%get_cla_list_varying_I1P proc~get_cla_list_varying_i1p~2->none~get_varying proc~get_cla_list_varying_i1p~2->proc~parse~2 proc~get_cla_list_varying_i2p~2 flap_command_line_interface_t::command_line_interface%get_cla_list_varying_I2P proc~get_cla_list_varying_i2p~2->none~get_varying proc~get_cla_list_varying_i2p~2->proc~parse~2 proc~get_cla_list_varying_i4p~2 flap_command_line_interface_t::command_line_interface%get_cla_list_varying_I4P proc~get_cla_list_varying_i4p~2->none~get_varying proc~get_cla_list_varying_i4p~2->proc~parse~2 proc~get_cla_list_varying_i8p~2 flap_command_line_interface_t::command_line_interface%get_cla_list_varying_I8P proc~get_cla_list_varying_i8p~2->none~get_varying proc~get_cla_list_varying_i8p~2->proc~parse~2 proc~get_cla_list_varying_logical~2 flap_command_line_interface_t::command_line_interface%get_cla_list_varying_logical proc~get_cla_list_varying_logical~2->none~get_varying proc~get_cla_list_varying_logical~2->proc~parse~2 proc~get_cla_list_varying_r16p~2 flap_command_line_interface_t::command_line_interface%get_cla_list_varying_R16P proc~get_cla_list_varying_r16p~2->none~get_varying proc~get_cla_list_varying_r16p~2->proc~parse~2 proc~get_cla_list_varying_r4p~2 flap_command_line_interface_t::command_line_interface%get_cla_list_varying_R4P proc~get_cla_list_varying_r4p~2->none~get_varying proc~get_cla_list_varying_r4p~2->proc~parse~2 proc~get_cla_list_varying_r8p~2 flap_command_line_interface_t::command_line_interface%get_cla_list_varying_R8P proc~get_cla_list_varying_r8p~2->none~get_varying proc~get_cla_list_varying_r8p~2->proc~parse~2 proc~get_cla_list~2 flap_command_line_interface_t::command_line_interface%get_cla_list proc~get_cla_list~2->none~get proc~get_cla_list~2->proc~parse~2 proc~get_cla~2 flap_command_line_interface_t::command_line_interface%get_cla proc~get_cla~2->none~get proc~get_cla~2->proc~parse~2 proc~parse~2->none~get_args none~get_varying~2 flap_command_line_interface_t::command_line_interface%get_varying none~get_varying~2->proc~get_cla_list_varying_char~2 none~get_varying~2->proc~get_cla_list_varying_i1p~2 none~get_varying~2->proc~get_cla_list_varying_i2p~2 none~get_varying~2->proc~get_cla_list_varying_i4p~2 none~get_varying~2->proc~get_cla_list_varying_i8p~2 none~get_varying~2->proc~get_cla_list_varying_logical~2 none~get_varying~2->proc~get_cla_list_varying_r4p~2 none~get_varying~2->proc~get_cla_list_varying_r8p~2 none~get~2 flap_command_line_interface_t::command_line_interface%get none~get~2->proc~get_cla_list~2 none~get~2->proc~get_cla~2 proc~fake_call flap_test_group::fake_call proc~fake_call->proc~parse~2 proc~fake_call->none~get~2 program~flap_save_bash_completion flap_save_bash_completion program~flap_save_bash_completion->proc~parse~2 program~flap_test_basic flap_test_basic program~flap_test_basic->proc~parse~2 program~flap_test_basic->none~get_varying~2 program~flap_test_basic->none~get~2 program~flap_test_choices_logical flap_test_choices_logical program~flap_test_choices_logical->proc~parse~2 program~flap_test_choices_logical->none~get~2 program~flap_test_nested flap_test_nested program~flap_test_nested->proc~parse~2 program~flap_test_nested->none~get~2 program~flap_test_string flap_test_string program~flap_test_string->proc~parse~2 program~flap_test_string->none~get~2 program~flap_save_man_page flap_save_man_page program~flap_save_man_page->none~get~2 program~flap_save_usage_to_markdown flap_save_usage_to_markdown program~flap_save_usage_to_markdown->none~get~2 program~flap_test_action_store flap_test_action_store program~flap_test_action_store->none~get_varying~2 program~flap_test_action_store->none~get~2 program~flap_test_ansi_color_style flap_test_ansi_color_style program~flap_test_ansi_color_style->none~get~2 program~flap_test_duplicated_clas flap_test_duplicated_clas program~flap_test_duplicated_clas->none~get~2 program~flap_test_group flap_test_group program~flap_test_group->proc~fake_call program~flap_test_group_examples flap_test_group_examples program~flap_test_group_examples->none~get~2 program~flap_test_hidden flap_test_hidden program~flap_test_hidden->none~get~2 program~flap_test_ignore_unknown_clas flap_test_ignore_unknown_clas program~flap_test_ignore_unknown_clas->none~get~2 program~flap_test_minimal flap_test_minimal program~flap_test_minimal->none~get~2 program~flap_test_nargs_insufficient flap_test_nargs_insufficient program~flap_test_nargs_insufficient->none~get~2 program~flap_test_nargs_insufficient~2 flap_test_nargs_insufficient program~flap_test_nargs_insufficient~2->none~get~2

Source Code

  pure subroutine tokenize(strin, delimiter, toks, Nt)
  !< Tokenize a string in order to parse it.
  !<
  !< @note The dummy array containing tokens must allocatable and its character elements must have the same length of the input
  !< string. If the length of the delimiter is higher than the input string one then the output tokens array is allocated with
  !< only one element set to input string.
  character(len=*),          intent(in)               :: strin     !< String to be tokenized.
  character(len=*),          intent(in)               :: delimiter !< Delimiter of tokens.
  character(len=len(strin)), intent(out), allocatable :: toks(:)   !< Tokens.
  integer(I4P),              intent(out), optional    :: Nt        !< Number of tokens.
  character(len=len(strin))                           :: strsub    !< Temporary string.
  integer(I4P)                                        :: dlen      !< Delimiter length.
  integer(I4P)                                        :: c         !< Counter.
  integer(I4P)                                        :: n         !< Counter.
  integer(I4P)                                        :: t         !< Counter.

  ! initialization
  if (allocated(toks)) deallocate(toks)
  strsub = strin
  dlen = len(delimiter)
  if (dlen>len(strin)) then
    allocate(toks(1:1)) ; toks(1) = strin ; if (present(Nt)) Nt = 1 ; return
  endif
  ! compute the number of tokens
  n = 1
  do c=1,len(strsub)-dlen ! loop over string characters
    if (strsub(c:c+dlen-1)==delimiter) n = n + 1
  enddo
  allocate(toks(1:n))
  ! tokenization
  do t=1,n ! loop over tokens
    c = index(strsub, delimiter)
    if (c>0) then
      toks(t) = strsub(1:c-1)
      strsub = strsub(c+dlen:)
    else
      toks(t) = strsub
    endif
  enddo
  if (present(Nt)) Nt = n
  endsubroutine tokenize