Re: CompareWithoutRegardToCase
From: Buster (none_at_none.com)
Date: 09/27/04
- Next message: Rolf Magnus: "Re: Mimic virtual functions with templates"
- Previous message: Rob Williscroft: "Re: anything wrong with conversion operator?"
- Next in thread: Buster: "Re: CompareWithoutRegardToCase"
- Reply: Buster: "Re: CompareWithoutRegardToCase"
- Reply: JKop: "Re: CompareWithoutRegardToCase"
- Maybe reply: Siemel Naran: "Re: CompareWithoutRegardToCase"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 27 Sep 2004 16:51:10 +0100
JKop wrote:
> Comments, Questions and Suggestions please!
>
>
> Returns true if strings are identical, returns false if strings are not
> identical. (without regard to case).
>
>
> bool CompareWithoutRegardToCase(const char* x, const char* y)
> {
> //Undefined Behaviour if supplied with null pointers ;-D
>
> if ( !*x || !*y ) //valid pointer to null string
> {
> if ( !*x && !*y ) return true; //2 null strings are equal.
>
> return false;
> }
>
> do
> {
> if ( tolower( static_cast<unsigned char>(*x) ) == tolower(
> static_cast<unsigned char>(*y) ) ) continue;
> else
> {
> return false;
> }
> }
> while ( (++x,++y) ,*x );
>
> return !*y;
> }
What happens if the null-terminated string to which y points is longer
than the one pointed to by x?
bool CompareWithoutRegardToCase (char x, char y)
{
return std::tolower (static_cast <int> (x))
== std::tolower (static_cast <int> (y);
}
bool CompareWithoutRegardToCase (const char * x, const char * y)
{
// Undefined behaviour if supplied with null pointers
while ((* x) && (* y) && CompareWithoutRegardToCase (* x, * y))
{
++ x;
++ y;
}
return ! ((* x) || (* y));
}
-- Regards, Buster
- Next message: Rolf Magnus: "Re: Mimic virtual functions with templates"
- Previous message: Rob Williscroft: "Re: anything wrong with conversion operator?"
- Next in thread: Buster: "Re: CompareWithoutRegardToCase"
- Reply: Buster: "Re: CompareWithoutRegardToCase"
- Reply: JKop: "Re: CompareWithoutRegardToCase"
- Maybe reply: Siemel Naran: "Re: CompareWithoutRegardToCase"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|