Encode short byte-strings as hex text
This commit is contained in:
parent
c74f5c2c63
commit
ef4278062a
|
@ -6,8 +6,8 @@ import std/[base64, endians, hashes, options, sets, sequtils, streams, tables, t
|
|||
|
||||
from std/json import escapeJson, escapeJsonUnquoted
|
||||
from std/macros import hasCustomPragma, getCustomPragmaVal
|
||||
import ./preserves/private/dot
|
||||
from std/strutils import parseEnum
|
||||
import ./preserves/private/dot
|
||||
|
||||
when defined(tracePreserves):
|
||||
template trace(args: varargs[untyped]) =
|
||||
|
@ -1070,15 +1070,22 @@ proc concat[E](result: var string; pr: Preserve[E]) =
|
|||
of pkString:
|
||||
result.add escapeJson(pr.string)
|
||||
of pkByteString:
|
||||
for b in pr.bytes:
|
||||
if b.char notin {'\20'..'\21', '#'..'[', ']'..'~'}:
|
||||
if sequtils.any(pr.bytes, proc (b: byte): bool = b.char in {'\20'..'\21', '#'..'[', ']'..'~'}):
|
||||
if pr.bytes.len > 64:
|
||||
result.add("#[") #]#
|
||||
result.add(base64.encode(pr.bytes))
|
||||
result.add(']')
|
||||
return
|
||||
result.add("#\"")
|
||||
result.add(cast[string](pr.bytes))
|
||||
result.add('"')
|
||||
else:
|
||||
const alphabet = "0123456789abcdef"
|
||||
result.add("#x\"")
|
||||
for b in pr.bytes:
|
||||
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:
|
||||
|
|
Loading…
Reference in New Issue