Convert sindex() and rsindex() to use const char * arguments

Also simply sindex() considerably using standard strspn() / strcspn() functions.
This commit is contained in:
Kevin Easton
2017-11-17 12:21:12 +11:00
parent fea88185f8
commit e8ce5fb973
3 changed files with 34 additions and 40 deletions

View File

@@ -28,8 +28,8 @@ char * BX_last_arg (char **);
char * BX_expand_twiddle (char *);
char * BX_upper (char *);
char * BX_lower (char *);
char * BX_sindex (register char *, char *);
char * BX_rsindex (register char *, char *, char *, int);
char * BX_sindex (const char *, const char *);
char * BX_rsindex (const char *, const char *, const char *, int);
char * BX_path_search (char *, char *);
char * BX_double_quote (const char *, const char *, char *);

View File

@@ -145,8 +145,8 @@ extern Function_ptr *global;
#define remove_trailing_spaces (*(char * (*)(char *))global[REMOVE_TRAILING_SPACES])
#define expand_twiddle (*(char * (*)(char *))global[EXPAND_TWIDDLE])
#define check_nickname (*(char * (*)(char *))global[CHECK_NICKNAME])
#define sindex (*(char * (*)(char *, char *))global[SINDEX])
#define rsindex (*(char * (*)(char *, char *, char *, int))global[RSINDEX])
#define sindex (*(char * (*)(const char *, const char *))global[SINDEX])
#define rsindex (*(char * (*)(const char *, const char *, const char *, int))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])

View File

@@ -1066,53 +1066,47 @@ char *BX_check_nickname (char *nick)
return *nick ? nick : NULL;
}
/*
* sindex: much like index(), but it looks for a match of any character in
* the group, and returns that position. If the first character is a ^, then
* this will match the first occurrence not in that group.
/* sindex()
*
* Returns a pointer to the first matching character in a string, or NULL if
* there are no matching characters.
*
* 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_sindex (register char *string, char *group)
char *BX_sindex(const char *string, const char *group)
{
char *ptr;
if (!string || !group)
return (char *) NULL;
return NULL;
if (*group == '^')
{
group++;
for (; *string; string++)
{
for (ptr = group; *ptr; ptr++)
{
if (*ptr == *string)
break;
}
if (*ptr == '\0')
return string;
}
string += strspn(string, group + 1);
}
else
{
for (; *string; string++)
{
for (ptr = group; *ptr; ptr++)
{
if (*ptr == *string)
return string;
}
}
}
return (char *) NULL;
string += strcspn(string, group);
}
/*
* rsindex: much like rindex(), but it looks for a match of any character in
* the group, and returns that position. If the first character is a ^, then
* this will match the first occurrence not in that group.
if (*string)
return (char *)string;
else
return NULL;
}
/* 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.
*
* 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 (register char *string, char *start, char *group, int howmany)
char *BX_rsindex(const char *string, const char *start, const char *group, int howmany)
{
register char *ptr;
const char *ptr;
if (howmany && string && start && group && start <= string)
{
@@ -1124,7 +1118,7 @@ char *BX_rsindex (register char *string, char *start, char *group, int howmany)
if (!strchr(group, *ptr))
{
if (--howmany == 0)
return ptr;
return (char *)ptr;
}
}
}
@@ -1135,7 +1129,7 @@ char *BX_rsindex (register char *string, char *start, char *group, int howmany)
if (strchr(group, *ptr))
{
if (--howmany == 0)
return ptr;
return (char *)ptr;
}
}
}