Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle basic special characters in write #939

Merged
merged 2 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/srfi/38.scm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
;; This code was written by Alex Shinn in 2009 and placed in the
;; Public Domain. All warranties are disclaimed.

(define escaped-chars
'((#\alarm . "alarm")
(#\backspace . "backspace")
(#\delete . "delete")
(#\escape . "escape")
(#\newline . "newline")
(#\null . "null")
(#\return . "return")
(#\space . "space")
(#\tab . "tab")))

(define (raise-typed-error type)
(lambda (msg . args) (raise (make-exception type msg args #f #f))))
(define read-error (raise-typed-error 'read))
Expand Down Expand Up @@ -111,7 +122,12 @@
(and (type? type) (type-printer type)))
=> (lambda (printer) (printer x wr out)))
((null? x) (display "()" out))
((char? x) (display "#\\" out) (write-char x out))
((char? x)
(display "#\\" out)
(let ((pair (assv x escaped-chars)))
(if pair
(display (cdr pair) out)
(write-char x out))))
((symbol? x) (write x out))
((number? x) (display (number->string x) out))
((eq? x #t) (display "#t" out))
Expand Down
2 changes: 2 additions & 0 deletions lib/srfi/38/test.sld
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
(vector-set! x 2 x)
x))

(test-io "#\\newline" #\newline)

(test '+.! (read-from-string "+.!"))

(test 255 (read-from-string "#xff"))
Expand Down