Parse a string into tokens. Each character in set is a token delimiter.
tokens is allocated with the lower bound equal to one and the upper bound equal to the number of tokens in string,
and with character length equal to the length of the longest token. It contains the tokens in string.
separator is allocated with the lower bound equal to one and the upper bound equal to one less than the number of
tokens in string, and with character length equal to one. It contains the token delimiters in string.
This subroutine implements the tokenize intrinsic procedure as defined in the Fortran 2023 language standard
(Section 16.9.210). We implement it ourselves because the compiler support may take years to become widespread.
(KCW, 2025-10-29)
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | string | |||
| character(len=*), | intent(in) | :: | set | |||
| character(len=:), | intent(out), | allocatable | :: | tokens(:) | ||
| character(len=:), | intent(out), | optional, | allocatable | :: | separator(:) |
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| integer, | private, | allocatable | :: | first(:) | |||
| integer, | private | :: | i | ||||
| integer, | private, | allocatable | :: | last(:) | |||
| integer, | private | :: | n |
pure subroutine tokenize_into_tokens_separator(string, set, tokens, separator) character(*), intent(in) :: string, set character(:), allocatable, intent(out) :: tokens(:) character(:), allocatable, optional, intent(out) :: separator(:) integer, allocatable :: first(:), last(:) integer :: i, n call tokenize(string, set, first, last) n = size(first) allocate(character(maxval(last - first) + 1) :: tokens(n)) do i = 1, n tokens(i) = string(first(i):last(i)) end do if (present(separator)) then allocate(character(1) :: separator(n - 1)) do i = 1, n - 1 separator(i) = string(last(i) + 1:last(i) + 1) end do end if end subroutine tokenize_into_tokens_separator