Module strutils

This module contains various string utility routines. See the module re for regular expression support. See the module pegs for PEG support. This module is available for the JavaScript target.

Types

TCharSet = set[char]
  Source
FloatFormatMode = enum 
  ffDefault,                  ## use the shorter floating point notation
  ffDecimal,                  ## use decimal floating point notation
  ffScientific                ## use scientific notation (using ``e`` character)
the different modes of floating point formating   Source

Consts

Whitespace = {' ', '\x09', '\x0B', '\x0D', '\x0A', '\x0C'}
All the characters that count as whitespace.   Source
Letters = {'A'..'Z', 'a'..'z'}
the set of letters   Source
Digits = {'0'..'9'}
the set of digits   Source
HexDigits = {'0'..'9', 'A'..'F', 'a'..'f'}
the set of hexadecimal digits   Source
IdentChars = {'a'..'z', 'A'..'Z', '0'..'9', '_'}
the set of characters an identifier can consist of   Source
IdentStartChars = {'a'..'z', 'A'..'Z', '_'}
the set of characters an identifier can start with   Source
NewLines = {'\x0D', '\x0A'}
the set of characters a newline terminator can start with   Source
AllChars = {'\0'..'\xFF'}

A set with all the possible characters.

Not very useful by its own, you can use it to create inverted sets to make the find() proc find invalid characters in strings. Example:

let invalid = AllChars - Digits
doAssert "01234".find(invalid) == -1
doAssert "01A34".find(invalid) == 2
  Source

Procs

proc toLower(c: char): char {.noSideEffect, procvar, gcsafe, 
                              extern: "nsuToLowerChar", raises: [], tags: [].}

Converts c into lower case.

This works only for the letters A-Z. See unicode.toLower for a version that works for any Unicode character.

  Source
proc toLower(s: string): string {.noSideEffect, procvar, gcsafe, 
                                  extern: "nsuToLowerStr", raises: [], tags: [].}

Converts s into lower case.

This works only for the letters A-Z. See unicode.toLower for a version that works for any Unicode character.

  Source
proc toUpper(c: char): char {.noSideEffect, procvar, gcsafe, 
                              extern: "nsuToUpperChar", raises: [], tags: [].}

Converts c into upper case.

This works only for the letters A-Z. See unicode.toUpper for a version that works for any Unicode character.

  Source
proc toUpper(s: string): string {.noSideEffect, procvar, gcsafe, 
                                  extern: "nsuToUpperStr", raises: [], tags: [].}

Converts s into upper case.

This works only for the letters A-Z. See unicode.toUpper for a version that works for any Unicode character.

  Source
proc capitalize(s: string): string {.noSideEffect, procvar, gcsafe, 
                                     extern: "nsuCapitalize", raises: [], 
                                     tags: [].}

Converts the first character of s into upper case.

This works only for the letters A-Z.

  Source
proc normalize(s: string): string {.noSideEffect, procvar, gcsafe, 
                                    extern: "nsuNormalize", raises: [], tags: [].}

Normalizes the string s.

That means to convert it to lower case and remove any '_'. This is needed for Nim identifiers for example.

  Source
proc cmpIgnoreCase(a, b: string): int {.noSideEffect, gcsafe, 
                                        extern: "nsuCmpIgnoreCase", procvar, 
                                        raises: [], tags: [].}
Compares two strings in a case insensitive manner. Returns:

0 iff a == b
< 0 iff a < b
> 0 iff a > b

  Source
proc cmpIgnoreStyle(a, b: string): int {.noSideEffect, gcsafe, 
    extern: "nsuCmpIgnoreStyle", procvar, raises: [], tags: [].}
Compares two strings normalized (i.e. case and underscores do not matter). Returns:

0 iff a == b
< 0 iff a < b
> 0 iff a > b

  Source
proc strip(s: string; leading = true; trailing = true): string {.noSideEffect, 
    gcsafe, extern: "nsuStrip", raises: [], tags: [].}

Strips whitespace from s and returns the resulting string.

If leading is true, leading whitespace is stripped. If trailing is true, trailing whitespace is stripped.

  Source
proc toOctal(c: char): string {.noSideEffect, gcsafe, extern: "nsuToOctal", 
                                raises: [], tags: [].}

