[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: string:sub
- From: Philippe Lhoste <PhiLho@...>
- Date: Sat, 06 Mar 2010 08:53:09 +0100
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
charm... :-)
--
Philippe Lhoste
-- (near) Paris -- France
-- http://Phi.Lho.free.fr
-- -- -- -- -- -- -- -- -- -- -- -- -- --