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

rdelim cleanups

* module/ice-9/rdelim.scm: Clean up export list.
  (read-line!, read-delimited!, read-delimited, read-line): Use
  define*.
This commit is contained in:
Andy Wingo 2010-10-19 22:58:00 +02:00
parent 534bbcc168
commit 3251222637

View file

@ -1,6 +1,6 @@
;;; installed-scm-file ;;; installed-scm-file
;;;; Copyright (C) 1997, 1999, 2000, 2001, 2006 Free Software Foundation, Inc. ;;;; Copyright (C) 1997, 1999, 2000, 2001, 2006, 2010 Free Software Foundation, Inc.
;;;; ;;;;
;;;; This library is free software; you can redistribute it and/or ;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public ;;;; modify it under the terms of the GNU Lesser General Public
@ -22,122 +22,105 @@
;;; similar to (scsh rdelim) but somewhat incompatible. ;;; similar to (scsh rdelim) but somewhat incompatible.
(define-module (ice-9 rdelim) (define-module (ice-9 rdelim)
:export (read-line read-line! read-delimited read-delimited! #:export (read-line
%read-delimited! %read-line write-line) ; C read-line!
) read-delimited
read-delimited!
%read-delimited!
%read-line
write-line))
(%init-rdelim-builtins) (%init-rdelim-builtins)
(define (read-line! string . maybe-port) (define* (read-line! string #:optional (port current-input-port))
;; corresponds to SCM_LINE_INCREMENTORS in libguile. ;; corresponds to SCM_LINE_INCREMENTORS in libguile.
(define scm-line-incrementors "\n") (define scm-line-incrementors "\n")
(let* ((rv (%read-delimited! scm-line-incrementors
string
#t
port))
(terminator (car rv))
(nchars (cdr rv)))
(cond ((and (= nchars 0)
(eof-object? terminator))
terminator)
((not terminator) #f)
(else nchars))))
(let* ((port (if (pair? maybe-port) (define* (read-delimited! delims buf #:optional
(car maybe-port) (port (current-input-port)) (handle-delim 'trim)
(current-input-port)))) (start 0) (end (string-length buf)))
(let* ((rv (%read-delimited! scm-line-incrementors (let* ((rv (%read-delimited! delims
string buf
#t (not (eq? handle-delim 'peek))
port)) port
(terminator (car rv)) start
(nchars (cdr rv))) end))
(cond ((and (= nchars 0) (terminator (car rv))
(eof-object? terminator)) (nchars (cdr rv)))
terminator) (cond ((or (not terminator) ; buffer filled
((not terminator) #f) (eof-object? terminator))
(else nchars))))) (if (zero? nchars)
(if (eq? handle-delim 'split)
(define (read-delimited! delims buf . args) (cons terminator terminator)
(let* ((num-args (length args)) terminator)
(port (if (> num-args 0) (if (eq? handle-delim 'split)
(car args) (cons nchars terminator)
(current-input-port))) nchars)))
(handle-delim (if (> num-args 1) (else
(cadr args) (case handle-delim
'trim)) ((trim peek) nchars)
(start (if (> num-args 2) ((concat) (string-set! buf (+ nchars start) terminator)
(caddr args) (+ nchars 1))
0)) ((split) (cons nchars terminator))
(end (if (> num-args 3) (else (error "unexpected handle-delim value: "
(cadddr args) handle-delim)))))))
(string-length buf))))
(let* ((rv (%read-delimited! delims
buf
(not (eq? handle-delim 'peek))
port
start
end))
(terminator (car rv))
(nchars (cdr rv)))
(cond ((or (not terminator) ; buffer filled
(eof-object? terminator))
(if (zero? nchars)
(if (eq? handle-delim 'split)
(cons terminator terminator)
terminator)
(if (eq? handle-delim 'split)
(cons nchars terminator)
nchars)))
(else
(case handle-delim
((trim peek) nchars)
((concat) (string-set! buf (+ nchars start) terminator)
(+ nchars 1))
((split) (cons nchars terminator))
(else (error "unexpected handle-delim value: "
handle-delim))))))))
(define (read-delimited delims . args) (define* (read-delimited delims #:optional (port (current-input-port))
(let* ((port (if (pair? args) (handle-delim 'trim))
(let ((pt (car args))) (let loop ((substrings '())
(set! args (cdr args)) (total-chars 0)
pt) (buf-size 100)) ; doubled each time through.
(current-input-port))) (let* ((buf (make-string buf-size))
(handle-delim (if (pair? args) (rv (%read-delimited! delims
(car args) buf
'trim))) (not (eq? handle-delim 'peek))
(let loop ((substrings '()) port))
(total-chars 0) (terminator (car rv))
(buf-size 100)) ; doubled each time through. (nchars (cdr rv))
(let* ((buf (make-string buf-size)) (join-substrings
(rv (%read-delimited! delims (lambda ()
buf (apply string-append
(not (eq? handle-delim 'peek)) (reverse
port)) (cons (if (and (eq? handle-delim 'concat)
(terminator (car rv)) (not (eof-object? terminator)))
(nchars (cdr rv)) (string terminator)
(join-substrings "")
(lambda () (cons (substring buf 0 nchars)
(apply string-append substrings))))))
(reverse (new-total (+ total-chars nchars)))
(cons (if (and (eq? handle-delim 'concat) (cond ((not terminator)
(not (eof-object? terminator))) ;; buffer filled.
(string terminator) (loop (cons (substring buf 0 nchars) substrings)
"") new-total
(cons (substring buf 0 nchars) (* buf-size 2)))
substrings)))))) ((eof-object? terminator)
(new-total (+ total-chars nchars))) (if (zero? new-total)
(cond ((not terminator) (if (eq? handle-delim 'split)
;; buffer filled. (cons terminator terminator)
(loop (cons (substring buf 0 nchars) substrings) terminator)
new-total (if (eq? handle-delim 'split)
(* buf-size 2))) (cons (join-substrings) terminator)
((eof-object? terminator) (join-substrings))))
(if (zero? new-total) (else
(if (eq? handle-delim 'split) (case handle-delim
(cons terminator terminator) ((trim peek concat) (join-substrings))
terminator) ((split) (cons (join-substrings) 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: " (else (error "unexpected handle-delim value: "
handle-delim))))))))) 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,
@ -149,14 +132,9 @@
;;; `split', the newline is split from the string and read-line ;;; `split', the newline is split from the string and read-line
;;; returns a pair consisting of the truncated string and the newline. ;;; returns a pair consisting of the truncated string and the newline.
(define (read-line . args) (define* (read-line #:optional (port (current-input-port))
(let* ((port (if (null? args) (handle-delim 'trim))
(current-input-port) (let* ((line/delim (%read-line port))
(car args)))
(handle-delim (if (> (length args) 1)
(cadr args)
'trim))
(line/delim (%read-line port))
(line (car line/delim)) (line (car line/delim))
(delim (cdr line/delim))) (delim (cdr line/delim)))
(case handle-delim (case handle-delim