bytes->jsexpr: toss exn:fail:contract? when given invalid UTF-8

This commit is contained in:
Edward Lee 2015-05-12 12:46:23 -04:00 committed by Matthew Flatt
parent ec6060b9da
commit 46030642fa
2 changed files with 5 additions and 2 deletions

View File

@ -119,6 +119,9 @@
(string->jsexpr "") => eof
(string->jsexpr " \t\r\n") => eof
;; Invalid UTF-8 input.
(bytes->jsexpr #"\"\377\377\377\"") =error> exn:fail:contract?
;; More string escapes:
(string->jsexpr @T{ "hel\"lo" }) => "hel\"lo"
(string->jsexpr @T{ "\\//\\\\//" }) => "\\//\\\\//"

View File

@ -116,7 +116,7 @@
;; Reading a string *could* have been nearly trivial using the racket
;; reader, except that it won't handle a "\/"...
(define (read-string)
(define result (open-output-string))
(define result (open-output-bytes))
(let loop ()
(define esc
(let loop ()
@ -127,7 +127,7 @@
[(= c 92) (read-bytes 1 i)] ;; 92 = \
[else (write-byte c result) (loop)])))
(cond
[(not esc) (get-output-string result)]
[(not esc) (bytes->string/utf-8 (get-output-bytes result))]
[(assoc esc '([#"b" . #"\b"] [#"n" . #"\n"] [#"r" . #"\r"]
[#"f" . #"\f"] [#"t" . #"\t"]
[#"\\" . #"\\"] [#"\"" . #"\""] [#"/" . #"/"]))