[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: A useful useless fact about Lua keywords
- From: Philippe Verdy <verdyp@...>
- Date: Wed, 15 Sep 2021 17:56:13 +0200
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.
On Wed, Sep 15, 2021 at 2:53 PM Flyer31 Test <firstname.lastname@example.org> 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
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