- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
I feel vaguely offended, because I submitted exactly this algorithm, in C++, for case-insensitive compares, months ago, and was told it wasn't wacky enough for the WTF. :)
I was in a QA job at the time, and the code was... Astounding. They had a lot of performance problems related to code much like this.
Admin
NAME strcasecmp, strncasecmp - case-insensitive string comparions
SYNOPSIS #include <strings.h> int strcasecmp(const char *s1, const char *s2); int strncasecmp(const char *s1, const char *s2, size_t n);
I think you might guess the behaviour of these standard library functions by the names... And the implementation of the code was a quite nice WTF in itself, guess the coder gets payed by the line.
Admin
I like the way the empty string is not equal to itself
Admin
i for "case insensitive." Points for not actually mangling the string in-place. Too bad there's a library function!
I love how it returns 0 if one of the strings is NULL. Is that standard?
One potential "optimization" this programmer could make, since they've already gone fancy anyway: return -1 if the strings are different lengths. (Of course this probably craps all over i18n; it would return -1 on different representations of the same string that haven't been optimized to the shortest representation in UTF-8.)
Honestly?
Toss it, use library functions. You don't get paid for rewriting libraries.
DISCLAIMER: I work in C#.
Admin
I love the way they check for NULL at the bottom before deleting ... you mean p1 or p2 could be NULL before that ?
Admin
Oh...my...word...
I would murder him; death is not good enough though, so I would have to find something even worse.
Admin
Wow. I mean really, wow.
Memory allocation in a string comparison.
There should be some sort of "computer exam" that people need to take before they're allowed within 100 feet of a computer.
Seriously.
Capcha: DOOM.
Admin
Lovely. I especially like the clever use of return codes:
<0 = one or both of the input strings has length 0, or the left string is less than the right one.
==0 = one or both of the input strings is NULL, or the strings are identical.
I also like the initialize, fill, fill again strategy employed to create the upper-case strings. One just cannot be too sure with memory, and writing three times seems far better than just once. Well done!
And finally, I haven't yet come across a system that doesn't have strmpi in some form or other, but for all we know this could be part of a compiler distribution, i.e. the very code that hides behind normal strcmpi; so I'm not counting that as a WTF.
Admin
Whoops, that should have been '>0'. My mistake.
Admin
In the programmer's defense, at least he doesn't leak memory!
Admin
Why do you write about case sensitive or insensitive? The 'i' means improved.
Admin
Not a WTF to me.
Defensive programming, and maybe poor unoptimiced code. Maybe ugly on the non-standard return values.
Other than that, boring code.
Admin
Err... I don't see a huge problem with checking for null before deleting something, even if it isn't needed. Maybe I missed some sarcasm here or something...
I like how this function returns "the strings are equal" if one string or the other is null. Hilarious.
Admin
Admin
Admin
The best bit about that is that delete on a NULL pointer is safe and just doesn't do anything.
Admin
It's not even intuitively right: if either but not both pointers are NULL, we return 0 for equality. It would perhaps be more sane to treat NULL as an empty string (therefore sorting before any non-empty string). Comparison between NULL and "" can either return 0 (two empty strings are equal), or treat the NULL as being "even shorter than empty" so again sorting earlier.
(Oh, the given code incorrectly compares "foo", "" - returning -1 if either string is non-NULL but empty. This is correct for "", "foo", but should be +1 for the other way round.)
Admin
Admin
Actually came across a 'strnicmp' in our code - turned out this code originally used to need to compile on windows 3.1 using borland c++ compiler, which didnt come with a strncasecmp. The code now (only) is used on FreeBSD/GCC toolchain, so svn rm'ed :)
For those not getting why this is a WTF, heres the gist of our impl for comparision
Theres probably even a more optimal version :o
Admin
Not to mention that if one ot the strings is 0 length, the pointer is not allocated. thus strcmpi ("wtf", ""); would crash on "delete [] p2;" if the NULL check were left out
Admin
Checking whether something is not NULL before deleting isnt called 'defensive programming', its called 'I am a retard and never learnt that deleting a NULL is perfectly sane, legal and allowed'.
Of course you are right, it is ESSENTIAL that we allow for code bloat in strncasecmp.... I'm always finding I have to go rewrite bzero(), add in some bloat......
You should really
Admin
Checking whether something is not NULL before deleting isnt called 'defensive programming', its called 'I am a retard and never learnt that deleting a NULL is perfectly sane, legal and allowed'.
Of course you are right, it is ESSENTIAL that we allow for code bloat in strncasecmp.... I'm always finding I have to go rewrite bzero(), add in some bloat......
Admin
The forum software here is quite ace and not at all susceptible to race conditions, oh no sir.
In C++ it is fine and dandy to delete NULL. Its not defensive to check they are not NULL before deleting, it is retarded. It would be defensive to check they are not NULL before dereferencing them.
Admin
I don't see how an empty string is equal to itself. I do see how a char* pointing to NULL isn't equal to itself, but that really isn't the same.
Thinking on that, though, is it a derivation of "no two NULLs are alike" that not even one NULL is alike to itself? Is that philisophical in some way.
CAPTCHA: stinky
Admin
Admin
Admin
Admin
Don't really remember much of my C, so don't really care to comment on this.
But honestly, Worse Than Failure??
What the phuk? I'm sure everyone else has already said it, don't really feel like looking for the comments.
cmon, change it back. worse than failure is... well.. a failure in and of itself as far as naming conventions go... Professional appeal, is that it?
woopdie doo.
Admin
Sure you do, if your PHB's H is P enough.
Admin
strcmp is intended to be used for sorting strings as well, not just as a binary comparator.
Indeed, POSIX (but not ANSI, looks like) defines stricmp and wcsicmp and ISO C++ has _stricmp, _wcsicmp, _mbsicmp, _stricmp_l, _wcsicmp_l, and _mbsicmp_l.
Well, according to Microsoft anyway.
Admin
best i have for now: int strcmpi(const char *s1, const char *s2) { for(; s1 != NULL && *s1 != '\0' && s2 != NULL && *s2 != '\0'; s1++,s2++) { if (toupper(*s1) < toupper(*s2)) return -1; if (toupper(*s1) > toupper(*s2)) return 1; } return 0; }
Admin
"strcmpi" == "String Comparisons For Idiots"?
Admin
The above fails to check for '\0'. :P
Fair enough, it works correctly for most of the cases, but one will be pulling out hair the one time it does return incorrect values.
Admin
Admin
Yes, yes it is, but this function's ultimate return values aren't useful for sorting, unless your sorting requirements are really really weird.
The most amusing thing here is not just that it duplicates a widely-available standard library function (which they could find a BSD-licensed implementation of and stick into their code if it really wasn't on the system), but that it does it entirely wrongly.
It actually makes me quite glad I'm not a commercial programmer anymore.
Admin
That still returns 0 (equality) if s1 is NULL and s2 is not, or vice versa. It also returns 0 (equality) if you compare "foo" and "fooo". That's fine for strncmpi with n set to 3, but here it's not.
Admin
Admin
It's necessary if you want to use the function for both sorting AND testing for equality. An STL-style less than comparator can't test for equality! Perl sorts are the same way (the <=> and cmp operators).
Admin
I'm currently a in a computer science 2 class in college and the sad thing is that this is exactly the kind of code crap they try to teach us to do. We rewrote dozens of different library functions for the last exam and now half the class doesn't realize that they can just use the ones in the library.
I try to correct the professor on insanely making copies of every string for no apparent reason but he insists its better because what if "somebody" alters your string. Thats right, the evil magic program gnome (no not the X11 gui) is going to jump in my program and delete all my origin strings so I had better copy them and use the copies at the start of every function. Better yet we better check several times in the function just to make sure they're still there and have not been scampered away by some unknown force. Many modern computer science classes are being taught by the people who only learned a modern language (to this guy, even c++ is modern) just to teach it. I understand learning how the string functions work, but do we really have to prompt reinventing the wheel in our universities?
Admin
This is just off the top of my head. I'm sure it's loaded with bugs.
I don't think it's possible to reasonably write this function in just "two lines", though.
Admin
Ha! Are you stupid or was that a joke??? That is an absolutely horrible implementation! There is no need to copy the strings, they can easily be compared, case-insensitive even, in place!
Ever heard of toupper()? if(toupper(*s1) != toupper(*s2)) return *s2-*s1
Admin
If the allocation for p2 throws std::bad_alloc, then he leaks p1, so no, he's still a tard.
CAPTCHA: quake -- had nothing on Wolfenstein 3D.
Admin
I fail to see the problem with learning how to implement basic functions at first...?
The evil magic program gnome is also known as the other threads but making copies willy nilly is not the way to avoid it.
Admin
Wow, that will compare the first two characters.
Admin
Yes, yours will return 0 once two characters in the same position match.
"dvqweF ewrgew" and "ksnw7Fed9 hw" will match in this case.
Also, *s1 == NULL? Why are you comparing aa char to NULL? Do you mean *s1 == '\0' or s1 == NULL?
Admin
At least he didn't do
Admin
Wow, that code is a real WHAT THE FUCK
Admin
Hmm, upon further reflection, maybe I can improve my implementation slightly...
I think this version still works...
Admin
Whoops, you're right, it should have been
instead.I thought it was legal in C to compare characters to NULL i.e. *s1 == '\0' is equivalent to *s1 == NULL, but I'll take your word for it.
Admin
Problems: