lua-users home
lua-l archive

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


On Thu, Apr 22, 2010 at 7:03 PM, Petite Abeille
<petite.abeille@gmail.com> wrote:
>
> On Apr 22, 2010, at 12:49 PM, Jerome Vuarand wrote:
>
>> local file = io.open(... or "bigfile.iso", "rb")
>> while true do
>>       local chunk = file:read(16*1024) -- 16kB at a time
>>       if not chunk then break end
>>       evp:update(chunk)
>> end
>
> A bit off topic, but... out of curiosity, I was wondering about the use of 'while true do' + 'if not chunk then break' vs. 'while aChunk do' + 'aChunk = aFile:read()', e.g.:
>
> -- using LHF's previous version of lmd5, the one before it was married to openssl :D
> local sha1 = require( 'sha1' )
> local aHash = sha1.new()
> local aFile = assert( io.open( 'bigfile.iso', 'rb' ) )
> local aChunk = aFile:read( 16384 )
>
> while aChunk do
>    aHash:update( aChunk )
>    aChunk = aFile:read( 16384 )
> end
>
> Any reasons, aside from stylistic ones, to favor one form or another? I personally prefer an explicit conditional when using a 'while' clause, as I find it easier to comprehend, but I have noticed the above style quite often. Just curious :)

I dislike having the "aChunk = aFile:read(16384)" bit in two places
because it feels too easy to change one and forget about the other.

> All things consider, I would rather use an iterator anyway, but perhaps that's just me:

I agree, that's not a bad way to do it.

-Duncan