Add pop for dictionaries
This commit is contained in:
parent
ca5d44ed32
commit
0c4ef8269f
|
@ -1,6 +1,6 @@
|
||||||
# Package
|
# Package
|
||||||
|
|
||||||
version = "20231028"
|
version = "20231102"
|
||||||
author = "Emery Hemingway"
|
author = "Emery Hemingway"
|
||||||
description = "data model and serialization format"
|
description = "data model and serialization format"
|
||||||
license = "Unlicense"
|
license = "Unlicense"
|
||||||
|
|
|
@ -284,6 +284,18 @@ proc getOrDefault(pr: Preserve; key: Preserve): Preserve =
|
||||||
result = v
|
result = v
|
||||||
break
|
break
|
||||||
|
|
||||||
|
proc pop*(pr: var Preserve; key: Preserve; val: var Preserve): bool =
|
||||||
|
## Deletes the `key` from a Preserves dictionary.
|
||||||
|
## Returns true, if the key existed, and sets `val` to the mapping
|
||||||
|
## of the key. Otherwise, returns false, and the `val` is unchanged.
|
||||||
|
if pr.kind == pkDictionary:
|
||||||
|
var i = 0
|
||||||
|
while i < pr.dict.len:
|
||||||
|
if pr.dict[i].key == key:
|
||||||
|
val = move pr.dict[i].val
|
||||||
|
delete(pr.dict, i, i)
|
||||||
|
return true
|
||||||
|
|
||||||
proc incl*(pr: var Preserve; key: Preserve) =
|
proc incl*(pr: var Preserve; key: Preserve) =
|
||||||
## Include `key` in the Preserves set `pr`.
|
## Include `key` in the Preserves set `pr`.
|
||||||
for i in 0..pr.set.high:
|
for i in 0..pr.set.high:
|
||||||
|
@ -638,7 +650,6 @@ proc encode*[E](pr: Preserve[E]): seq[byte] =
|
||||||
s.write pr
|
s.write pr
|
||||||
result = cast[seq[byte]](move s.data)
|
result = cast[seq[byte]](move s.data)
|
||||||
|
|
||||||
|
|
||||||
proc decodePreserves*(s: Stream; E = void): Preserve[E] =
|
proc decodePreserves*(s: Stream; E = void): Preserve[E] =
|
||||||
## Decode a Preserves value from a binary-encoded stream.
|
## Decode a Preserves value from a binary-encoded stream.
|
||||||
if s.atEnd: raise newException(IOError, "End of Preserves stream")
|
if s.atEnd: raise newException(IOError, "End of Preserves stream")
|
||||||
|
|
Loading…
Reference in New Issue