Skip to content

Commit

Permalink
got rid of JSON.stringify for writing strings; having our own functio…
Browse files Browse the repository at this point in the history
…n now,

which counts the " or ' characters in the string and generates the smallest
output.
  • Loading branch information
mishoo committed Nov 28, 2010
1 parent 3184a1a commit 9b619d1
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,28 @@ var DOT_CALL_NO_PARENS = jsp.array_to_hash([
"regexp"
]);

function make_string(str) {
var dq = 0, sq = 0;
str = str.replace(/[\\\b\f\n\r\t\x22\x27]/g, function(s){
switch (s) {
case "\\": return "\\\\";
case "\b": return "\\b";
case "\f": return "\\f";
case "\n": return "\\n";
case "\r": return "\\r";
case "\t": return "\\t";
case '"': ++dq; return '"';
case "'": ++sq; return "'";
}
return s;
});
if (dq > sq) {
return "'" + str.replace(/\x27/g, "\\'") + "'";
} else {
return '"' + str.replace(/\x22/g, '\\"') + '"';
}
};

function gen_code(ast, beautify) {
if (beautify) beautify = defaults(beautify, {
indent_start : 0,
Expand Down Expand Up @@ -1356,25 +1378,6 @@ function gen_code(ast, beautify) {
return add_spaces([ out, make_block(body) ]);
};

function make_string(str) {
// return '"' +
// str.replace(/\x5c/g, "\\\\")
// .replace(/\r?\n/g, "\\n")
// .replace(/\t/g, "\\t")
// .replace(/\r/g, "\\r")
// .replace(/\f/g, "\\f")
// .replace(/[\b]/g, "\\b")
// .replace(/\x22/g, "\\\"")
// .replace(/[\x00-\x1f]|[\x80-\xff]/g, function(c){
// var hex = c.charCodeAt(0).toString(16);
// if (hex.length < 2)
// hex = "0" + hex;
// return "\\x" + hex;
// })
// + '"';
return JSON.stringify(str); // STILL cheating.
};

function make_name(name) {
return name.toString();
};
Expand Down

0 comments on commit 9b619d1

Please sign in to comment.