lua-users home
lua-l archive

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


>> 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.:

> 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

I'd say that it matters for me in the sense of 'aFile:read( 16384 )' appearing twice, i.e. it
doesn't look nice when a buffer size is mentioned twice (you could have a constant for that though)
and a fetch call is made in two places. Anyway, I'd prefer a 'while true do ...' or 'repeat .. until
false' form as well as the author.

Be well,
Seny