@@ -8,14 +8,17 @@ function utf8_decode (str_data) {
8
8
// + bugfixed by: Onno Marsman
9
9
// + input by: Brett Zamir (http://brett-zamir.me)
10
10
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
11
+ // + bugfixed by: kirilloid
11
12
// * example 1: utf8_decode('Kevin van Zonneveld');
12
13
// * returns 1: 'Kevin van Zonneveld'
14
+
13
15
var tmp_arr = [ ] ,
14
16
i = 0 ,
15
17
ac = 0 ,
16
18
c1 = 0 ,
17
19
c2 = 0 ,
18
- c3 = 0 ;
20
+ c3 = 0 ,
21
+ c4 = 0 ;
19
22
20
23
str_data += '' ;
21
24
@@ -24,19 +27,25 @@ function utf8_decode (str_data) {
24
27
if ( c1 <= 191 ) {
25
28
tmp_arr [ ac ++ ] = String . fromCharCode ( c1 ) ;
26
29
i ++ ;
27
- } else if ( c1 >= 192 && c1 <= 223 ) {
30
+ } else if ( c1 <= 223 ) {
28
31
c2 = str_data . charCodeAt ( i + 1 ) ;
29
32
tmp_arr [ ac ++ ] = String . fromCharCode ( ( ( c1 & 31 ) << 6 ) | ( c2 & 63 ) ) ;
30
33
i += 2 ;
31
- } else if ( c1 >= 224 && c1 <= 239 ) {
34
+ } else if ( c1 <= 239 ) {
32
35
// http://en.wikipedia.org/wiki/UTF-8#Codepage_layout
33
36
c2 = str_data . charCodeAt ( i + 1 ) ;
34
37
c3 = str_data . charCodeAt ( i + 2 ) ;
35
38
tmp_arr [ ac ++ ] = String . fromCharCode ( ( ( c1 & 15 ) << 12 ) | ( ( c2 & 63 ) << 6 ) | ( c3 & 63 ) ) ;
36
39
i += 3 ;
37
40
} else {
38
- tmp_arr [ ac ++ ] = String . fromCharCode ( c1 ) ;
39
- i ++ ;
41
+ c2 = str_data . charCodeAt ( i + 1 ) ;
42
+ c3 = str_data . charCodeAt ( i + 2 ) ;
43
+ c4 = str_data . charCodeAt ( i + 3 ) ;
44
+ c1 = ( ( c1 & 7 ) << 18 ) | ( ( c2 & 63 ) << 12 ) | ( ( c3 & 63 ) << 6 ) | ( c4 & 63 ) ;
45
+ c1 -= 0x10000 ;
46
+ tmp_arr [ ac ++ ] = String . fromCharCode ( 0xD800 | ( ( c1 >> 10 ) & 0x3FF ) ) ;
47
+ tmp_arr [ ac ++ ] = String . fromCharCode ( 0xDC00 | ( c1 & 0x3FF ) ) ;
48
+ i += 4 ;
40
49
}
41
50
}
42
51
0 commit comments