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.
Type | Intent | Optional | 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. |
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