awwx.ws

readline1

a version of readline that accepts both lf and crlf endings

http://awwx.ws/readline1.arc:

(def readline ((o s (stdin)))
  (aif (readc s)
    (string
     (accum a
       (xloop (c it)
         (if (is c #\return)
              (if (is (peekc s) #\newline)
                   (readc s))
             (is c #\newline)
              nil
              (do (a c)
                  (aif (readc s)
                        (next it)))))))))

This version of readline fixes a bug in arc3.1 that reads empty lines incorrectly, and extends the function to also accept CRLF line endings (which is useful for some Internet protocols):

arc> (fromstring "header one\r\nheader two\r\n\r\nhello\nthere\n"
                 (drain (readline)))
("header one" "header two" "" "hello" "there")

Prerequisites

This hack depends on arc3.1 and xloop0.

Get this hack

Using the hackinator:

$ hack \
    ycombinator.com/arc/arc3.1.tar \
    awwx.ws/xloop0.arc \
    awwx.ws/readline1.arc

License

Same as Arc.

Contact me

Twitter: awwx
Email: andrew.wilcox [at] gmail.com