[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Lua Suggestion: return nil in string.find/match() for indexes that are before the string
- From: Coroutines <coroutines@...>
- Date: Sat, 1 Mar 2014 23:06:16 -0800
I am using string.find() in a situation where I don't know the index
(where the needle/pattern should match). The index is arbitrary; +/-31 is
used in these examples.
This works as expected:
> = string.find('cat', 'cat', 31, true)
nil
This does not:
> = string.find('cat', 'cat', -31, true)
1 3
I expect it to match the string at the index I passed to it, not
silently reposition the start at a valid index (1). The function I think
needs fixing is str_find_aux() in lstrlib.c:
http://www.lua.org/source/5.2/lstrlib.c.html#str_find_aux
The first if-statement body should have this within it to correct the
behavior (replace the "init = 1"):
lua_pushnil(L); /* cannot find anything */
return 1;
If I am wrong and this was an intended behavior, then I would suggest
making it consistent -- if the 'init'/starting index is before or
after the string, make it a valid index in the string. I'd rather
have it return
nil because the string doesn't "exist" at that point in memory,
though.
PEACE, CHEETO PEOPLE