lua-users home
lua-l archive

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


Hi Nicola,
as I understand it, this is a smart trick to write something like
C['A'], and then you get 0x41.

So to check the first string character of str to be 'A', you can then
write str.byte(1,1)== C['A'], and this somehow looks much more nice
than str.byte(1,1)==0x41.. .

And it avoids the memory "mini alloc" if you write str.sub(1,1)== 'a'.


On Thu, Sep 16, 2021 at 8:06 AM Fontana Nicola <ntd@entidi.it> wrote:
>
> Il giorno mer, 15/09/2021 alle 15.16 -0300, Luiz Henrique de Figueiredo ha
> scritto:
> > ...
> > do
> > local C={}
> > for i=0,255 do C[i]=string.char(i); C[C[i]]=i end
> > function char(s,i) return C[s:byte(i)] end
> > end
> > ...
>
> What's the purpose of `C[C[i]]=i`? Can `string.byte` return a string?
>
> Ciao.
> --
> Nicola
>