Do not leak null characters into the #"" encoding

This commit is contained in:
Emery Hemingway 2021-12-05 16:34:12 +00:00
parent 061898c3c2
commit 200e0a0080
1 changed files with 5 additions and 5 deletions

View File

@ -1039,7 +1039,11 @@ proc concat[E](result: var string; pr: Preserve[E]) =
of pkString:
result.add escapeJson(pr.string)
of pkByteString:
if sequtils.any(pr.bytes, proc (b: byte): bool = b.char in {'\20'..'\21', '#'..'[', ']'..'~'}):
if pr.bytes.allIt(char(it) in {' '..'!', '#'..'~'}):
result.add("#\"")
result.add(cast[string](pr.bytes))
result.add('"')
else:
if pr.bytes.len > 64:
result.add("#[") #]#
result.add(base64.encode(pr.bytes))
@ -1051,10 +1055,6 @@ proc concat[E](result: var string; pr: Preserve[E]) =
result.add(alphabet[int(b shr 4)])
result.add(alphabet[int(b and 0xf)])
result.add('"')
else:
result.add("#\"")
result.add(cast[string](pr.bytes))
result.add('"')
of pkSymbol:
result.add(escapeJsonUnquoted(pr.symbol))
of pkRecord: