$ for strtabs; skipUntil, skipWhile for parseutils

This commit is contained in:
Araq 2011-04-05 00:33:32 +02:00
commit ae5074455d
2 changed files with 29 additions and 9 deletions

View file

@ -77,7 +77,7 @@ proc parseIdent*(s: string, ident: var string, start = 0): int =
result = i-start result = i-start
proc parseToken*(s: string, token: var string, validChars: set[char], proc parseToken*(s: string, token: var string, validChars: set[char],
start = 0): int = start = 0): int {.inline.} =
## parses a token and stores it in ``token``. Returns ## parses a token and stores it in ``token``. Returns
## the number of the parsed characters or 0 in case of an error. A token ## the number of the parsed characters or 0 in case of an error. A token
## consists of the characters in `validChars`. ## consists of the characters in `validChars`.
@ -91,7 +91,7 @@ proc skipWhitespace*(s: string, start = 0): int {.inline.} =
## skipped characters. ## skipped characters.
while s[start+result] in Whitespace: inc(result) while s[start+result] in Whitespace: inc(result)
proc skip*(s, token: string, start = 0): int = proc skip*(s, token: string, start = 0): int {.inline.} =
while result < token.len and s[result+start] == token[result]: inc(result) while result < token.len and s[result+start] == token[result]: inc(result)
if result != token.len: result = 0 if result != token.len: result = 0
@ -100,6 +100,16 @@ proc skipIgnoreCase*(s, token: string, start = 0): int =
toLower(s[result+start]) == toLower(token[result]): inc(result) toLower(s[result+start]) == toLower(token[result]): inc(result)
if result != token.len: result = 0 if result != token.len: result = 0
proc skipUntil*(s: string, until: set[char], start = 0): int {.inline.} =
## Skips all characters until one char from the set `token` is found.
## Returns number of characters skipped.
while s[result+start] notin until and s[result+start] != '\0': inc(result)
proc skipWhile*(s: string, toSkip: set[char], start = 0): int {.inline.} =
## Skips all characters while one char from the set `token` is found.
## Returns number of characters skipped.
while s[result+start] in toSkip and s[result+start] != '\0': inc(result)
{.push overflowChecks: on.} {.push overflowChecks: on.}
# this must be compiled with overflow checking turned on: # this must be compiled with overflow checking turned on:
proc rawParseInt(s: string, b: var biggestInt, start = 0): int = proc rawParseInt(s: string, b: var biggestInt, start = 0): int =

View file

@ -77,8 +77,7 @@ proc nextTry(h, maxHash: THash): THash {.inline.} =
result = ((5 * h) + 1) and maxHash result = ((5 * h) + 1) and maxHash
proc RawGet(t: PStringTable, key: string): int = proc RawGet(t: PStringTable, key: string): int =
var h: THash var h: THash = myhash(t, key) and high(t.data) # start with real hash value
h = myhash(t, key) and high(t.data) # start with real hash value
while not isNil(t.data[h].key): while not isNil(t.data[h].key):
if mycmp(t, t.data[h].key, key): if mycmp(t, t.data[h].key, key):
return h 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 ## 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 ## and no exception is raised. One can check with ``hasKey`` whether the key
## exists. ## exists.
var index: int var index = RawGet(t, key)
index = RawGet(t, key)
if index >= 0: result = t.data[index].val if index >= 0: result = t.data[index].val
else: result = "" else: result = ""
@ -99,8 +97,7 @@ proc hasKey*(t: PStringTable, key: string): bool {.rtl, extern: "nst$1".} =
result = rawGet(t, key) >= 0 result = rawGet(t, key) >= 0
proc RawInsert(t: PStringTable, data: var TKeyValuePairSeq, key, val: string) = proc RawInsert(t: PStringTable, data: var TKeyValuePairSeq, key, val: string) =
var h: THash var h: THash = myhash(t, key) and high(data)
h = myhash(t, key) and high(data)
while not isNil(data[h].key): while not isNil(data[h].key):
h = nextTry(h, high(data)) h = nextTry(h, high(data))
data[h].key = key data[h].key = key
@ -188,3 +185,16 @@ proc `%`*(f: string, t: PStringTable, flags: set[TFormatFlag] = {}): string {.
add(result, f[i]) add(result, f[i])
inc(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("}")