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