Replace uses of sindex() with constant accept strings with strpbrk() / strchr()

sindex(), where neither argument is NULL and the accept string does not start with ^, is exactly equivalent
to the standard function strpbrk().

Further, strpbrk() where the accept string is only one character long, is exactly equivalent to strchr().
This commit is contained in:
Kevin Easton 2017-11-17 16:23:39 +11:00
parent eb6ffc7395
commit 8c7d9334dd
4 changed files with 26 additions and 30 deletions

View File

@ -4352,8 +4352,7 @@ void BX_parse_line (const char *name, char *org_line, const char *args, int hist
{
char *line = NULL,
*stuff,
*s,
*t;
*s;
int args_flag = 0,
die = 0;
@ -4416,13 +4415,10 @@ void BX_parse_line (const char *name, char *org_line, const char *args, int hist
{
while ((s = line))
{
if ((t = sindex(line, "\r\n")))
{
*t++ = '\0';
line = t;
}
else
line = NULL;
line = strpbrk(line, "\r\n");
if (line)
*line++ = '\0';
parse_command(s, hist_flag, (char *)args);
if ((will_catch_break_exceptions && break_exception) ||
@ -4945,7 +4941,7 @@ BUILT_IN_COMMAND(BX_load)
char *optr = start;
/* Skip slashed brackets */
while ((ptr = sindex(optr, "{};/")) &&
while ((ptr = strpbrk(optr, "{};/")) &&
ptr != optr && ptr[-1] == '\\')
optr = ptr+1;

View File

@ -194,8 +194,8 @@ void add_to_history(char *line)
{
while (line && *line)
{
if ((ptr = sindex(line, "\n\r")) != NULL)
*(ptr++) = '\0';
if ((ptr = strpbrk(line, "\r\n")) != NULL)
*ptr++ = '\0';
add_to_history_list(hist_count, line);
last_dir = PREV;
hist_count++;

View File

@ -832,11 +832,11 @@ static char *cut_n_fix_glob ( char *nickuserhost )
/* doh! */
user = userhost;
if ((host = sindex(userhost, "@")))
if ((host = strchr(userhost, '@')))
/* USER IS CORRECT HERE */
*host++ = 0;
else if (sindex(userhost, "."))
else if (strchr(userhost, '.'))
{
user = "*";
host = userhost;
@ -850,14 +850,14 @@ static char *cut_n_fix_glob ( char *nickuserhost )
else
{
user = copy;
if ((host = sindex(user, "@")))
if ((host = strchr(user, '@')))
{
nick = "*";
*host++ = 0;
}
else
{
if (sindex(user, "."))
if (strchr(user, '.'))
{
nick = "*";
host = user;

View File

@ -460,25 +460,28 @@ extern int word_scount (char *str)
}
#endif
char *BX_next_arg (char *str, char **new_ptr)
char *BX_next_arg(char *str, char **new_ptr)
{
char *ptr;
char *ptr;
/* added by Sheik (kilau@prairie.nodak.edu) -- sanity */
if (!str || !*str)
if (!str)
return NULL;
if ((ptr = sindex(str, "^ ")) != NULL)
{
if ((str = sindex(ptr, space)) != NULL)
*str++ = (char) 0;
if ((str = strchr(ptr, ' ')) != NULL)
*str++ = 0;
else
str = empty_string;
}
else
{
str = empty_string;
}
if (new_ptr)
*new_ptr = str;
return ptr;
}
@ -616,7 +619,7 @@ char *safe_new_next_arg (char *str, char **new_ptr)
if (*ptr == '"')
{
start = ++ptr;
while ((str = sindex(ptr, "\"\\")) != NULL)
while ((str = strpbrk(ptr, "\"\\")) != NULL)
{
switch (*str)
{
@ -637,7 +640,7 @@ char *safe_new_next_arg (char *str, char **new_ptr)
}
else
{
if ((str = sindex(ptr, " \t")) != NULL)
if ((str = strpbrk(ptr, " \t")) != NULL)
*str++ = '\0';
else
str = empty_string;
@ -667,14 +670,11 @@ char *BX_new_new_next_arg (char *str, char **new_ptr, char *type)
{
if ((*ptr == '"') || (*ptr == '\''))
{
char blah[3];
blah[0] = *ptr;
blah[1] = '\\';
blah[2] = '\0';
char accept[] = { *ptr, '\\', 0 };
*type = *ptr;
start = ++ptr;
while ((str = sindex(ptr, blah)) != NULL)
while ((str = strpbrk(ptr, accept)) != NULL)
{
switch (*str)
{
@ -697,7 +697,7 @@ char *BX_new_new_next_arg (char *str, char **new_ptr, char *type)
else
{
*type = '\"';
if ((str = sindex(ptr, " \t")) != NULL)
if ((str = strpbrk(ptr, " \t")) != NULL)
*str++ = 0;
else
str = empty_string;