chibi-scheme icon indicating copy to clipboard operation
chibi-scheme copied to clipboard

read-line doesn't recognise \r as end of line

Open Oxyd opened this issue 3 years ago • 13 comments

(import (scheme base) (scheme read) (scheme write) (scheme file))

(write (read-line (open-input-file "foo.txt")))

Then:

% echo "foo\rbar" >foo.txt
% ./tools/chibi-run testcase.scm
"foo\rbar"

R7RS says about read-line, in part: “For the purpose of this procedure, an end of line consists of either a linefeed character, a carriage return character, or a sequence of a carriage return character followed by a linefeed character.” Which means that read-line is required to recognise \r as end of line, which means the correct output is just "foo".

This is on Linux. I suspect the behaviour might be different on different platforms – which it shouldn't be.

Oxyd avatar Dec 04 '22 13:12 Oxyd

This is not platform dependent. Chibi reads until \n, then discards any trailing \r. I can't recall why we chose to support a lone \r - pre-BSD MacOS doesn't seem worth supporting. I doubt Chibi can even compile on it.

ashinn avatar Dec 04 '22 14:12 ashinn

I agree that recognising lone \r is a strange choice. Nevertheless, I just tested with Kawa, CHICKEN and Gauche and they all correctly recognise \r as end of line, so Chibi is an outlier here.

Oxyd avatar Dec 04 '22 14:12 Oxyd

One question is that in REPL (read-line (open-input-file "foo.txt")) and (read-line (open-input-string "foo\rbar")) .there are different output. The first one is "foo\rbar" , and second one is "foo".

APIPLM avatar Jan 30 '23 12:01 APIPLM

The content of the foo.txt file is foo\rbar

APIPLM avatar Jan 30 '23 12:01 APIPLM

Yes. it is that same result in REPL in Chicken. But the only different is that (read-line (open-input-file "foo.txt")) in Chicken. \r character be recognised , and need to escape this character. so that output is "foo\\rbar"

APIPLM avatar Jan 30 '23 12:01 APIPLM

In MIT/GNU Scheme, it is different, (read-line (open-input-file "foo.txt")) and (read-line (open-input-string "foo\rbar")) , the output is same. which is ;Value: "foo\rbar"

APIPLM avatar Jan 30 '23 12:01 APIPLM

Adding a survey to https://github.com/schemedoc/surveys/tree/master/surveys would be appreciated.

lassik avatar Jan 30 '23 14:01 lassik

Yes. But seem like it has one file md for this survey, and it does not have too much content in the file. I will raise the issue there.

APIPLM avatar Jan 31 '23 12:01 APIPLM

read-line on FILE* backed ports is optimized to use fgets, whereas the pure Scheme version does handle \r as you expect.

ashinn avatar Feb 01 '23 10:02 ashinn

Thanks. It is the commit https://github.com/ashinn/chibi-scheme/commit/6615a746096274e0f6cdf27912563599ed613c49.

But somehow, I feel like that read-line is kind of contradiction in the context. I mean that running (read-line (open-input-file "foo.txt")) and (read-line (open-input-string "foo\rbar")) in the REPL. In (open-input-file "foo.txt"), The reader read byte by byte and in (open-input-string "foo\rbar") The reader read character by character.

APIPLM avatar Feb 02 '23 13:02 APIPLM

One more point, running the below lines in the REPL.

(read (open-input-string "foo\ee")) The output is fooee (read (open-input-string "foo\\ee")) The output is fooee (read (open-input-string "foo\\\ee")) The output is fooee (read (open-input-string "foo\\\\ee")) The output is |foo\ee|

APIPLM avatar Feb 04 '23 12:02 APIPLM

Those are correct (except that |foo\ee| is |foo\\ee|):

> 'fooee
fooee
> 'foo\ee
fooee
> 'foo\ee
fooee
> 'foo\\ee
|foo\\ee|
> (read (open-input-string "foo\\\\ee"))
|foo\\ee|

lassik avatar Feb 04 '23 17:02 lassik

Note that "\e" is the same as "e" in Chibi, so the \ is not seen by read at all.

lassik avatar Feb 04 '23 17:02 lassik