summary refs log tree commit diff stats
path: root/lib/pure/strtabs.nim
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2011-04-23 17:11:24 +0200
committerAraq <rumpf_a@web.de>2011-04-23 17:11:24 +0200
commit4ba4999bb7b172b683cf7b8d574adbf04afa7527 (patch)
treef1216df68dba7abb0e9324afa11464db885800dd /lib/pure/strtabs.nim
parent05fee773ec48b5b45cdb7469a6c8410b59fcb542 (diff)
downloadNim-4ba4999bb7b172b683cf7b8d574adbf04afa7527.tar.gz
slice support in system.nim; syntactic sugar for tables; cleanup of grammar/parser
Diffstat (limited to 'lib/pure/strtabs.nim')
-rwxr-xr-xlib/pure/strtabs.nim16
1 files changed, 16 insertions, 0 deletions
diff --git a/lib/pure/strtabs.nim b/lib/pure/strtabs.nim
index d11ff31c8..fda0740d3 100755
--- a/lib/pure/strtabs.nim
+++ b/lib/pure/strtabs.nim
@@ -155,6 +155,16 @@ proc newStringTable*(keyValuePairs: openarray[string],
     result[keyValuePairs[i]] = keyValuePairs[i + 1]
     inc(i, 2)
 
+proc newStringTable*(keyValuePairs: openarray[tuple[key, val: string]],
+                     mode: TStringTableMode = modeCaseSensitive): PStringTable {.
+  rtl, extern: "nst$1WithTableConstr".} =
+  ## creates a new string table with given key value pairs.
+  ## Example::
+  ##   var mytab = newStringTable({"key1": "val1", "key2": "val2"},
+  ##                              modeCaseInsensitive)
+  result = newStringTable(mode)
+  for key, val in items(keyvaluePairs): result[key] = val
+
 proc `%`*(f: string, t: PStringTable, flags: set[TFormatFlag] = {}): string {.
   rtl, extern: "nstFormat".} =
   ## The `%` operator for string tables.
@@ -198,3 +208,9 @@ proc `$`*(t: PStringTable): string {.rtl, extern: "nstDollar".} =
       result.add(val)
     result.add("}")
 
+when isMainModule:
+  var x = {"k": "v", "11": "22", "565": "67"}.newStringTable
+  assert x["k"] == "v"
+  assert x["11"] == "22"
+  assert x["565"] == "67"
+