Skip to content

Commit

Permalink
Change defaults to be more unicodey
Browse files Browse the repository at this point in the history
  • Loading branch information
robsimmons committed Dec 27, 2024
1 parent 444331b commit 689d937
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ test('Basic operation', () => {

test('String escapes', () => {
expect(
solutions(new Dusa('res "\\0\\b\\f\\n\\r\\t\\v\\\'\\"\\\\\\x12\\u{12}\\u{2601}".')),
).toStrictEqual(['res "\\x00\\x08\\x0c\\n\\x0d\\x09\\x0b\'\\"\\\\\\x12\\x12\\u{2601}"']);
solutions(new Dusa('res "\\0\\b\\f\\n\\r\\t\\v\\\'\\"\\\\\\x12\\u{12}\\u{2601}".')),
).toStrictEqual(['res "\\x00\\x08\\x0c\\n\\x0d\\x09\\x0b\'\\"\\\\\\x12\\x12☁☁"']);

expect(runForDusaError('a is "\\u{d901}".\n')).toStrictEqual([
'Cannot encode lone surrogate \\u{d901}',
Expand Down
4 changes: 2 additions & 2 deletions src/datastructures/data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ test('Internalizing basic types', () => {
'1',
'-1',
'"abc"',
'"\\"\\u{1f98a}\\""',
'"\'\\n\\x09\\x9d\\u{126}\\x00\\\\"',
'"\\"🦊\\""',
'"\'\\n\\x09\\x9dĦ\\x00\\\\"',
'a',
'b',
'c',
Expand Down
35 changes: 17 additions & 18 deletions src/datastructures/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,28 +161,27 @@ export function escapeString(input: string): string {
const escaped = [];
let i = 0;
while (i < input.length) {
if (input.codePointAt(i)! > 0xffff) {
escaped.push(`\\u{${input.codePointAt(i)!.toString(16)}}`);
i += 2;
} else {
const ch = input.charAt(i);
if (ch.charCodeAt(0) > 0xff) {
escaped.push(`\\u{${input.charCodeAt(i).toString(16)}}`);
} else if (ch.match(/[ !#-[\]-~]/)) {
escaped.push(ch);
} else if (ch === '\\') {
const code = input.codePointAt(i)!;
const ch = String.fromCodePoint(code);
switch (ch) {
case '\\':
escaped.push('\\\\');
} else if (ch === '"') {
break;
case '"':
escaped.push('\\"');
} else if (ch === '\n') {
break;
case '\n':
escaped.push('\\n');
} else if (ch.charCodeAt(0) >= 16) {
escaped.push(`\\x${input.charCodeAt(i).toString(16)}`);
} else {
escaped.push(`\\x0${input.charCodeAt(i).toString(16)}`);
}
i += 1;
break;
default:
if (code < 32 || (128 <= code && code < 160)) {
escaped.push(`\\x${input.charCodeAt(i).toString(16).padStart(2, '0')}`);
} else {
escaped.push(ch);
}
}
i += 1;
if (code > 0xffff) i += 1;
}
return escaped.join('');
}
Expand Down

0 comments on commit 689d937

Please sign in to comment.