Converts a character c to its octal representation.

The resulting string may not have a leading zero. Its length is always exactly 3.

  Source
proc splitLines(s: string): seq[string] {.noSideEffect, gcsafe, 
    extern: "nsuSplitLines", raises: [], tags: [].}
The same as the splitLines iterator, but is a proc that returns a sequence of substrings.   Source
proc countLines(s: string): int {.noSideEffect, gcsafe, extern: "nsuCountLines", 
                                  raises: [], tags: [].}

Returns the number of new line separators in the string s.

This is the same as len(splitLines(s)), but much more efficient because it doesn't modify the string creating temporal objects. Every character literal newline combination (CR, LF, CR-LF) is supported.

Despite its name this proc might not actually return the number of lines in s because the concept of what a line is can vary. For example, a string like Hello world is a line of text, but the proc will return a value of zero because there are no newline separators. Also, text editors usually don't count trailing newline characters in a text file as a new empty line, but this proc will.

  Source
proc split(s: string; seps: set[char] = Whitespace): seq[string] {.noSideEffect, 
    gcsafe, extern: "nsuSplitCharSet", raises: [], tags: [].}
The same as the split iterator, but is a proc that returns a sequence of substrings.   Source
proc split(s: string; sep: char): seq[string] {.noSideEffect, gcsafe, 
    extern: "nsuSplitChar", raises: [], tags: [].}
The same as the split iterator, but is a proc that returns a sequence of substrings.   Source
proc split(s: string; sep: string): seq[string] {.noSideEffect, gcsafe, 
    extern: "nsuSplitString", raises: [], tags: [].}

Splits the string s into substrings using a string separator.

Substrings are separated by the string sep. This is a wrapper around the split iterator.

  Source
proc toHex(x: BiggestInt; len: Positive): string {.noSideEffect, gcsafe, 
    extern: "nsuToHex", raises: [], tags: [].}

Converts x to its hexadecimal representation.

The resulting string will be exactly len characters long. No prefix like 0x is generated. x is treated as an unsigned value.

  Source
proc intToStr(x: int; minchars: Positive = 1): string {.noSideEffect, 
    gcsafe, extern: "nsuIntToStr", raises: [], tags: [].}

Converts x to its decimal representation.

The resulting string will be minimally minchars characters long. This is achieved by adding leading zeros.

  Source
proc parseInt(s: string): int {.noSideEffect, procvar, gcsafe, 
                                extern: "nsuParseInt", 
                                raises: [OverflowError, ValueError], tags: [].}

Parses a decimal integer value contained in s.

If s is not a valid integer, ValueError is raised.

  Source
proc parseBiggestInt(s: string): BiggestInt {.noSideEffect, procvar, gcsafe, 
    extern: "nsuParseBiggestInt", raises: [ValueError], tags: [].}

Parses a decimal integer value contained in s.

If s is not a valid integer, ValueError is raised.

  Source
proc parseFloat(s: string): float {.noSideEffect, procvar, gcsafe, 
                                    extern: "nsuParseFloat", 
                                    raises: [ValueError], tags: [].}
Parses a decimal floating point value contained in s. If s is not a valid floating point number, ValueError is raised. NAN, INF, -INF are also supported (case insensitive comparison).   Source
proc parseHexInt(s: string): int {.noSideEffect, procvar, gcsafe, 
                                   extern: "nsuParseHexInt", 
                                   raises: [ValueError], tags: [].}

Parses a hexadecimal integer value contained in s.

If s is not a valid integer, ValueError is raised. s can have one of the following optional prefixes: 0x, 0X, #. Underscores within s are ignored.

  Source
proc parseBool(s: string): bool {.raises: [ValueError], tags: [].}

Parses a value into a bool.

If s is one of the following values: y, yes, true, 1, on, then returns true. If s is one of the following values: n, no, false, 0, off, then returns false. If s is something else a ValueError exception is raised.

  Source
proc parseEnum[T](s: string): T

Parses an enum T.

Raises ValueError for an invalid value in s. The comparison is done in a style insensitive way.

  Source
proc parseEnum[T](s: string; default: T): T

