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:
@@ -1,5 +1,7 @@
|
|||||||
[Changes 1.2.2]
|
[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
|
* scr-bx now lists the detached screens if there is more than one that matches
|
||||||
the string supplied by the user. (caf)
|
the string supplied by the user. (caf)
|
||||||
|
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ char * BX_upper (char *);
|
|||||||
char * BX_lower (char *);
|
char * BX_lower (char *);
|
||||||
char *inv_strpbrk(const char *s, const char *reject);
|
char *inv_strpbrk(const char *s, const char *reject);
|
||||||
char * BX_sindex (const char *, const char *);
|
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_path_search (char *, char *);
|
||||||
char * BX_double_quote (const char *, const 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 */
|
/* From words.c */
|
||||||
#define SOS -32767
|
#define SOS -32767
|
||||||
#define EOS 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_to_word(const char *, int);
|
||||||
char *BX_move_word_rel(const char *, char **, int);
|
char *BX_move_word_rel(const char *, char **, int);
|
||||||
char *BX_extract(char *, int, int);
|
char *BX_extract(char *, int, int);
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
* if we change the table below, we change this module number to the
|
* if we change the table below, we change this module number to the
|
||||||
* current date (YYYYMMDDxx where xx is a serial number).
|
* current date (YYYYMMDDxx where xx is a serial number).
|
||||||
*/
|
*/
|
||||||
#define MODULE_VERSION 2017111401UL
|
#define MODULE_VERSION 2017112301UL
|
||||||
|
|
||||||
#include "struct.h"
|
#include "struct.h"
|
||||||
|
|
||||||
|
|||||||
@@ -146,7 +146,7 @@ extern Function_ptr *global;
|
|||||||
#define expand_twiddle (*(char * (*)(char *))global[EXPAND_TWIDDLE])
|
#define expand_twiddle (*(char * (*)(char *))global[EXPAND_TWIDDLE])
|
||||||
#define check_nickname (*(char * (*)(char *))global[CHECK_NICKNAME])
|
#define check_nickname (*(char * (*)(char *))global[CHECK_NICKNAME])
|
||||||
#define sindex (*(char * (*)(const char *, const char *))global[SINDEX])
|
#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 is_number (*(int (*)(const char *))global[ISNUMBER])
|
||||||
#define rfgets (*(char * (*)(char *, int , FILE *))global[RFGETS])
|
#define rfgets (*(char * (*)(char *, int , FILE *))global[RFGETS])
|
||||||
#define path_search (*(char * (*)(char *, char *))global[PATH_SEARCH])
|
#define path_search (*(char * (*)(char *, char *))global[PATH_SEARCH])
|
||||||
@@ -199,7 +199,7 @@ extern Function_ptr *global;
|
|||||||
|
|
||||||
|
|
||||||
/* words.c reg.c */
|
/* 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_to_word (*(char *(*)(const char *, int))global[MOVE_TO_WORD])
|
||||||
#define move_word_rel (*(char *(*)(const char *, char **, int ))global[MOVE_WORD_REL])
|
#define move_word_rel (*(char *(*)(const char *, char **, int ))global[MOVE_WORD_REL])
|
||||||
#define extract (*(char *(*)(char *, int , int ))global[EXTRACT])
|
#define extract (*(char *(*)(char *, int , int ))global[EXTRACT])
|
||||||
|
|||||||
@@ -1520,7 +1520,7 @@ BUILT_IN_FUNCTION(function_rindex, word)
|
|||||||
|
|
||||||
/* need to find out why ^x doesn't work */
|
/* need to find out why ^x doesn't work */
|
||||||
GET_STR_ARG(chars, word);
|
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);
|
RETURN_INT(last ? last - word : -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1993,7 +1993,7 @@ BUILT_IN_FUNCTION(function_idle, input)
|
|||||||
*/
|
*/
|
||||||
BUILT_IN_FUNCTION(function_before, word)
|
BUILT_IN_FUNCTION(function_before, word)
|
||||||
{
|
{
|
||||||
char *pointer = NULL;
|
char *pointer;
|
||||||
char *chars;
|
char *chars;
|
||||||
char *tmp;
|
char *tmp;
|
||||||
long numint;
|
long numint;
|
||||||
@@ -2011,10 +2011,7 @@ BUILT_IN_FUNCTION(function_before, word)
|
|||||||
chars = tmp;
|
chars = tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (numint < 0 && strlen(word))
|
pointer = strsearch(word, chars, numint);
|
||||||
pointer = word + strlen(word) - 1;
|
|
||||||
|
|
||||||
pointer = strsearch(word, pointer, chars, numint);
|
|
||||||
|
|
||||||
if (!pointer)
|
if (!pointer)
|
||||||
RETURN_EMPTY;
|
RETURN_EMPTY;
|
||||||
@@ -2031,7 +2028,7 @@ BUILT_IN_FUNCTION(function_before, word)
|
|||||||
BUILT_IN_FUNCTION(function_after, word)
|
BUILT_IN_FUNCTION(function_after, word)
|
||||||
{
|
{
|
||||||
char *chars;
|
char *chars;
|
||||||
char *pointer = NULL;
|
char *pointer;
|
||||||
char *tmp;
|
char *tmp;
|
||||||
long numint;
|
long numint;
|
||||||
|
|
||||||
@@ -2046,10 +2043,7 @@ BUILT_IN_FUNCTION(function_after, word)
|
|||||||
chars = tmp;
|
chars = tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (numint < 0 && strlen(word))
|
pointer = strsearch(word, chars, numint);
|
||||||
pointer = word + strlen(word) - 1;
|
|
||||||
|
|
||||||
pointer = strsearch(word, pointer, chars, numint);
|
|
||||||
|
|
||||||
if (!pointer || !*pointer)
|
if (!pointer || !*pointer)
|
||||||
RETURN_EMPTY;
|
RETURN_EMPTY;
|
||||||
|
|||||||
@@ -1117,41 +1117,32 @@ char *BX_sindex(const char *string, const char *group)
|
|||||||
|
|
||||||
/* rsindex()
|
/* rsindex()
|
||||||
*
|
*
|
||||||
* Returns a pointer to the howmany'th last matching character in a string, or
|
* Returns a pointer to the last matching character that appears in a string BEFORE the
|
||||||
* NULL if there are less than howmany matching characters. Returns NULL
|
* initial search point (to search the entire string, set the initial search point to
|
||||||
* if howmany is zero.
|
* 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'
|
* 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'.
|
* 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)
|
||||||
|
|
||||||
if (howmany && string && start && group && start <= string)
|
|
||||||
{
|
{
|
||||||
|
int invert = 0;
|
||||||
|
|
||||||
if (*group == '^')
|
if (*group == '^')
|
||||||
{
|
{
|
||||||
group++;
|
group++;
|
||||||
for (ptr = string; (ptr >= start) && howmany; ptr--)
|
invert = 1;
|
||||||
{
|
|
||||||
if (!strchr(group, *ptr))
|
|
||||||
{
|
|
||||||
if (--howmany == 0)
|
|
||||||
return (char *)ptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
while (ptr > start)
|
||||||
{
|
{
|
||||||
for (ptr = string; (ptr >= start) && howmany; ptr--)
|
ptr--;
|
||||||
{
|
if (invert == !strchr(group, *ptr))
|
||||||
if (strchr(group, *ptr))
|
return (char *)ptr;
|
||||||
{
|
|
||||||
if (--howmany == 0)
|
|
||||||
return (char *)ptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|||||||
@@ -18,27 +18,24 @@ CVS_REVISION(words_c)
|
|||||||
/* strsearch()
|
/* strsearch()
|
||||||
*
|
*
|
||||||
* If how > 0, returns a pointer to the how'th matching character forwards
|
* 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
|
* 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.
|
* If how == 0, returns NULL.
|
||||||
*
|
*
|
||||||
* NULL mark begins the search at start.
|
|
||||||
*
|
|
||||||
* A matching character is any character in chars, unless chars starts with ^,
|
* A matching character is any character in chars, unless chars starts with ^,
|
||||||
* in which case a matching character is any character NOT in chars.
|
* in which case a matching character is any character NOT in chars.
|
||||||
*
|
*
|
||||||
* If there are insufficient matching characters, NULL is returned.
|
* 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;
|
const char *ptr = NULL;
|
||||||
|
|
||||||
if (!mark)
|
|
||||||
mark = start;
|
|
||||||
|
|
||||||
if (how > 0) /* forward search */
|
if (how > 0) /* forward search */
|
||||||
{
|
{
|
||||||
|
const char *mark = start;
|
||||||
|
|
||||||
for (; how > 0 && mark; how--)
|
for (; how > 0 && mark; how--)
|
||||||
{
|
{
|
||||||
ptr = sindex(mark, chars);
|
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)
|
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;
|
return (char *)ptr;
|
||||||
|
|||||||
Reference in New Issue
Block a user