regexp: fix O(size) behavior for large strings

This commit is contained in:
Matthew Flatt 2019-01-16 20:39:23 -07:00
parent 6c8ba483a1
commit cddcd57267

View File

@ -285,13 +285,12 @@
(and (input-port? in) (and (input-port? in)
(not (eq? 'eof end-offset)) (not (eq? 'eof end-offset))
(- end-offset start-offset)))) (- end-offset start-offset))))
(define end-pos (if (eq? 'eof end-offset) (define end-pos (if (or (eq? 'eof end-offset)
(string? in))
'eof 'eof
(+ start-pos (+ start-pos
(cond (- end-offset start-offset))))
[(string? in) (string-utf-8-length in start-offset end-offset)]
[else (- end-offset start-offset)]))))
;; Search for a match: ;; Search for a match:
(define-values (ms-pos me-pos) (define-values (ms-pos me-pos)
(if any-bytes-left? (if any-bytes-left?
@ -314,7 +313,8 @@
(copy-port-bytes port-in #f (- me-pos prefix-len)))] (copy-port-bytes port-in #f (- me-pos prefix-len)))]
[(eq? end-pos 'eof) [(eq? end-pos 'eof)
;; Copy all remaining bytes from input to output ;; Copy all remaining bytes from input to output
(copy-port-bytes port-in out #f)] (when (or out (input-port? in))
(copy-port-bytes port-in out #f))]
[else [else
(when out (when out
;; Copy all bytes to output ;; Copy all bytes to output