Source Edit

This module contains helpers for parsing tokens, numbers, integers, floats, identifiers, etc.

To unpack raw bytes look at the streams module.

  1. let logs = @["2019-01-10: OK_", "2019-01-11: FAIL_", "2019-01: aaaa"]
  2. var outp: seq[string]
  3. for log in logs:
  4. var res: string
  5. if parseUntil(log, res, ':') == 10: # YYYY-MM-DD == 10
  6. outp.add(res & " - " & captureBetween(log, ' ', '_'))
  7. doAssert outp == @["2019-01-10 - OK", "2019-01-11 - FAIL"]
  1. from std/strutils import Digits, parseInt
  2. let
  3. input1 = "2019 school start"
  4. input2 = "3 years back"
  5. startYear = input1[0 .. skipWhile(input1, Digits)-1] # 2019
  6. yearsBack = input2[0 .. skipWhile(input2, Digits)-1] # 3
  7. examYear = parseInt(startYear) + parseInt(yearsBack)
  8. doAssert "Examination is in " & $examYear == "Examination is in 2022"

See also:

Types

  1. InterpolatedKind = enum
  2. ikStr, ## ``str`` part of the interpolated string
  3. ikDollar, ## escaped ``$`` part of the interpolated string
  4. ikVar, ## ``var`` part of the interpolated string
  5. ikExpr ## ``expr`` part of the interpolated string

Describes for interpolatedFragments which part of the interpolated string is yielded; for example in “str$$$var${expr}” Source Edit

Procs

  1. proc captureBetween(s: openArray[char]; first: char; second = '\x00'): string {.
  2. ...raises: [], tags: [], forbids: [].}

Finds the first occurrence of first, then returns everything from there up to second (if second is ‘0’, then first is used).

Example:

  1. doAssert captureBetween("Hello World", 'e') == "llo World"
  2. doAssert captureBetween("Hello World", 'e', 'r') == "llo Wo"
  3. doAssert captureBetween("Hello World".toOpenArray(6, "Hello World".high), 'l') == "d"

Source Edit

  1. proc captureBetween(s: string; first: char; second = '\x00'; start = 0): string {.
  2. ...raises: [], tags: [], forbids: [].}

Finds the first occurrence of first, then returns everything from there up to second (if second is ‘0’, then first is used).

Example:

  1. doAssert captureBetween("Hello World", 'e') == "llo World"
  2. doAssert captureBetween("Hello World", 'e', 'r') == "llo Wo"
  3. doAssert captureBetween("Hello World", 'l', start = 6) == "d"

Source Edit

  1. proc parseBiggestFloat(s: openArray[char]; number: var BiggestFloat): int {.
  2. magic: "ParseBiggestFloat", importc: "nimParseBiggestFloat", noSideEffect,
  3. ...raises: [], tags: [], forbids: [].}

Parses a float and stores the value into number. Result is the number of processed chars or 0 if a parsing error occurred. Source Edit

  1. proc parseBiggestFloat(s: string; number: var BiggestFloat; start = 0): int {.
  2. noSideEffect, ...raises: [], tags: [], forbids: [].}

Parses a float starting at start and stores the value into number. Result is the number of processed chars or 0 if a parsing error occurred. Source Edit

  1. proc parseBiggestInt(s: openArray[char]; number: var BiggestInt): int {....gcsafe,
  2. extern: "npuParseBiggestInt", noSideEffect, ...raises: [ValueError], tags: [],
  3. forbids: [].}

Parses an integer and stores the value into number. Result is the number of processed chars or 0 if there is no integer. ValueError is raised if the parsed integer is out of the valid range.

Example:

  1. var res: BiggestInt
  2. doAssert parseBiggestInt("9223372036854775807", res) == 19
  3. doAssert res == 9223372036854775807

Source Edit

  1. proc parseBiggestInt(s: string; number: var BiggestInt; start = 0): int {.
  2. noSideEffect, ...raises: [ValueError], tags: [], forbids: [].}

Parses an integer starting at start and stores the value into number. Result is the number of processed chars or 0 if there is no integer. ValueError is raised if the parsed integer is out of the valid range.

Example:

  1. var res: BiggestInt
  2. doAssert parseBiggestInt("9223372036854775807", res, 0) == 19
  3. doAssert res == 9223372036854775807

Source Edit

  1. proc parseBiggestUInt(s: openArray[char]; number: var BiggestUInt): int {.
  2. ...gcsafe, extern: "npuParseBiggestUInt", noSideEffect, ...raises: [ValueError],
  3. tags: [], forbids: [].}

Parses an unsigned integer and stores the value into number. ValueError is raised if the parsed integer is out of the valid range.

Example:

  1. var res: BiggestUInt
  2. doAssert parseBiggestUInt("12", res, 0) == 2
  3. doAssert res == 12
  4. doAssert parseBiggestUInt("1111111111111111111", res, 0) == 19
  5. doAssert res == 1111111111111111111'u64

Source Edit

  1. proc parseBiggestUInt(s: string; number: var BiggestUInt; start = 0): int {.
  2. noSideEffect, ...raises: [ValueError], tags: [], forbids: [].}

Parses an unsigned integer starting at start and stores the value into number. ValueError is raised if the parsed integer is out of the valid range.

Example:

  1. var res: BiggestUInt
  2. doAssert parseBiggestUInt("12", res, 0) == 2
  3. doAssert res == 12
  4. doAssert parseBiggestUInt("1111111111111111111", res, 0) == 19
  5. doAssert res == 1111111111111111111'u64

Source Edit

  1. proc parseBin[T: SomeInteger](s: openArray[char]; number: var T; maxLen = 0): int {.
  2. noSideEffect.}

Parses a binary number and stores its value in number.

Returns the number of the parsed characters or 0 in case of an error. If error, the value of number is not changed.

If maxLen == 0, the parsing continues until the first non-bin character or to the end of the string. Otherwise, no more than maxLen characters are parsed starting from the start position.

It does not check for overflow. If the value represented by the string is too big to fit into number, only the value of last fitting characters will be stored in number without producing an error.

Example:

  1. var num: int
  2. doAssert parseBin("0100_1110_0110_1001_1110_1101", num) == 29
  3. doAssert num == 5138925
  4. doAssert parseBin("3", num) == 0
  5. var num8: int8
  6. doAssert parseBin("0b_0100_1110_0110_1001_1110_1101", num8) == 32
  7. doAssert num8 == 0b1110_1101'i8
  8. doAssert parseBin("0b_0100_1110_0110_1001_1110_1101", num8, 3, 9) == 9
  9. doAssert num8 == 0b0100_1110'i8
  10. var num8u: uint8
  11. doAssert parseBin("0b_0100_1110_0110_1001_1110_1101", num8u) == 32
  12. doAssert num8u == 237
  13. var num64: int64
  14. doAssert parseBin("0100111001101001111011010100111001101001", num64) == 40
  15. doAssert num64 == 336784608873

Source Edit

  1. proc parseBin[T: SomeInteger](s: string; number: var T; start = 0; maxLen = 0): int {.
  2. noSideEffect.}

Parses a binary number and stores its value in number.

Returns the number of the parsed characters or 0 in case of an error. If error, the value of number is not changed.

If maxLen == 0, the parsing continues until the first non-bin character or to the end of the string. Otherwise, no more than maxLen characters are parsed starting from the start position.

It does not check for overflow. If the value represented by the string is too big to fit into number, only the value of last fitting characters will be stored in number without producing an error.

Example:

  1. var num: int
  2. doAssert parseBin("0100_1110_0110_1001_1110_1101", num) == 29
  3. doAssert num == 5138925
  4. doAssert parseBin("3", num) == 0
  5. var num8: int8
  6. doAssert parseBin("0b_0100_1110_0110_1001_1110_1101", num8) == 32
  7. doAssert num8 == 0b1110_1101'i8
  8. doAssert parseBin("0b_0100_1110_0110_1001_1110_1101", num8, 3, 9) == 9
  9. doAssert num8 == 0b0100_1110'i8
  10. var num8u: uint8
  11. doAssert parseBin("0b_0100_1110_0110_1001_1110_1101", num8u) == 32
  12. doAssert num8u == 237
  13. var num64: int64
  14. doAssert parseBin("0100111001101001111011010100111001101001", num64) == 40
  15. doAssert num64 == 336784608873

Source Edit

  1. proc parseChar(s: openArray[char]; c: var char): int {....raises: [], tags: [],
  2. forbids: [].}

Parses a single character, stores it in c and returns 1. In case of error (if start >= s.len) it returns 0 and the value of c is unchanged.

Example:

  1. var c: char
  2. doAssert "nim".parseChar(c, 3) == 0
  3. doAssert c == '\0'
  4. doAssert "nim".parseChar(c, 0) == 1
  5. doAssert c == 'n'

