-
Notifications
You must be signed in to change notification settings - Fork 222
/
Copy pathutf8_encode.js
57 lines (56 loc) · 1.52 KB
/
utf8_encode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* UTF8 encoder
* @private
*/
var utf8_encode;
export default utf8_encode = function(argString) {
var c1, enc, end, n, start, string, stringl, utftext;
// http://kevin.vanzonneveld.net
// + original by: Webtoolkit.info (http://www.webtoolkit.info/)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: sowberry
// + tweaked by: Jack
// + bugfixed by: Onno Marsman
// + improved by: Yves Sucaet
// + bugfixed by: Onno Marsman
// + bugfixed by: Ulrich
// + bugfixed by: Rafal Kukawski
// + improved by: kirilloid
// * example 1: utf8_encode('Kevin van Zonneveld');
// * returns 1: 'Kevin van Zonneveld'
if (argString === null || typeof argString === 'undefined') {
return '';
}
string = argString + '';
// .replace(/\r\n/g, "\n").replace(/\r/g, "\n");
utftext = '';
start = void 0;
end = void 0;
stringl = 0;
start = end = 0;
stringl = string.length;
n = 0;
while (n < stringl) {
c1 = string.charCodeAt(n);
enc = null;
if (c1 < 128) {
end++;
} else if (c1 > 127 && c1 < 2048) {
enc = String.fromCharCode(c1 >> 6 | 192, c1 & 63 | 128);
} else {
enc = String.fromCharCode(c1 >> 12 | 224, c1 >> 6 & 63 | 128, c1 & 63 | 128);
}
if (enc !== null) {
if (end > start) {
utftext += string.slice(start, end);
}
utftext += enc;
start = end = n + 1;
}
n++;
}
if (end > start) {
utftext += string.slice(start, stringl);
}
return utftext;
};