1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-12 06:41:13 +02:00

read-delimited is clearer and conses less

* module/ice-9/rdelim.scm (read-delimited): Clarify and somewhat
  optimize implementation.
This commit is contained in:
Andy Wingo 2010-12-03 18:03:51 +01:00
parent 80993fa438
commit 8556760c23

View file

@ -89,38 +89,30 @@
port)) port))
(terminator (car rv)) (terminator (car rv))
(nchars (cdr rv)) (nchars (cdr rv))
(join-substrings
(lambda ()
(apply string-append
(reverse
(cons (if (and (eq? handle-delim 'concat)
(not (eof-object? terminator)))
(string terminator)
"")
(cons (substring buf 0 nchars)
substrings))))))
(new-total (+ total-chars nchars))) (new-total (+ total-chars nchars)))
(cond ((not terminator) (cond
;; buffer filled. ((not terminator)
(loop (cons (substring buf 0 nchars) substrings) ;; buffer filled.
new-total (loop (cons (substring buf 0 nchars) substrings)
(* buf-size 2))) new-total
((eof-object? terminator) (* buf-size 2)))
(if (zero? new-total) ((and (eof-object? terminator) (zero? new-total))
(if (eq? handle-delim 'split) (if (eq? handle-delim 'split)
(cons terminator terminator) (cons terminator terminator)
terminator) terminator))
(if (eq? handle-delim 'split) (else
(cons (join-substrings) terminator) (let ((joined
(join-substrings)))) (string-concatenate-reverse
(else (cons (substring buf 0 nchars) substrings))))
(case handle-delim (case handle-delim
((trim peek concat) (join-substrings)) ((concat)
((split) (cons (join-substrings) terminator)) (if (eof-object? terminator)
joined
(string-append joined (string terminator))))
(else (error "unexpected handle-delim value: " ((trim peek) joined)
handle-delim)))))))) ((split) (cons joined terminator))
(else (error "unexpected handle-delim value: "
handle-delim)))))))))
;;; read-line [PORT [HANDLE-DELIM]] reads a newline-terminated string ;;; read-line [PORT [HANDLE-DELIM]] reads a newline-terminated string
;;; from PORT. The return value depends on the value of HANDLE-DELIM, ;;; from PORT. The return value depends on the value of HANDLE-DELIM,