- 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 love reinventing the wheel!
Admin
That's just splendid. At least s/he breaks at the first occurrence of an invalid char.
Oh and FIRST!!!!!!111111
Admin
I swear I had the first post. DAMMIT
Admin
Cool. ".-." is a number.
Admin
woo hoo, ellipses are numbers...
Admin
According to the msdn link:
Admin
so is 8-0
it's almost an emoticon
Admin
...and it should return
true
falue
NaN
Admin
Equal to 0, right?
This is not totally unselective code though. According to it, +5 is not a number.
Sincerely,
Gene Wirchenko
Admin
Clearly, you are all missing the utter brilliance of this function.
Everyone with more than a little experience with regular expressions knows that in some cases, they need to be constructed carefully to avoid becoming resource hungry. Here, the author has cleverly limited the amount of damage this regexp can do by simplifying it to a single list of characters, then iterating over the string containing the number.
The fact that he has enumerated each digit individually rather than using a character reference like \d highlights his dedication to self-documenting code, and ensures that if those godless heathen who support commie pinko trash like the Metric system ever succeed in adding new digits to our numbering system. It's poetry.
Admin
If that's true, then the only WTF I see here is that David Conrad thinks he's smarter than his colleague. But, I'm pretty much a n00b. All I see here is a straightforward, well commented function that will validate a float. Am I wrong?
Admin
Almost?
I think that's the DailyWTF emoticon mascot ...
Admin
My apologies. The above should have read:
Admin
Yes. It does not validate a float, as many other comments have pointed out. ".-.---." is accepted, but "+5.3" is rejected, for example.
Admin
Regrettably, you are wrong. The code in today'sWTF will also interpret Morse Code as a valid Float.
testFloat("...---...") // Returns true
Admin
Especially when you reinvent it square...
Admin
Unless I'm mistaken (happened once), wouldn't ";;;;;" be a number too?
Admin
Silliest ascii art that can be validated by this code contest!
I'll start with a bridge!
.1----1.
Admin
testFloat("...---...") should throw a UserHasFallenAndCantGetUpException.
Admin
Add more digits to our system, like when A-F and x were added (like 0xAD, for example ;) ).
Admin
No. It should return...
True False FileNotFound
Admin
Woo Hoo! I'm first!
Admin
I knew my Social Security number was a number.
Admin
As a bonus, your Morse message would also reflect the truth... :)
.-- - ..-. !!!
Admin
Guh? Are you referring to the base 10 metric system? What about using base 12 for counting inches in a foot, huh?
And then, you are forgetting those crackpots who have added the letters A through F.
Admin
While we are reinventing the wheel, I thought I would share this little function that was written for a common C library. It's been a while, so forgive any syntax errors. But I'm sure you get the point.
void setNull(char *variable)
{
variable = null;
}
I always love coming across home grown functions like checkDate or checkNumeric that were written to somehow make the existing isDate() and isNumeric() functions irrelavant and obsolete.
Admin
I don't use the language in question, but WTF? What kind of regular expression is that?
Admin
I like the way it allows several smilies as numbers.
testNum(";-0")
testNum("8-0")
Admin
I guess the first version of the function was written straight-forward (with all sorts of varieties of loops and the like). E.g.
validChars = "0123456789.-";
...
isValid = (validChars.indexOf(theChar)>-1);
Then someone more clever came along, saw it and said: "Stupid! You can do that much better with regular expressions!" Said that and went along. And poor little stupid programmer read the first 10 lines of the first chapter of a book about regular expressions and used that knowledge to adopt his function to use regular expressions.
Admin
Not only did the programmer fail to use a standard library routine, the programmer was clearly ignorant of what regular expressions could do.
This would've caught many more invalid inputs and been much faster (depending on the exact semantics of search, maybe there's a match function that should be used instead.:
I can see a reason to write your own validating function instead of relying on the system float parser as you may not want people appending random stuff to the values that gets ignore, or allow them to specify exponents or whatever. But, yeah, it's pretty obvious what's wrong with this.
Admin
It's either perl shoehorned into a C syntax, or more likely javascript.
I actually learned something today, which is that JS uses perl-syntax rather than c-strings to hold regexps. Cool. =D
I'm surprised the unescaped - doesn't lead to a parse error, since it has a different meaning in a character set. And the blatant misunderstandings of regex sets via the semicolons is <3
I think this guy would have been much more comfortable looping through an array of characters for every digit in the input instead. It's more his style.
Now you have to wonder, when "-.-" gets through and shelved into the database, what does he do when the error is flagged? Convert the field to varchar2! And when the parser on the other end dies, because it tries to directly convert to float, the obvious answer is to write a new function to replace microsoft's buggy and unreliable one. Awesome. =D
Admin
I believe a good version would be:
function testNum(strNum) {
return !isNaN(strNum)
}
Admin
Even better, creating the "charArray" is pointless. JS lets you access chars in a string by index directly.
Admin
It is the same as [0123456789;.-] which matches any character between the brackets. Having the ; in there multiple times is redundant but harmless.
;--0 so is a valid number too.
Admin
It has to be Javascript.
If you want a - character inside a character set, it must be either the first or the last character. Putting it anywhere in the middle creates a range of characters (e.g. [a-z-] is any lowercase letter OR a dash). All regexp syntaxes do this.
Admin
Actually, what is the point? This function doesn't do anything! Remember that C functions are pass-by-value.
Admin
^-?\d+.\d+$
How hard was that?
Admin
Too bad you didn't escape the period...
Admin
Hey dont knock it! It validates IP addresses too (with the sort of quality it validates numbers) ;)
Admin
You mean:
^-?\d+(.\d+)?$
Admin
I get it, he forgot to consider exponential notation...
Admin
I've seen this one in a pile of code before.
Some simple WTFs crop up way too often.
Admin
Yes, that is correct that C is strictly pass-by-value; however he did ask for forgiveness on syntax errors.
He might have meant...
void setNull(char **variable)
{
*variable = null;
}
Which when called as....
char test = 't';
setNull(&test);
It should put a null into the test variable.
I may be totally off-base on my function since I have not worked in C-land in many years.
Admin
Actually, I believe the correct regex would be: /^[+-]?(?:.?\d+|\d+.?|\d+.\d+)(?:[eE][+-]?\d+)?$/
Which rather nicely illustrates why you should just use the built-in methods.
Admin
I believe most C compilers will error out on that code.
&test
is of typechar *
, notchar **
.The above would work. :-)
Admin
So, what do you do when you don't want to allow exponential notation and force a leading 0 before the . and not allow a leading +? There are reasons to not allow complicated floats in a field. I don't think writing your own float validator is necessarily the wrong thing, but this one is a very broken float validator.
Admin
My thoughts exactly. it should be "void setNull(char &variable)" to pass by reference.
I shut (shat? wtf, english preparser is broken) my hole earlier because I thought I was too drunk to remember my primary language.
.
..
...
....
Althou it is still mighty possible that I am too drunk to remember my primary language. Whatever that might be.
Which reminds me, was there a convention in Visual C++ 6< to use NULL as an object for null? (just a matter of case)
Admin
And now, after seeing the other comments about how to handle a nulling off a parameter pointer (pointer parameter?), I have to withdraw to my cave and produce 2.000 lines of test code to illustrate how the different cases of c/c++ pointer passing cases work. I'll be back in three weeks after I have specified this project. At that point I will file for Request For Comments to start the actual implementation of the trial.
PS. Where's the fucking preview button?! This site is a big WTF in itself.
Admin
I finally understood why this code is bad: multiple return statements.
Never break the flow. A real WTF ;)
Admin
<font size="5">L</font>ooks more like Elton John to me. Big horn-rimmed glasses and a gaping maw.