Source Edit

  1. proc parseChar(s: string; c: var char; start = 0): int {....raises: [], tags: [],
  2. forbids: [].}

Parses a single character, stores it in c and returns 1. In case of error (if start >= s.len) it returns 0 and the value of c is unchanged.

Example:

  1. var c: char
  2. doAssert "nim".parseChar(c, 3) == 0
  3. doAssert c == '\0'
  4. doAssert "nim".parseChar(c, 0) == 1
  5. doAssert c == 'n'

Source Edit

  1. proc parseFloat(s: openArray[char]; number: var float): int {....gcsafe,
  2. extern: "npuParseFloat", noSideEffect, ...raises: [], tags: [], forbids: [].}

Parses a float and stores the value into number. Result is the number of processed chars or 0 if there occurred a parsing error.

Example:

  1. var res: float
  2. doAssert parseFloat("32", res, 0) == 2
  3. doAssert res == 32.0
  4. doAssert parseFloat("32.57", res, 0) == 5
  5. doAssert res == 32.57
  6. doAssert parseFloat("32.57", res, 3) == 2
  7. doAssert res == 57.00

Source Edit

  1. proc parseFloat(s: string; number: var float; start = 0): int {.noSideEffect,
  2. ...raises: [], tags: [], forbids: [].}

Parses a float starting at start and stores the value into number. Result is the number of processed chars or 0 if there occurred a parsing error.

Example:

  1. var res: float
  2. doAssert parseFloat("32", res, 0) == 2
  3. doAssert res == 32.0
  4. doAssert parseFloat("32.57", res, 0) == 5
  5. doAssert res == 32.57
  6. doAssert parseFloat("32.57", res, 3) == 2
  7. doAssert res == 57.00

Source Edit

  1. proc parseHex[T: SomeInteger](s: openArray[char]; number: var T; maxLen = 0): int {.
  2. noSideEffect.}

Parses a hexadecimal number and stores its value in number.

Returns the number of the parsed characters or 0 in case of an error. If error, the value of number is not changed.

If maxLen == 0, the parsing continues until the first non-hex character or to the end of the string. Otherwise, no more than maxLen characters are parsed starting from the start position.

It does not check for overflow. If the value represented by the string is too big to fit into number, only the value of last fitting characters will be stored in number without producing an error.

Example:

  1. var num: int
  2. doAssert parseHex("4E_69_ED", num) == 8
  3. doAssert num == 5138925
  4. doAssert parseHex("X", num) == 0
  5. doAssert parseHex("#ABC", num) == 4
  6. var num8: int8
  7. doAssert parseHex("0x_4E_69_ED", num8) == 11
  8. doAssert num8 == 0xED'i8
  9. doAssert parseHex("0x_4E_69_ED", num8, 3, 2) == 2
  10. doAssert num8 == 0x4E'i8
  11. var num8u: uint8
  12. doAssert parseHex("0x_4E_69_ED", num8u) == 11
  13. doAssert num8u == 237
  14. var num64: int64
  15. doAssert parseHex("4E69ED4E69ED", num64) == 12
  16. doAssert num64 == 86216859871725

Source Edit

  1. proc parseHex[T: SomeInteger](s: string; number: var T; start = 0; maxLen = 0): int {.
  2. noSideEffect.}

Parses a hexadecimal number and stores its value in number.

Returns the number of the parsed characters or 0 in case of an error. If error, the value of number is not changed.

If maxLen == 0, the parsing continues until the first non-hex character or to the end of the string. Otherwise, no more than maxLen characters are parsed starting from the start position.

It does not check for overflow. If the value represented by the string is too big to fit into number, only the value of last fitting characters will be stored in number without producing an error.

Example:

  1. var num: int
  2. doAssert parseHex("4E_69_ED", num) == 8
  3. doAssert num == 5138925
  4. doAssert parseHex("X", num) == 0
  5. doAssert parseHex("#ABC", num) == 4
  6. var num8: int8
  7. doAssert parseHex("0x_4E_69_ED", num8) == 11
  8. doAssert num8 == 0xED'i8
  9. doAssert parseHex("0x_4E_69_ED", num8, 3, 2) == 2
  10. doAssert num8 == 0x4E'i8
  11. var num8u: uint8
  12. doAssert parseHex("0x_4E_69_ED", num8u) == 11
  13. doAssert num8u == 237
  14. var num64: int64
  15. doAssert parseHex("4E69ED4E69ED", num64) == 12
  16. doAssert num64 == 86216859871725

