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

Test eol used by lua io.write() under different OSs #6

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

muzimuzhi
Copy link
Owner

@muzimuzhi muzimuzhi commented Mar 19, 2023

Backgrounds

  • l3build save <name> uses CRLF on Win and LF on *nix. Since test output is always rewritten rather than incrementally appended/edited, this causes unnecessary EOL changes.
    See https://github.com/lvjr/tabularray/pull/385#issuecomment-1474855272
  • l3build already tries to normalize EOLs in rewrite() using gsub(file_content, "\r\n", "\n"), but has no effect.
    local function rewrite(source,result,processor,...)
      local file = assert(open(source,"rb"))
      local content = gsub(file:read("*all") .. "\n","\r\n","\n")
      close(file)
      local new_content = processor(content,...)
      local newfile = open(result,"w")
      output(newfile)
      write(new_content)
      close(newfile)
    end

@muzimuzhi
Copy link
Owner Author

First test: How does lua io.write() behave under different OSs?

On Win, io.write("abc\nabc\n") still writes CRLF (\x0d\x0a)

$ xxd eol-lf.txt
00000000: 6162 630d 0a61 6263 0d0a                 abc..abc..

From mailing list thread http://lua-users.org/lists/lua-l/2015-05/msg00348.html,

Open the file in binary mode.
http://lua-users.org/lists/lua-l/2015-05/msg00349.html

It's CRT (not lua) that does the translation.
In C, one can call _setmode( _fileno( stdout ), _O_BINARY ) to set output to binary mode.
I don't see how it can be done directly in lua.
http://lua-users.org/lists/lua-l/2015-05/msg00358.html

@muzimuzhi
Copy link
Owner Author

muzimuzhi commented Mar 19, 2023

Open the file in binary mode.
http://lua-users.org/lists/lua-l/2015-05/msg00349.html

Test 2: Try with io.open(fname, "wb")

Succeeded

On Win,

$ xxd eol-crlf.txt
00000000: 6162 630d 0a61 6263 0d0a                 abc..abc..
$ xxd eol-lf.txt
00000000: 6162 630a 6162 630a                      abc.abc.
$ xxd eol-mixed.txt
00000000: 6162 630d 0a61 6263 0a                   abc..abc.

@muzimuzhi
Copy link
Owner Author

muzimuzhi commented Mar 19, 2023

Also see this thread http://lua-users.org/lists/lua-l/2009-11/msg00815.html

In windows if you open with "r" it will translate LF to CRLF
Try using "rb" on the open and mac and win should give the same result
David Burgess
http://lua-users.org/lists/lua-l/2009-11/msg00817.html

So my question is, why the script get incorrect(?) result in windows?
Is the issue in windows's fwrite function?

Yes. When you open in text mode, "\n" is written as CRLF instead of LF.
That's what text mode means.

In text mode, when you write "\r\n", it is written as CRCRLF. The first
CR is for the "\r" and the CRLF is for the "\n".
--
roger ivie
[email protected]
http://lua-users.org/lists/lua-l/2009-11/msg00819.html

This answer https://stackoverflow.com/a/43967013 quoted PIL

The simple model functions io.input and io.output always open a file in text mode (the default). In Unix, there is no difference between binary files and text files. But in some systems, notably Windows, binary files must be opened with a special flag. To handle such binary files, you must use io.open, with the letter `b´ in the mode string.
https://www.lua.org/pil/21.2.2.html

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

Successfully merging this pull request may close these issues.

1 participant