From: Jay Freeman (saurik) Date: Thu, 27 Dec 2012 03:26:26 +0000 (+0000) Subject: Add replacements for charCodeAt and fromCharCode. X-Git-Url: https://git.saurik.com/utf16js.git/commitdiff_plain/dce456a2330210174fdcf03668142f8c4d123a07?ds=inline Add replacements for charCodeAt and fromCharCode. --- diff --git a/utf16.js b/utf16.js index 9c06cdd..87f9b91 100644 --- a/utf16.js +++ b/utf16.js @@ -90,6 +90,13 @@ var charAt = function(string, offset) { return substring(string, offset, offset + 1); }; +var charCodeAt = function(string, offset) { + var char = charAt(string, offset); + if (char.length != 2) + return char.charCodeAt(0); + return 0x10000 | (char.charCodeAt(0) & 0x03ff) << 10 | char.charCodeAt(1) & 0x03ff +}; + var length = function(string) { return unit2point(string, [string.length])[0]; }; @@ -111,10 +118,18 @@ var Unicode = function(string) { this.length = length(string); }; +Unicode.fromCharCode = function() { + return encode(arguments); +}; + Unicode.prototype.charAt = function(index) { return new Unicode(charAt(this.string, index)); }; +Unicode.prototype.charCodeAt = function(index) { + return charCodeAt(this.string, index); +}; + Unicode.prototype.substr = function(start, length) { return new Unicode(substr(this.string, start, length)); }; @@ -135,6 +150,7 @@ return { unit2point: unit2point, charAt: charAt, + charCodeAt: charCodeAt, length: length, substr: substr, substring: substring,