Source Edit

  1. proc parseIdent(s: openArray[char]): string {....raises: [], tags: [], forbids: [].}

Parses an identifier and returns it or an empty string in case of an error.

Example:

  1. doAssert parseIdent("Hello World", 0) == "Hello"
  2. doAssert parseIdent("Hello World", 1) == "ello"
  3. doAssert parseIdent("Hello World", 5) == ""
  4. doAssert parseIdent("Hello World", 6) == "World"

Source Edit

  1. proc parseIdent(s: openArray[char]; ident: var string): int {....raises: [],
  2. tags: [], forbids: [].}

Parses an identifier and stores it in ident. Returns the number of the parsed characters or 0 in case of an error. If error, the value of ident is not changed.

Example:

  1. var res: string
  2. doAssert parseIdent("Hello World", res, 0) == 5
  3. doAssert res == "Hello"
  4. doAssert parseIdent("Hello World", res, 1) == 4
  5. doAssert res == "ello"
  6. doAssert parseIdent("Hello World", res, 6) == 5
  7. doAssert res == "World"

Source Edit

  1. proc parseIdent(s: string; ident: var string; start = 0): int {....raises: [],
  2. tags: [], forbids: [].}

Parses an identifier and stores it in ident. Returns the number of the parsed characters or 0 in case of an error. If error, the value of ident is not changed.

Example:

  1. var res: string
  2. doAssert parseIdent("Hello World", res, 0) == 5
  3. doAssert res == "Hello"
  4. doAssert parseIdent("Hello World", res, 1) == 4
  5. doAssert res == "ello"
  6. doAssert parseIdent("Hello World", res, 6) == 5
  7. doAssert res == "World"

Source Edit

  1. proc parseIdent(s: string; start = 0): string {....raises: [], tags: [],
  2. forbids: [].}

Parses an identifier and returns it or an empty string in case of an error.

Example:

  1. doAssert parseIdent("Hello World", 0) == "Hello"
  2. doAssert parseIdent("Hello World", 1) == "ello"
  3. doAssert parseIdent("Hello World", 5) == ""
  4. doAssert parseIdent("Hello World", 6) == "World"

Source Edit

  1. proc parseInt(s: openArray[char]; number: var int): int {....gcsafe,
  2. extern: "npuParseInt", noSideEffect, ...raises: [ValueError], tags: [],
  3. forbids: [].}

Parses an integer and stores the value into number. Result is the number of processed chars or 0 if there is no integer. ValueError is raised if the parsed integer is out of the valid range.

Example:

  1. var res: int
  2. doAssert parseInt("2019", res, 0) == 4
  3. doAssert res == 2019
  4. doAssert parseInt("2019", res, 2) == 2
  5. doAssert res == 19

Source Edit

  1. proc parseInt(s: string; number: var int; start = 0): int {.noSideEffect,
  2. ...raises: [ValueError], tags: [], forbids: [].}

Parses an integer starting at start and stores the value into number. Result is the number of processed chars or 0 if there is no integer. ValueError is raised if the parsed integer is out of the valid range.

Example:

  1. var res: int
  2. doAssert parseInt("2019", res, 0) == 4
  3. doAssert res == 2019
  4. doAssert parseInt("2019", res, 2) == 2
  5. doAssert res == 19

Source Edit

  1. proc parseOct[T: SomeInteger](s: openArray[char]; number: var T; maxLen = 0): int {.
  2. noSideEffect.}

Parses an octal number and stores its value in number.

Returns the number of the parsed characters or 0 in case of an error. If error, the value of number is not changed.

If maxLen == 0, the parsing continues until the first non-oct character or to the end of the string. Otherwise, no more than maxLen characters are parsed starting from the start position.

It does not check for overflow. If the value represented by the string is too big to fit into number, only the value of last fitting characters will be stored in number without producing an error.

Example:

  1. var num: int
  2. doAssert parseOct("0o23464755", num) == 10
  3. doAssert num == 5138925
  4. doAssert parseOct("8", num) == 0
  5. var num8: int8
  6. doAssert parseOct("0o_1464_755", num8) == 11
  7. doAssert num8 == -19
  8. doAssert parseOct("0o_1464_755", num8, 3, 3) == 3
  9. doAssert num8 == 102
  10. var num8u: uint8
  11. doAssert parseOct("1464755", num8u) == 7
  12. doAssert num8u == 237
  13. var num64: int64
  14. doAssert parseOct("2346475523464755", num64) == 16
  15. doAssert num64 == 86216859871725

