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

Pervasives.input_line does'nt remove \r at end of lines #6088

Closed
vicuna opened this issue Jul 26, 2013 · 5 comments
Closed

Pervasives.input_line does'nt remove \r at end of lines #6088

vicuna opened this issue Jul 26, 2013 · 5 comments

Comments

@vicuna
Copy link

vicuna commented Jul 26, 2013

Original bug ID: 6088
Reporter: AltGr
Status: closed (set by @xavierleroy on 2015-12-11T18:21:17Z)
Resolution: not a bug
Priority: normal
Severity: minor
OS: Windows
OS Version: any
Version: 4.00.1
Category: standard library
Monitored by: @hcarty

Bug description

Documentation says: "Return the string of all characters read, without the newline character at the end."

Well, on Windows, the newline characters are two, "\r\n" ; one would expect that they are both removed, and that would avoid portability issues. Instead, the function returns strings ending with '\r'.

@vicuna
Copy link
Author

vicuna commented Jul 26, 2013

Comment author: @gasche

Did you open the file with open_in or open_in_bin? open_in will do newline translation (removing the \r as appropriate), but not open_in_bin. Do you have a complete code example as a repro case?

@vicuna
Copy link
Author

vicuna commented Jul 29, 2013

Comment author: @xavierleroy

Gasche is right: the Windows programming model (for OCaml and most other languages) is that for in-memory strings, lines are terminated by a single "\n" character. It's only within text files that "\r\n" is used, and the conversion happens when reading or writing to/from files. So, just open your text files with "open_in" and not "open_in_bin" and input_line will work fine.

@vicuna
Copy link
Author

vicuna commented Jul 29, 2013

Comment author: AltGr

Right, I suspected it was done that way for open_in (the documentation isn't completely explicit but mentions it), and I think that this is a good way to handle things. My issue then is that I was actually using "Unix.open_process_in" in that case, which has no "_bin" counterpart (and commands, typically ocaml, answer with Windows EOLs).

I am not sure what the correct behaviour would be in that case, but the current one isn't completely consistent.

(note: this is using the mingw32 version)

@vicuna
Copy link
Author

vicuna commented Jul 29, 2013

Comment author: @xavierleroy

I was actually using "Unix.open_process_in" in that case, which has no "_bin" counterpart

Right, the open_process_* functions default to binary mode, somewhat arbitrarily. It wouldn't hurt to document this fact.

You can, however, use "set_binary_mode_in" or "set_binary_mode_out" on the returned channel to put it into text mode. These operations are no-ops under non-Windows systems, so your code remains fully portable.

@vicuna
Copy link
Author

vicuna commented Jul 29, 2013

Comment author: AltGr

Seems like I somehow overlooked the "set_binary_mode_*" functions.
Thanks a lot !

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

No branches or pull requests

1 participant