lua-users home
lua-l archive

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


>Peter Melnichenko <mpeterval@gmail.com> Sat, 22 Aug 2015 09:54:16 +0300

>On Fri, Aug 21, 2015 at 11:58 PM, Sean Conner <sean@conman.org> wrote:
>> <snip>
>> But here's both to compare and contrast:
>>
>>         val = x:sub(1,1):byte() * 2^24
>>             + x:sub(2,2):byte() * 2^16
>>             + x:sub(3,3):byte() * 2^8
>>             + x:sub(4,4):byte()
>>
>>         val = x:sub(1,1):byte() << 24
>>             | x:sub(2,2):byte() << 16
>>             | x:sub(3,3):byte() <<  8
>>             | x:sub(4,4):byte()
>>

>By the way, `x:sub(i, i):byte()` can be written as `x:byte(i)`, so it's
less verbose than that.

Thanks Peter!

Also of course:

val = 0
for i=0, 3 do  val = val | (x:byte(i+1) << (8*i)) end

This suggests that Lua ought to support index operation on strings now we
are encouraged to
see them as byte streams. It's easy to add this to the metatable, but if Lua
did it natively, then
this kind of expression could be pure (not involving function calls) and so
more efficient.

- John


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus