Skip to content

Commit

Permalink
added support for \xHH and \uHHHH to escapeChar() and unescape()
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwarth committed Jul 1, 2011
1 parent 896ea65 commit 23e66f4
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,38 +122,60 @@ ReadStream.prototype.next = function() { return this.src.at(this.pos++) }

// escape characters

String.prototype.pad = function(s, len) {
var r = this
while (r.length < len)
r = s + r
return r
}

escapeStringFor = new Object()
for (var c = 0; c < 256; c++)
for (var c = 0; c < 128; c++)
escapeStringFor[c] = String.fromCharCode(c)
escapeStringFor["\\".charCodeAt(0)] = "\\\\"
escapeStringFor['"'.charCodeAt(0)] = '\\"'
escapeStringFor["'".charCodeAt(0)] = "\\'"
escapeStringFor["\r".charCodeAt(0)] = "\\r"
escapeStringFor['"'.charCodeAt(0)] = '\\"'
escapeStringFor["\\".charCodeAt(0)] = "\\\\"
escapeStringFor["\b".charCodeAt(0)] = "\\b"
escapeStringFor["\f".charCodeAt(0)] = "\\f"
escapeStringFor["\n".charCodeAt(0)] = "\\n"
escapeStringFor["\r".charCodeAt(0)] = "\\r"
escapeStringFor["\t".charCodeAt(0)] = "\\t"
escapeStringFor["\v".charCodeAt(0)] = "\\v"
escapeChar = function(c) {
var charCode = c.charCodeAt(0)
return charCode > 255 ? String.fromCharCode(charCode) : escapeStringFor[charCode]
if (charCode < 128)
return escapeStringFor[charCode]
else if (128 <= charCode && charCode < 256)
return "\\x" + charCode.toString(16).pad("0", 2)
else
return "\\u" + charCode.toString(16).pad("0", 4)
}

function unescape(s) {
if (s.charAt(0) == '\\')
switch (s.charAt(1)) {
case "'": return "'"
case '"': return '"'
case '\\': return '\\'
case 'r': return '\r'
case 'b': return '\b'
case 'f': return '\f'
case 'n': return '\n'
case 'r': return '\r'
case 't': return '\t'
case 'v': return '\v'
case 'x': return String.fromCharCode(parseInt(s.substring(2, 4), 16))
case 'u': return String.fromCharCode(parseInt(s.substring(2, 6), 16))
default: return s.charAt(1)
}
else
return s
}

String.prototype.toProgramString = function() {
var ws = "\"".writeStream()
var ws = '"'.writeStream()
for (var idx = 0; idx < this.length; idx++)
ws.nextPutAll(escapeChar(this.charAt(idx)))
ws.nextPutAll("\"")
ws.nextPutAll('"')
return ws.contents()
}

Expand Down

0 comments on commit 23e66f4

Please sign in to comment.