• asdf (unregistered)

    I love reinventing the wheel!

  • (cs)

    That's just splendid. At least s/he breaks at the first occurrence of an invalid char.

    Oh and FIRST!!!!!!111111

  • (cs) in reply to rogthefrog
    rogthefrog:
    That's just splendid. At least s/he breaks at the first occurrence of an invalid char.

    Oh and FIRST!!!!!!111111


    I swear I had the first post. DAMMIT
  • Mark (unregistered)

    Cool.  ".-." is a number.

  • (cs)

    woo hoo, ellipses are numbers... 

  • Colin (unregistered)

    According to the msdn link:

    parseFloat("1.2abc")   // Returns 1.2.


    which is not what the code above does. The above code would return false when it sees the 'a'....
  • jkl; (unregistered) in reply to Mark
    Anonymous:
    Cool.  ".-." is a number.


    so is 8-0

    it's almost an emoticon
  • (cs)

    ...and it should return

    true

    falue

    NaN

  • (cs) in reply to Mark
    Anonymous:
    Cool.  ".-." is a number.


    Equal to 0, right?

    This is not totally unselective code though.  According to it, +5 is not a number.

    Sincerely,


    Gene Wirchenko

  • (cs)

    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. 

  • Zen Bastard (unregistered) in reply to Colin
    Anonymous:
    According to the msdn link:

    parseFloat("1.2abc")   // Returns 1.2.


    which is not what the code above does. The above code would return false when it sees the 'a'....


    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?
  • (cs) in reply to jkl;

    Anonymous:
    Anonymous:
    Cool.  ".-." is a number.


    so is 8-0

    it's almost an emoticon

    Almost?

    I think that's the DailyWTF emoticon mascot ...

  • (cs) in reply to sammybaby

    My apologies. The above should have read:

    sammybaby:

    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, his code shall remain unsullied by their taint. It's poetry.

     

  • boa13 (unregistered) in reply to Zen Bastard
    Anonymous:
    All I see here is a straightforward, well commented function that will validate a float. Am I wrong?


    Yes. It does not validate a float, as many other comments have pointed out. ".-.---." is accepted, but "+5.3" is rejected, for example.
  • (cs) in reply to Zen Bastard
    Anonymous:
    Anonymous:
    According to the msdn link:

    parseFloat("1.2abc")   // Returns 1.2.


    which is not what the code above does. The above code would return false when it sees the 'a'....



    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?

    Regrettably, you are wrong. The code in today'sWTF will also interpret Morse Code as a valid Float.

    testFloat("...---...")    // Returns true

  • (cs) in reply to asdf
    Anonymous:
    I love reinventing the wheel!

    Especially when you reinvent it square...
  • (cs) in reply to rbriem

    Unless I'm mistaken (happened once), wouldn't ";;;;;" be a number too?

  • (cs) in reply to sammybaby

    Silliest ascii art that can be validated by this code contest!

    I'll start with a bridge!

    .1----1.

  • clyde (unregistered) in reply to sammybaby

    testFloat("...---...") should throw a UserHasFallenAndCantGetUpException.

  • Nicholas Shanks (unregistered) in reply to sammybaby

    Add more digits to our system, like when A-F and x were added (like 0xAD, for example ;) ).

  • (cs) in reply to BlackTigerX
    BlackTigerX:

    ...and it should return

    true

    falue

    NaN

    No. It should return...

    True False FileNotFound

  • (cs)

    Woo Hoo! I'm first!

  • Disgruntled DBA (unregistered)

    I knew my Social Security number was a number.

  • (cs) in reply to sammybaby
    sammybaby:
    Anonymous:
    Anonymous:
    According to the msdn link:

    parseFloat("1.2abc")   // Returns 1.2.


    which is not what the code above does. The above code would return false when it sees the 'a'....



    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?

    Regrettably, you are wrong. The code in today'sWTF will also interpret Morse Code as a valid Float.

    testFloat("...---...")    // Returns true

    As a bonus, your Morse message would also reflect the truth... :)

    .-- - ..-. !!!

  • (cs) in reply to sammybaby
    sammybaby:

    ... 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.



    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.

  • (cs)

    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.

  • (cs)
    Alex Papadimoulis:
      <font color="#000099">var</font> theRegExp = <font color="#666666">/[0;1;2;3;4;5;6;7;8;9;.;-]/</font>
    


    I don't use the language in question, but WTF? What kind of regular expression is that?
  • Dave (unregistered) in reply to Reun

    I like the way it allows several smilies as numbers.

    testNum(";-0")

    testNum("8-0")

     

  • (cs)

    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.

  • (cs)

    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.:

    function testNum(strNum)
    {
       var the theRegExp = /^(-)?[0-9]+(\.[0-9]+)?$/;
       return strNum.search(theRegExp) != -1;
    }

    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.

  • (cs) in reply to Reun
    Reun:
    Alex Papadimoulis:
      <font color="#000099">var</font> theRegExp = <font color="#666666">/[0;1;2;3;4;5;6;7;8;9;.;-]/</font>
    


    I don't use the language in question, but WTF? What kind of regular expression is that?

    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
  • Jeremy (unregistered) in reply to Colin
    Anonymous:
    According to the msdn link:

    parseFloat("1.2abc")   // Returns 1.2.


    which is not what the code above does. The above code would return false when it sees the 'a'....


    I believe a good version would be:

    function testNum(strNum) {
        return !isNaN(strNum)
    }
  • (cs)

    Even better, creating the "charArray" is pointless. JS lets you access chars in a string by index directly.

  • (cs) in reply to Reun
    Reun:
    Alex Papadimoulis:
      <font color="#000099">var</font> theRegExp = <font color="#666666">/[0;1;2;3;4;5;6;7;8;9;.;-]/</font>
    


    I don't use the language in question, but WTF? What kind of regular expression is that?

    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.

  • (cs) in reply to foxyshadis
    foxyshadis:

    It's either perl shoehorned into a C syntax, or more likely javascript.

    It has to be Javascript.

    foxyshadis:

    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

    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.

  • (cs) in reply to richleick
    richleick:
    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;
    }

    Actually, what is the point?  This function doesn't do anything!  Remember that C functions are pass-by-value.

  • anon (unregistered)

    ^-?\d+.\d+$
    How hard was that?

  • Anon (unregistered) in reply to anon

    Too bad you didn't escape the period...

  • phx (unregistered) in reply to Anon

    Hey dont knock it! It validates IP addresses too (with the sort of quality it validates numbers) ;)

  • (cs) in reply to anon

    You mean:
    ^-?\d+(.\d+)?$

  • (cs)

    I get it, he forgot to consider exponential notation...

  • (cs) in reply to loneprogrammer
    loneprogrammer:
    richleick:
    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;
    }

    Actually, what is the point?  This function doesn't do anything!  Remember that C functions are pass-by-value.


    I've seen this one in a pile of code before.
    Some simple WTFs crop up way too often.
  • (cs) in reply to loneprogrammer
    loneprogrammer:
    richleick:
    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;
    }

    Actually, what is the point?  This function doesn't do anything!  Remember that C functions are pass-by-value.



    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.

  • Erin (unregistered) in reply to don

    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.

  • (cs) in reply to GalacticCmdr
    GalacticCmdr:
    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.

    I believe most C compilers will error out on that code. &test is of type char *, not char **.

    char *test = "t";
    setNull(&test);

    The above would work. :-)

  • (cs) in reply to Erin
    Anonymous:
    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.

    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.

  • (cs) in reply to loneprogrammer
    loneprogrammer:
    richleick:
    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;
    }


    Actually, what is the point?  This function doesn't do anything!  Remember that C functions are pass-by-value.



    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)

  • (cs) in reply to Fizzl
    Fizzl:
    loneprogrammer:
    richleick:
    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;
    }


    Actually, what is the point?  This function doesn't do anything!  Remember that C functions are pass-by-value.



    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)


    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.

  • (cs)

    I finally understood why this code is bad: multiple return statements.
    Never break the flow. A real WTF ;)

  • (cs) in reply to rbriem
    rbriem:

    Anonymous:

    so is 8-0

    it's almost an emoticon

    Almost?

    I think that's the DailyWTF emoticon mascot ...


    <font size="5">L</font>ooks more like Elton John to me.  Big horn-rimmed glasses and a gaping maw.

Leave a comment on “Irregular Expressions”

Log In or post as a guest

Replying to comment #:

« Return to Article