Skip to content

Commit 1782076

Browse files
fix(decode): support \uXXXX escape per §7.1
Per the v3.2 spec, control characters U+0000–U+001F outside of \n, \r, \t encode as \uXXXX escapes and decode them back. Lone surrogates are rejected. - escapeString emits \uXXXX for any remaining U+0000–U+001F byte - unescapeString recognises \uXXXX (4 hex digits) and rejects D800–DFFF - isSafeUnquoted widens the control-char check to cover the full range
1 parent caa59ae commit 1782076

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

‎packages/toon/src/shared/string-utils.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { BACKSLASH, CARRIAGE_RETURN, DOUBLE_QUOTE, NEWLINE, TAB } from '../const
55
*
66
* @remarks
77
* Handles backslashes, quotes, newlines, carriage returns, and tabs.
8+
* Other U+0000–U+001F control characters are emitted as `\uXXXX`.
89
*/
910
export function escapeString(value: string): string {
1011
return value
@@ -13,13 +14,15 @@ export function escapeString(value: string): string {
1314
.replace(/\n/g, `${BACKSLASH}n`)
1415
.replace(/\r/g, `${BACKSLASH}r`)
1516
.replace(/\t/g, `${BACKSLASH}t`)
17+
.replace(/[\u0000-\u001f]/g, c => `${BACKSLASH}u${c.charCodeAt(0).toString(16).padStart(4, '0')}`)
1618
}
1719

1820
/**
1921
* Unescapes a string by processing escape sequences.
2022
*
2123
* @remarks
22-
* Handles `\n`, `\t`, `\r`, `\\`, and `\"` escape sequences.
24+
* Handles `\n`, `\t`, `\r`, `\\`, `\"`, and `\uXXXX` escape sequences.
25+
* Lone surrogates in `\uXXXX` are rejected.
2326
*/
2427
export function unescapeString(value: string): string {
2528
let unescaped = ''
@@ -57,6 +60,22 @@ export function unescapeString(value: string): string {
5760
i += 2
5861
continue
5962
}
63+
if (next === 'u') {
64+
if (i + 6 > value.length) {
65+
throw new SyntaxError(`Invalid escape sequence: truncated \\u escape at "${value.slice(i, i + 6)}"`)
66+
}
67+
const hex = value.slice(i + 2, i + 6)
68+
if (!/^[0-9a-f]{4}$/i.test(hex)) {
69+
throw new SyntaxError(`Invalid escape sequence: \\u must be followed by 4 hex digits, got "${hex}"`)
70+
}
71+
const codeUnit = Number.parseInt(hex, 16)
72+
if (codeUnit >= 0xD800 && codeUnit <= 0xDFFF) {
73+
throw new SyntaxError(`Invalid escape sequence: \\u${hex} is a lone surrogate; supplementary code points MUST appear as literal UTF-8`)
74+
}
75+
unescaped += String.fromCodePoint(codeUnit)
76+
i += 6
77+
continue
78+
}
6079

6180
throw new SyntaxError(`Invalid escape sequence: \\${next}`)
6281
}

‎packages/toon/src/shared/validation.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ export function isSafeUnquoted(value: string, delimiter: string = DEFAULT_DELIMI
7171
return false
7272
}
7373

74-
// Check for control characters (newline, carriage return, tab - always need quoting/escaping)
75-
if (/[\n\r\t]/.test(value)) {
74+
// Check for control characters (any U+0000–U+001F always need quoting/escaping)
75+
if (/[\u0000-\u001f]/.test(value)) {
7676
return false
7777
}
7878

0 commit comments

Comments
 (0)