Added strutils.unescape and fixed issue with strutils.escape.
This commit is contained in:
parent
0e5e852f5c
commit
c4743805d9
1 changed files with 39 additions and 1 deletions
|
|
@ -850,7 +850,7 @@ proc escape*(s: string, prefix = "\"", suffix = "\""): string {.noSideEffect,
|
||||||
for c in items(s):
|
for c in items(s):
|
||||||
case c
|
case c
|
||||||
of '\0'..'\31', '\128'..'\255':
|
of '\0'..'\31', '\128'..'\255':
|
||||||
add(result, '\\')
|
add(result, "\\x")
|
||||||
add(result, toHex(ord(c), 2))
|
add(result, toHex(ord(c), 2))
|
||||||
of '\\': add(result, "\\\\")
|
of '\\': add(result, "\\\\")
|
||||||
of '\'': add(result, "\\'")
|
of '\'': add(result, "\\'")
|
||||||
|
|
@ -858,6 +858,44 @@ proc escape*(s: string, prefix = "\"", suffix = "\""): string {.noSideEffect,
|
||||||
else: add(result, c)
|
else: add(result, c)
|
||||||
add(result, suffix)
|
add(result, suffix)
|
||||||
|
|
||||||
|
proc unescape*(s: string, prefix = "\"", suffix = "\""): string {.noSideEffect,
|
||||||
|
rtl, extern: "nsuUnescape".} =
|
||||||
|
## Unescapes a string `s`. This complements ``escape`` as it performs the
|
||||||
|
## opposite operations.
|
||||||
|
##
|
||||||
|
## If `s` does not begin with ``prefix`` and end with ``suffix`` a EInvalidValue
|
||||||
|
## exception will be raised.
|
||||||
|
result = newStringOfCap(s.len)
|
||||||
|
var i = 0
|
||||||
|
if s[0 .. prefix.len-1] != prefix:
|
||||||
|
raise newException(EInvalidValue,
|
||||||
|
"String does not start with a prefix of: " & prefix)
|
||||||
|
i.inc()
|
||||||
|
while True:
|
||||||
|
if i == s.len-suffix.len: break
|
||||||
|
case s[i]
|
||||||
|
of '\\':
|
||||||
|
case s[i+1]:
|
||||||
|
of 'x':
|
||||||
|
let j = parseHexInt(s[i+2 .. i+3])
|
||||||
|
result.add(chr(j))
|
||||||
|
inc(i, 2)
|
||||||
|
of '\\':
|
||||||
|
result.add('\\')
|
||||||
|
of '\'':
|
||||||
|
result.add('\'')
|
||||||
|
of '\"':
|
||||||
|
result.add('\"')
|
||||||
|
else: result.add("\\" & s[i+1])
|
||||||
|
inc(i)
|
||||||
|
of '\0': break
|
||||||
|
else:
|
||||||
|
result.add(s[i])
|
||||||
|
i.inc()
|
||||||
|
if s[i .. -1] != suffix:
|
||||||
|
raise newException(EInvalidValue,
|
||||||
|
"String does not end with a suffix of: " & suffix)
|
||||||
|
|
||||||
proc validIdentifier*(s: string): bool {.noSideEffect,
|
proc validIdentifier*(s: string): bool {.noSideEffect,
|
||||||
rtl, extern: "nsuValidIdentifier".} =
|
rtl, extern: "nsuValidIdentifier".} =
|
||||||
## returns true if `s` is a valid identifier. A valid identifier starts
|
## returns true if `s` is a valid identifier. A valid identifier starts
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue