Created
April 17, 2010 22:59
-
-
Save notmasteryet/369881 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head></head> | |
<body> | |
<script type="text/javascript"> | |
(function () { | |
this.P = function() { | |
var r = []; | |
for(var i=0;i<100000;++i) r.push(i); // fill memory | |
var aCode = 'String b = "Stringb";\n// "string" \n alert("b");'; | |
alert(P.parse(aCode, new Object())); | |
}; | |
P.parse = function(aCode, p) { | |
// Force characters-as-bytes to work. | |
//aCode = aCode.replace(/('(.){1}')/g, "$1.charCodeAt(0)"); | |
aCode = aCode.replace(/'.{1}'/g, function(all) { | |
return "(new Char(" + all + "))"; | |
}); | |
// Saves all strings into an array | |
// masks all strings into <STRING n> | |
// to be replaced with the array strings after parsing is finished | |
var strings = []; | |
aCode = aCode.replace(/(["'])(\\\1|.)*?(\1)/g, function(all) { | |
strings.push(all); | |
return "<STRING " + (strings.length - 1) + ">"; | |
}); | |
// Windows newlines cause problems: | |
aCode = aCode.replace(/\r\n?/g, "\n"); | |
// Remove end-of-line comments | |
aCode = aCode.replace(/\/\/.*\n/g, "\n"); | |
// replaces all masked strings from <STRING n> to the appropriate string contained in the strings array | |
for (var n = 0; n < strings.length; n++) { | |
aCode = (function() { | |
return aCode.replace(new RegExp("(.*)(<STRING " + n + ">)(.*)", "g"), function(all, quoteStart, match, quoteEnd) { | |
var notString = true, | |
quoteType = "", | |
escape = false; | |
for (var x = 0; x < quoteStart.length; x++) { | |
if (notString) { | |
if (quoteStart.charAt(x) === "\"" || quoteStart.charAt(x) === "'") { | |
quoteType = quoteStart.charAt(x); | |
notString = false; | |
} | |
} else { | |
if (!escape) { | |
if (quoteStart.charAt(x) === "\\") { | |
escape = true; | |
} else if (quoteStart.charAt(x) === quoteType) { | |
notString = true; | |
quoteType = ""; | |
} | |
} else { | |
escape = false; | |
} | |
} | |
} | |
if (notString) { // Match is not inside a string | |
return quoteStart + strings[n] + quoteEnd; | |
} else { | |
return all; | |
} | |
}); | |
}()); | |
} | |
return aCode; | |
}; | |
var init = function() { | |
P(); | |
}; | |
document.addEventListener('DOMContentLoaded', function() { | |
init(); | |
}, false); | |
})(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment