Features
One type, one module
Everything is in the string type, exported by the stringifor module together with the numeric kinds and a few procedures:
fortran
use stringifor
type(string) :: s
s = 'Hello World' ! assign
print '(A)', s%upper()//'' ! call a method, get a character with //Feature map
| Area | Features | Where |
|---|---|---|
| The type | a string of any length; arrays with elements of different lengths; assignment from characters, strings and numbers; // (a character) and .cat. (a string); ==, /=, <, <=, >=, >; defined I/O | Strings and I/O |
| Intrinsics | adjustl, adjustr, count, index, len_trim, repeat, scan, trim, verify accept a string | Strings and I/O |
| Case | upper, lower, swapcase, capitalize, camelcase, snakecase, startcase; is_upper, is_lower | String Manipulation |
| Classes | is_alpha, is_alnum, is_digit, is_xdigit, is_punct, is_space | String Manipulation |
| Cleaning | strip (blanks or a set of characters), replace, unique, insert, escape, unescape, lstrip, rstrip, compact, squeeze, expand_tabs, transliterate, quote, unquote | String Manipulation |
| Tokens | split (empty fields optionally kept), split_chunked, partition, join, strjoin (1D and 2D arrays) | String Manipulation |
| Regular expressions | regex (Pike VM, linear time, Python re results): search, match, fullmatch, finditer, findall, split, sub; re_search and friends | Regular Expressions |
| Sorting | strsort (stable merge sort, permutation), orders strless, strless_nocase, strless_natural (file2 before file10), or your own | String Manipulation |
| Parts | slice with a stride, reverse, reverse_words, start_with, end_with, count, index, scan, verify, search between tags | String Manipulation |
| Layout | fill, center, ljust, rjust, justify, len_last_word | String Manipulation |
| Comparing | common_prefix, compare_version, edit_distance | String Manipulation |
| UTF-8 | is_utf8, utf8_error, codepoints, from_codepoints, to_valid_utf8: units are well-formed sequences, ill-formed bytes are kept; C escapes unescape_c, escape_c, escapes_error; XML escaping escape_xml, unescape_xml, xml_error; Unicode whitespace with unicode=.true. on is_space, strip, lstrip, rstrip, compact; byte order mark has_bom, skip_bom=, bom=; positions and widths in characters with by=SF_CHARS, byte_pos, char_pos; display width width(), by=SF_COLUMNS | Unicode and UTF-8 |
| Numbers | assignment from any integer and real kind; to_number, read_number with an error status, correctly rounded without read; shortest; is_number, is_integer, is_real, is_digit; hex | Numbers |
| Files | read_file, read_lines, read_line, write_file, write_lines, write_line, formatted or unformatted stream | Files and Paths |
| Paths | basedir, basename, extension, glob, match of wildcard patterns, tempname | Files and Paths |
| Encoding and colours | base64 encode and decode; ANSI colorize | Files and Paths |
The methods summary lists every method with its arguments.
Design goals
| Goal | How |
|---|---|
Interchangeable with character | overloaded assignment, //, comparisons and intrinsics: a string goes wherever a character does |
| Low memory consumption | one allocatable character is the only component: each element of an array is as long as its content |
| Safe | almost every method is elemental or pure; the methods return new values and leave the string unchanged |
| Tested | every method carries doctests in its source; every example of these pages is compiled and run |
Module architecture
Use only the stringifor module: it exports the type, the kinds (I1P, I2P, I4P, I8P, R4P, R8P, R16P), the character kind CK, the overloaded intrinsics, and the procedures read_file, read_lines, write_file, write_lines, glob, strjoin, strsort and its orders, the regex type and the re_* procedures.
Compiler support
| Compiler | Minimum version | Status |
|---|---|---|
| GNU gfortran | ≥ 9.2.0 | Full support |
Intel ifort / ifx | ≥ 19.0.4 | Full support |
NVIDIA nvfortran | — | Builds with the _NVF macro defined, which disables the I2P kind; not regularly tested |
| IBM XL | — | Not tested |
| NAG | — | Not tested |
Any feature request is welcome: open an issue on GitHub.