MCPcopy Create free account
hub / github.com/aiscript-dev/aiscript / decodeUnicodeEscapeSequence

Function decodeUnicodeEscapeSequence

src/utils/characters.ts:27–74  ·  view source on GitHub ↗
(string: string)

Source from the content-addressed store, hash-verified

25}
26
27export function decodeUnicodeEscapeSequence(string: string): string {
28 let result = '';
29 let state: 'string' | 'escape' | 'digit' = 'string';
30 let digits = '';
31
32 for (let i = 0; i < string.length; i++) {
33 const char = string[i]!;
34
35 switch (state) {
36 case 'string': {
37 if (char === '\\') {
38 state = 'escape';
39 } else {
40 result += char;
41 }
42 break;
43 }
44
45 case 'escape': {
46 if (char !== 'u') {
47 throw new SyntaxError('invalid escape sequence');
48 }
49 state = 'digit';
50 break;
51 }
52
53 case 'digit': {
54 if (HEX_DIGIT.test(char)) {
55 digits += char;
56 } else {
57 throw new SyntaxError('invalid escape sequence');
58 }
59 if (digits.length === 4) {
60 result += String.fromCharCode(Number.parseInt(digits, 16));
61 state = 'string';
62 digits = '';
63 }
64 break;
65 }
66 }
67 }
68
69 if (state !== 'string') {
70 throw new SyntaxError('invalid escape sequence');
71 }
72
73 return result;
74}

Callers 2

characters.tsFile · 0.90
tryReadWordMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected