On Thu, Nov 12, 2015 at 9:21 PM, Ludovic Courtès <[email protected]> wrote:
> If we go for the CRLF conversion port, we should avoid the pipe and
> extra thread. Instead, I would suggest something like:
>
> (define (canonical-newline-port port)
> "Return an input port that wraps PORT such that all newlines consist
> of a single carriage return."
> (make-custom-binary-input-port …))
I like this suggestion :-)
I never used custom ports. Is something like this OK? (Seems to work
in the REPL.)
---------------------------------------------------------------
(define (canonical-newline-port port)
"Return an input port that wraps PORT such that all newlines consist
of a single carriage return."
(define (get-position)
(if (port-has-port-position? port) (port-position port) #f))
(define (set-position! position)
(if (port-has-set-port-position!? port)
(set-port-position! position port)
#f))
(define (close) (close-port port))
(define (read! bv start n)
(let loop ((count 0)
(byte (get-u8 port)))
(cond ((or (eof-object? byte) (= count n)) count)
((eqv? byte (char->integer #\return)) (loop count (get-u8 port)))
(else
(bytevector-u8-set! bv (+ start count) byte)
(loop (+ count 1) (get-u8 port))))))
(make-custom-binary-input-port "canonical-newline-port"
read!
get-position
set-position!
close))
---------------------------------------------------------------
IMO this is general enough that it could go into "guix/utils.scm". Are
you OK with this?
Regards,
Fede