Add escaping to $ operator for Symbol

This commit is contained in:
Emery Hemingway 2023-06-12 21:01:26 +01:00
parent 10d20600bf
commit 9fa6685071
2 changed files with 28 additions and 2 deletions

View File

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

View File

@ -33,7 +33,33 @@ proc hash*(s: Symbol): Hash {.borrow.}
proc len*(s: Symbol): int {.borrow.}
proc `$`*(s: Symbol): string =
if s.len == 0: "||" else: string(s)
let sym = string s
if sym.len > 0 and sym.string[0] in {'A'..'z'} and not sym.anyIt(char(it) in { '\x00'..'\x19', '"', '\\', '|' }):
result = string sym
else:
result = newStringOfCap(sym.len shl 1)
result.add('|')
for c in sym.string:
case c
of '\\':
result.add("\\\\")
of '/':
result.add("\\/")
of '\x08':
result.add("\\b")
of '\x0c':
result.add("\\f")
of '\x0a':
result.add("\\n")
of '\x0d':
result.add("\\r")
of '\x09':
result.add("\\t")
of '|':
result.add("\\|")
else:
result.add(c)
result.add('|')
type
Preserve*[E] = object