]> git.saurik.com Git - utf16js.git/blob - utf16.js
89322ec7fc86ea264063ec9761beebb4f75e67cb
[utf16js.git] / utf16.js
1 if (typeof define !== 'function') { var define = require('amdefine')(module) }
2
3 define(function(require) {
4
5 var decode = function(array, string) {
6 for (var i = 0, e = string.length; i != e; ++i) {
7 var unit = string.charCodeAt(i);
8 var part = unit & 0xfc00;
9 if (part == 0xdc00)
10 return null;
11 else if (part != 0xd800)
12 array.push(unit);
13 else if (++i == e)
14 return null;
15 else {
16 var next = string.charCodeAt(i);
17 if ((next & 0xfc00) != 0xdc00)
18 return null;
19 array.push(0x10000 | (unit & 0x03ff) << 10 | next & 0x03ff);
20 }
21 }
22
23 return array;
24 };
25
26 var encode = function(array) {
27 var units = [];
28 for (var i = 0, e = array.length; i != e; ++i) {
29 var point = array[i];
30 if (point < 0x10000)
31 units.push(point);
32 else {
33 point -= 0x10000;
34 units.push(0xd800 | (0xffc00 & point) >> 10, 0xdc00 | 0x03ff & point);
35 }
36 } return String.fromCharCode.apply(String, units);
37 };
38
39 return {
40 decode: decode,
41 encode: encode,
42 };
43
44 });