lua-users home
lua-l archive

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




On 01/10/16 08:00 AM, Egor Skriptunoff wrote:

    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?  :-)

Wrap-around on the length of the string. Makes more sense with the whole negative starting index thing.



    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.

The consistency that 0 comes before 1. Negative indices are treated the way they are because Lua has some sort of wrap-around for when negative indices are meaningless - except this wrap-around only exists for a single iteration as it currently stands.


            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.

The manual doesn't specify their limits either, or what a negative index does. It's just left to the reader to assume it works the same as string.sub.

There is absolutely no reason why a correct Lua program should never pass zero to them.

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.