The strtabs module implements an efficient hash table that is a mapping from strings to strings. Supports a case-sensitive, case-insensitive and style-insensitive mode.
Example:
import std/strtabs var t = newStringTable() t["name"] = "John" t["city"] = "Monaco" doAssert t.len == 2 doAssert t.hasKey "name" doAssert "name" in tString tables can be created from a table constructor:
Example:
import std/strtabs
var t = {"name": "John", "city": "Monaco"}.newStringTableWhen using the style insensitive mode (modeStyleInsensitive), all letters are compared case insensitively within the ASCII range and underscores are ignored. Example:
import std/strtabs var x = newStringTable(modeStyleInsensitive) x["first_name"] = "John" x["LastName"] = "Doe" doAssert x["firstName"] == "John" doAssert x["last_name"] == "Doe"An efficient string substitution operator % for the string table is also provided.
Example:
import std/strtabs
var t = {"name": "John", "city": "Monaco"}.newStringTable
doAssert "${name} lives in ${city}" % t == "John lives in Monaco"
See also:FormatFlag = enum
useEnvironment, ## Use environment variable if the ``$key``
## is not found in the table.
## Does nothing when using `js` target.
useEmpty, ## Use the empty string as a default, thus it
## won't throw an exception if ``$key`` is not
## in the table.
useKey ## Do not replace ``$key`` if it is not found
## in the table (or in the environment).% operator. Source Edit proc `%`(f: string; t: StringTableRef; flags: set[FormatFlag] = {}): string {.
...gcsafe, extern: "nstFormat", raises: [ValueError], tags: [ReadEnvEffect],
forbids: [].}% operator for string tables. Example:
var t = {"name": "John", "city": "Monaco"}.newStringTable
doAssert "${name} lives in ${city}" % t == "John lives in Monaco" Source Edit proc `[]`(t: StringTableRef; key: string): var string {....gcsafe,
extern: "nstTake", raises: [KeyError], tags: [], forbids: [].}Retrieves the location at t[key].
If key is not in t, the KeyError exception is raised. One can check with hasKey proc whether the key exists.
See also:
Example:
var t = {"name": "John", "city": "Monaco"}.newStringTable
doAssert t["name"] == "John"
doAssertRaises(KeyError):
echo t["occupation"] Source Edit proc clear(s: StringTableRef) {....raises: [], tags: [], forbids: [].}proc clear(s: StringTableRef; mode: StringTableMode) {....gcsafe, extern: "nst$1",
raises: [], tags: [], forbids: [].}Resets a string table to be empty again, perhaps altering the mode.
See also:
Example:
var t = {"name": "John", "city": "Monaco"}.newStringTable
clear(t, modeCaseSensitive)
doAssert len(t) == 0
doAssert "name" notin t
doAssert "city" notin t Source Edit proc contains(t: StringTableRef; key: string): bool {....raises: [], tags: [],
forbids: [].}in operator. Example:
var t = {"name": "John", "city": "Monaco"}.newStringTable
doAssert "name" in t
doAssert "occupation" notin t Source Edit proc del(t: StringTableRef; key: string) {....raises: [], tags: [], forbids: [].}Removes key from t.
See also:
Example:
var t = {"name": "John", "city": "Monaco"}.newStringTable
t.del("name")
doAssert len(t) == 1
doAssert "name" notin t
doAssert "city" in t Source Edit proc getOrDefault(t: StringTableRef; key: string; default: string = ""): string {.
...raises: [], tags: [], forbids: [].}Retrieves the location at t[key].
If key is not in t, the default value is returned (if not specified, it is an empty string ("")).
See also:
Example:
var t = {"name": "John", "city": "Monaco"}.newStringTable
doAssert t.getOrDefault("name") == "John"
doAssert t.getOrDefault("occupation") == ""
doAssert t.getOrDefault("occupation", "teacher") == "teacher"
doAssert t.getOrDefault("name", "Paul") == "John" Source Edit proc hasKey(t: StringTableRef; key: string): bool {....gcsafe, extern: "nst$1",
raises: [], tags: [], forbids: [].}Returns true if key is in the table t.
See also:
Example:
var t = {"name": "John", "city": "Monaco"}.newStringTable
doAssert t.hasKey("name")
doAssert not t.hasKey("occupation") Source Edit proc newStringTable(keyValuePairs: varargs[string]; mode: StringTableMode): owned(
StringTableRef) {....gcsafe, extern: "nst$1WithPairs", noSideEffect,
...raises: [], tags: [], forbids: [].}Creates a new string table with given key, value string pairs.
StringTableMode must be specified.
Example:
var mytab = newStringTable("key1", "val1", "key2", "val2",
modeCaseInsensitive) Source Edit proc newStringTable(keyValuePairs: varargs[tuple[key, val: string]];
mode: StringTableMode = modeCaseSensitive): owned(
StringTableRef) {....gcsafe, extern: "nst$1WithTableConstr", noSideEffect,
...raises: [], tags: [], forbids: [].}Creates a new string table with given (key, value) tuple pairs.
The default mode is case sensitive.
Example:
var
mytab1 = newStringTable({"key1": "val1", "key2": "val2"}, modeCaseInsensitive)
mytab2 = newStringTable([("key3", "val3"), ("key4", "val4")]) Source Edit
© 2006–2024 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/strtabs.html