lua-users home
lua-l archive

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


String:sub dues not need to allocate anything, it uses shared buffers, because all strings are immutable. The collector just needs to handle the main string wgre all references to its substrings should be counted.
Garbage collection *may* occur only for concatenations.

Le mer. 15 sept. 2021 à 16:03, Viacheslav Usov <via.usov@gmail.com> a écrit :
On Wed, Sep 15, 2021 at 2:53 PM Flyer31 Test <flyer31@googlemail.com> wrote:
>
> But what is really a bit not so nice in lua, if you are used to C at
> least, is that you can not easily check the first character of a
> string... e. g. if you use a string for setting some function options,
> it would be quite nice if you could check the start character beeing
> 'a' or other char without invoking a string lib function

You can: 'a' <= str and str < 'b'.

Caveat from the manual: "if both arguments are strings, then their
values are compared _according to the current locale_" - emphasis
added.

Another thing to observe is that if you need access to individual
bytes of a string, string.byte() may be a superior choice over
string.sub() because, at least in theory, the former does not need to
allocate garbage-collected memory unlike the latter.

If your strings are UTF-8 encoded, then utf8.codepoint() might be
similarly considered.

Cheers,
V.