[Top][All Lists]

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

Re: [Chicken-users] fix for read-string

From: felix
Subject: Re: [Chicken-users] fix for read-string
Date: Sun, 05 Jan 2003 23:31:59 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.0.0) Gecko/20020529

Joerg F. Wittenberger wrote:
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))) ] ) )) ) ) ) ) )

Ok, ok. Fixed. (geez - I'm having trouble keeping up... ;-)

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

Well, the code that comes out of different combinations of
if/and/or can influence subsequent compiler optimizations.
I suggest using the construct that shows the programmers
intention most clearly and tune for speed only if it makes
a noticable difference.


reply via email to

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