M_strings(3f) - [M_strings::INTRO] Fortran string module
Description
Synopsis
See Also
Examples
Author
License
The M_strings(3fm) module is a collection of Fortran procedures that supplement the built-in intrinsic string routines. Routines for parsing, tokenizing, changing case, substituting new strings for substrings, locating strings with simple wildcard expressions, removing tabs and line terminators and other string manipulations are included.M_strings__oop(3fm) is a companion module that provides an OOP interface to the M_strings module.
public entities:
use M_strings,only : split, slice, sep, delim, chomp, strtok use M_strings,only : split2020, find_field use M_strings,only : substitute, change, modif, transliterate, & & reverse, squeeze use M_strings,only : replace, join use M_strings,only : upper, lower, upper_quoted, lower_quoted use M_strings,only : rotate13, percent_encode, percent_decode use M_strings,only : encode_base64, decode_base64 use M_strings,only : adjustc, compact, nospace, indent use M_strings,only : crop, clip, unquote, quote, matching_delimiter use M_strings,only : len_white, pad, lpad, cpad, rpad, zpad, & & stretch, lenset, merge_str use M_strings,only : switch, s2c, c2s use M_strings,only : noesc, notabs, dilate, expand, visible use M_strings,only : longest_common_substring use M_strings,only : string_to_value, string_to_values, s2v, s2vs use M_strings,only : int, real, dble, nint use M_strings,only : atoi, atol, aton use M_strings,only : value_to_string, v2s, str, fmt use M_strings,only : listout, getvals use M_strings,only : glob, ends_with use M_strings,only : paragraph use M_strings,only : base, decodebase, codebase, base2 use M_strings,only : isalnum, isalpha, iscntrl, isdigit use M_strings,only : isgraph, islower, isprint, ispunct use M_strings,only : isspace, isupper, isascii, isblank, isxdigit use M_strings,only : isnumber use M_strings,only : fortran_name use M_strings,only : describe use M_strings,only : edit_distance use M_strings,only : bundle
split subroutine parses string using specified delimiter characters and stores tokens into an array sep function interface to split(3f) slice subroutine parses string using specified delimiter characters and stores beginning and ending positions in arrays delim subroutine parses string using specified delimiter characters and store tokens into an array and records beginning and end chomp function consumes input line as it returns next token in a string using specified delimiters paragraph convert a string into a paragraph strtok tokenize a string like C strtok(3c) routine
split2020 split a string using prototype of proposed standard procedure find_field parse a string into tokens
substitute subroutine non-recursively globally replaces old substring with new substring replace function non-recursively globally replaces old substring with new substring using allocatable string (version of substitute(3f) without limitation on length of output string) change subroutine non-recursively globally replaces old substring with new substring with a directive like line editor modif subroutine modifies a string with a directive like the XEDIT line editor MODIFY command transliterate replace characters found in set one with characters from set two reverse reverse character order in a string join join an array of CHARACTER variables with specified separator rotate13 apply trivial encryption algorithm ROT13 to a string percent_encode apply percent-encryption (aka. URL encryption) to characters percent_decode apply percent-decryption (aka. URL decryption) to characters encode_base64 apply base64 encoding (as defined in RFC-4648) to an array of bytes decode_base64 apply base64 decoding (as defined in RFC-4648) to an array of bytes squeeze delete adjacent duplicate characters from a string
upper function converts string to uppercase lower function converts string to miniscule upper_quoted function converts string to uppercase skipping strings quoted per Fortran rules lower_quoted function converts string to lowercase skipping strings quoted per Fortran rules
len_white find location of last non-whitespace character lenset return a string of specified length pad return a string of at least specified length zpad pad integer or string to length with zero characters on left lpad convert scalar intrinsic to a string padded on left to specified length cpad convert scalar intrinsic to a centered string of the specified length rpad convert scalar intrinsic to a string padded on right to specified length stretch return a string of at least specified length with suffix merge_str make strings of equal length and then call MERGE(3f) intrinsic
See Also: squeeze
adjustc elemental function centers text within the length of the input string compact left justify string and replace duplicate whitespace with single characters or nothing nospace function replaces whitespace with nothing indent find number of leading spaces crop function trims leading and trailing spaces and control characters clip trim leading and trailings spaces or set of characters from string
matching_delimiter find position of matching delimiter unquote remove quotes from string as if read with list-directed input quote add quotes to string as if written with list-directed output
switch switch between a string and an array of single characters s2c convert string to array of single characters and add null terminator for passing to C c2s convert null-terminated array of single characters to string for converting strings returned from C
noesc convert non-printable ASCII8 characters to a space notabs convert tabs to spaces while maintaining columns, assuming tabs are set every 8 characters dilate function to convert tabs to spaces assuming tabs are set every 8 characters expand expand escape sequences in a string visible expand escape sequences in a string to "control" and meta-control representations
string_to_value generic subroutine returns numeric value (REAL, DOUBLEPRECISION, INTEGER) from string string_to_values subroutine reads an array of numbers from a string getvals subroutine reads a relatively arbitrary number of values from a string using list-directed read s2v function returns DOUBLEPRECISION numeric value from string s2vs function returns a DOUBLEPRECISION array of numbers from a string s2vs function returns a DOUBLEPRECISION array of numbers from a string atoi function returns INTEGER(kind=int32) from a string atol function returns INTEGER(kind=int64) from a string aton changes string to numeric value str append the values of up to twenty values into a string, including user-specified separator and a CSV-style option fmt return string from generic intrinsic value using optionally specified format. value_to_string generic subroutine returns string given numeric value (REAL, DOUBLEPRECISION, INTEGER, LOGICAL ) v2s generic function returns string from numeric value (REAL, DOUBLEPRECISION, INTEGER ) listout expand a list of numbers where negative numbers denote range ends (1 -10 means 1 thru 10) isnumber determine if string represents a number
glob compares given string for match to pattern which may contain wildcard characters ends_with test whether strings ends with one of the specified suffixes
o isalnum returns .true. if character is a letter or digit o isalpha returns .true. if character is a letter and [char46]false. otherwise o iscntrl returns .true. if character is a delete character or ordinary control character o isdigit returns .true. if character is a digit (0,1,...,9) and .false. otherwise o isgraph returns .true. if character is a printable character except a space is considered non-printable o islower returns .true. if character is a miniscule letter (a-z) o isprint returns .true. if character is an ASCII printable character o ispunct returns .true. if character is a printable punctuation character o isspace returns .true. if character is a null, space, tab, carriage return, new line, vertical tab, or formfeed o isupper returns .true. if character is an uppercase letter (A-Z) o isascii returns .true. if the character is in the range char(0) to char(127) o isblank returns .true. if character is a blank character (space or horizontal tab. o isxdigit returns .true. if character is a hexadecimal digit (0-9, a-f, or A-F). fortran_name returns .true. if input string is a valid Fortran name
base convert whole number string in base [2-36] to string in alternate base [2-36] base2 convert INTEGER to a string representing a binary value codebase convert whole number string in base [2-36] to base 10 number decodebase convert whole number in base 10 to string in base [2-36]
bundle return up to twenty strings of arbitrary length as an array describe returns a string describing the name of a single character edit_distance returns a naive edit distance using the Levenshtein distance algorithm longest_common_substring function that returns the longest common substring of two strings.
The M_strings(3fm) module supplements and works in combination with the Fortran built-in intrinsics. Stand-alone Fortran lets you access the characters in a string using ranges much like they are character arrays, assignment, comparisons with standard operators, supports dynamically allocatable strings and supports concatenation using the // operator, as well as a number of intrinsic string routines:
adjustl Left adjust a string adjustr Right adjust a string index Position of a substring within a string repeat Repeated string concatenation scan Scan a string for the presence of a set of characters trim Remove trailing blank characters of a string verify Scan a string for the absence of a set of characters len It returns the length of a character string achar converts an integer into a character iachar converts a character into an integer len_trim finds length of string with trailing spaces ignored new_line Newline character selected_char_kind Choose character kind lge Lexical greater than or equal lgt Lexical greater than lle Lexical less than or equal llt Lexical less than
The M_strings__oop(3fm) module (included with the M_strings(3fm) module) provides an OOP (Object-Oriented Programming) interface to the M_strings(3fm) module.
There are additional routines in other GPF modules for working with expressions (M_calculator), time strings (M_time), random strings (M_random, M_uuid), lists (M_list), and interfacing with the C regular expression library (M_regex).
Each of the procedures includes an [example](example/) program in the corresponding man(1) page for the function.Sample program:
program demo_M_strings use M_strings,only : SPLIT, slice, sep, delim, chomp, strtok use M_strings,only : split2020, find_field use M_strings,only : substitute, change, modif, transliterate, & & reverse, squeeze use M_strings,only : REPLACE, join use M_strings,only : UPPER, LOWER, upper_quoted, lower_quoted use M_strings,only : rotate13, percent_encode, percent_decode use M_strings,only : encode_base64, decode_base64 use M_strings,only : adjustc, compact, nospace, indent use M_strings,only : crop, clip, unquote, quote, matching_delimiter use M_strings,only : len_white, pad, lpad, cpad, rpad, zpad, & & stretch, lenset, merge_str use M_strings,only : switch, s2c, c2s use M_strings,only : noesc, notabs, dilate, expand, visible use M_strings,only : longest_common_substring use M_strings,only : string_to_value, string_to_values, s2v, s2vs use M_strings,only : int, real, dble, nint use M_strings,only : atoi, atol, aton use M_strings,only : value_to_string, v2s, str, fmt use M_strings,only : listout, getvals use M_strings,only : glob, ends_with use M_strings,only : paragraph use M_strings,only : base, decodebase, codebase, base2 use M_strings,only : isalnum, isalpha, iscntrl, isdigit use M_strings,only : isgraph, islower, isprint, ispunct use M_strings,only : isspace, isupper, isascii, isblank, isxdigit use M_strings,only : isnumber use M_strings,only : fortran_name use M_strings,only : describe use M_strings,only : edit_distance use M_strings,only : bundle character(len=:),allocatable :: string character(len=:),allocatable :: array(:) ! output array of tokens character(len=*),parameter :: gen=(*(g0)) character(len=*),parameter :: genx=(*("[",g0,"] ":)) string=abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 01234567890 write(*,gen)string write(*,gen)upper(string) write(*,gen)lower(string) call split(string,array) write(*,genx)array write(*,gen)replace(string,qrs,--RePlace--,ignorecase=.true.) end program demo_M_stringsResults:
> abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 01234567890 > ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ 01234567890 > abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz 01234567890 > [abcdefghijklmnopqrstuvwxyz] [ABCDEFGHIJKLMNOPQRSTUVWXYZ] [01234567890 ] > abcdefghijklmnop--RePlace--tuvwxyz ABCDEFGHIJKLMNOP--RePlace--TUVWXYZ 01234567890
John S. Urban
Public Domain
Nemo Release 3.1 | M_strings (3m_strings) | January 10, 2025 |