comparison 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
comparison
equal deleted inserted replaced
145:7db37f040a6f 146:d8f92ebf1ff6
165 var nextStringId = 0; 165 var nextStringId = 0;
166 166
167 strlit.prototype.toC = function() { 167 strlit.prototype.toC = function() {
168 if (!(this.val in declaredStrings)) { 168 if (!(this.val in declaredStrings)) {
169 //TODO: get the proper byte length 169 //TODO: get the proper byte length
170 toplevelcode += 'string str_' + nextStringId + ' = {{&string_meta, NULL}, ' + this.val.length + ', ' + this.val.length + ', "' + this.val.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n').replace('\r', '\\r') + '"};\n'; 170 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';
171 declaredStrings[this.val] = nextStringId++; 171 declaredStrings[this.val] = nextStringId++;
172 } 172 }
173 return '((object *)&str_' + declaredStrings[this.val] + ')'; 173 return '((object *)&str_' + declaredStrings[this.val] + ')';
174 }; 174 };
175 strlit.prototype.toCLLExpr = function(vars) { 175 strlit.prototype.toCLLExpr = function(vars) {
176 return '"' + this.val.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n').replace('\r', '\\r') + '"'; 176 return '"' + this.val.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, '\\n').replace(/\r/g, '\\r') + '"';
177 }; 177 };
178 strlit.prototype.toCLines = function(vars, needsreturn) { 178 strlit.prototype.toCLines = function(vars, needsreturn) {
179 return [ (needsreturn ? 'return (object *)' : '' ) + this.toCLLExpr(vars) +';' ]; 179 return [ (needsreturn ? 'return (object *)' : '' ) + this.toCLLExpr(vars) +';' ];
180 }; 180 };
181 181