Source Edit

  1. proc parseOct[T: SomeInteger](s: string; number: var T; start = 0; maxLen = 0): int {.
  2. noSideEffect.}

Parses an octal number and stores its value in number.

Returns the number of the parsed characters or 0 in case of an error. If error, the value of number is not changed.

If maxLen == 0, the parsing continues until the first non-oct character or to the end of the string. Otherwise, no more than maxLen characters are parsed starting from the start position.

It does not check for overflow. If the value represented by the string is too big to fit into number, only the value of last fitting characters will be stored in number without producing an error.

Example:

  1. var num: int
  2. doAssert parseOct("0o23464755", num) == 10
  3. doAssert num == 5138925
  4. doAssert parseOct("8", num) == 0
  5. var num8: int8
  6. doAssert parseOct("0o_1464_755", num8) == 11
  7. doAssert num8 == -19
  8. doAssert parseOct("0o_1464_755", num8, 3, 3) == 3
  9. doAssert num8 == 102
  10. var num8u: uint8
  11. doAssert parseOct("1464755", num8u) == 7
  12. doAssert num8u == 237
  13. var num64: int64
  14. doAssert parseOct("2346475523464755", num64) == 16
  15. doAssert num64 == 86216859871725

Source Edit

  1. proc parseSaturatedNatural(s: openArray[char]; b: var int): int {....raises: [],
  2. tags: [], forbids: [].}

Parses a natural number into b. This cannot raise an overflow error. high(int) is returned for an overflow. The number of processed character is returned. This is usually what you really want to use instead of parseInt.

Example:

  1. var res = 0
  2. discard parseSaturatedNatural("848", res)
  3. doAssert res == 848

Source Edit

  1. proc parseSaturatedNatural(s: string; b: var int; start = 0): int {....raises: [],
  2. tags: [], forbids: [].}

Parses a natural number into b. This cannot raise an overflow error. high(int) is returned for an overflow. The number of processed character is returned. This is usually what you really want to use instead of parseInt.

Example:

  1. var res = 0
  2. discard parseSaturatedNatural("848", res)
  3. doAssert res == 848

Source Edit

  1. func parseSize(s: openArray[char]; size: var int64; alwaysBin = false): int {.
  2. ...raises: [], tags: [], forbids: [].}

Parse a size qualified by binary or metric units into size. This format is often called “human readable”. Result is the number of processed chars or 0 on parse errors and size is rounded to the nearest integer. Trailing garbage like “/s” in “1k/s” is allowed and detected by result < s.len.

To simplify use, following non-rare wild conventions, and since fractional data like milli-bytes is so rare, unit matching is case-insensitive but for the ‘i’ distinguishing binary-metric from metric (which cannot be ‘I’).

An optional trailing ‘B|b’ is ignored but processed. I.e., you must still know if units are bytes | bits or infer this fact via the case of s^1 (if users can even be relied upon to use ‘B’ for byte and ‘b’ for bit or have that be s^1).

If alwaysBin==true then scales are always binary-metric, but e.g. “KiB” is still accepted for clarity. If the value would exceed the range of int64, size saturates to int64.high. Supported metric prefix chars include k, m, g, t, p, e, z, y (but z & y saturate unless the number is a small fraction).

See also:

Example:

  1. var res: int64 # caller must still know if 'b' refers to bytes|bits
  2. doAssert parseSize("10.5 MB", res) == 7
  3. doAssert res == 10_500_000 # decimal metric Mega prefix
  4. doAssert parseSize("64 mib", res) == 6
  5. doAssert res == 67108864 # 64 shl 20
  6. doAssert parseSize("1G/h", res, true) == 2 # '/' stops parse
  7. doAssert res == 1073741824 # 1 shl 30, forced binary metric

Source Edit

  1. proc parseUInt(s: openArray[char]; number: var uint): int {....gcsafe,
  2. extern: "npuParseUInt", noSideEffect, ...raises: [ValueError], tags: [],
  3. forbids: [].}

Parses an unsigned integer and stores the value into number. ValueError is raised if the parsed integer is out of the valid range.

Example:

  1. var res: uint
  2. doAssert parseUInt("3450", res) == 4
  3. doAssert res == 3450
  4. doAssert parseUInt("3450", res, 2) == 2
  5. doAssert res == 50

