|
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).
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.