This repository has been archived by the owner on Jan 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
utf8.js
executable file
·81 lines (69 loc) · 1.71 KB
/
utf8.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* utf8
*
* @version 0.9
* @copyright Copyright (c) 2007-2012 Harald Hanek
* @license MIT (http://harrydeluxe.mit-license.org)
*/
(function(){
/**
* Extend the string prototype with the method under the given name if it doesn't currently exist.
*
* @private
*/
function append(name, method)
{
if(!String.prototype[name])
String.prototype[name] = method;
}
/**
* Decodes a utf-8 encoded string back into multi-byte characters.
*
* @example "\xE2\x82\xAC".decodeUTF8();
* @result "€"
*
* @name decodeUTF8
* @return String
*/
append("decodeUTF8", function(){
var str = this;
// 2-byte chars
str = str.replace(/[\u00c0-\u00df][\u0080-\u00bf]/g, function(c)
{
var cc = (c.charCodeAt(0)&0x1f)<<6 | c.charCodeAt(1)&0x3f;
return String.fromCharCode(cc);
});
// 3-byte chars
str = str.replace(/[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/g, function(c)
{
var cc = (c.charCodeAt(0)&0x0f)<<12 | (c.charCodeAt(1)&0x3f<<6) | c.charCodeAt(2)&0x3f;
return String.fromCharCode(cc);
});
return str;
});
/**
* Encodes a multi-byte string into utf-8 multiple single-byte characters.
*
* @example "Straße".encodeUTF8();
* @result "StraÃe"
*
* @name encodeUTF8
* @return String
*/
append("encodeUTF8", function(){
var str = this;
// U+0080 - U+07FF = 2-byte chars
str = str.replace(/[\u0080-\u07ff]/g, function(c)
{
var cc = c.charCodeAt(0);
return String.fromCharCode(0xc0 | cc>>6, 0x80 | cc&0x3f);
});
// U+0800 - U+FFFF = 3-byte chars
str = str.replace(/[\u0800-\uffff]/g, function(c)
{
var cc = c.charCodeAt(0);
return String.fromCharCode(0xe0 | cc>>12, 0x80 | cc>>6&0x3F, 0x80 | cc&0x3f);
});
return str;
});
})();