Messages in this thread | | | Date | Mon, 31 Aug 1998 23:50:57 +0200 | From | Hans Eric <> | Subject | Re: 2.1.120pre2: Bug in strnicmp() noted in 2.1.119pre1 is still there! |
| |
Linus Torvalds wrote:
> Ok, shoot holes in this one instead. > > Linus > > ----- > static int strnicmp(const char *s1, const char *s2, int len) > { > /* Yes, Virginia, it had better be unsigned */ > unsigned char c1, c2; > > while (len) { > c1 = *s1; c2 = *s2; > s1++; s2++; > if (!c1) > goto end_of_string1; > if (!c2) > goto end_of_string2; > if (c1 != c2) { > c1 = tolower(c1); > c2 = tolower(c2); > if (c1 != c2) > goto different; > } > len--; > } > return 0; > > end_of_string1: > return c2 ? -1 : 0; > > end_of_string2: > return 1; > > different: > return c1 < c2 ? -1 : 1; > }
Cool, creative use of gotos keeps nested ifs to a minimum. Since this seems to be hand optimized code you might as well move s1++;s2++ after the if statements to save some cycles. That is if the compiler does'nt catch it.
Well, lets do a fast test... gcc -O1... gcc -O2... gcc-O3... gcc -O4. Yup gcc -O4 catches this and generates the same code regardless of where I put the s1++;s2++. egcs cathes this at -O3. I must be tired why do I bother checking all this...
Hans Eric (hes@xinit.se)
- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.altern.org/andrebalsa/doc/lkml-faq.html
| |