lua-users home
lua-l archive

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


Hisham wrote:

> I understand the convenience of normalizing end-of-lines for
> file:read("*l") and for Lua source code,...

I hate to drag out a discussion, but I want to make sure I understand this.

I happened to be coding up a script last night and notice that when I
combined lines which were read with '*l', they had a CR in the middle. I
was running under Linux but the input file was generated under Windows
so it had CR's in it. Here's a sample that shows what I mean:

> f=assert(io.open('test.txt','w'))
> f:write('line 1\r\nline 2\r\n')
> f:close()
> f=assert(io.open('test.txt','r'))
> s1=f:read('*l')
> s2=f:read('*l')
> print(s1:len(),s2:len())
7       7
> print(s1:sub(-1):byte())
13
>

I was expecting the CR to be removed, though the manual doesn't say it
will be. It was easy enough to fix, if a little clunky.

My personal preference is that '*line' strip CR/LF/CR-LF since it is
obviously intended for line-oriented transactions. Same with f:lines().
But I can't think of any other times that I would want this behavior.
When loading source code the parser can treat any of CR/LF/CR-LF as
newline, but embedded long strings should not be modified.

It is probably worth mentioning in the manual whenever the input data
are modified.

Doug

-- 
Innovative Concepts, Inc. www.innocon.com 703-893-2007 x220