add case-insensitive string compare

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@2381 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Linas Vepstas 2000-05-25 18:30:24 +00:00
parent e94ec22a5d
commit 7566872593
2 changed files with 121 additions and 0 deletions

View File

@ -134,6 +134,102 @@ dcoresize(void)
}
#endif
/********************************************************************\
\********************************************************************/
#define UPPER(c) (((c) >= 'a' && (c) <= 'z') ? (c) + 'A' - 'a' : (c))
/* Search for str2 in first nchar chars of str1, ignore case..
* Return pointer to first match, or null.
*/
char *
strncasestr(const char *str1, const char *str2, size_t len)
{
while (*str1 && len--)
{
if (UPPER(*str1) == UPPER(*str2))
{
if (strncasecmp(str1,str2,strlen(str2)) == 0)
{
return (char *) str1;
}
}
str1++;
}
return NULL;
}
/* Search for str2 in str1, ignore case.
* Return pointer to first match, or null.
*/
char *
strcasestr(const char *str1, const char *str2)
{
size_t len = strlen (str1);
char * retval = strncasestr (str1, str2, len);
return retval;
}
/* Reversed strstr -- search for a needle in the haystack,
* from the far end
*/
char *
rstrstr (const char *haystack, const char * needle)
{
int haylen = strlen (haystack);
int neelen = strlen (needle);
const char * hp = haystack + haylen - 1;
const char * np = needle + neelen - 1;
if ((0 == neelen) || (0 == haylen)) return 0x0;
while (hp >= haystack+neelen) {
if (*hp == *np) {
--np;
if (np < needle) return (char *) hp;
} else {
np = needle + neelen - 1;
}
--hp;
}
return 0x0;
}
/* The strpskip() function locates the first occurrence in the
* string s that does not match any of the characters in "reject".
* This is the opposite of strpbrk()
*/
char *
strpskip (const char * s, const char *reject)
{
size_t i, rlen;
char * retval;
if (!s) return NULL;
if (!reject) return (char *) s;
rlen = sizeof (reject);
retval = (char *) s;
while (*retval) {
int match = 0;
for (i=0; i<rlen; i++) {
if (reject[i] == *retval) {match=1; break; }
}
if (!match) return retval;
retval ++;
}
return NULL;
}
/********************************************************************\
\********************************************************************/

View File

@ -128,6 +128,31 @@ size_t dcoresize();
int safe_strcmp (const char * da, const char * db);
/********************************************************/
/* libc 'enhancements' */
/* Search for needle in haystack, from the back of the haystack.
* This is a reversed version of strstr()
*/
extern char * rstrstr (const char *, const char *);
/* Search for str2 in first nchar chars of str1, ignore case..
* Return pointer to first match, or null.
* These are just like that strnstr and the strstr functions, except
* that they ignore the case.
*/
extern char *strncasestr(const char *str1, const char *str2, size_t len);
extern char *strcasestr(const char *str1, const char *str2);
/* The strpskip() function locates the first occurrence in the
* string s that does not match any of the characters in "reject".
* This is the opposite of strpbrk().
* An alernate (better??) implementation might be
* #define strpskip(s,r) (s+strspn(s,r))
*/
extern char * strpskip (const char * s, const char *reject);
/********************************************************/
/* the ultostr() subroutihne is the inverse of strtoul().
* It accepts a number and prints it in the indicated base.