Parses an enum T.

Uses default for an invalid value in s. The comparison is done in a style insensitive way.

  Source
proc repeat(c: char; count: Natural): string {.noSideEffect, gcsafe, 
    extern: "nsuRepeatChar", raises: [], tags: [].}
Returns a string of length count consisting only of the character c. You can use this proc to left align strings. Example:
proc tabexpand(indent: int, text: string, tabsize: int = 4) =
  echo '\t'.repeat(indent div tabsize), ' '.repeat(indent mod tabsize), text

tabexpand(4, "At four")
tabexpand(5, "At five")
tabexpand(6, "At six")
  Source
proc repeat(s: string; n: Natural): string {.noSideEffect, gcsafe, 
    extern: "nsuRepeatStr", raises: [], tags: [].}
Returns String s concatenated n times. Example:
echo "+++ STOP ".repeat(4), "+++"
  Source
proc repeatChar(count: Natural; c: char = ' '): string {.deprecated, raises: [], 
    tags: [].}
deprecated: use repeat() or spaces()   Source
proc repeatStr(count: Natural; s: string): string {.deprecated, raises: [], 
    tags: [].}
deprecated: use repeat(string, count) or string.repeat(count)   Source
proc align(s: string; count: Natural; padding = ' '): string {.noSideEffect, 
    gcsafe, extern: "nsuAlignString", raises: [], tags: [].}

Aligns a string s with padding, so that it is of length count.

padding characters (by default spaces) are added before s resulting in right alignment. If s.len >= count, no spaces are added and s is returned unchanged. If you need to left align a string use the repeatChar proc. Example:

assert align("abc", 4) == " abc"
assert align("a", 0) == "a"
assert align("1232", 6) == "  1232"
assert align("1232", 6, '#') == "##1232"
  Source
proc wordWrap(s: string; maxLineWidth = 80; splitLongWords = true; 
              seps: set[char] = Whitespace; newLine = "\x0A"): string {.
    noSideEffect, gcsafe, extern: "nsuWordWrap", raises: [], tags: [].}
Word wraps s.   Source
proc unindent(s: string; eatAllIndent = false): string {.noSideEffect, 
    gcsafe, extern: "nsuUnindent", raises: [], tags: [].}
Unindents s.   Source
proc startsWith(s, prefix: string): bool {.noSideEffect, gcsafe, 
    extern: "nsuStartsWith", raises: [], tags: [].}

Returns true iff s starts with prefix.

If prefix == "" true is returned.

  Source
proc endsWith(s, suffix: string): bool {.noSideEffect, gcsafe, 
    extern: "nsuEndsWith", raises: [], tags: [].}

Returns true iff s ends with suffix.

If suffix == "" true is returned.

  Source
proc continuesWith(s, substr: string; start: Natural): bool {.noSideEffect, 
    gcsafe, extern: "nsuContinuesWith", raises: [], tags: [].}

Returns true iff s continues with substr at position start.

If substr == "" true is returned.

  Source
proc addSep(dest: var string; sep = ", "; startLen: Natural = 0) {.noSideEffect, 
    inline, raises: [], tags: [].}

Adds a separator to dest only if its length is bigger than startLen.

A shorthand for:

if dest.len > startLen: add(dest, sep)

This is often useful for generating some code where the items need to be separated by sep. sep is only added if dest is longer than startLen. The following example creates a string describing an array of integers:

var arr = "["
for x in items([2, 3, 5, 7, 11]):
  addSep(arr, startLen=len("["))
  add(arr, $x)
add(arr, "]")
  Source
proc allCharsInSet(s: string; theSet: set[char]): bool {.raises: [], tags: [].}
Returns true iff each character of s is in the set theSet.   Source
proc abbrev(s: string; possibilities: openArray[string]): int {.raises: [], 
    tags: [].}

Returns the index of the first item in possibilities if not ambiguous.

Returns -1 if no item has been found and -2 if multiple items match.

  Source
proc join(a: openArray[string]; sep: string): string {.noSideEffect, gcsafe, 
    extern: "nsuJoinSep", raises: [], tags: [].}
Concatenates all strings in a separating them with sep.   Source
proc join(a: openArray[string]): string {.noSideEffect, gcsafe, 
    extern: "nsuJoin", raises: [], tags: [].}
Concatenates all strings in a.   Source
proc find(s, sub: string; start: Natural = 0): int {.noSideEffect, gcsafe, 
    extern: "nsuFindStr", raises: [], tags: [].}

Searches for sub in s starting at position start.

Searching is case-sensitive. If sub is not in s, -1 is returned.

  Source
proc find(s: string; sub: char; start: Natural = 0): int {.noSideEffect, 
    gcsafe, extern: "nsuFindChar", raises: [], tags: [].}

Searches for sub in s starting at position start.

Searching is case-sensitive. If sub is not in s, -1 is returned.

  Source
proc find(s: string; chars: set[char]; start: Natural = 0): int {.noSideEffect, 
    gcsafe, extern: "nsuFindCharSet", raises: [], tags: [].}

Searches for chars in s starting at position start.

If s contains none of the characters in chars, -1 is returned.

  Source
proc rfind(s, sub: string; start: int = - 1): int {.noSideEffect, raises: [], 
    tags: [].}

Searches for sub in s in reverse, starting at start and going backwards to 0.

Searching is case-sensitive. If sub is not in s, -1 is returned.

  Source
proc rfind(s: string; sub: char; start: int = - 1): int {.noSideEffect, 
    gcsafe, raises: [], tags: [].}

Searches for sub in s in reverse starting at position start.

Searching is case-sensitive. If sub is not in s, -1 is returned.

  Source
proc count(s: string; sub: string; overlapping: bool = false): int {.
    noSideEffect, gcsafe, extern: "nsuCountString", raises: [], tags: [].}
Count the occurrences of a substring sub in the string s. Overlapping occurrences of sub only count when overlapping is set to true.   Source
proc count(s: string; sub: char): int {.noSideEffect, gcsafe, 
                                        extern: "nsuCountChar", raises: [], 
                                        tags: [].}
Count the occurrences of the character sub in the string s.   Source
proc count(s: string; subs: set[char]): int {.noSideEffect, gcsafe, 
    extern: "nsuCountCharSet", raises: [], tags: [].}
Count the occurrences of the group of character subs in the string s.   Source
proc quoteIfContainsWhite(s: string): string {.deprecated, raises: [], tags: [].}

Returns '"' & s & '"' if s contains a space and does not start with a quote, else returns s.

DEPRECATED as it was confused for shell quoting function. For this application use osproc.quoteShell.

  Source
proc contains(s: string; c: char): bool {.noSideEffect, raises: [], tags: [].}
Same as find(s, c) >= 0.   Source
proc contains(s, sub: string): bool {.noSideEffect, raises: [], tags: [].}
Same as find(s, sub) >= 0.   Source
proc contains(s: string; chars: set[char]): bool {.noSideEffect, raises: [], 
    tags: [].}
Same as find(s, chars) >= 0.   Source
proc replace(s, sub: string; by = ""): string {.noSideEffect, gcsafe, 
    extern: "nsuReplaceStr", raises: [], tags: [].}
Replaces sub in s by the string by.   Source
proc replace(s: string; sub, by: char): string {.noSideEffect, gcsafe, 
    extern: "nsuReplaceChar", raises: [], tags: [].}

Replaces sub in s by the character by.

Optimized version of replace for characters.

  Source
proc replaceWord(s, sub: string; by = ""): string {.noSideEffect, gcsafe, 
    extern: "nsuReplaceWord", raises: [], tags: [].}

Replaces sub in s by the string by.

Each occurrence of sub has to be surrounded by word boundaries (comparable to \\w in regular expressions), otherwise it is not replaced.

  Source
proc delete(s: var string; first, last: int) {.noSideEffect, gcsafe, 
    extern: "nsuDelete", raises: [], tags: [].}

Deletes in s the characters at position first .. last.

This modifies s itself, it does not return a copy.

  Source
proc parseOctInt(s: string): int {.noSideEffect, gcsafe, 
                                   extern: "nsuParseOctInt", 
                                   raises: [ValueError], tags: [].}

Parses an octal integer value contained in s.

