lua-users home
lua-l archive

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


Oh, very helpful and impressive info is this, thank you very much ... .

(this was my main objection against this string.sub, that extensive
use would somehow start many many super-small allocations on the
heap... I would not have expected that Lua has some means to avoid
this - very ingenious indeed then ... In this case I really could
consider to make some "mini string" lib which mainly reduces to
string.sub and a "mini-version" of string.find... (but I must admit
that I really have to check again whether I really would still gain
50kB ROM if I skip openlibs... possibly this was only in some very
basic initial version of my software, which also then possibly did not
even use the ARMCC string.h c lib... ARMCC somehow is extremely
efficient in stripping any non-used functions, and as long as you do
not use functions like sprintf, it certainly will also NOT use it, and
alone sprintf I think will add about 10kB ROM code... ).


On Wed, Sep 15, 2021 at 5:56 PM Philippe Verdy <verdyp@gmail.com> wrote:
>
> 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.