lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



So with the “*l” option on Windows it converts line endings, but on Mac it does not, Mac retains the CRs, which seems weird.

On Mac CR is not a magic character.
In Unix world CR is a regular character having nothing to do with line breaks.
Why should it be removed from the end of line?
(Historically CR is a matrix printer command to move caret to the leftmost position, it has no special meaning in text files.)

I get what you’re saying, but simply because “*l” is used for text files, and CR has no place on any line, in text files (as in Lua source code being compiled for example). Hence iy should be removed.



 
1) on Mac, to also convert “CRLF” to “LF”, when using “*l”

This might be useful - to remove final CR (if it exists) from the result of "*l" mode reading on all the platforms.
Although the adaptation of text files from Windows world to Unix world is beyond the goals of the Lua standard library.
BTW, you can implement it yourself without patching Lua: just redefine the function
debug.getregistry()["FILE*"].__index.read
at the beginning of your Lua program.
  

2) on Windows, to NOT convert “CRLF” to “LF” when using “*L”

Why do you address this question to Lua?  :-)
Lua does not convert CRLF to LF.
The operating system does this.
Just open the file in binary mode to disable this conversion if you really need it.

Because the manual reads: "L": reads the next line keeping the end-of-line character (if present), returning nil on end of file.
Which reads like I get the exact set of bytes, which is not the case. Though it says “character” not “characters” which might be considered a hint.



There is one more surprise for you:
Multiline string literals [[ ... ]] insert LF for newlines even on Windows.

So, the correct way to make your Lua application cross-platform is:
1) forget about CR character: newline = LF for any platform
2) open text files in text mode
3) the problem of moving text files between Win and Mac is the problem of a user, not yours.
If the user works on both Win and Mac systems he probably already knows how to solve this problem.
There are special utilities such as dos2unix.


I think we’re aligned here, as to;
- why it happens and 
- how one could work around it and
- it’s on OS issue not Lua
- etc

But simply from a user perspective the current way of doing things will get any user in trouble at some point probably. And systems change, there’s more and more inter system exchange, the example Francisco gave; uploading a config file to some system from a windows machine. So where 10 years ago I would have agreed to all this being a user problem, I think in todays world, if a user expects the language to take care of this for him/her, I think that would be a fair ask.

I think the trend is that files become more and more cross-platform, and if that’s the case, then both options “*l” and “*L” are just becoming big foot guns because you cannot rely on them, and should either be removed from the implementation, or be made consistent across platforms.

Thijs