-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathescaping.js
112 lines (103 loc) · 2.37 KB
/
escaping.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Copyright (c) Sebastian Kucharczyk <kuchen@kekse.biz>
* https://kekse.biz/ https://github.com/kekse1/javascripts/
* v0.2.0
*
* String extensions to support C {un,}escaping. Example given
* `\n` is the newline character.
*
* Supports both escaping and unescaping. The latter will
* produce strings with the encoded values, so `\n` will
* become a real newline byte, and the first one will
* encode the string `\n` out of the `\10` byte code.
*
* This will implement/extend globally (String.prototype),
* so you don't need to import it twice (but checks it).
*/
//
if(typeof String.prototype.escape !== 'function')
{
Reflect.defineProperty(String.prototype, 'escape', { value: function()
{
var result = '', byte;
for(var i = 0; i < this.length; ++i)
{
if((byte = this.charCodeAt(i)) < 32) switch(byte)
{
case 0: result += '\\0'; break;
case 7: result += '\\a'; break;
case 8: result += '\\b'; break;
case 9: result += '\\t'; break;
case 10: result += '\\n'; break;
case 11: result += '\\v'; break;
case 12: result += '\\f'; break;
case 13: result += '\\r'; break;
case 27: result += '\\e'; break;
default: result += this[i]; break;
}
else
{
result += this[i];
}
}
return result;
}});
}
if(typeof String.prototype.unescape !== 'function')
{
Reflect.defineProperty(String.prototype, 'unescape', { value: function()
{
var result = '', byte;
for(var i = 0; i < this.length; ++i)
{
if(this[i] === '\\' && i < (this.length - 1))
{
if(this[i + 1] === '\\')
{
result += this[++i];
continue;
}
byte = this.charCodeAt(++i);
switch(byte)
{
case 48:
result += String.fromCharCode(0);
break;
case 97:
result += String.fromCharCode(7);
break;
case 98:
result += String.fromCharCode(8);
break;
case 101:
result += String.fromCharCode(27);
break;
case 116:
result += String.fromCharCode(9);
break;
case 110:
result += String.fromCharCode(10);
break;
case 118:
result += String.fromCharCode(11);
break;
case 102:
result += String.fromCharCode(12);
break;
case 114:
result += String.fromCharCode(13);
break;
default:
result += this[--i];
break;
}
}
else
{
result += this[i];
}
}
return result;
}});
}
//