MCPcopy Create free account
hub / github.com/WebFreak001/code-debug / parseString

Function parseString

src/backend/mi_parse.ts:8–55  ·  view source on GitHub ↗
(str: string)

Source from the content-addressed store, hash-verified

6
7const octalMatch = /^[0-7]{3}/;
8function parseString(str: string): string {
9 const ret = Buffer.alloc(str.length * 4);
10 let bufIndex = 0;
11
12 if (str[0] != '"' || str[str.length - 1] != '"')
13 throw new Error("Not a valid string");
14 str = str.slice(1, -1);
15 let escaped = false;
16 for (let i = 0; i < str.length; i++) {
17 if (escaped) {
18 let m;
19 if (str[i] == '\\')
20 bufIndex += ret.write('\\', bufIndex);
21 else if (str[i] == '"')
22 bufIndex += ret.write('"', bufIndex);
23 else if (str[i] == '\'')
24 bufIndex += ret.write('\'', bufIndex);
25 else if (str[i] == 'n')
26 bufIndex += ret.write('\n', bufIndex);
27 else if (str[i] == 'r')
28 bufIndex += ret.write('\r', bufIndex);
29 else if (str[i] == 't')
30 bufIndex += ret.write('\t', bufIndex);
31 else if (str[i] == 'b')
32 bufIndex += ret.write('\b', bufIndex);
33 else if (str[i] == 'f')
34 bufIndex += ret.write('\f', bufIndex);
35 else if (str[i] == 'v')
36 bufIndex += ret.write('\v', bufIndex);
37 else if (str[i] == '0')
38 bufIndex += ret.write('\0', bufIndex);
39 else if (m = octalMatch.exec(str.substring(i))) {
40 ret.writeUInt8(parseInt(m[0], 8), bufIndex++);
41 i += 2;
42 } else
43 bufIndex += ret.write(str[i], bufIndex);
44 escaped = false;
45 } else {
46 if (str[i] == '\\')
47 escaped = true;
48 else if (str[i] == '"')
49 throw new Error("Not a valid string");
50 else
51 bufIndex += ret.write(str[i], bufIndex);
52 }
53 }
54 return ret.slice(0, bufIndex).toString("utf8");
55}
56
57export class MINode implements MIInfo {
58 token: number;

Callers 1

parseCStringFunction · 0.85

Calls 1

toStringMethod · 0.80

Tested by

no test coverage detected