C Library Functions  - find_field (3)

NAME

find_field(3f) - [M_strings:TOKENS] parse a string into tokens (LICENSE:MIT)

CONTENTS

Synopsis
Description
Options
Examples
Author
License
Version

SYNOPSIS

subroutine find_field (string, field, position, delims, delim, found)

    character*(*),intent(in)           :: string
    character*(*),intent(out)          :: field
    integer,optional,intent(inout)     :: position
    character*(*),optional,intent(in)  :: delims
    character*(*),optional,intent(out) :: delim
    logical,optional,intent(out)       :: found

DESCRIPTION

Find a delimited field in a string.

Here’s my equivalent, which I’ve used for nearly 2 decades, as you can see from the date. This doesn’t try to mimic the C strtok (and doesn’t have its limitations either). It is in a much more native Fortran style.

It is a little more complicated than some because it does some things that I regularly find useful. For example, it can tell the caller what trailing delimiter it found. This can be useful, for example, to distinguish between

       somefield, someotherfield

versus

       somefield=somevalue, someotherfield

Also, I have a bit of special handling for blanks. All the usage information is in the argument descriptions. Note that most of the arguments are optional.

       from comp.lang.fortran @ Richard Maine

OPTIONS

STRING The string input.
FIELD The returned field. Blank if no field found.
POSITION
  On entry, the starting position for searching for the field. Default is 1 if the argument is not present. On exit, the starting position of the next field or len(string)+1 if there is no following field.
DELIMS String containing the characters to be accepted as delimiters. If this includes a blank character, then leading blanks are removed from the returned field and the end delimiter may optionally be preceded by blanks. If this argument is not present, the default delimiter set is a blank.
DELIM Returns the actual delimiter that terminated the field. Returns char(0) if the field was terminated by the end of the string or if no field was found. If blank is in delimiters and the field was terminated by one or more blanks, followed by a non-blank delimiter, the non-blank delimiter is returned.
FOUND True if a field was found.

EXAMPLES

Sample of uses

       program demo_find_field
       use M_strings, only : find_field
       implicit none
       character(len=256)           :: string
       character(len=256)           :: field
       integer                      :: position
       character(len=:),allocatable :: delims
       character(len=1)             :: delim
       logical                      :: found

delims=’[,]’ position=1 found=.true. string=’[a,b,[ccc,ddd],and more]’ write(*,’(a)’)trim(string) do call find_field(string,field,position,delims,delim,found=found) if(.not.found)exit write(*,’("<",a,">")’)trim(field) enddo write(*,’(*(g0))’)repeat(’=’,70)

position=1 found=.true. write(*,’(a)’)trim(string) do call find_field(string,field,position,’[], ’,delim,found=found) if(.not.found)exit write(*,’("<",a,">",i0,1x,a)’)trim(field),position,delim enddo write(*,’(*(g0))’)repeat(’=’,70)

end program demo_find_field

‘‘‘ Results: ‘‘‘text > [a,b,[ccc,ddd],and more] > <> > <a> > <b> > <> > <ccc> > <ddd> > <> > <and more> > <> > ====================================================================== > [a,b,[ccc,ddd],and more] > <>2 [ > <a>4 , > <b>6 , > <>7 [ > <ccc>11 , > <ddd>15 ] > <>16 , > <and>20 > <more>257 ] > ======================================================================

AUTHOR

Richard Maine

LICENSE

    MIT

VERSION

version 0.1.0, copyright Nov 15 1990, Richard Maine

Minor editing to conform to inclusion in the string procedure module


Nemo Release 3.1 find_field (3) July 22, 2023
Generated by manServer 1.08 from 78a993ea-bed5-47ef-94bf-0fb7e8289f97 using man macros.