[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: string:sub
- From: David Burgess <dabsoft@...>
- Date: Sat, 06 Mar 2010 23:59:18 +1100
Whats not very "Luaish" is the inconsistency. As I was unclear,
I have no problems with parameter 1, it is #2 thats the problem.
The inconsistency is what the value 0 does on parameter 2,
seemingly the same as -1. I would have expected -1 on parameter 2
to mean 1 less than zero, i.e. -1 from the end.
On 6/3/2010 6:53 PM, Philippe Lhoste wrote:
On 06/03/2010 01:29, David Burgess wrote:
It seems to me that negative values of string.sub() are inconsistent
with not only the first parameter but also with string.find().
Perhaps that's because I am French, but I have hard time to understand
that sentence. string.sub() returns strings, no negative values.
If you refer to negative values of parameters, I don't see how they are
inconsistent with "the first parameter".
And why it is inconsistent with string.find().
For both functions, negatives values are for indexes are explained at
the start of the String Manipulation chapter: "When indexing a string in
Lua, the first character is at position 1 (not at 0, as in C). Indices
are allowed to be negative and are interpreted as indexing backwards,
from the end of the string. Thus, the last character is at position -1,
and so on."
The results of the examples you show seems consistent with this definition.
I am boldly suggesting that the second parameter with negative indices
behave the same as the first.
It does. If they are negative, their absolute value is relative to the
end of the string, going backward.
Maybe I have used too many similar functions in other languages but -1
meaning the last character seems intuitive to me.
Perhaps you can use a wrapper around this function so it behaves as you
expect? Or your own function.
Lua, in many ways, isn't like the other languages, that's part of its