This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
Re: strncasecmp bugfix and improvements
- From: Wizards' Guild <wizardsguild at earthlink dot net>
- To: newlib at sourceware dot org
- Date: Mon, 6 Apr 2009 21:14:29 -0400 (EDT)
- Subject: Re: strncasecmp bugfix and improvements
- Reply-to: Wizards' Guild <wizardsguild at earthlink dot net>
Thanks! I hadn't expected such a quick response! :)
Here are the other routines changed similarly. The first gets a similar performance improvement; the others are changed only to avoid passing negative values to toupper/tolower. I have _ansi'fied this time.
The docs for both strcasecmp and strncasecmp say "after both are converted to uppercase" - this is wrong, because both are converted to lowercase.
int
_DEFUN (strcasecmp, (s1, s2, n),
_CONST char *s1 _AND
_CONST char *s2)
{
_CONST unsigned char *ucs1 = (_CONST unsigned char *) s1;
_CONST unsigned char *ucs2 = (_CONST unsigned char *) s2;
int d = 0;
for ( ; ; )
{
_CONST int c1 = tolower(*ucs1++);
_CONST int c2 = tolower(*ucs2++);
if (((d = c1 - c2) != 0) || (c2 == '\0'))
break;
}
return d;
}
char *
_DEFUN (strlwr, (s),
char *s)
{
unsigned char *ucs = (unsigned char *) s;
for ( ; *ucs != '\0'; ucs++)
{
*ucs = tolower(*ucs);
}
return s;
}
char *
_DEFUN (strupr, (s),
char *s)
{
unsigned char *ucs = (unsigned char *) s;
for ( ; *ucs != '\0'; ucs++)
{
*ucs = toupper(*ucs);
}
return s;
}
---
Mike Burgess
Avanex Corporation
607-562-7169