Add pop for dictionaries

This commit is contained in:
Emery Hemingway 2023-11-02 13:33:34 +00:00
parent ca5d44ed32
commit 0c4ef8269f
2 changed files with 13 additions and 2 deletions

View File

@ -1,6 +1,6 @@
# Package
version = "20231028"
version = "20231102"
author = "Emery Hemingway"
description = "data model and serialization format"
license = "Unlicense"

View File

@ -284,6 +284,18 @@ proc getOrDefault(pr: Preserve; key: Preserve): Preserve =
result = v
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) =
## Include `key` in the Preserves set `pr`.
for i in 0..pr.set.high:
@ -638,7 +650,6 @@ proc encode*[E](pr: Preserve[E]): seq[byte] =
s.write pr
result = cast[seq[byte]](move s.data)
proc decodePreserves*(s: Stream; E = void): Preserve[E] =
## Decode a Preserves value from a binary-encoded stream.
if s.atEnd: raise newException(IOError, "End of Preserves stream")