If s is not a valid integer, ValueError is raised. s can have one of the following optional prefixes: 0o, 0O. Underscores within s are ignored.

  Source
proc toOct(x: BiggestInt; len: Positive): string {.noSideEffect, gcsafe, 
    extern: "nsuToOct", raises: [], tags: [].}

Converts x into its octal representation.

The resulting string is always len characters long. No leading 0o prefix is generated.

  Source
proc toBin(x: BiggestInt; len: Positive): string {.noSideEffect, gcsafe, 
    extern: "nsuToBin", raises: [], tags: [].}

Converts x into its binary representation.

The resulting string is always len characters long. No leading 0b prefix is generated.

  Source
proc insertSep(s: string; sep = '_'; digits = 3): string {.noSideEffect, 
    gcsafe, extern: "nsuInsertSep", raises: [], tags: [].}

Inserts the separator sep after digits digits from right to left.

Even though the algorithm works with any string s, it is only useful if s contains a number. Example: insertSep("1000000") == "1_000_000"

  Source
proc escape(s: string; prefix = "\""; suffix = "\""): string {.noSideEffect, 
    gcsafe, extern: "nsuEscape", raises: [], tags: [].}

Escapes a string s.

This does these operations (at the same time):

  • replaces any \ by \\
  • replaces any ' by \'
  • replaces any " by \"
  • replaces any other character in the set {'\0'..'\31', '\128'..'\255'} by \xHH where HH is its hexadecimal value.

The procedure has been designed so that its output is usable for many different common syntaxes. The resulting string is prefixed with prefix and suffixed with suffix. Both may be empty strings.

  Source
proc unescape(s: string; prefix = "\""; suffix = "\""): string {.noSideEffect, 
    gcsafe, extern: "nsuUnescape", raises: [ValueError], tags: [].}

Unescapes a string s.

This complements escape as it performs the opposite operations.

If s does not begin with prefix and end with suffix a ValueError exception will be raised.

  Source
proc validIdentifier(s: string): bool {.noSideEffect, gcsafe, 
                                        extern: "nsuValidIdentifier", 
                                        raises: [], tags: [].}

Returns true if s is a valid identifier.

A valid identifier starts with a character of the set IdentStartChars and is followed by any number of characters of the set IdentChars.

  Source
proc editDistance(a, b: string): int {.noSideEffect, gcsafe, 
                                       extern: "nsuEditDistance", raises: [], 
                                       tags: [].}

Returns the edit distance between a and b.

This uses the Levenshtein distance algorithm with only a linear memory overhead. This implementation is highly optimized!

  Source
proc formatBiggestFloat(f: BiggestFloat; format: FloatFormatMode = ffDefault; 
                        precision: range[0 .. 32] = 16): string {.noSideEffect, 
    gcsafe, extern: "nsu$1", raises: [], tags: [].}

Converts a floating point value f to a string.

If format == ffDecimal then precision is the number of digits to be printed after the decimal point. If format == ffScientific then precision is the maximum number of significant digits to be printed. precision's default value is the maximum number of meaningful digits after the decimal point for Nim's biggestFloat type.

If precision == 0, it tries to format it nicely.

  Source
proc formatFloat(f: float; format: FloatFormatMode = ffDefault; 
                 precision: range[0 .. 32] = 16): string {.noSideEffect, 
    gcsafe, extern: "nsu$1", raises: [], tags: [].}

Converts a floating point value f to a string.

If format == ffDecimal then precision is the number of digits to be printed after the decimal point. If format == ffScientific then precision is the maximum number of significant digits to be printed. precision's default value is the maximum number of meaningful digits after the decimal point for Nim's float type.

  Source
proc formatSize(bytes: BiggestInt; decimalSep = '.'): string {.raises: [], 
    tags: [].}
Rounds and formats bytes. Examples:
formatSize(1'i64 shl 31 + 300'i64) == "2.204GB"
formatSize(4096) == "4KB"
  Source
proc addf(s: var string; formatstr: string; a: varargs[string, `$`]) {.
    noSideEffect, gcsafe, extern: "nsuAddf", raises: [ValueError], tags: [].}
