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:
Kevin Easton
2017-11-23 17:22:38 +11:00
parent de303ba554
commit ba1b9742ec
7 changed files with 36 additions and 49 deletions

View File

@@ -18,27 +18,24 @@ CVS_REVISION(words_c)
/* strsearch()
*
* If how > 0, returns a pointer to the how'th matching character forwards
* from mark, in the string starting at start.
* from the beginning of the string starting at start.
* If how < 0, returns a pointer to the -how'th matching character backwards
* from mark, in the string starting at start.
* from the end of the string starting at start.
* If how == 0, returns NULL.
*
* NULL mark begins the search at start.
*
* A matching character is any character in chars, unless chars starts with ^,
* in which case a matching character is any character NOT in chars.
*
* If there are insufficient matching characters, NULL is returned.
*/
extern char *BX_strsearch(const char *start, const char *mark, const char *chars, int how)
extern char *BX_strsearch(const char *start, const char *chars, int how)
{
const char *ptr = NULL;
if (!mark)
mark = start;
if (how > 0) /* forward search */
{
const char *mark = start;
for (; how > 0 && mark; how--)
{
ptr = sindex(mark, chars);
@@ -50,7 +47,10 @@ extern char *BX_strsearch(const char *start, const char *mark, const char *chars
}
else if (how < 0)
{
ptr = rsindex(mark, start, chars, -how);
ptr = start + strlen(start);
for (; how < 0 && ptr; how++)
ptr = rsindex(ptr, start, chars);
}
return (char *)ptr;