summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authorIco Doornekamp <github@zevv.nl>2019-01-07 00:52:19 +0100
committerAndreas Rumpf <rumpf_a@web.de>2019-01-07 00:52:19 +0100
commitbe9d1280ae39b64e6c76179a788034ad6c50a52d (patch)
treef1ea9f1977c7911c071ca5fb34ae105a4c41c5c0 /lib/pure
parente77dd683eb9b6b2b9c2638f4145f8857fece12cb (diff)
downloadNim-be9d1280ae39b64e6c76179a788034ad6c50a52d.tar.gz
Added examples to strtabs module (#10160)
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/strtabs.nim31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/pure/strtabs.nim b/lib/pure/strtabs.nim
index d8a23286a..7bafe1675 100644
--- a/lib/pure/strtabs.nim
+++ b/lib/pure/strtabs.nim
@@ -12,6 +12,34 @@
 ## style-insensitive mode. An efficient string substitution operator  ``%``
 ## for the string table is also provided.
 
+runnableExamples:
+
+  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:
+
+runnableExamples:
+  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.
+
+runnableExamples:
+
+  var x = newStringTable(modeStyleInsensitive)
+  x["first_name"] = "John"
+  x["LastName"] = "Doe"
+
+  doAssert x["firstName"] == "John"
+  doAssert x["last_name"] == "Doe"
+
 import
   hashes, strutils
 
@@ -214,6 +242,9 @@ proc newStringTable*(keyValuePairs: varargs[tuple[key, val: string]],
 proc `%`*(f: string, t: StringTableRef, flags: set[FormatFlag] = {}): string {.
   rtlFunc, extern: "nstFormat".} =
   ## The `%` operator for string tables.
+  runnableExamples:
+    var t = {"name": "John", "city": "Monaco"}.newStringTable
+    doAssert "${name} lives in ${city}" % t == "John lives in Monaco"
   const
     PatternChars = {'a'..'z', 'A'..'Z', '0'..'9', '_', '\x80'..'\xFF'}
   result = ""