lua-users home
lua-l archive

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


 
Technically speaking, a purist might even argue that ("a"):sub(1,3) should result in "aaa" (e.g. me). Or that ("abc"):sub(1,4) should result in "a".

Why on earth someone may want this?  :-)


Note how select, strings and files have no meaningful concept of negative indices - they're exceptions. Thus, handling negative indices exceptionally makes sense. But you should still aim for consistency wherever possible.

I don't know what the Lua devs think. But it seems they do everything to keep things consistent, only applying exceptions in exceptional circumstances.
 
Let me ask you again: what consistency are you talking about?
Please give an example of the "consistency" that may be broken by redefining treatment of zero index for strings.

    And don't forget, it'd also require a change to string.find,
    string.gsub, string.gmatch, string.match, string.byte, etc
    basically anything that takes an index.

Yes, "string.byte" should be modified the same way as "string.sub" as it uses the same rules for specifying an interval of indexes.
But there is absolutely nothing nothing to change in implementations of string.find, string.gsub, string.gmatch and string.match.

string.find, string.gsub, string.gmatch and string.match all take a starting index, which can be negative.

First of all, string.gsub and string.gmatch does not take starting index as their argument.

Other functions mentioned above (select, string.find and string.match) do take a starting index, which can be positive or negative.
But according to Lua manual, there is no behavior defined for zero index in these functions.
Correct Lua programs NEVER pass zero index to them.
So, there is no need to change their implementation.
And there is no consistency-related problems with these functions.