Re: [PATCH 1/2] string: simplify stricmp()

From: Linus Torvalds
Date: Fri Jan 22 2010 - 20:47:52 EST




On Fri, 22 Jan 2010, Andrew Morton wrote:
>
> hm, that function seems a little broken.
>
> If it reaches the end of s1 or s2 it will return (c1 - c2). If however
> it detects a difference due to other than end-of-string, it returns
> (tolower(c1) - tolower(c2)).
>
> IOW, perhaps it should be performing tolower() in the
> I-reached-end-of-string case.

No, it doesn't matter. It's like strcmp - it returns "positive, negative
or zero" depending on whether the string compared bigger than, smaller
than or equal.

So "(int)c1 - (int)c2" is just a way to do that. And if the string ends
and c1 or c2 is NUL, there's no point in doing the "tolower()", because it
doesn't matter what the other character is: the sign is all that matters
(or, if they are equally long, and both c1 and c2 are zero, then 0).

> I wonder what strnicmp() is _supposed_ to return..

Same as strncmp.

We could make it always return -1/0/+1 (many libraries do that - then the
return value is guaranteed to also fit in a 'char', not just an 'int'),
but it really shouldn't matter for any correct caller.

Linus
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/