[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] fix for read-string

From: Joerg F. Wittenberger
Subject: [Chicken-users] fix for read-string
Date: 04 Jan 2003 14:23:25 +0100

Hi all,

the read-string procedure in extras.scm is broken when it comes to
length terminated reads (one of those nice off-by-one's).  Here a
fixed version:

(define read-string
  (let ([read-char read-char]
        [write-char write-char]
        [open-output-string open-output-string]
        [get-output-string get-output-string] )
    (lambda (n . port)
      (let ([p (if (pair? port) (car port) ##sys#standard-input)]
            [str (open-output-string)] )
        (when n (##sys#check-exact n 'read-string))
        (let loop ([n n])
          (or (and (eq? n 0) (get-output-string str))
              (let ([c (read-char p)])
                (cond [(eof-object? c) (get-output-string str)]
                       (write-char c str)
                       (loop (and n (fx- n 1))) ] ) )) ) ) ) ) )

One remark: I'd tend to use `if' instead of the `or/and' construction
I used here, but I noticed (in some different context) that `or/and'
is faster with chicken than `if'.  To me this is something between
surprizing, strange and suspicious.  I'm not sure whether my test
benchmark was broken or that's really the truth, anybody who can



The worst of harm may often result from the best of intentions.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]