diff cbackend.js @ 146:d8f92ebf1ff6

Fix string literal escaping to allow more than one of each type of escaped character
author Mike Pavone <pavone@retrodev.com>
date Fri, 09 Aug 2013 04:29:37 -0700
parents 282b8056b702
children 9de2572a34a7 18598163e3ef
line wrap: on
line diff
--- a/cbackend.js	Fri Aug 09 04:15:53 2013 -0700
+++ b/cbackend.js	Fri Aug 09 04:29:37 2013 -0700
@@ -167,13 +167,13 @@
 strlit.prototype.toC = function() {
 	if (!(this.val in declaredStrings)) {
 		//TODO: get the proper byte length
-		toplevelcode += 'string str_' + nextStringId + ' = {{&string_meta, NULL}, ' + this.val.length + ', ' + this.val.length + ', "' + this.val.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n').replace('\r', '\\r') + '"};\n';
+		toplevelcode += 'string str_' + nextStringId + ' = {{&string_meta, NULL}, ' + this.val.length + ', ' + this.val.length + ', "' + this.val.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, '\\n').replace(/\r/g, '\\r') + '"};\n';
 		declaredStrings[this.val] = nextStringId++;
 	}
 	return '((object *)&str_' + declaredStrings[this.val] + ')';
 };
 strlit.prototype.toCLLExpr = function(vars) {
-	return '"' + this.val.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n').replace('\r', '\\r') + '"';
+	return '"' + this.val.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, '\\n').replace(/\r/g, '\\r') + '"';
 };
 strlit.prototype.toCLines = function(vars, needsreturn) {
 	return [ (needsreturn ? 'return (object *)' : '' ) + this.toCLLExpr(vars) +';' ];