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

@@ -1,5 +1,7 @@
[Changes 1.2.2]
* Simplify rsindex() and strsearch() exported functions. (caf)
* scr-bx now lists the detached screens if there is more than one that matches
the string supplied by the user. (caf)

View File

@@ -30,7 +30,7 @@ char * BX_upper (char *);
char * BX_lower (char *);
char *inv_strpbrk(const char *s, const char *reject);
char * BX_sindex (const char *, const char *);
char * BX_rsindex (const char *, const char *, const char *, int);
char * BX_rsindex (const char *, const char *, const char *);
char * BX_path_search (char *, char *);
char * BX_double_quote (const char *, const char *, char *);
@@ -133,7 +133,7 @@ char * base64_encode (const void *data, size_t size);
/* From words.c */
#define SOS -32767
#define EOS 32767
char *BX_strsearch(const char *, const char *, const char *, int);
char *BX_strsearch(const char *, const char *, int);
char *BX_move_to_word(const char *, int);
char *BX_move_word_rel(const char *, char **, int);
char *BX_extract(char *, int, int);

View File

@@ -10,7 +10,7 @@
* if we change the table below, we change this module number to the
* current date (YYYYMMDDxx where xx is a serial number).
*/
#define MODULE_VERSION 2017111401UL
#define MODULE_VERSION 2017112301UL
#include "struct.h"

View File

@@ -146,7 +146,7 @@ extern Function_ptr *global;
#define expand_twiddle (*(char * (*)(char *))global[EXPAND_TWIDDLE])
#define check_nickname (*(char * (*)(char *))global[CHECK_NICKNAME])
#define sindex (*(char * (*)(const char *, const char *))global[SINDEX])
#define rsindex (*(char * (*)(const char *, const char *, const char *, int))global[RSINDEX])
#define rsindex (*(char * (*)(const char *, const char *, const char *))global[RSINDEX])
#define is_number (*(int (*)(const char *))global[ISNUMBER])
#define rfgets (*(char * (*)(char *, int , FILE *))global[RFGETS])
#define path_search (*(char * (*)(char *, char *))global[PATH_SEARCH])
@@ -199,7 +199,7 @@ extern Function_ptr *global;
/* words.c reg.c */
#define strsearch (*(char *(*)(const char *, const char *, const char *, int ))global[STRSEARCH])
#define strsearch (*(char *(*)(const char *, const char *, int))global[STRSEARCH])
#define move_to_word (*(char *(*)(const char *, int))global[MOVE_TO_WORD])
#define move_word_rel (*(char *(*)(const char *, char **, int ))global[MOVE_WORD_REL])
#define extract (*(char *(*)(char *, int , int ))global[EXTRACT])

View File

@@ -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;

View File

@@ -1117,43 +1117,34 @@ char *BX_sindex(const char *string, const char *group)
/* rsindex()
*
* Returns a pointer to the howmany'th last matching character in a string, or
* NULL if there are less than howmany matching characters. Returns NULL
* if howmany is zero.
* Returns a pointer to the last matching character that appears in a string BEFORE the
* initial search point (to search the entire string, set the initial search point to
* the terminating NUL). Returns NULL if no matching characters are found.
*
* The string to be searched starts at 'start' and the initial search point is 'ptr'.
*
* A matching character is any character in 'group', unless the first character of 'group'
* is a ^, in which case a matching character is any character NOT in 'group'.
*/
char *BX_rsindex(const char *string, const char *start, const char *group, int howmany)
char *BX_rsindex(const char *ptr, const char *start, const char *group)
{
const char *ptr;
if (ptr && start && group && start <= ptr)
{
int invert = 0;
if (howmany && string && start && group && start <= string)
{
if (*group == '^')
{
group++;
for (ptr = string; (ptr >= start) && howmany; ptr--)
invert = 1;
}
while (ptr > start)
{
if (!strchr(group, *ptr))
{
if (--howmany == 0)
ptr--;
if (invert == !strchr(group, *ptr))
return (char *)ptr;
}
}
}
else
{
for (ptr = string; (ptr >= start) && howmany; ptr--)
{
if (strchr(group, *ptr))
{
if (--howmany == 0)
return (char *)ptr;
}
}
}
}
return NULL;
}

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;