[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua Suggestion: return nil in string.find/match() for indexes that are before the string
- From: Francisco Olarte <folarte@...>
- Date: Mon, 3 Mar 2014 18:24:46 +0100
Hi:
On Sun, Mar 2, 2014 at 8:21 PM, Coroutines <coroutines@gmail.com> wrote:
> 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.
I do not agree. string.find is trying to find AFTER the passed
position, not AT it, so it has its logic to clip it to 1, it's a
simple optimization. I.e, string length 5, you pass -7, string cannot
be found seven chars before the end, because there is no string there,
try 6 chars before the end, same problem, try 5 chars, now we are
talking. Clipping to 1 just optimizes out the impossible end. Ie, if
you tell someone to find 'b' in 'abc' between offsets minus and plus
infinity, it can be found at 2, and you can clip the bounds to 1..3
before starting.
Francisco Olarte.