Source Edit

  1. proc parseUInt(s: string; number: var uint; start = 0): int {.noSideEffect,
  2. ...raises: [ValueError], tags: [], forbids: [].}

Parses an unsigned integer starting at start and stores the value into number. ValueError is raised if the parsed integer is out of the valid range.

Example:

  1. var res: uint
  2. doAssert parseUInt("3450", res) == 4
  3. doAssert res == 3450
  4. doAssert parseUInt("3450", res, 2) == 2
  5. doAssert res == 50

Source Edit

  1. proc parseUntil(s: openArray[char]; token: var string; until: char): int {.
  2. inline, ...raises: [], tags: [], forbids: [].}

Parses a token and stores it in token. Returns the number of the parsed characters or 0 in case of an error. A token consists of any character that is not the until character.

Example:

  1. var myToken: string
  2. doAssert parseUntil("Hello World", myToken, 'W') == 6
  3. doAssert myToken == "Hello "
  4. doAssert parseUntil("Hello World", myToken, 'o') == 4
  5. doAssert myToken == "Hell"
  6. doAssert parseUntil("Hello World", myToken, 'o', 2) == 2
  7. doAssert myToken == "ll"

Source Edit

  1. proc parseUntil(s: openArray[char]; token: var string; until: set[char]): int {.
  2. inline, ...raises: [], tags: [], forbids: [].}

Parses a token and stores it in token. Returns the number of the parsed characters or 0 in case of an error. A token consists of the characters notin until.

Example:

  1. var myToken: string
  2. doAssert parseUntil("Hello World", myToken, {'W', 'o', 'r'}) == 4
  3. doAssert myToken == "Hell"
  4. doAssert parseUntil("Hello World", myToken, {'W', 'r'}) == 6
  5. doAssert myToken == "Hello "
  6. doAssert parseUntil("Hello World", myToken, {'W', 'r'}, 3) == 3
  7. doAssert myToken == "lo "

Source Edit

  1. proc parseUntil(s: openArray[char]; token: var string; until: string): int {.
  2. inline, ...raises: [], tags: [], forbids: [].}

Parses a token and stores it in token. Returns the number of the parsed characters or 0 in case of an error. A token consists of any character that comes before the until token.

Example:

  1. var myToken: string
  2. doAssert parseUntil("Hello World", myToken, "Wor") == 6
  3. doAssert myToken == "Hello "
  4. doAssert parseUntil("Hello World", myToken, "Wor", 2) == 4
  5. doAssert myToken == "llo "

Source Edit

  1. proc parseUntil(s: string; token: var string; until: char; start = 0): int {.
  2. inline, ...raises: [], tags: [], forbids: [].}

Parses a token and stores it in token. Returns the number of the parsed characters or 0 in case of an error. A token consists of any character that is not the until character.

Example:

  1. var myToken: string
  2. doAssert parseUntil("Hello World", myToken, 'W') == 6
  3. doAssert myToken == "Hello "
  4. doAssert parseUntil("Hello World", myToken, 'o') == 4
  5. doAssert myToken == "Hell"
  6. doAssert parseUntil("Hello World", myToken, 'o', 2) == 2
  7. doAssert myToken == "ll"

Source Edit

  1. proc parseUntil(s: string; token: var string; until: set[char]; start = 0): int {.
  2. inline, ...raises: [], tags: [], forbids: [].}

Parses a token and stores it in token. Returns the number of the parsed characters or 0 in case of an error. A token consists of the characters notin until.

Example:

  1. var myToken: string
  2. doAssert parseUntil("Hello World", myToken, {'W', 'o', 'r'}) == 4
  3. doAssert myToken == "Hell"
  4. doAssert parseUntil("Hello World", myToken, {'W', 'r'}) == 6
  5. doAssert myToken == "Hello "
  6. doAssert parseUntil("Hello World", myToken, {'W', 'r'}, 3) == 3
  7. doAssert myToken == "lo "

Source Edit

  1. proc parseUntil(s: string; token: var string; until: string; start = 0): int {.
  2. inline, ...raises: [], tags: [], forbids: [].}

Parses a token and stores it in token. Returns the number of the parsed characters or 0 in case of an error. A token consists of any character that comes before the until token.

Example:

  1. var myToken: string
  2. doAssert parseUntil("Hello World", myToken, "Wor") == 6
  3. doAssert myToken == "Hello "
  4. doAssert parseUntil("Hello World", myToken, "Wor", 2) == 4
  5. doAssert myToken == "llo "

Source Edit

  1. proc parseWhile(s: openArray[char]; token: var string; validChars: set[char]): int {.
  2. inline, ...raises: [], tags: [], forbids: [].}

Parses a token and stores it in token. Returns the number of the parsed characters or 0 in case of an error. A token consists of the characters in validChars.

Example:

  1. var myToken: string
  2. doAssert parseWhile("Hello World", myToken, {'W', 'o', 'r'}, 0) == 0
  3. doAssert myToken.len() == 0
  4. doAssert parseWhile("Hello World", myToken, {'W', 'o', 'r'}, 6) == 3
  5. doAssert myToken == "Wor"

Source Edit

  1. proc parseWhile(s: string; token: var string; validChars: set[char]; start = 0): int {.
  2. inline, ...raises: [], tags: [], forbids: [].}

Parses a token and stores it in token. Returns the number of the parsed characters or 0 in case of an error. A token consists of the characters in validChars.

Example:

  1. var myToken: string
  2. doAssert parseWhile("Hello World", myToken, {'W', 'o', 'r'}, 0) == 0
  3. doAssert myToken.len() == 0
  4. doAssert parseWhile("Hello World", myToken, {'W', 'o', 'r'}, 6) == 3
  5. doAssert myToken == "Wor"

Source Edit

  1. proc skip(s, token: openArray[char]): int {.inline, ...raises: [], tags: [],
  2. forbids: [].}

Skips the token starting at s[start]. Returns the length of token or 0 if there was no token at s[start].

Example:

  1. doAssert skip("2019-01-22", "2019", 0) == 4
  2. doAssert skip("2019-01-22", "19", 0) == 0
  3. doAssert skip("2019-01-22", "19", 2) == 2
  4. doAssert skip("CAPlow", "CAP", 0) == 3
  5. doAssert skip("CAPlow", "cap", 0) == 0

Source Edit

  1. proc skip(s, token: string; start = 0): int {.inline, ...raises: [], tags: [],
  2. forbids: [].}

Skips the token starting at s[start]. Returns the length of token or 0 if there was no token at s[start].

Example:

  1. doAssert skip("2019-01-22", "2019", 0) == 4
  2. doAssert skip("2019-01-22", "19", 0) == 0
  3. doAssert skip("2019-01-22", "19", 2) == 2
  4. doAssert skip("CAPlow", "CAP", 0) == 3
  5. doAssert skip("CAPlow", "cap", 0) == 0

Source Edit

  1. proc skipIgnoreCase(s, token: openArray[char]): int {....raises: [], tags: [],
  2. forbids: [].}

Same as skip but case is ignored for token matching.

Example:

  1. doAssert skipIgnoreCase("CAPlow", "CAP", 0) == 3
  2. doAssert skipIgnoreCase("CAPlow", "cap", 0) == 3

Source Edit

  1. proc skipIgnoreCase(s, token: string; start = 0): int {....raises: [], tags: [],
  2. forbids: [].}

Same as skip but case is ignored for token matching.

Example:

  1. doAssert skipIgnoreCase("CAPlow", "CAP", 0) == 3
  2. doAssert skipIgnoreCase("CAPlow", "cap", 0) == 3

Source Edit

  1. proc skipUntil(s: openArray[char]; until: char): int {.inline, ...raises: [],
  2. tags: [], forbids: [].}

Skips all characters until the char until is found or the end is reached. Returns number of characters skipped.

Example:

  1. doAssert skipUntil("Hello World", 'o', 0) == 4
  2. doAssert skipUntil("Hello World", 'o', 4) == 0
  3. doAssert skipUntil("Hello World", 'W', 0) == 6
  4. doAssert skipUntil("Hello World", 'w', 0) == 11

Source Edit

  1. proc skipUntil(s: openArray[char]; until: set[char]): int {.inline, ...raises: [],
  2. tags: [], forbids: [].}

Skips all characters until one char from the set until is found or the end is reached. Returns number of characters skipped.

Example:

  1. doAssert skipUntil("Hello World", {'W', 'e'}, 0) == 1
  2. doAssert skipUntil("Hello World", {'W'}, 0) == 6
  3. doAssert skipUntil("Hello World", {'W', 'd'}, 0) == 6

