define(function(require) {
-var decode = function(string, points) {
+var utf16 = function(string) {
+ this.string = string;
+ this.length = utf16.strlen(string);
+};
+
+utf16.decode = function(string, points) {
if (typeof points === "undefined")
points = [];
return points;
};
-var encode = function(points) {
+utf16.encode = function(points) {
var units = [];
for (var i = 0, e = points.length; i != e; ++i) {
var point = points[i];
} return String.fromCharCode.apply(String, units);
};
-var adjust = function(mode, string, after) {
+utf16.fromCharCode = function() {
+ return new utf16(utf16.encode(arguments), arguments.length);
+};
+
+utf16.adjust = function(mode, string, after) {
var before = after.slice();
var offset = 0, index = 0;
return after;
};
-var point2unit = function(string, offsets) {
- return adjust(false, string, offsets);
+utf16.point2unit = function(string, offsets) {
+ return utf16.adjust(false, string, offsets);
};
-var unit2point = function(string, offsets) {
- return adjust(true, string, offsets);
+utf16.unit2point = function(string, offsets) {
+ return utf16.adjust(true, string, offsets);
};
-var charAt = function(string, offset) {
- return substring(string, offset, offset + 1);
+utf16.charAt = function(string, offset) {
+ return utf16.substring(string, offset, offset + 1);
};
-var charCodeAt = function(string, offset) {
- var char = charAt(string, offset);
+utf16.charCodeAt = function(string, offset) {
+ var char = utf16.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];
+utf16.strlen = function(string) {
+ return utf16.unit2point(string, [string.length])[0];
};
-var substr = function(string, start, length) {
+utf16.substr = function(string, start, length) {
var stop;
if (typeof length !== "undefined")
stop = start + length;
- return substring(string, start, stop);
+ return utf16.substring(string, start, stop);
};
-var substring = function(string, start, stop) {
- var range = point2unit(string, [start, stop]);
+utf16.substring = function(string, start, stop) {
+ var range = utf16.point2unit(string, [start, stop]);
return string.substring(range[0], range[1]);
};
-var Unicode = function(string) {
- this.string = string;
- this.length = length(string);
-};
-
-Unicode.fromCharCode = function() {
- return encode(arguments);
-};
-
-Unicode.prototype.charAt = function(index) {
- return new Unicode(charAt(this.string, index));
+utf16.prototype.charAt = function(index) {
+ return new utf16(utf16.charAt(this.string, index));
};
-Unicode.prototype.charCodeAt = function(index) {
- return charCodeAt(this.string, index);
+utf16.prototype.charCodeAt = function(index) {
+ return utf16.charCodeAt(this.string, index);
};
-Unicode.prototype.substr = function(start, length) {
- return new Unicode(substr(this.string, start, length));
+utf16.prototype.substr = function(start, length) {
+ return new utf16(utf16.substr(this.string, start, length));
};
-Unicode.prototype.substring = function(start, end) {
- return new Unicode(substring(this.string, start, end));
+utf16.prototype.substring = function(start, end) {
+ return new utf16(utf16.substring(this.string, start, end));
};
-Unicode.prototype.toString = function() {
+utf16.prototype.toString = function() {
return this.string;
};
-return {
- decode: decode,
- encode: encode,
-
- point2unit: point2unit,
- unit2point: unit2point,
-
- charAt: charAt,
- charCodeAt: charCodeAt,
- length: length,
- substr: substr,
- substring: substring,
-
- Unicode: Unicode,
-};
+return utf16;
});