lua-users home
lua-l archive

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


On Fri, Sep 30, 2016 at 11:12 PM, Soni L. <fakedme@gmail.com> wrote:


    I'd suggest to redefine the interpretation of zero value for
    "index_from" argument of string:sub.
    The documentation may look like the following:

    Positive indexes 1, 2, 3... count from the beginning of the string.
    Negative indexes -1, -2, -3,... count backward from the end of the
    string.
    Index 0 has special meaning, its semantic is different for
    "index_from" and "index_to" arguments:
    index_from = 0 means "the index after the last character of the
    string"
    index_to = 0 means "the index before the first character of the
    string"
    In other words, the "rule of maximum restriction" holds.
    There are two related Lua idioms: for any non-negative N prefix of
    length N is ":sub(1, N)" and suffix of length N is ":sub(-N)".


"s:sub(k,0)" and "s:sub(0,k)" are always empty strings for any s and k
Zero index always "kills" the substring.
This property is quite understandable for beginners, descriptive and memorable.


Which is stupid if you consider all the things from numeric for, table.move, table.unpack, table.concat, etc. Remember, just because strings don't support index 0, doesn't mean they aren't consistent with tables. Even # is consistent between strings and tables (however, strings don't have a concept of nil).

What consistency you are talking about?
Unlike the "string" library, "table" library DOES NOT have the possibility to use negative indexes for counting backwards from the end of a table.
For example, table.unpack(t, -2) DOES NOT return two last elements of a table.
There is no "consistency" that may be broken.
And there is nothing to change in implementation of "table" library.
 
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.