string-literals.js
1.95 KB
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
// string literal characters cannot contain control codes
var CONTROL_CODES = [0, // null
7, // bell
8, // backspace
9, // horizontal
10, // line feed
11, // vertical tab
12, // form feed
13, // carriage return
26, // Control-Z
27, // escape
127 // delete
]; // escaped sequences can either be a two character hex value, or one of the
// following single character codes
function decodeControlCharacter(char) {
switch (char) {
case "t":
return 0x09;
case "n":
return 0x0a;
case "r":
return 0x0d;
case '"':
return 0x22;
case "′":
return 0x27;
case "\\":
return 0x5c;
}
return -1;
}
var ESCAPE_CHAR = 92; // backslash
var QUOTE_CHAR = 34; // backslash
// parse string as per the spec:
// https://webassembly.github.io/spec/core/multipage/text/values.html#text-string
export function parseString(value) {
var byteArray = [];
var index = 0;
while (index < value.length) {
var charCode = value.charCodeAt(index);
if (CONTROL_CODES.indexOf(charCode) !== -1) {
throw new Error("ASCII control characters are not permitted within string literals");
}
if (charCode === QUOTE_CHAR) {
throw new Error("quotes are not permitted within string literals");
}
if (charCode === ESCAPE_CHAR) {
var firstChar = value.substr(index + 1, 1);
var decodedControlChar = decodeControlCharacter(firstChar);
if (decodedControlChar !== -1) {
// single character escaped values, e.g. \r
byteArray.push(decodedControlChar);
index += 2;
} else {
// hex escaped values, e.g. \2a
var hexValue = value.substr(index + 1, 2);
if (!/^[0-9A-F]{2}$/i.test(hexValue)) {
throw new Error("invalid character encoding");
}
byteArray.push(parseInt(hexValue, 16));
index += 3;
}
} else {
// ASCII encoded values
byteArray.push(charCode);
index++;
}
}
return byteArray;
}