Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > comparing two strcasecmp (stricmp) implementations

Reply
Thread Tools

comparing two strcasecmp (stricmp) implementations

 
 
pete
Guest
Posts: n/a
 
      11-10-2005
pete wrote:
>
> *(unsigned char *)byte, is the what "interpreted as" means.


*(unsigned char *)&byte

> The standard isn't shy about using the word "converted".


--
pete
 
Reply With Quote
 
 
 
 
Randy Howard
Guest
Posts: n/a
 
      11-10-2005
Ben Pfaff wrote
(in article <(E-Mail Removed)>):

> "William Krick" <(E-Mail Removed)> writes:
>
> [case-insensitive strcmp-like function]
>
>> I've modified the return statement so that it
>> returns -1 / 0 / 1 to bring it in line with the behaviour of other
>> similar functions...

>
> strcmp() isn't specified so strictly. You can't depend on it
> returning exactly -1 or 1. Here's what the standard says:
>
> 3 The strcmp function returns an integer greater than, equal to,
> or less than zero, accordingly as the string pointed to by
> s1 is greater than, equal to, or less than the string
> pointed to by s2.


And what 'harm' does it do, relative to the standard, to
restrict it in such a fashion? Are there programs that depend
upon larger and smaller values being returned?


--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw





 
Reply With Quote
 
 
 
 
Chris McDonald
Guest
Posts: n/a
 
      11-11-2005
Randy Howard <(E-Mail Removed)> writes:

>Ben Pfaff wrote
>(in article <(E-Mail Removed)>):


>> "William Krick" <(E-Mail Removed)> writes:
>>
>> [case-insensitive strcmp-like function]
>>
>>> I've modified the return statement so that it
>>> returns -1 / 0 / 1 to bring it in line with the behaviour of other
>>> similar functions...

>>
>> strcmp() isn't specified so strictly. You can't depend on it
>> returning exactly -1 or 1. Here's what the standard says:
>>
>> 3 The strcmp function returns an integer greater than, equal to,
>> or less than zero, accordingly as the string pointed to by
>> s1 is greater than, equal to, or less than the string
>> pointed to by s2.


>And what 'harm' does it do, relative to the standard, to
>restrict it in such a fashion? Are there programs that depend
>upon larger and smaller values being returned?



Surely it's not any standard's responsibility to protect programs which
make incorrect assumptions about their working within that standard's
environment?

--
Chris.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      11-11-2005
Jordan Abel wrote:

> strcmp("foo","bar") returns 4 on my system, and probably yours. Only a
> positive value is required by the standard in that case.


No.

It might be required according to every character set
that you ever heard of,
but that result isn't required by the standard.

The values of 'f' and 'b' are implementation defined,
and there's nothing in the standard requiring either
one to be greater.

--
pete
 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      11-11-2005
On 2005-11-11, pete <(E-Mail Removed)> wrote:
> Jordan Abel wrote:
>
>> strcmp("foo","bar") returns 4 on my system, and probably yours. Only a
>> positive value is required by the standard in that case.

>
> No.
>
> It might be required according to every character set
> that you ever heard of,
> but that result isn't required by the standard.
>
> The values of 'f' and 'b' are implementation defined,
> and there's nothing in the standard requiring either
> one to be greater.


....that's nitpicking. i meant that nothing is required of the result but
the sign in general.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-11-2005
pete wrote:

> Eric Sosman wrote:
>
>>pete wrote On 11/10/05 15:30,:

>
>
>>> of characters (both interpreted as unsigned char)

>
>
>> The Standard is not entirely clear about what should
>>be done with negative `char' values. We know they need to
>>be made non-negative to become `unsigned char' values, but
>>is this to be done by conversion (my assumption) or by
>>reinterpretation (yours)?

>
>
> I'm seeing "interpreted as unsigned char"
> in the above quote from the standard.
> *(unsigned char *)byte, is the what "interpreted as" means.
> The standard isn't shy about using the word "converted".


I do not subscribe to the idea that the Standard's
description of a Standard library function should govern
the design of non-Standard functions.

Besides, you haven't addressed the issue of the
argument values for <ctype.h> functions. Shy, schmy.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      11-11-2005
Eric Sosman wrote:
>
> pete wrote:
>
> > Eric Sosman wrote:
> >
> >>pete wrote On 11/10/05 15:30,:

> >
> >
> >>> of characters (both interpreted as unsigned char)

> >
> >
> >> The Standard is not entirely clear about what should
> >>be done with negative `char' values.


> I do not subscribe to the idea that the Standard's
> description of a Standard library function should govern
> the design of non-Standard functions.


Then what difference does it make to you
whether or not the Standard is entirely clear
about what should be done with negative `char' values?

