lua-users home
lua-l archive

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


On Fri, Mar 25, 2022 at 2:06 AM Thijs Schreijer wrote:
On 24 Mar 2022, at 18:50, Egor Skriptunoff wrote:

On Thu, Mar 24, 2022 at 4:51 PM Thijs Schreijer wrote:
This means that reading a Windows based text file, with CRLF as
line endings, the returned lines will have a trailing CR (char 13).

I can not reproduce this problem.
Everything works as expected on Windows.
Here’s my test code:
local CR = string.char(13)
local LF = string.char(10)
local function writefile(name, content)
  local f = io.open(name, "w")
  f:write(content)
  f:close()
end
local lines = {               -- "*L" result:
  "1234567890"..LF,           -- 1 line, 11 bytes
  "1234567890"..LF..CR..LF,   -- 2 lines, 11 bytes + 2 bytes
  "1234567890"..CR..LF,       -- 1 line, 12 bytes
  "1234567890"..CR..LF..LF,   -- 2 lines, 12 bytes + 1 byte
  "1234567890"                -- 1 line, 10 bytes
}
lines = table.concat(lines)
writefile("test.txt", lines)

The bug is in your script, not in Lua.
You are saving CRLF-strings to the file opened in text mode.
As a result, all LF are implicitly converted to CRLF.
Just look in "test.txt" with a hex editor: instead of
"1234567890"..LF..CR..LF
you actually have
"1234567890"..CR..LF..CR..CR..LF
in the file.