Add inv_strpbrk() function and convert equivalent sindex() calls

inv_strpbrk() is the inverse of the standard function strpbrk().

Calls to sindex() where both strings are not NULL and the second string begins with ^ are equivalent
to calls to inv_strpbrk() (but without the ^).  Convert those calls.
This commit is contained in:
Kevin Easton
2017-11-17 17:19:07 +11:00
parent 8c7d9334dd
commit 0d5698d41b
3 changed files with 26 additions and 17 deletions

View File

@@ -28,6 +28,7 @@ char * BX_last_arg (char **);
char * BX_expand_twiddle (char *); char * BX_expand_twiddle (char *);
char * BX_upper (char *); char * BX_upper (char *);
char * BX_lower (char *); char * BX_lower (char *);
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 *, int);
char * BX_path_search (char *, char *); char * BX_path_search (char *, char *);

View File

@@ -3526,7 +3526,7 @@ BUILT_IN_COMMAND(untopic)
ChannelList *chan; ChannelList *chan;
int server; int server;
args = sindex(args, "^ "); args = inv_strpbrk(args, " ");
if (is_channel(args)) if (is_channel(args))
channel = next_arg(args, &args); channel = next_arg(args, &args);
@@ -3543,12 +3543,12 @@ BUILT_IN_COMMAND(e_topic)
ChannelList *chan; ChannelList *chan;
int server; int server;
args = sindex(args, "^ "); args = inv_strpbrk(args, " ");
if (is_channel(args)) if (is_channel(args))
{ {
channel = next_arg(args, &args); channel = next_arg(args, &args);
args = sindex(args, "^ "); args = inv_strpbrk(args, " ");
} }
if (!(chan = prepare_command(&server, channel, args ? PC_TOPIC : NO_OP))) if (!(chan = prepare_command(&server, channel, args ? PC_TOPIC : NO_OP)))

View File

@@ -467,7 +467,7 @@ char *BX_next_arg(char *str, char **new_ptr)
if (!str) if (!str)
return NULL; return NULL;
if ((ptr = sindex(str, "^ ")) != NULL) if ((ptr = inv_strpbrk(str, " ")) != NULL)
{ {
if ((str = strchr(ptr, ' ')) != NULL) if ((str = strchr(ptr, ' ')) != NULL)
*str++ = 0; *str++ = 0;
@@ -614,7 +614,7 @@ char *safe_new_next_arg (char *str, char **new_ptr)
if (!str || !*str) if (!str || !*str)
return empty_string; return empty_string;
if ((ptr = sindex(str, "^ \t")) != NULL) if ((ptr = inv_strpbrk(str, " \t")) != NULL)
{ {
if (*ptr == '"') if (*ptr == '"')
{ {
@@ -666,7 +666,7 @@ char *BX_new_new_next_arg (char *str, char **new_ptr, char *type)
if (!str || !*str) if (!str || !*str)
return NULL; return NULL;
if ((ptr = sindex(str, "^ \t")) != NULL) if ((ptr = inv_strpbrk(str, " \t")) != NULL)
{ {
if ((*ptr == '"') || (*ptr == '\'')) if ((*ptr == '"') || (*ptr == '\''))
{ {
@@ -1066,6 +1066,23 @@ char *BX_check_nickname (char *nick)
return *nick ? nick : NULL; return *nick ? nick : NULL;
} }
/* inv_strcpbrk()
*
* Returns a pointer to the first character in a string which is NOT found
* in 'reject'.
*
* This is the inverse of the standard function strpbrk().
*/
char *inv_strpbrk(const char *s, const char *reject)
{
s += strspn(s, reject);
if (!*s)
return NULL;
return (char *)s;
}
/* sindex() /* sindex()
* *
* Returns a pointer to the first matching character in a string, or NULL if * Returns a pointer to the first matching character in a string, or NULL if
@@ -1081,18 +1098,9 @@ char *BX_sindex(const char *string, const char *group)
return NULL; return NULL;
if (*group == '^') if (*group == '^')
{ return inv_strpbrk(string, group + 1);
string += strspn(string, group + 1);
}
else else
{ return strpbrk(string, group);
string += strcspn(string, group);
}
if (*string)
return (char *)string;
else
return NULL;
} }
/* rsindex() /* rsindex()