# 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.

用例:

    var t = newStringTable()
    t["name"] = "John"
    t["city"] = "Monaco"
    doAssert t.len == 2
    doAssert t.hasKey "name"
    doAssert "name" in t

String tables can be created from a table constructor:

用例:

    var t = {"name": "John", "city": "Monaco"}.newStringTable

When using the style insensitive mode (`modeStyleInsensitive`), all letters are compared case insensitively within the ASCII range and underscores are ignored.

用例:

    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.

用例:

    var t = {"name": "John", "city": "Monaco"}.newStringTable
    doAssert "${name} lives in ${city}" % t == "John lives in Monaco"

関連:

- tables module for general hash tables
- sharedtables module for shared hash table support
- strutils module for common string functions
- json module for table-like structure which allows heterogeneous members

# インポート

hashes, strutils, os

# 型

    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

  ソース 編集

    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. ソース 編集

# プロシージャ

    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 proc whether the key exists.

  関連:

  - getOrDefault proc
  - []= proc for inserting a new (key, value) pair in the table
  - hasKey proc for checking if a key is in the table

  用例:

      var t = {"name": "John", "city": "Monaco"}.newStringTable
      doAssert t["name"] == "John"
      doAssertRaises(KeyError):
        echo t["occupation"]

  ソース 編集

    proc getOrDefault(t: StringTableRef; key: string; default: string = ""): string {...}{.
        raises: [], tags: [].}

  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 (`""`)).

  関連:

  - [] proc for retrieving a value of a key
  - hasKey proc for checking if a key is in the table
  - []= proc for inserting a new (key, value) pair in the table

  用例:

      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"

  ソース 編集

    proc hasKey(t: StringTableRef; key: string): bool {...}{.gcsafe, extern: "nst$1", raises: [],
        tags: [].}

  Returns true if `key` is in the table `t`.

  関連:

  - getOrDefault proc
  - contains proc

  用例:

      var t = {"name": "John", "city": "Monaco"}.newStringTable
      doAssert t.hasKey("name")
      doAssert not t.hasKey("occupation")

  ソース 編集

    proc contains(t: StringTableRef; key: string): bool {...}{.raises: [], tags: [].}

  Alias of hasKey proc for use with the `in` operator.

  用例:

      var t = {"name": "John", "city": "Monaco"}.newStringTable
      doAssert "name" in t
      doAssert "occupation" notin t

  ソース 編集

    proc `[]=`(t: StringTableRef; key, val: string) {...}{.gcsafe, extern: "nstPut", noSideEffect,
        raises: [], tags: [].}

  Inserts a `(key, value)` pair into `t`.

  関連:

  - [] proc for retrieving a value of a key
  - del proc for removing a key from the table

  用例:

      var t = {"name": "John", "city": "Monaco"}.newStringTable
      t["occupation"] = "teacher"
      doAssert t.hasKey("occupation")

  ソース 編集

    proc newStringTable(mode: StringTableMode): owned(StringTableRef) {...}{.gcsafe,
        extern: "nst$1", raises: [], tags: [].}

  Creates a new empty string table.

  関連:

  - newStringTable(keyValuePairs) proc
  ソース 編集

    proc newStringTable(keyValuePairs: varargs[string]; mode: StringTableMode): owned(
        StringTableRef) {...}{.gcsafe, extern: "nst$1WithPairs", raises: [], tags: [].}

  Creates a new string table with given `key, value` string pairs.

  `StringTableMode` must be specified.

  用例:

      var mytab = newStringTable("key1", "val1", "key2", "val2", modeCaseInsensitive)

  ソース 編集

    proc newStringTable(keyValuePairs: varargs[tuple[key, val: string]];
                       mode: StringTableMode = modeCaseSensitive): owned(StringTableRef) {...}{.
        gcsafe, extern: "nst$1WithTableConstr", raises: [], tags: [].}

  Creates a new string table with given `(key, value)` tuple pairs.

  The default mode is case sensitive.

  用例:

      var
        mytab1 = newStringTable({"key1": "val1", "key2": "val2"}, modeCaseInsensitive)
        mytab2 = newStringTable([("key3", "val3"), ("key4", "val4")])

  ソース 編集

    proc clear(s: StringTableRef; mode: StringTableMode) {...}{.gcsafe, extern: "nst$1",
        raises: [], tags: [].}

  Resets a string table to be empty again.

  関連:

  - del proc for removing a key from the table

  用例:

      var t = {"name": "John", "city": "Monaco"}.newStringTable
      clear(t, modeCaseSensitive)
      doAssert len(t) == 0
      doAssert "name" notin t
      doAssert "city" notin t

  ソース 編集

    proc del(t: StringTableRef; key: string) {...}{.raises: [], tags: [].}

  Removes `key` from `t`.

  関連:

  - clear proc for resetting a table to be empty
  - []= proc for inserting a new (key, value) pair in the table

  用例:

      var t = {"name": "John", "city": "Monaco"}.newStringTable
      t.del("name")
      doAssert len(t) == 1
      doAssert "name" notin t
      doAssert "city" in t

  ソース 編集

    proc `$`(t: StringTableRef): string {...}{.gcsafe, extern: "nstDollar", raises: [], tags: [].}

  The `$` operator for string tables. Used internally when calling `echo` on a table. ソース 編集

    proc `%`(f: string; t: StringTableRef; flags: set[FormatFlag] = {}): string {...}{.gcsafe,
        extern: "nstFormat", raises: [ValueError], tags: [ReadEnvEffect].}

  The `%` operator for string tables.

  用例:

      var t = {"name": "John", "city": "Monaco"}.newStringTable
      doAssert "${name} lives in ${city}" % t == "John lives in Monaco"

  ソース 編集

# イテレータ

    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`. ソース 編集