The same as add(s, formatstr % a), but more efficient.   Source
proc `%`(formatstr: string; a: openArray[string]): string {.noSideEffect, 
    gcsafe, extern: "nsuFormatOpenArray", raises: [ValueError], tags: [].}

Interpolates a format string with the values from a.

The substitution operator performs string substitutions in formatstr and returns a modified formatstr. This is often called string interpolation.

This is best explained by an example:

"$1 eats $2." % ["The cat", "fish"]

Results in:

"The cat eats fish."

The substitution variables (the thing after the $) are enumerated from 1 to a.len. To produce a verbatim $, use $$. The notation $# can be used to refer to the next substitution variable:

"$# eats $#." % ["The cat", "fish"]

Substitution variables can also be words (that is [A-Za-z_]+[A-Za-z0-9_]*) in which case the arguments in a with even indices are keys and with odd indices are the corresponding values. An example:

"$animal eats $food." % ["animal", "The cat", "food", "fish"]

Results in:

"The cat eats fish."

The variables are compared with cmpIgnoreStyle. ValueError is raised if an ill-formed format string has been passed to the % operator.

  Source
proc `%`(formatstr, a: string): string {.noSideEffect, gcsafe, 
    extern: "nsuFormatSingleElem", raises: [ValueError], tags: [].}
This is the same as formatstr % [a].   Source
proc format(formatstr: string; a: varargs[string, `$`]): string {.noSideEffect, 
    gcsafe, extern: "nsuFormatVarargs", raises: [ValueError], tags: [].}
This is the same as formatstr % a except that it supports auto stringification.   Source

Iterators

iterator split(s: string; seps: set[char] = Whitespace): string {.raises: [], 
    tags: [].}

Splits the string s into substrings using a group of separators.

Substrings are separated by a substring containing only seps. Note that whole sequences of characters found in seps will be counted as a single split point and leading/trailing separators will be ignored. The following example:

for word in split("  this is an  example  "):
  writeln(stdout, word)

...generates this output:

"this"
"is"
"an"
"example"

And the following code:

for word in split(";;this;is;an;;example;;;", {';'}):
  writeln(stdout, word)

...produces the same output as the first example. The code:

let date = "2012-11-20T22:08:08.398990"
let separators = {' ', '-', ':', 'T'}
for number in split(date, separators):
  writeln(stdout, number)

...results in:

"2012"
"11"
"20"
"22"
"08"
"08.398990"
  Source
iterator split(s: string; sep: char): string {.raises: [], tags: [].}

Splits the string s into substrings using a single separator.

Substrings are separated by the character sep. Unlike the version of the iterator which accepts a set of separator characters, this proc will not coalesce groups of the separator, returning a string for each found character. The code:

for word in split(";;this;is;an;;example;;;", ';'):
  writeln(stdout, word)

Results in:

""
""
"this"
"is"
"an"
""
"example"
""
""
""
  Source
iterator split(s: string; sep: string): string {.raises: [], tags: [].}

Splits the string s into substrings using a string separator.

Substrings are separated by the string sep.

  Source
iterator splitLines(s: string): string {.raises: [], tags: [].}

Splits the string s into its containing lines.

Every character literal newline combination (CR, LF, CR-LF) is supported. The result strings contain no trailing \n.

Example:

for line in splitLines("\nthis\nis\nan\n\nexample\n"):
  writeln(stdout, line)

Results in:

""
"this"
"is"
"an"
""
"example"
""
  Source
iterator tokenize(s: string; seps: set[char] = Whitespace): tuple[token: string, 
    isSep: bool] {.raises: [], tags: [].}

Tokenizes the string s into substrings.

Substrings are separated by a substring containing only seps. Examples:

for word in tokenize("  this is an  example  "):
  writeln(stdout, word)

Results in:

("  ", true)
("this", false)
(" ", true)
("is", false)
(" ", true)
("an", false)
("  ", true)
("example", false)
("  ", true)
  Source

Templates

template spaces(n: Natural): string
Returns a String with n space characters. You can use this proc to left align strings. Example:
let
  width = 15
  text1 = "Hello user!"
  text2 = "This is a very long string"
echo text1 & spaces(max(0, width - text1.len)) & "|"
echo text2 & spaces(max(0, width - text2.len)) & "|"
  Source