lua-users home
lua-l archive

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



> It seems like this could be fixed without messing anything
> else up by having the interpreter strip out all "\r"s (which
> the Windows interpreter [or a Macintosh interpreter] would
> never even see because it reads the file in text mode), but
> maybe there's something deeper going on that I don't
> understand.


Well, it would break any script that had a \r in a string :)
(Or whatever \r happens to be on your OS.) That is maybe a
bad idea, but in principle you can put random binary strings
into a Lua script by surrounding them with [[ ]], as long
as they don't contain unbalanced [ and ] characters.

And it would break files created on Mac OS <=9 or in
"classic" mode on Mac OS X, which use a bare carriage
return as a line ending character. (Moving Mac OS 9
scripts to another OS is pretty well broken anyway,
though.)

It's even worse with regular strings: moving this program:

   print "foo\
bar"

from DOS to Unix produces a syntax error, since \<cr> is not
recognised as a continuation, so an incomplete quote is
reported when the <nl> is read.

Some, perhaps many, text editors recognise but do not
normalise line endings, so if you create a script on one
system and edit it on another one, you can end up with
inconsistent line-endings, adding to the fun.

I honestly do not believe there is a simple solution other
than renormalising line endings when you move a script from
one system to another, which is a pain (unless you are
old-fashioned and transfer your scripts with ftp or email,
rather than (g)zipping them).

Of course, you can avoid the problem by using explicit \n
throughout:

foo =
  "line 1\n" ..
  "line 2\n" ..
  "line 3\n"

or by using constructs like:

function L(t) return table.concat(t, "\n") end
foo = L {
  "line 1",
  "line 2",
  "line 3",
}

or even this extravaganza of metamethods:
(see http://lua-users.org/wiki/SimpleStringBuffer for a fuller implementation)

do
  local meta = {}
  function meta:__call(str) table.insert(self, str); return self end
  function meta:__unm() return table.concat(self, "\n") end
  function L(...) return setmetatable(arg, meta) end
end

foo = -L
  "line 1"
  "line 2"
  "line 3"

I know; it's yucky -- but cross-platform stuff always is. :(