This module contains helpers for parsing tokens, numbers, identifiers, etc.
To unpack raw bytes look at the streams module.
InterpolatedKind = enum ikStr, ## ``str`` part of the interpolated string ikDollar, ## escaped ``$`` part of the interpolated string ikVar, ## ``var`` part of the interpolated string ikExpr ## ``expr`` part of the interpolated string
proc parseHex(s: string; number: var int; start = 0; maxLen = 0): int {...}{.gcsafe, extern: "npuParseHex", noSideEffect, raises: [], tags: [].}
Parses a hexadecimal number and stores its value in number
.
Returns the number of the parsed characters or 0 in case of an error. This proc is sensitive to the already existing value of number
and will likely not do what you want unless you make sure number
is zero. You can use this feature to chain calls, though the result int will quickly overflow. Example:
var value = 0 discard parseHex("0x38", value) assert value == 56 discard parseHex("0x34", value) assert value == 56 * 256 + 52 value = -1 discard parseHex("0x38", value) assert value == -200
If maxLen == 0
the length of the hexadecimal number has no upper bound. Else no more than start + maxLen
characters are parsed, up to the length of the string.
proc parseOct(s: string; number: var int; start = 0; maxLen = 0): int {...}{.gcsafe, extern: "npuParseOct", noSideEffect, raises: [], tags: [].}
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 maxLen == 0
the length of the octal number has no upper bound. Else no more than start + maxLen
characters are parsed, up to the length of the string.
proc parseBin(s: string; number: var int; start = 0; maxLen = 0): int {...}{.gcsafe, extern: "npuParseBin", noSideEffect, raises: [], tags: [].}
Parses an binary number and stores its value in number
. Returns the number of the parsed characters or 0 in case of an error.
If maxLen == 0
the length of the binary number has no upper bound. Else no more than start + maxLen
characters are parsed, up to the length of the string.
proc parseIdent(s: string; ident: var string; start = 0): int {...}{.raises: [], tags: [].}
ident
. Returns the number of the parsed characters or 0 in case of an error. proc parseIdent(s: string; start = 0): string {...}{.raises: [], tags: [].}
ident
. Returns the parsed identifier or an empty string in case of an error. proc parseToken(s: string; token: var string; validChars: set[char]; start = 0): int {...}{. inline, deprecated, raises: [], tags: [].}
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.
Deprecated since version 0.8.12: Use parseWhile
instead.
proc skipWhitespace(s: string; start = 0): int {...}{.inline, raises: [], tags: [].}
s[start]
. Returns the number of skipped characters. proc skip(s, token: string; start = 0): int {...}{.inline, raises: [], tags: [].}
s[start]
. Returns the length of token or 0 if there was no token at s[start]
. proc skipIgnoreCase(s, token: string; start = 0): int {...}{.raises: [], tags: [].}
proc skipUntil(s: string; until: set[char]; start = 0): int {...}{.inline, raises: [], tags: [].}
proc skipUntil(s: string; until: char; start = 0): int {...}{.inline, raises: [], tags: [].}
proc skipWhile(s: string; toSkip: set[char]; start = 0): int {...}{.inline, raises: [], tags: [].}
proc parseUntil(s: string; token: var string; until: set[char]; start = 0): int {...}{.inline, raises: [], tags: [].}
token
. Returns the number of the parsed characters or 0 in case of an error. A token consists of the characters notin until. proc parseUntil(s: string; token: var string; until: char; start = 0): int {...}{.inline, raises: [], tags: [].}
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. proc parseUntil(s: string; token: var string; until: string; start = 0): int {...}{.inline, raises: [], tags: [].}
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. proc parseWhile(s: string; token: var string; validChars: set[char]; start = 0): int {...}{. inline, raises: [], tags: [].}
token
. Returns the number of the parsed characters or 0 in case of an error. A token consists of the characters in validChars. proc captureBetween(s: string; first: char; second = '\x00'; start = 0): string {...}{. raises: [], tags: [].}
first
, then returns everything from there up to second
(if second
is '0', then first
is used). proc parseBiggestInt(s: string; number: var BiggestInt; start = 0): int {...}{.gcsafe, extern: "npuParseBiggestInt", noSideEffect, raises: [], tags: [].}
proc parseInt(s: string; number: var int; start = 0): int {...}{.gcsafe, extern: "npuParseInt", noSideEffect, raises: [OverflowError], tags: [].}
proc parseSaturatedNatural(s: string; b: var int; start = 0): int {...}{.raises: [], tags: [].}
b
. This cannot raise an overflow error. Instead of an Overflow
exception high(int)
is returned. The number of processed character is returned. This is usually what you really want to use instead of parseInt. Example:var res = 0 discard parseSaturatedNatural("848", res) doAssert res == 848
proc parseBiggestUInt(s: string; number: var BiggestUInt; start = 0): int {...}{.gcsafe, extern: "npuParseBiggestUInt", noSideEffect, raises: [], tags: [].}
proc parseUInt(s: string; number: var uint; start = 0): int {...}{.gcsafe, extern: "npuParseUInt", noSideEffect, raises: [], tags: [].}
proc parseBiggestFloat(s: string; number: var BiggestFloat; start = 0): int {...}{. magic: "ParseBiggestFloat", importc: "nimParseBiggestFloat", noSideEffect.}
proc parseFloat(s: string; number: var float; start = 0): int {...}{.gcsafe, extern: "npuParseFloat", noSideEffect, raises: [], tags: [].}
iterator interpolatedFragments(s: string): tuple[kind: InterpolatedKind, value: string] {...}{.raises: [ValueError], tags: [].}
Tokenizes the string s into substrings for interpolation purposes.
Example:
for k, v in interpolatedFragments(" $this is ${an example} $$"): echo "(", k, ", \"", v, "\")"
Results in:
(ikString, " ") (ikExpr, "this") (ikString, " is ") (ikExpr, "an example") (ikString, " ") (ikDollar, "$")
© 2006–2018 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/parseutils.html