From b4b691fafd7934f3eecbe31fe42c824d5319d612 Mon Sep 17 00:00:00 2001 From: "Robert J. Simmons" <442315+robsimmons@users.noreply.github.com> Date: Thu, 26 Dec 2024 21:40:32 -0500 Subject: [PATCH] Change defaults to be more unicodey (#95) In response to C's request [here](https://icosahedron.website/@cxli/113721094857952517), change the defaults to show the unicode character in more cases, instead of showing the unicode escapes. Before: ![image](https://github.com/user-attachments/assets/ac19cf5b-5c2a-4710-925f-15168f2d77fe) After: ![image](https://github.com/user-attachments/assets/37700d2e-6eef-4bf1-a58c-c55118030509) This technically changes the Dusa interface, but I think the `escapeString` function was mostly being used in debugging and via the web editor's sketchzone. --- src/client.test.ts | 4 ++-- src/datastructures/data.test.ts | 4 ++-- src/datastructures/data.ts | 35 ++++++++++++++++----------------- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/client.test.ts b/src/client.test.ts index 3bc1830..def6bce 100644 --- a/src/client.test.ts +++ b/src/client.test.ts @@ -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}', diff --git a/src/datastructures/data.test.ts b/src/datastructures/data.test.ts index 7682eeb..6122986 100644 --- a/src/datastructures/data.test.ts +++ b/src/datastructures/data.test.ts @@ -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', diff --git a/src/datastructures/data.ts b/src/datastructures/data.ts index b56479c..16e9910 100644 --- a/src/datastructures/data.ts +++ b/src/datastructures/data.ts @@ -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(''); }