Re: [racket-users] telling apart files ending with a newline

2020-07-30 Thread Ryan Culpepper
If I understand the docs correctly, the OS-specific handling is in
open-input-file, but it is not the default.

Here is an alternative to read-line that preserves line endings:

  #lang racket/base

  ;; my-read-line : InputPort -> String
  ;; Like read-line, but preserves line ending.
  ;; Fails if port contains specials.
  (define (my-read-line in [mode 'any])
(define rx
  (case mode
[(any) #rx"^[^\n\r]*(?:\r\n|\n|\r|$)"]
[(linefeed) #rx"^[^\n]*(?:\n|$)"]
;; ...
[else (error 'my-read-line "unsupported mode: ~e" mode)]))
(define m (car (regexp-match rx in))) ;; rx accepts "", can't fail
(if (equal? m #"") eof (bytes->string/utf-8 m)))

  (require rackunit racket/port)

  (check-equal?
   (port->list my-read-line (open-input-string "abc\ndef\r\ngh\n"))
   '("abc\n" "def\r\n" "gh\n"))

  (check-equal?
   (port->list my-read-line (open-input-string "abc\ndef\r\ngh"))
   '("abc\n" "def\r\n" "gh"))

  (check-equal?
   (port->list my-read-line (open-input-string "\n\r\n\n\r\r\n"))
   '("\n" "\r\n" "\n" "\r" "\r\n"))

  (check-equal?
   (port->list (lambda (in) (my-read-line in 'linefeed))
   (open-input-string "\n\r\n\n\r\r\n"))
   '("\n" "\r\n" "\n" "\r\r\n"))

Ryan


On Thu, Jul 30, 2020 at 5:11 AM Shriram Krishnamurthi 
wrote:

> Suppose I have two files that are identical, except one ends in a newline
> and the other does not. If I use `read-line` to read the successive lines
> of this file, because it swallows the line separators, there is no way to
> tell them apart. E.g., these two strings
>
> "a
> b"
>
> and
>
> "a
> b
> "
>
> read using `read-line` and `open-input-string` produce the same result.
>
> This is unfortunate for a program SPDEGabrielle has induced me to write
> (-:.
>
> Any reasonable ways to work around this that rely, as much as possible, on
> the OS-specific handling `read-line` already provides?
>
> Shriram
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/250c95c9-24b6-467a-ad08-0cd81abded66n%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CANy33qn61uHVZXuJdEBuOQV%3D%2BT%3DdS-6%3DfVgTGZat_3uJCSTKwA%40mail.gmail.com.


[racket-users] telling apart files ending with a newline

2020-07-29 Thread Shriram Krishnamurthi
Suppose I have two files that are identical, except one ends in a newline 
and the other does not. If I use `read-line` to read the successive lines 
of this file, because it swallows the line separators, there is no way to 
tell them apart. E.g., these two strings

"a
b"

and

"a
b
"

read using `read-line` and `open-input-string` produce the same result.

This is unfortunate for a program SPDEGabrielle has induced me to write (-:.

Any reasonable ways to work around this that rely, as much as possible, on 
the OS-specific handling `read-line` already provides?

Shriram

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/250c95c9-24b6-467a-ad08-0cd81abded66n%40googlegroups.com.