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-05 00:33:32 +0200
committerAraq <rumpf_a@web.de>2011-04-05 00:33:32 +0200
commitae5074455d3547f0d8ca65e591fce5d814d61f21 (patch)
treed46f372b689d948a0daa667afa7f854ba26649bf /lib/pure/strtabs.nim
parent7e62c39f8ed137e05505f21ca6759a377b1afcdc (diff)
downloadNim-ae5074455d3547f0d8ca65e591fce5d814d61f21.tar.gz
$ for strtabs; skipUntil, skipWhile for parseutils
Diffstat (limited to 'lib/pure/strtabs.nim')
-rwxr-xr-xlib/pure/strtabs.nim22
1 files changed, 16 insertions, 6 deletions
diff --git a/lib/pure/strtabs.nim b/lib/pure/strtabs.nim
index 1ac8930b0..d11ff31c8 100755
--- a/lib/pure/strtabs.nim
+++ b/lib/pure/strtabs.nim
@@ -77,8 +77,7 @@ proc nextTry(h, maxHash: THash): THash {.inline.} =
   result = ((5 * h) + 1) and maxHash
 
 proc RawGet(t: PStringTable, key: string): int =
-  var h: THash
-  h = myhash(t, key) and high(t.data) # start with real hash value
+  var h: THash = myhash(t, key) and high(t.data) # start with real hash value
   while not isNil(t.data[h].key):
     if mycmp(t, t.data[h].key, key):
       return h
@@ -89,8 +88,7 @@ proc `[]`*(t: PStringTable, key: string): string {.rtl, extern: "nstGet".} =
   ## retrieves the value at ``t[key]``. If `key` is not in `t`, "" is returned
   ## and no exception is raised. One can check with ``hasKey`` whether the key
   ## exists.
-  var index: int
-  index = RawGet(t, key)
+  var index = RawGet(t, key)
   if index >= 0: result = t.data[index].val
   else: result = ""
 
@@ -99,8 +97,7 @@ proc hasKey*(t: PStringTable, key: string): bool {.rtl, extern: "nst$1".} =
   result = rawGet(t, key) >= 0
 
 proc RawInsert(t: PStringTable, data: var TKeyValuePairSeq, key, val: string) =
-  var h: THash
-  h = myhash(t, key) and high(data)
+  var h: THash = myhash(t, key) and high(data)
   while not isNil(data[h].key):
     h = nextTry(h, high(data))
   data[h].key = key
@@ -188,3 +185,16 @@ proc `%`*(f: string, t: PStringTable, flags: set[TFormatFlag] = {}): string {.
       add(result, f[i])
       inc(i)
 
+proc `$`*(t: PStringTable): string {.rtl, extern: "nstDollar".} =
+  ## The `$` operator for string tables.
+  if t.len == 0:
+    result = "{:}"
+  else:
+    result = "{"
+    for key, val in pairs(t): 
+      if result.len > 1: result.add(", ")
+      result.add(key)
+      result.add(": ")
+      result.add(val)
+    result.add("}")
+