Federico Beffa <[email protected]> skribis: > --------------------------------------------------------------- > (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)
BYTE is lost here in the case it is not EOF. It may be best to move the (= count n) case right before the recursive call below. > ((eqv? byte (char->integer #\return)) (loop count (get-u8 port))) In practice this discards LF even if it’s not following CR; that’s probably a good enough approximation, but an XXX comment would be welcome. > (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? Looks good! Could you make a patch that does that, along with adding a test or two in tests/utils.scm? Thank you! Ludo’.
