diff --git a/Changelog b/Changelog index aa93420..2d6dcfe 100644 --- a/Changelog +++ b/Changelog @@ -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) diff --git a/include/ircaux.h b/include/ircaux.h index a8330f3..fe44b5e 100644 --- a/include/ircaux.h +++ b/include/ircaux.h @@ -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); diff --git a/include/module.h b/include/module.h index 8c78a37..d64b147 100644 --- a/include/module.h +++ b/include/module.h @@ -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" diff --git a/include/modval.h b/include/modval.h index be59a1f..da542e5 100644 --- a/include/modval.h +++ b/include/modval.h @@ -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]) diff --git a/source/functions.c b/source/functions.c index cf55e88..d38b72b 100644 --- a/source/functions.c +++ b/source/functions.c @@ -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; diff --git a/source/ircaux.c b/source/ircaux.c index 4c9ef8d..11a2508 100644 --- a/source/ircaux.c +++ b/source/ircaux.c @@ -1117,41 +1117,32 @@ 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 (howmany && string && start && group && start <= string) + if (ptr && start && group && start <= ptr) { + int invert = 0; + if (*group == '^') { group++; - for (ptr = string; (ptr >= start) && howmany; ptr--) - { - if (!strchr(group, *ptr)) - { - if (--howmany == 0) - return (char *)ptr; - } - } + invert = 1; } - else + + while (ptr > start) { - for (ptr = string; (ptr >= start) && howmany; ptr--) - { - if (strchr(group, *ptr)) - { - if (--howmany == 0) - return (char *)ptr; - } - } + ptr--; + if (invert == !strchr(group, *ptr)) + return (char *)ptr; } } return NULL; diff --git a/source/words.c b/source/words.c index ba7b9b5..c2ba198 100644 --- a/source/words.c +++ b/source/words.c @@ -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;