]> git.saurik.com Git - utf16js.git/blame - utf16.js
Export a Unicode class as replacement for string.
[utf16js.git] / utf16.js
CommitLineData
5951f915
JF
1if (typeof define !== 'function') { var define = require('amdefine')(module) }
2
3define(function(require) {
4
9d0c291d
JF
5var decode = function(string, points) {
6 if (typeof points === "undefined")
7 points = [];
8
5951f915
JF
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)
9d0c291d 15 points.push(unit);
5951f915
JF
16 else if (++i == e)
17 return null;
18 else {
19 var next = string.charCodeAt(i);
20 if ((next & 0xfc00) != 0xdc00)
21 return null;
9d0c291d 22 points.push(0x10000 | (unit & 0x03ff) << 10 | next & 0x03ff);
5951f915
JF
23 }
24 }
25
9d0c291d 26 return points;
5951f915
JF
27};
28
9d0c291d 29var encode = function(points) {
5951f915 30 var units = [];
9d0c291d
JF
31 for (var i = 0, e = points.length; i != e; ++i) {
32 var point = points[i];
5951f915
JF
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
bd3618a4
JF
42var adjust = function(mode, string, after) {
43 var before = after.slice();
44 var offset = 0, index = 0;
45
46 var check = function() {
47 var from = mode ? index : offset;
48 var to = mode ? offset : index;
49
50 for (var o = 0, l = after.length; o != l; ++o)
51 if (before[o] == from) {
52 before[o] = null;
53 after[o] = to;
54 }
55 };
56
57 for (var e = string.length; index != e; ++index) {
58 check();
59
60 var unit = string.charCodeAt(index);
61 var part = unit & 0xfc00;
62
63 if (part == 0xdc00)
64 return null;
65 else if (part != 0xd800);
66 else if (++index == e)
67 return null;
68 else {
69 var next = string.charCodeAt(index);
70 if ((next & 0xfc00) != 0xdc00)
71 return null;
72 }
73
74 ++offset;
75 }
76
77 check();
78 return after;
79};
80
81var point2unit = function(string, offsets) {
82 return adjust(false, string, offsets);
83};
84
85var unit2point = function(string, offsets) {
86 return adjust(true, string, offsets);
87};
88
89var charAt = function(string, offset) {
90 return substring(string, offset, offset + 1);
91};
92
93var length = function(string) {
94 return unit2point(string, [string.length])[0];
95};
96
97var substr = function(string, start, length) {
98 var stop;
99 if (typeof length !== "undefined")
100 stop = start + length;
101 return substring(string, start, stop);
102};
103
104var substring = function(string, start, stop) {
105 var range = point2unit(string, [start, stop]);
106 return string.substring(range[0], range[1]);
107};
108
8317d54a
JF
109var Unicode = function(string) {
110 this.string = string;
111 this.length = length(string);
112};
113
114Unicode.prototype.charAt = function(index) {
115 return new Unicode(charAt(this.string, index));
116};
117
118Unicode.prototype.substr = function(start, length) {
119 return new Unicode(substr(this.string, start, length));
120};
121
122Unicode.prototype.substring = function(start, end) {
123 return new Unicode(substring(this.string, start, end));
124};
125
126Unicode.prototype.toString = function() {
127 return this.string;
128};
129
5951f915
JF
130return {
131 decode: decode,
132 encode: encode,
bd3618a4
JF
133
134 point2unit: point2unit,
135 unit2point: unit2point,
136
137 charAt: charAt,
138 length: length,
139 substr: substr,
140 substring: substring,
8317d54a
JF
141
142 Unicode: Unicode,
5951f915
JF
143};
144
145});