[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Lua Suggestion: return nil in string.find/match() for indexes that are before the string
- From: "Thiago L." <fakedme@...>
- Date: Sun, 02 Mar 2014 16:36:46 -0300
On 02/03/2014 16:21, Coroutines wrote:
Ok, you want a plain match, same rules apply: it doesn't care about the
index, it'll just try to find the data, for index -30 and string "cat"
-30 would be translated into -27 (because string length - 30) and then
it would still match as "cat" is still on the string (which's 30 chars
long with a padding of 27 empty spaces on the left).
On Sun, Mar 2, 2014 at 11:08 AM, Thiago L. <firstname.lastname@example.org> wrote:
On 02/03/2014 15:56, Coroutines wrote:
On Sun, Mar 2, 2014 at 12:18 AM, Dirk Laurie <email@example.com>
2014-03-02 9:06 GMT+02:00 Coroutines <firstname.lastname@example.org>:
This does not:
= string.find('cat', 'cat', -31, true)
I expect it to match the string at the index I passed to it, not
silently reposition the start at a valid index (1).
All the string functions do that. It is documented under string.sub.
| If, after the translation of negative indices, i is less than 1, it
is corrected to 1.
Yes :-) I believe it makes sense for string.sub(), as you would want
the valid portion of the string from (let's say) -31 to 5. This would
be readjusted to only "sub out" indexes 1 through 5. I do not believe
it makes sense for string.find()/string.match() -- as false-positives
are possible when I tell it to match somewhere clearly before the
string (in a place it doesn't exist).
I'm not sure if you know how matching works, but if you want to match at the
start of a string, you have to use string.match("cat","^cat"). The only bug
I see (on Lua 5.1 if that matters) is when using negative indices and
matching at the start of a string...
I was attempting to do a plain-match from index -31 (an index I said I
cannot predict, -31 is just an example). I do understand how to use
the pattern matching facilities, I am saying that if the index is
before the string it should not be corrected to be 1, it should not be
silently corrected to be a valid index. If the starting index is
after the string nil is returned because it cannot match a string that
doesn't exist at that point. The same should be true if the index is
before the string.
As I was telling Mr. Laurie, string.sub() is fine but I very much
disagree with string.find()/string.match() moving the starting index
The problem is still there in 5.2:
The bug is that when matching with ^cat and patterns it doesn't care
about the empty spaces.