Source Edit

  1. proc skipUntil(s: string; until: char; start = 0): int {.inline, ...raises: [],
  2. tags: [], forbids: [].}

Skips all characters until the char until is found or the end is reached. Returns number of characters skipped.

Example:

  1. doAssert skipUntil("Hello World", 'o', 0) == 4
  2. doAssert skipUntil("Hello World", 'o', 4) == 0
  3. doAssert skipUntil("Hello World", 'W', 0) == 6
  4. doAssert skipUntil("Hello World", 'w', 0) == 11

Source Edit

  1. proc skipUntil(s: string; until: set[char]; start = 0): int {.inline,
  2. ...raises: [], tags: [], forbids: [].}

Skips all characters until one char from the set until is found or the end is reached. Returns number of characters skipped.

Example:

  1. doAssert skipUntil("Hello World", {'W', 'e'}, 0) == 1
  2. doAssert skipUntil("Hello World", {'W'}, 0) == 6
  3. doAssert skipUntil("Hello World", {'W', 'd'}, 0) == 6

Source Edit

  1. proc skipWhile(s: openArray[char]; toSkip: set[char]): int {.inline, ...raises: [],
  2. tags: [], forbids: [].}

Skips all characters while one char from the set toSkip is found. Returns number of characters skipped.

Example:

  1. doAssert skipWhile("Hello World", {'H', 'e'}) == 2
  2. doAssert skipWhile("Hello World", {'e'}) == 0
  3. doAssert skipWhile("Hello World", {'W', 'o', 'r'}, 6) == 3

Source Edit

  1. proc skipWhile(s: string; toSkip: set[char]; start = 0): int {.inline,
  2. ...raises: [], tags: [], forbids: [].}

Skips all characters while one char from the set toSkip is found. Returns number of characters skipped.

Example:

  1. doAssert skipWhile("Hello World", {'H', 'e'}) == 2
  2. doAssert skipWhile("Hello World", {'e'}) == 0
  3. doAssert skipWhile("Hello World", {'W', 'o', 'r'}, 6) == 3

Source Edit

  1. proc skipWhitespace(s: openArray[char]): int {.inline, ...raises: [], tags: [],
  2. forbids: [].}

Skips the whitespace starting at s[start]. Returns the number of skipped characters.

Example:

  1. doAssert skipWhitespace("Hello World", 0) == 0
  2. doAssert skipWhitespace(" Hello World", 0) == 1
  3. doAssert skipWhitespace("Hello World", 5) == 1
  4. doAssert skipWhitespace("Hello World", 5) == 2

Source Edit

  1. proc skipWhitespace(s: string; start = 0): int {.inline, ...raises: [], tags: [],
  2. forbids: [].}

Skips the whitespace starting at s[start]. Returns the number of skipped characters.

Example:

  1. doAssert skipWhitespace("Hello World", 0) == 0
  2. doAssert skipWhitespace(" Hello World", 0) == 1
  3. doAssert skipWhitespace("Hello World", 5) == 1
  4. doAssert skipWhitespace("Hello World", 5) == 2

Source Edit

Iterators

  1. iterator interpolatedFragments(s: openArray[char]): tuple[
  2. kind: InterpolatedKind, value: string] {....raises: [ValueError], tags: [],
  3. forbids: [].}

Tokenizes the string s into substrings for interpolation purposes.

Example:

  1. var outp: seq[tuple[kind: InterpolatedKind, value: string]]
  2. for k, v in interpolatedFragments(" $this is ${an example} $$"):
  3. outp.add (k, v)
  4. doAssert outp == @[(ikStr, " "),
  5. (ikVar, "this"),
  6. (ikStr, " is "),
  7. (ikExpr, "an example"),
  8. (ikStr, " "),
  9. (ikDollar, "$")]

Source Edit

  1. iterator interpolatedFragments(s: string): tuple[kind: InterpolatedKind,
  2. value: string] {....raises: [ValueError], tags: [], forbids: [].}

Tokenizes the string s into substrings for interpolation purposes.

Example:

  1. var outp: seq[tuple[kind: InterpolatedKind, value: string]]
  2. for k, v in interpolatedFragments(" $this is ${an example} $$"):
  3. outp.add (k, v)
  4. doAssert outp == @[(ikStr, " "),
  5. (ikVar, "this"),
  6. (ikStr, " is "),
  7. (ikExpr, "an example"),
  8. (ikStr, " "),
  9. (ikDollar, "$")]

Source Edit