aboutsummaryrefslogtreecommitdiff
path: root/src/runtime.js
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2012-07-14 20:40:32 -0700
committerAlon Zakai <alonzakai@gmail.com>2012-07-14 20:40:32 -0700
commit5fd21fc62fd407517ddfb0da706f7fcb757cf1be (patch)
treefe8a5ef761183cd77128b584ba96624fa724d6b0 /src/runtime.js
parent4ffb769331329f8997d3597854cabe4e109e8684 (diff)
refactor utf8processing
Diffstat (limited to 'src/runtime.js')
-rw-r--r--src/runtime.js34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/runtime.js b/src/runtime.js
index 1f8a618f..e39fa61e 100644
--- a/src/runtime.js
+++ b/src/runtime.js
@@ -334,6 +334,40 @@ var Runtime = {
return Runtime.funcWrappers[func];
},
+ UTF8Processor: function() {
+ var buffer = [];
+ var needed = 0;
+ this.feed = function (code) {
+ code = code & 0xff;
+ if (needed) {
+ buffer.push(code);
+ needed--;
+ }
+ if (buffer.length == 0) {
+ if (code < 128) return String.fromCharCode(code);
+ buffer.push(code);
+ if (code > 191 && code < 224) {
+ needed = 1;
+ } else {
+ needed = 2;
+ }
+ return '';
+ }
+ if (needed > 0) return '';
+ var c1 = buffer[0];
+ var c2 = buffer[1];
+ var c3 = buffer[2];
+ var ret;
+ if (c1 > 191 && c1 < 224) {
+ ret = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
+ } else {
+ ret = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
+ }
+ buffer.length = 0;
+ return ret;
+ }
+ },
+
#if RUNTIME_DEBUG
debug: true, // Switch to false at runtime to disable logging at the right times