]>
git.saurik.com Git - utf16js.git/blob - utf16.js
87f9b910e870700fa55c9fb027cf507344750834
1 if (typeof define
!== 'function') { var define
= require('amdefine')(module
) }
3 define(function(require
) {
5 var decode = function(string
, points
) {
6 if (typeof points
=== "undefined")
9 for (var i
= 0, e
= string
.length
; i
!= e
; ++i
) {
10 var unit
= string
.charCodeAt(i
);
11 var part
= unit
& 0xfc00;
14 else if (part
!= 0xd800)
19 var next
= string
.charCodeAt(i
);
20 if ((next
& 0xfc00) != 0xdc00)
22 points
.push(0x10000 | (unit
& 0x03ff) << 10 | next
& 0x03ff);
29 var encode = function(points
) {
31 for (var i
= 0, e
= points
.length
; i
!= e
; ++i
) {
32 var point
= points
[i
];
37 units
.push(0xd800 | (0xffc00 & point
) >> 10, 0xdc00 | 0x03ff & point
);
39 } return String
.fromCharCode
.apply(String
, units
);
42 var adjust = function(mode
, string
, after
) {
43 var before
= after
.slice();
44 var offset
= 0, index
= 0;
46 var check = function() {
47 var from = mode
? index : offset
;
48 var to
= mode
? offset : index
;
50 for (var o
= 0, l
= after
.length
; o
!= l
; ++o
)
51 if (before
[o
] == from) {
57 for (var e
= string
.length
; index
!= e
; ++index
) {
60 var unit
= string
.charCodeAt(index
);
61 var part
= unit
& 0xfc00;
65 else if (part
!= 0xd800);
66 else if (++index
== e
)
69 var next
= string
.charCodeAt(index
);
70 if ((next
& 0xfc00) != 0xdc00)
81 var point2unit = function(string
, offsets
) {
82 return adjust(false, string
, offsets
);
85 var unit2point = function(string
, offsets
) {
86 return adjust(true, string
, offsets
);
89 var charAt = function(string
, offset
) {
90 return substring(string
, offset
, offset
+ 1);
93 var charCodeAt = function(string
, offset
) {
94 var char = charAt(string
, offset
);
96 return char.charCodeAt(0);
97 return 0x10000 | (char.charCodeAt(0) & 0x03ff) << 10 | char.charCodeAt(1) & 0x03ff
100 var length = function(string
) {
101 return unit2point(string
, [string
.length
])[0];
104 var substr = function(string
, start
, length
) {
106 if (typeof length
!== "undefined")
107 stop
= start
+ length
;
108 return substring(string
, start
, stop
);
111 var substring = function(string
, start
, stop
) {
112 var range
= point2unit(string
, [start
, stop
]);
113 return string
.substring(range
[0], range
[1]);
116 var Unicode = function(string
) {
117 this.string
= string
;
118 this.length
= length(string
);
121 Unicode
.fromCharCode = function() {
122 return encode(arguments
);
125 Unicode
.prototype.charAt = function(index
) {
126 return new Unicode(charAt(this.string
, index
));
129 Unicode
.prototype.charCodeAt = function(index
) {
130 return charCodeAt(this.string
, index
);
133 Unicode
.prototype.substr = function(start
, length
) {
134 return new Unicode(substr(this.string
, start
, length
));
137 Unicode
.prototype.substring = function(start
, end
) {
138 return new Unicode(substring(this.string
, start
, end
));
141 Unicode
.prototype.toString = function() {
149 point2unit: point2unit
,
150 unit2point: unit2point
,
153 charCodeAt: charCodeAt
,
156 substring: substring
,