read a string's escapes the way C spells them

An escape the reader did not know lost its backslash, so "\x41" read
as "x41" and "\012" as NUL and "12". A string is text, so \x and octal
reach only as far as ASCII; past it, \u and \U name the character.
This commit is contained in:
2026-10-16 17:30:47 +03:00
parent b5f9089bf1
commit eed5a209b3
2 changed files with 76 additions and 10 deletions

View File

@@ -92,6 +92,29 @@
;; a name that only begins like a number is still a name
(reader-test '(3dmath.h) "3dmath.h")
;; A string is text, and an escape is one of its characters, spelled
;; the way C spells it. An escape the reader did not know lost its
;; backslash, so "\x41" read as "x41" and "\012" as NUL and "12"
(reader-test '("A" "A" "A" "A") "\"\\x41\" \"\\101\" \"\\u0041\" \"\\U00000041\"")
(reader-test '("\n" "'?" "\x7f;") "\"\\012\" \"\\'\\?\" \"\\x7f\"")
(reader-test '("é" "😀") "\"\\u00e9\" \"\\U0001F600\"")
;; a backslash ending a line joins it to the next, as in C
(reader-test '("ab") "\"a\\\nb\"")
;; C reads \xff and \377 as a byte, which text has none of: what is
;; past ASCII is written as the character, or its \u
(test "a byte past ASCII is not text"
"a byte past ASCII is not text; write the character or \\u, or a [u8 N] array for bytes"
(read-error "\"\\xff\""))
(test "in octal neither" #t (string? (read-error "\"\\377\"")))
(test "nor is a run of hex digits, which \\x takes all of"
#t (string? (read-error "\"\\x41BC\"")))
(test "an escape C has not is an error" "unknown escape" (read-error "\"\\q\""))
(test "\\x needs a digit" "\\x needs a hex digit" (read-error "\"\\xg\""))
(test "\\u takes four"
"\\u takes four hex digits and \\U eight" (read-error "\"\\u41\""))
(test "a surrogate is no character"
"no character has this code point" (read-error "\"\\uD800\""))
;; `;' comments are preserved as (comment "...") forms
(reader-test '((comment " hi")) "; hi")
(reader-test '((comment ";; Prototypes")) ";;; Prototypes")