P
pete
pete said:*(unsigned char *)byte, is the what "interpreted as" means.
*(unsigned char *)&byte
pete said:*(unsigned char *)byte, is the what "interpreted as" means.
(in article said:[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.
Randy Howard said:Ben Pfaff wrote
(in article <[email protected]>):[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?
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 said: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.
Jordan said:...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?
(in article said:Randy Howard said:Ben Pfaff wrote
(in article <[email protected]>):[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'm currently evaluating two implementations of a case insensitive
string comparison function to replace the non-ANSI stricmp(). Both of
the implementations below seem to work fine but I'm wondering if one is
better than the other or if there is some sort of hybrid of the two
that would be superior.
IMPLEMENTATION 1:
#ifndef HAVE_STRCASECMP
#define ccmp(a,b) ((a) == (b) ? 0 : ((a) > (b) ? 1 : -1))
int strcasecmp(unsigned char *s1, unsigned char *s2)
Randy Howard said:Chris McDonald wrote
(in article <[email protected]>):
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.
William Krick said:One final revision. 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...
int str_ccmp( const char *s1, const char *s2 )
{
int c1, c2;
for(;
{
c1 = tolower( (unsigned char) *s1++ );
c2 = tolower( (unsigned char) *s2++ );
if (c1 == 0 || c1 != c2)
return c1 == c2 ? 0 : c1 > c2 ? 1 : -1;
}
}
Jordan said:I meant "If 'f' > 'b', then strcmp("foo","bar") is required to return a
positive number." And I thought that was perfectly clear.
I'm currently evaluating two implementations of a case insensitive
string comparison function to replace the non-ANSI stricmp(). Both of
the implementations below seem to work fine but I'm wondering if one is
better than the other or if there is some sort of hybrid of the two
that would be superior.
IMPLEMENTATION 1:
#ifndef HAVE_STRCASECMP
#define ccmp(a,b) ((a) == (b) ? 0 : ((a) > (b) ? 1 : -1))
int strcasecmp(unsigned char *s1, unsigned char *s2)
[snip]
You have quite a few useful replies, but I notice one thing nobody has
pointed out.
Since you seem to be concerned with "ANSI-ness", you should not be
defining external or even file scope identifiers that start with "str"
followed by a lower case letter, as they are reserved for the
implementation.
Consider 'str_casecmp'.
Skarmander said:I'd prefer
assert(s1 && s2);
Or a (documented) redefinition of the semantics (e.g., treat 0 as "").
You could use WHATEVERYOUWANT if you document either it or the fact that
passing null pointers will yield an indeterminate value. Don't keep it
under the hood, in any case.
Peter said:Some previous queries by myself on the issue...
Tim said:If the tests are written differently, the return values
might be somewhat clearer:
int
str_ccmp( const char *s1, const char *s2 ){
int c1, c2;
for( ; ; s1++, s2++ ){
c1 = tolower( (unsigned char) *s1 );
c2 = tolower( (unsigned char) *s2 );
if( c1 > c2 ) return 1;
if( c1 < c2 ) return -1;
if( c1 == 0 ) return 0;
}
}
Incidentally, please use spaces rather than tabs when
posting to this newsgroup.
Tim said:If the tests are written differently, the return values
might be somewhat clearer:
int
str_ccmp( const char *s1, const char *s2 ){
int c1, c2;
for( ; ; s1++, s2++ ){
c1 = tolower( (unsigned char) *s1 );
c2 = tolower( (unsigned char) *s2 );
if( c1 > c2 ) return 1;
if( c1 < c2 ) return -1;
if( c1 == 0 ) return 0;
}
}
Incidentally, please use spaces rather than tabs when
posting to this newsgroup.
Actually, when you cleaned up the return conditions, you left out some
of the conditions and broke the code. I've added them back in but I'm
sure it could still be simplified...
int str_ccmp( const char *s1, const char *s2 ){
int c1, c2;
for( ; ; s1++, s2++ ){
c1 = tolower( (unsigned char) *s1 );
c2 = tolower( (unsigned char) *s2 );
if (c1 != c2 || c1 == 0) {
if( c1 > c2 ) return 1;
if( c1 < c2 ) return -1;
if( c1 == 0 ) return 0;
}
}
}
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.