Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Oxyd opened this issue Dec 4, 2022 · 13 comments
Open

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

Oxyd opened this issue Dec 4, 2022 · 13 comments

Comments

@Oxyd
Copy link
Contributor

Oxyd commented Dec 4, 2022

(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.

@ashinn
Copy link
Owner

ashinn commented Dec 4, 2022

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.

@Oxyd
Copy link
Contributor Author

Oxyd commented Dec 4, 2022

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.

@APIPLM
Copy link

APIPLM commented Jan 30, 2023

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
Copy link

APIPLM commented Jan 30, 2023

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

@APIPLM
Copy link

APIPLM commented Jan 30, 2023

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
Copy link

APIPLM commented Jan 30, 2023

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"

@lassik
Copy link
Contributor

lassik commented Jan 30, 2023

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

@APIPLM
Copy link

APIPLM commented Jan 31, 2023

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.

@ashinn
Copy link
Owner

ashinn commented Feb 1, 2023

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

@APIPLM
Copy link

APIPLM commented Feb 2, 2023

Thanks. It is the commit 6615a74.

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
Copy link

APIPLM commented Feb 4, 2023

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|

@lassik
Copy link
Contributor

lassik commented Feb 4, 2023

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
Copy link
Contributor

lassik commented Feb 4, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants