1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-10 14:00:21 +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))
(terminator (car 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)))
(cond ((not terminator)
;; buffer filled.
(loop (cons (substring buf 0 nchars) substrings)
new-total
(* buf-size 2)))
((eof-object? terminator)
(if (zero? new-total)
(if (eq? handle-delim 'split)
(cons terminator terminator)
terminator)
(if (eq? handle-delim 'split)
(cons (join-substrings) terminator)
(join-substrings))))
(else
(case handle-delim
((trim peek concat) (join-substrings))
((split) (cons (join-substrings) terminator))
(else (error "unexpected handle-delim value: "
handle-delim))))))))
(cond
((not terminator)
;; buffer filled.
(loop (cons (substring buf 0 nchars) substrings)
new-total
(* buf-size 2)))
((and (eof-object? terminator) (zero? new-total))
(if (eq? handle-delim 'split)
(cons terminator terminator)
terminator))
(else
(let ((joined
(string-concatenate-reverse
(cons (substring buf 0 nchars) substrings))))
(case handle-delim
((concat)
(if (eof-object? terminator)
joined
(string-append joined (string terminator))))
((trim peek) joined)
((split) (cons joined terminator))
(else (error "unexpected handle-delim value: "
handle-delim)))))))))
;;; read-line [PORT [HANDLE-DELIM]] reads a newline-terminated string
;;; from PORT. The return value depends on the value of HANDLE-DELIM,