slice(3f) - [M_strings:TOKENS] parse string into an array using specified delimiters (LICENSE:PD)
Synopsis
Description
Options
Examples
Author
License
subroutine slice(input_line,ibegin,iend,delimiters,nulls)
character(len=*),intent(in) :: input_line integer,allocatable,intent(out) :: ibegin(:),iend(:) character(len=*),optional,intent(in) :: delimiters character(len=*),optional,intent(in) :: nulls
slice(3f) parses a string using specified delimiter characters and store token beginning and ending positions into allocatable arrays
INPUT_LINE Input string to tokenize IBEGIN,IEND arrays containing start and end positions of tokens. IEND(I)<IBEGIN(I) designates a null token. DELIMITERS List of delimiter characters. The default delimiters are the "whitespace" characters (space, tab,new line, vertical tab, formfeed, carriage return, and null). You may specify an alternate set of delimiter characters. Multi-character delimiters are not supported (Each character in the DELIMITERS list is considered to be a delimiter).
Quoting of delimiter characters is not supported.
NULLS= IGNORE | RETURN | IGNOREEND Treatment of null fields. By default adjacent delimiters in the input string do not create an empty string in the output array. if NULLS=return adjacent delimiters create an empty element in the output ARRAY. If NULLS=ignoreend then only trailing delimiters at the right of the string are ignored.
Sample program:
program demo_slice use M_strings, only: slice implicit none integer :: i character(len=*),parameter :: & & line= aBcdef ghijklmnop qrstuvwxyz 1:|:2 333|333 a B cc integer,allocatable :: ibegin(:), iend(:) ! output arrays of positions character(len=*),parameter :: title=(80("="),t1,a) write(*,*)INPUT LINE:[//line//] ! write(*,title)typical call: call slice(line,ibegin,iend) call printme() ! write(*,title)custom list of delimiters=":|" : call slice(line,ibegin,iend,delimiters=:|,nulls=ignore) call printme() ! write(*,title)delimiters=":|", and count null fields: call slice(line,ibegin,iend,delimiters=:|,nulls=return) call printme() ! write(*,title)default delimiters and return null fields: call slice(line,ibegin,iend,delimiters=,nulls=return) call printme() contains subroutine printme() write(*,((*(:/,3x,"[",g0,"]"))))& & (line(ibegin(i):iend(i)),i=1,size(ibegin)) write(*,(*(g0,1x)))SIZE:,size(ibegin) end subroutine printme end program demo_sliceResults:
> INPUT LINE: > [ aBcdef ghijklmnop qrstuvwxyz 1:|:2 333|333 a B cc ] > typical call: ======================================================== > > [aBcdef] > [ghijklmnop] > [qrstuvwxyz] > [1:|:2] > [333|333] > [a] > [B] > [cc] > SIZE: 8 > custom list of delimiters=":|" : ===================================== > > [ aBcdef ghijklmnop qrstuvwxyz 1] > [2 333] > [333 a B cc ] > SIZE: 3 > delimiters=":|", and count null fields: ============================== > > [ aBcdef ghijklmnop qrstuvwxyz 1] > [] > [] > [2 333] > [333 a B cc ] > SIZE: 5 > default delimiters and return null fields: =========================== > > [] > [] > [aBcdef] > [] > [] > [ghijklmnop] > [qrstuvwxyz] > [] > [1:|:2] > [] > [] > [] > [] > [333|333] > [a] > [B] > [cc] > [] > [] > [] > SIZE: 20======================================================================
John S. Urban
Public Domain
Nemo Release 3.1 | slice (3m_strings) | January 10, 2025 |