lua-users home
lua-l archive

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




On Sun, Mar 27, 2022 at 10:59 PM Thijs Schreijer wrote:
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.)

 
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.

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.