diff options
Diffstat (limited to 'src/runtime.js')
-rw-r--r-- | src/runtime.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/runtime.js b/src/runtime.js index 1f8a618f..6defbb35 100644 --- a/src/runtime.js +++ b/src/runtime.js @@ -334,6 +334,51 @@ var Runtime = { return Runtime.funcWrappers[func]; }, + // Returns a processor of UTF. + // processCChar() receives characters from a C-like UTF representation and returns JS string fragments. + // processJSString() receives a JS string and returns a C-like UTF representation in an array + UTF8Processor: function() { + var buffer = []; + var needed = 0; + this.processCChar = 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; + } + this.processJSString = function(string) { + string = unescape(encodeURIComponent(string)); + var ret = []; + for (var i = 0; i < string.length; i++) { + ret.push(string.charCodeAt(i)); + } + return ret; + } + }, + #if RUNTIME_DEBUG debug: true, // Switch to false at runtime to disable logging at the right times |