Skip to content

Commit 4c97cec

Browse files
committed
Merge pull request #55 from michaelficarra/GH-53
fixes #53: properly escape output of Show instance for String
2 parents 43b6eb3 + 4a249ab commit 4c97cec

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/Prelude.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,27 @@ exports.showCharImpl = function (c) {
227227
};
228228

229229
exports.showStringImpl = function (s) {
230-
return JSON.stringify(s);
230+
var l = s.length;
231+
return "\"" + s.replace(
232+
/[\0-\x1F\x7F"\\]/g,
233+
function (c, i) { // jshint ignore:line
234+
switch (c) {
235+
case "\"":
236+
case "\\":
237+
return "\\" + c;
238+
case "\a": return "\\a";
239+
case "\b": return "\\b";
240+
case "\f": return "\\f";
241+
case "\n": return "\\n";
242+
case "\r": return "\\r";
243+
case "\t": return "\\t";
244+
case "\v": return "\\v";
245+
}
246+
var k = i + 1;
247+
var empty = k < l && s[k] >= "0" && s[k] <= "9" ? "\\&" : "";
248+
return "\\" + c.charCodeAt(0).toString(10) + empty;
249+
}
250+
) + "\"";
231251
};
232252

233253
exports.showArrayImpl = function (f) {

0 commit comments

Comments
 (0)