@@ -36,16 +36,16 @@ private function TL($a)
36
36
$ tkk = $ this ->TKK ();
37
37
$ b = $ tkk [0 ];
38
38
39
- for ($ d = [], $ e = 0 , $ f = 0 ; $ f < mb_strlen ($ a, ' UTF-8 ' ); $ f ++) {
40
- $ g = $ this ->charCodeAt ($ a , $ f );
39
+ for ($ d = [], $ e = 0 , $ f = 0 ; $ f < $ this -> JS_length ($ a ); $ f ++) {
40
+ $ g = $ this ->JS_charCodeAt ($ a , $ f );
41
41
if (128 > $ g ) {
42
42
$ d [$ e ++] = $ g ;
43
43
} else {
44
44
if (2048 > $ g ) {
45
45
$ d [$ e ++] = $ g >> 6 | 192 ;
46
46
} else {
47
- if (55296 == ($ g & 64512 ) && $ f + 1 < mb_strlen ($ a, ' UTF-8 ' ) && 56320 == ($ this ->charCodeAt ($ a , $ f + 1 ) & 64512 )) {
48
- $ g = 65536 + (($ g & 1023 ) << 10 ) + ($ this ->charCodeAt ($ a , ++$ f ) & 1023 );
47
+ if (55296 == ($ g & 64512 ) && $ f + 1 < $ this -> JS_length ($ a ) && 56320 == ($ this ->JS_charCodeAt ($ a , $ f + 1 ) & 64512 )) {
48
+ $ g = 65536 + (($ g & 1023 ) << 10 ) + ($ this ->JS_charCodeAt ($ a , ++$ f ) & 1023 );
49
49
$ d [$ e ++] = $ g >> 18 | 240 ;
50
50
$ d [$ e ++] = $ g >> 12 & 63 | 128 ;
51
51
} else {
@@ -138,23 +138,27 @@ private function unsignedRightShift($a, $b)
138
138
}
139
139
140
140
/**
141
- * Get the Unicode of the character at the specified index in a string.
141
+ * Get JS charCodeAt equivalent result with UTF-16 encoding
142
142
*
143
143
* @param string $str
144
144
* @param int $index
145
145
*
146
- * @return null| number
146
+ * @return number
147
147
*/
148
- private function charCodeAt ($ str , $ index )
149
- {
150
- $ char = mb_substr ($ str , $ index , 1 , 'UTF-8 ' );
151
- if (mb_check_encoding ($ char , 'UTF-8 ' )) {
152
- $ ret = mb_convert_encoding ($ char , 'UTF-32BE ' , 'UTF-8 ' );
153
- $ result = hexdec (bin2hex ($ ret ));
154
-
155
- return $ result ;
156
- }
148
+ private function JS_charCodeAt ($ str , $ index ) {
149
+ $ utf16 = mb_convert_encoding ($ str , 'UTF-16LE ' , 'UTF-8 ' );
150
+ return ord ($ utf16 [$ index *2 ]) + (ord ($ utf16 [$ index *2 +1 ]) << 8 );
151
+ }
157
152
158
- return ;
153
+ /**
154
+ * Get JS equivalent string length with UTF-16 encoding
155
+ *
156
+ * @param string $str
157
+ *
158
+ * @return number
159
+ */
160
+ private function JS_length ($ str ) {
161
+ $ utf16 = mb_convert_encoding ($ str , 'UTF-16LE ' , 'UTF-8 ' );
162
+ return strlen ($ utf16 )/2 ;
159
163
}
160
164
}
0 commit comments