minor stdlib cleanups
This commit is contained in:
parent
0a2cc05541
commit
3a3aeb94ec
3 changed files with 19 additions and 19 deletions
|
|
@ -81,7 +81,7 @@ proc newHttpHeaders*(keyValuePairs:
|
||||||
openarray[tuple[key: string, val: string]]): HttpHeaders =
|
openarray[tuple[key: string, val: string]]): HttpHeaders =
|
||||||
var pairs: seq[tuple[key: string, val: seq[string]]] = @[]
|
var pairs: seq[tuple[key: string, val: seq[string]]] = @[]
|
||||||
for pair in keyValuePairs:
|
for pair in keyValuePairs:
|
||||||
pairs.add((pair.key.toLower(), @[pair.val]))
|
pairs.add((pair.key.toLowerAscii(), @[pair.val]))
|
||||||
new result
|
new result
|
||||||
result.table = newTable[string, seq[string]](pairs)
|
result.table = newTable[string, seq[string]](pairs)
|
||||||
|
|
||||||
|
|
@ -96,7 +96,7 @@ proc `[]`*(headers: HttpHeaders, key: string): HttpHeaderValues =
|
||||||
##
|
##
|
||||||
## To access multiple values of a key, use the overloaded ``[]`` below or
|
## To access multiple values of a key, use the overloaded ``[]`` below or
|
||||||
## to get all of them access the ``table`` field directly.
|
## to get all of them access the ``table`` field directly.
|
||||||
return headers.table[key.toLower].HttpHeaderValues
|
return headers.table[key.toLowerAscii].HttpHeaderValues
|
||||||
|
|
||||||
converter toString*(values: HttpHeaderValues): string =
|
converter toString*(values: HttpHeaderValues): string =
|
||||||
return seq[string](values)[0]
|
return seq[string](values)[0]
|
||||||
|
|
@ -105,26 +105,26 @@ proc `[]`*(headers: HttpHeaders, key: string, i: int): string =
|
||||||
## Returns the ``i``'th value associated with the given key. If there are
|
## Returns the ``i``'th value associated with the given key. If there are
|
||||||
## no values associated with the key or the ``i``'th value doesn't exist,
|
## no values associated with the key or the ``i``'th value doesn't exist,
|
||||||
## an exception is raised.
|
## an exception is raised.
|
||||||
return headers.table[key.toLower][i]
|
return headers.table[key.toLowerAscii][i]
|
||||||
|
|
||||||
proc `[]=`*(headers: HttpHeaders, key, value: string) =
|
proc `[]=`*(headers: HttpHeaders, key, value: string) =
|
||||||
## Sets the header entries associated with ``key`` to the specified value.
|
## Sets the header entries associated with ``key`` to the specified value.
|
||||||
## Replaces any existing values.
|
## Replaces any existing values.
|
||||||
headers.table[key.toLower] = @[value]
|
headers.table[key.toLowerAscii] = @[value]
|
||||||
|
|
||||||
proc `[]=`*(headers: HttpHeaders, key: string, value: seq[string]) =
|
proc `[]=`*(headers: HttpHeaders, key: string, value: seq[string]) =
|
||||||
## Sets the header entries associated with ``key`` to the specified list of
|
## Sets the header entries associated with ``key`` to the specified list of
|
||||||
## values.
|
## values.
|
||||||
## Replaces any existing values.
|
## Replaces any existing values.
|
||||||
headers.table[key.toLower] = value
|
headers.table[key.toLowerAscii] = value
|
||||||
|
|
||||||
proc add*(headers: HttpHeaders, key, value: string) =
|
proc add*(headers: HttpHeaders, key, value: string) =
|
||||||
## Adds the specified value to the specified key. Appends to any existing
|
## Adds the specified value to the specified key. Appends to any existing
|
||||||
## values associated with the key.
|
## values associated with the key.
|
||||||
if not headers.table.hasKey(key.toLower):
|
if not headers.table.hasKey(key.toLowerAscii):
|
||||||
headers.table[key.toLower] = @[value]
|
headers.table[key.toLowerAscii] = @[value]
|
||||||
else:
|
else:
|
||||||
headers.table[key.toLower].add(value)
|
headers.table[key.toLowerAscii].add(value)
|
||||||
|
|
||||||
iterator pairs*(headers: HttpHeaders): tuple[key, value: string] =
|
iterator pairs*(headers: HttpHeaders): tuple[key, value: string] =
|
||||||
## Yields each key, value pair.
|
## Yields each key, value pair.
|
||||||
|
|
@ -136,10 +136,10 @@ proc contains*(values: HttpHeaderValues, value: string): bool =
|
||||||
## Determines if ``value`` is one of the values inside ``values``. Comparison
|
## Determines if ``value`` is one of the values inside ``values``. Comparison
|
||||||
## is performed without case sensitivity.
|
## is performed without case sensitivity.
|
||||||
for val in seq[string](values):
|
for val in seq[string](values):
|
||||||
if val.toLower == value.toLower: return true
|
if val.toLowerAscii == value.toLowerAscii: return true
|
||||||
|
|
||||||
proc hasKey*(headers: HttpHeaders, key: string): bool =
|
proc hasKey*(headers: HttpHeaders, key: string): bool =
|
||||||
return headers.table.hasKey(key.toLower())
|
return headers.table.hasKey(key.toLowerAscii())
|
||||||
|
|
||||||
proc getOrDefault*(headers: HttpHeaders, key: string,
|
proc getOrDefault*(headers: HttpHeaders, key: string,
|
||||||
default = @[""].HttpHeaderValues): HttpHeaderValues =
|
default = @[""].HttpHeaderValues): HttpHeaderValues =
|
||||||
|
|
|
||||||
|
|
@ -595,12 +595,12 @@ when not declared(ENOENT) and not defined(Windows):
|
||||||
|
|
||||||
when defined(Windows):
|
when defined(Windows):
|
||||||
when useWinUnicode:
|
when useWinUnicode:
|
||||||
template deleteFile(file: expr): expr {.immediate.} = deleteFileW(file)
|
template deleteFile(file: untyped): untyped = deleteFileW(file)
|
||||||
template setFileAttributes(file, attrs: expr): expr {.immediate.} =
|
template setFileAttributes(file, attrs: untyped): untyped =
|
||||||
setFileAttributesW(file, attrs)
|
setFileAttributesW(file, attrs)
|
||||||
else:
|
else:
|
||||||
template deleteFile(file: expr): expr {.immediate.} = deleteFileA(file)
|
template deleteFile(file: untyped): untyped = deleteFileA(file)
|
||||||
template setFileAttributes(file, attrs: expr): expr {.immediate.} =
|
template setFileAttributes(file, attrs: untyped): untyped =
|
||||||
setFileAttributesA(file, attrs)
|
setFileAttributesA(file, attrs)
|
||||||
|
|
||||||
proc removeFile*(file: string) {.rtl, extern: "nos$1", tags: [WriteDirEffect].} =
|
proc removeFile*(file: string) {.rtl, extern: "nos$1", tags: [WriteDirEffect].} =
|
||||||
|
|
|
||||||
|
|
@ -90,10 +90,10 @@ proc innerHash(state: var Sha1State, w: var Sha1Buffer) =
|
||||||
|
|
||||||
var round = 0
|
var round = 0
|
||||||
|
|
||||||
template rot(value, bits: uint32): uint32 {.immediate.} =
|
template rot(value, bits: uint32): uint32 =
|
||||||
(value shl bits) or (value shr (32 - bits))
|
(value shl bits) or (value shr (32 - bits))
|
||||||
|
|
||||||
template sha1(fun, val: uint32): stmt =
|
template sha1(fun, val: uint32) =
|
||||||
let t = rot(a, 5) + fun + e + val + w[round]
|
let t = rot(a, 5) + fun + e + val + w[round]
|
||||||
e = d
|
e = d
|
||||||
d = c
|
d = c
|
||||||
|
|
@ -101,12 +101,12 @@ proc innerHash(state: var Sha1State, w: var Sha1Buffer) =
|
||||||
b = a
|
b = a
|
||||||
a = t
|
a = t
|
||||||
|
|
||||||
template process(body: stmt): stmt =
|
template process(body: untyped) =
|
||||||
w[round] = rot(w[round - 3] xor w[round - 8] xor w[round - 14] xor w[round - 16], 1)
|
w[round] = rot(w[round - 3] xor w[round - 8] xor w[round - 14] xor w[round - 16], 1)
|
||||||
body
|
body
|
||||||
inc(round)
|
inc(round)
|
||||||
|
|
||||||
template wrap(dest, value: expr): stmt {.immediate.} =
|
template wrap(dest, value: untyped) =
|
||||||
let v = dest + value
|
let v = dest + value
|
||||||
dest = v
|
dest = v
|
||||||
|
|
||||||
|
|
@ -136,7 +136,7 @@ proc innerHash(state: var Sha1State, w: var Sha1Buffer) =
|
||||||
wrap state[3], d
|
wrap state[3], d
|
||||||
wrap state[4], e
|
wrap state[4], e
|
||||||
|
|
||||||
template computeInternal(src: expr): stmt {.immediate.} =
|
template computeInternal(src: untyped) =
|
||||||
#Initialize state
|
#Initialize state
|
||||||
var state: Sha1State
|
var state: Sha1State
|
||||||
init(state)
|
init(state)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue