W3cubDocs

/Nim

Module strtabs

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. An efficient string substitution operator % for the string table is also provided.

Imports

hashes, strutils, os

Types

StringTableMode = enum
  modeCaseSensitive,          ## the table is case sensitive
  modeCaseInsensitive,        ## the table is case insensitive
  modeStyleInsensitive        ## the table is style insensitive
describes the tables operation mode
StringTableObj = object of RootObj
  counter: int
  data: KeyValuePairSeq
  mode: StringTableMode
StringTableRef = ref StringTableObj
use this type to declare string tables
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)
flags for the % operator

Procs

proc len(t: StringTableRef): int {...}{.gcsafe, extern: "nst$1", raises: [], tags: [].}
returns the number of keys in t.
proc `[]`(t: StringTableRef; key: string): var string {...}{.gcsafe, extern: "nstTake",
    raises: [KeyError], tags: [].}
retrieves the location at t[key]. If key is not in t, the KeyError exception is raised. One can check with hasKey whether the key exists.
proc getOrDefault(t: StringTableRef; key: string; default: string = ""): string {...}{.
    raises: [], tags: [].}
"is greater or equals" operator. This is the same as y <= x.
proc hasKey(t: StringTableRef; key: string): bool {...}{.gcsafe, extern: "nst$1", raises: [],
    tags: [].}
returns true iff key is in the table t.
proc contains(t: StringTableRef; key: string): bool {...}{.raises: [], tags: [].}
alias of hasKey for use with the in operator.
proc `[]=`(t: StringTableRef; key, val: string) {...}{.gcsafe, extern: "nstPut", raises: [],
    tags: [].}
puts a (key, value)-pair into t.
proc newStringTable(mode: StringTableMode): StringTableRef {...}{.gcsafe, extern: "nst$1",
    raises: [], tags: [].}
creates a new string table that is empty.
proc clear(s: StringTableRef; mode: StringTableMode) {...}{.gcsafe, extern: "nst$1",
    raises: [], tags: [].}
resets a string table to be empty again.
proc newStringTable(keyValuePairs: varargs[string]; mode: StringTableMode): StringTableRef {...}{.
    gcsafe, extern: "nst$1WithPairs", raises: [], tags: [].}
creates a new string table with given key value pairs. Example:
var mytab = newStringTable("key1", "val1", "key2", "val2",
                           modeCaseInsensitive)
proc newStringTable(keyValuePairs: varargs[tuple[key, val: string]];
                   mode: StringTableMode = modeCaseSensitive): StringTableRef {...}{.
    gcsafe, extern: "nst$1WithTableConstr", raises: [], tags: [].}
creates a new string table with given key value pairs. Example:
var mytab = newStringTable({"key1": "val1", "key2": "val2"},
                           modeCaseInsensitive)
proc `%`(f: string; t: StringTableRef; flags: set[FormatFlag] = {}): string {...}{.gcsafe,
    extern: "nstFormat", raises: [ValueError], tags: [ReadEnvEffect].}
The % operator for string tables.
proc `$`(t: StringTableRef): string {...}{.gcsafe, extern: "nstDollar", raises: [], tags: [].}
The $ operator for string tables.

Iterators

iterator pairs(t: StringTableRef): tuple[key, value: string] {...}{.raises: [], tags: [].}
iterates over every (key, value) pair in the table t.
iterator keys(t: StringTableRef): string {...}{.raises: [], tags: [].}
iterates over every key in the table t.
iterator values(t: StringTableRef): string {...}{.raises: [], tags: [].}
iterates over every value in the table t.

© 2006–2018 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/strtabs.html