-
Notifications
You must be signed in to change notification settings - Fork 5
/
esc.js
125 lines (116 loc) · 3.29 KB
/
esc.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
113
114
115
116
117
118
119
120
121
122
123
124
125
//noinspection JSLint
var g = typeof global === 'undefined' ? window : global;
//noinspection JSHint,JSLint
(function (papyrus) {
require('./spcf.js');
function tightJoin() {
return this.length === 1 ? this[0] : this.join('');
}
var escs = {
'\n': '\\n',
'\t': '\\t',
' ': '\\s',
'\u0008': '\\b',
'\u0018': '\\B',
'\r': '\\r',
'\\': '\\\\'
};
var cses = (function initUnEsc() {
var res = {};
var s;
for (s in escs) {
res[escs[s]] = s;
}
return res;
}());
var esctypes = {
'.pw': 1,
'.pt': 1,
'.in': 1,
'?ia': 1,
',er': 1
};
var re_meta = /([\\\.\^\$\*\+\?\(\)\[\]\{\}:=!\|,\-])/g;
var esc = {};
esc.esc = function (str) {
return (str || '').replace(/[\r\n\t \u0008\u0018\\]/g, function (a) { return escs[a]; });
};
esc.unesc = function (esc) {
//noinspection JSLint
return esc.replace(/\\./g, function (e) { return cses[e]; });
};
esc.svbuf = function (spec, val) {
var ret = [];
ret.svpush = esc.bufPush;
ret.toString = tightJoin;
if (spec) {
ret.svpush(spec, val);
}
return ret;
};
esc.bufPush = function (spec, val) {
if (esctypes[papyrus.spcf.type(spec)]) {
val = esc.esc(val);
}
this.push(spec + '\t' + val + '\n');
};
esc.svbufParse = function (msg) {
var ret = msg.split(/[\n\r]+/) || []; //msg.match(spcf.re_specval_mg) || [];
ret.reverse();
ret.svpop = esc.bufPop;
ret.toString = tightJoin;
return ret;
};
esc.bufPop = function () {
var line = this.pop();
if (!line) {
return null;
}
var spcf = papyrus.spcf;
var m = spcf.re_specval.exec(line);
if (!m) {
throw new Error('can\'t parse operation: "' + line + '"');
}
var ret = { spec: spcf.split(m[1]), val: m[5], specStr: m[1] };
if (esctypes[spcf.type(ret.spec)]) {
ret.val = esc.unesc(ret.val);
}
return ret;
};
esc.svbufParse_bySplit = function (msg) {
var ret;
if (!msg) {
ret = [];
} else {
ret = msg.split('\n');
}
ret.reverse();
ret.svpop = esc.bufPop;
ret.toString = tightJoin;
return ret;
};
esc.bufPop_bySubstr = function () {
var line = this.pop();
if (!line) { return null; }
var pos = line.indexOf('\t');
//noinspection JSLint
if (!~pos) { throw new TypeError('malformed specval'); }
var spcf = papyrus.spcf;
var specStr = line.substr(0, pos);
var val = line.substr(pos + 1);
var ret = {spec: spcf.split(specStr), val: val, specStr: specStr};
if (esctypes[spcf.type(ret.spec)]) {
ret.val = esc.unesc(ret.val);
}
return ret;
};
esc.htmlesc = function (text) {
return text.replace(/[&<>"'`]/g, function (chr) {
return '&#' + chr.charCodeAt(0) + ';';
});
};
esc.regesc = function (str) {
return str.replace(re_meta, '\\$1');
};
papyrus.esc = esc;
}(g['papyrus'] = g['papyrus'] || {}));