Simplify calling of rsindex() and strsearch()
Remove the 'how' parameter to rsindex(), making it reverse-search for only the first matching character, as sindex() already does. There are only two callers, and one already passed a hardcoded value of 1. Also change rsindex() so that it starts searching at the character BEFORE the passed in position. This makes it easier to repeatedly call rsindex() in a loop to search for the Nth matching character, and also fixes a technical instance of undefined behaviour where a pointer is decremented to point before the start of the string. Remove the 'mark' parameter to strsearch(). Instead, always forward-search from the beginning of the string and reverse-search from the end of the string, as this is what the two callers want anyway. Bump the module ABI version because these functions are exported to modules.
This commit is contained in:
@@ -1520,7 +1520,7 @@ BUILT_IN_FUNCTION(function_rindex, word)
|
||||
|
||||
/* need to find out why ^x doesn't work */
|
||||
GET_STR_ARG(chars, word);
|
||||
last = rsindex(word + strlen(word) - 1, word, chars, 1);
|
||||
last = rsindex(word + strlen(word), word, chars);
|
||||
RETURN_INT(last ? last - word : -1);
|
||||
}
|
||||
|
||||
@@ -1993,7 +1993,7 @@ BUILT_IN_FUNCTION(function_idle, input)
|
||||
*/
|
||||
BUILT_IN_FUNCTION(function_before, word)
|
||||
{
|
||||
char *pointer = NULL;
|
||||
char *pointer;
|
||||
char *chars;
|
||||
char *tmp;
|
||||
long numint;
|
||||
@@ -2011,10 +2011,7 @@ BUILT_IN_FUNCTION(function_before, word)
|
||||
chars = tmp;
|
||||
}
|
||||
|
||||
if (numint < 0 && strlen(word))
|
||||
pointer = word + strlen(word) - 1;
|
||||
|
||||
pointer = strsearch(word, pointer, chars, numint);
|
||||
pointer = strsearch(word, chars, numint);
|
||||
|
||||
if (!pointer)
|
||||
RETURN_EMPTY;
|
||||
@@ -2031,7 +2028,7 @@ BUILT_IN_FUNCTION(function_before, word)
|
||||
BUILT_IN_FUNCTION(function_after, word)
|
||||
{
|
||||
char *chars;
|
||||
char *pointer = NULL;
|
||||
char *pointer;
|
||||
char *tmp;
|
||||
long numint;
|
||||
|
||||
@@ -2046,10 +2043,7 @@ BUILT_IN_FUNCTION(function_after, word)
|
||||
chars = tmp;
|
||||
}
|
||||
|
||||
if (numint < 0 && strlen(word))
|
||||
pointer = word + strlen(word) - 1;
|
||||
|
||||
pointer = strsearch(word, pointer, chars, numint);
|
||||
pointer = strsearch(word, chars, numint);
|
||||
|
||||
if (!pointer || !*pointer)
|
||||
RETURN_EMPTY;
|
||||
|
||||
Reference in New Issue
Block a user