> Besides, you haven't addressed the issue of the
> argument values for <ctype.h> functions. Shy, schmy.


It's all here:

*(const unsigned char *)s1 is the value of the byte.
That value is the argument to toupper.
I don't see why anything else would be better.

int str_ccmp(const char *s1, const char *s2)
{
const unsigned char *p1 = (const unsigned char *)s1;
const unsigned char *p2 = (const unsigned char *)s2;

while (toupper(*p1) == toupper(*p2)) {
if (*p1 == '\0') {
return 0;
}
++p1;
++p2;
}
return toupper(*p2) > toupper(*p1) ? -1 : 1;
}


--
pete
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      11-11-2005
Jordan Abel wrote:
>
> On 2005-11-11, pete <(E-Mail Removed)> wrote:
> > Jordan Abel wrote:
> >
> >> strcmp("foo","bar") returns 4 on my system,
> >> and probably yours. Only a
> >> positive value is required by the standard in that case.

> >
> > No.
> >
> > It might be required according to every character set
> > that you ever heard of,
> > but that result isn't required by the standard.
> >
> > The values of 'f' and 'b' are implementation defined,
> > and there's nothing in the standard requiring either
> > one to be greater.

>
> ...that's nitpicking.
> i meant that nothing is required of the result but
> the sign in general.


strcmp("foo","bar") is not required by
the standard to return a positive value.

You mean only that strcmp("foo","bar") should not return zero?

--
pete
 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      11-11-2005
On 2005-11-11, pete <(E-Mail Removed)> wrote:
> Jordan Abel wrote:
>>
>> On 2005-11-11, pete <(E-Mail Removed)> wrote:
>> > Jordan Abel wrote:
>> >
>> >> strcmp("foo","bar") returns 4 on my system,
>> >> and probably yours. Only a
>> >> positive value is required by the standard in that case.
>> >
>> > No.
>> >
>> > It might be required according to every character set
>> > that you ever heard of,
>> > but that result isn't required by the standard.
>> >
>> > The values of 'f' and 'b' are implementation defined,
>> > and there's nothing in the standard requiring either
>> > one to be greater.

>>
>> ...that's nitpicking.
>> i meant that nothing is required of the result but
>> the sign in general.

>
> strcmp("foo","bar") is not required by
> the standard to return a positive value.
>
> You mean only that strcmp("foo","bar") should not return zero?


I meant "If 'f' > 'b', then strcmp("foo","bar") is required to return a
positive number." And I thought that was perfectly clear.
 
Reply With Quote
 
Randy Howard
Guest
Posts: n/a
 
      11-11-2005
Chris McDonald wrote
(in article <dl0ntm$k9d$(E-Mail Removed)>):

> Randy Howard <(E-Mail Removed)> writes:
>
>> Ben Pfaff wrote
>> (in article <(E-Mail Removed)>):

>
>>> "William Krick" <(E-Mail Removed)> writes:
>>>
>>> [case-insensitive strcmp-like function]
>>>
>>>> I've modified the return statement so that it
>>>> returns -1 / 0 / 1 to bring it in line with the behaviour of other
>>>> similar functions...
>>>
>>> strcmp() isn't specified so strictly. You can't depend on it
>>> returning exactly -1 or 1. Here's what the standard says:
>>>
>>> 3 The strcmp function returns an integer greater than, equal to,
>>> or less than zero, accordingly as the string pointed to by
>>> s1 is greater than, equal to, or less than the string
>>> pointed to by s2.

>
>> And what 'harm' does it do, relative to the standard, to
>> restrict it in such a fashion? Are there programs that depend
>> upon larger and smaller values being returned?

>
>
> Surely it's not any standard's responsibility to protect programs which
> make incorrect assumptions about their working within that standard's
> environment?


I didn't say or imply that it was. As such, I don't understand
your question. All I said was that returning only three values
instead of a much larger range didn't seem like a problem from
here.


--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Two different `architecture' implementations? Merciadri Luca VHDL 4 11-02-2010 08:21 PM
stricmp() versus strcasecmp( ahso C++ 4 11-25-2009 03:29 PM
Two implementations of simple math equation yield different results Avi C++ 6 05-14-2008 08:04 AM
How to compare two SOAP Envelope or two Document or two XML files GenxLogic Java 3 12-06-2006 08:41 PM
Comparing two textboxes Steffen Loringer ASP .Net 1 04-26-2004 09:55 AM



Advertisments