json: Remove regexp-try-match used to parse strings, replace with less-memory intensive functions.

This commit is contained in:
Edward Lee 2015-05-09 19:56:32 -04:00 committed by Matthew Flatt
parent 7ef9bd618d
commit ec6060b9da

View File

@ -116,18 +116,22 @@
;; Reading a string *could* have been nearly trivial using the racket ;; Reading a string *could* have been nearly trivial using the racket
;; reader, except that it won't handle a "\/"... ;; reader, except that it won't handle a "\/"...
(define (read-string) (define (read-string)
(let loop ([l* '()]) (define result (open-output-string))
;; note: use a string regexp to extract utf-8-able text (let loop ()
(define m (cdr (or (regexp-try-match #rx"^([^\"\\]*)(\"|\\\\(.))" i) (define esc
(err "unterminated string")))) (let loop ()
(define l (if ((bytes-length (car m)) . > . 0) (cons (car m) l*) l*)) (define c (read-byte i))
(define esc (caddr m)) (cond
[(eof-object? c) (err "unterminated string")]
[(= c 34) #f] ;; 34 = "
[(= c 92) (read-bytes 1 i)] ;; 92 = \
[else (write-byte c result) (loop)])))
(cond (cond
[(not esc) (bytes->string/utf-8 (apply bytes-append (reverse l)))] [(not esc) (get-output-string result)]
[(assoc esc '([#"b" . #"\b"] [#"n" . #"\n"] [#"r" . #"\r"] [(assoc esc '([#"b" . #"\b"] [#"n" . #"\n"] [#"r" . #"\r"]
[#"f" . #"\f"] [#"t" . #"\t"] [#"f" . #"\f"] [#"t" . #"\t"]
[#"\\" . #"\\"] [#"\"" . #"\""] [#"/" . #"/"])) [#"\\" . #"\\"] [#"\"" . #"\""] [#"/" . #"/"]))
=> (λ (m) (loop (cons (cdr m) l)))] => (λ (m) (write-bytes (cdr m) result) (loop))]
[(equal? esc #"u") [(equal? esc #"u")
(let* ([e (or (regexp-try-match #px#"^[a-fA-F0-9]{4}" i) (let* ([e (or (regexp-try-match #px#"^[a-fA-F0-9]{4}" i)
(err "bad string \\u escape"))] (err "bad string \\u escape"))]
@ -144,7 +148,8 @@
(err "bad string \\u escape, ~a" (err "bad string \\u escape, ~a"
"bad second half of a UTF16 pair"))) "bad second half of a UTF16 pair")))
e)) ; single \u escape e)) ; single \u escape
(loop (cons (string->bytes/utf-8 (string (integer->char e*))) l)))] (write-string (string (integer->char e*)) result)
(loop))]
[else (err "bad string escape: \"~a\"" esc)]))) [else (err "bad string escape: \"~a\"" esc)])))
;; ;;
(define (read-list what end-rx read-one) (define (read-list what end-rx read-one)