• Christian (unregistered) in reply to Double-Posting Guy
    Double-Posting Guy:
    At what time does Alex post articles on this so-called "daily" site?
    It's more ironic... Like The Daily Show with Jon Stewart.
  • AnonymousCoward (unregistered)

    Wouldn't empty strings be accepted too ?

  • PedanticCurmudgeon (cs) in reply to Double-Posting Guy
    Double-Posting Guy:
    At what time does Alex post articles on this so-called "daily" site?
    That reminds me...The "Alex is dead" guy hasn't been seen recently. Is he on vacation or something?
  • not frits at all (unregistered)

    Are you saying that the "Alex is dead" guy is dead ?

  • Sarcastic kebab reaper (unregistered)

    Imagine this thing is written in PHP Half of the comments is about how much PHP sucks.

  • PedanticCurmudgeon (cs) in reply to not frits at all
    not frits at all:
    Are you saying that the "Alex is dead" guy is dead ?
    I would, but I don't have any creative stories about how he died. Maybe one of the others can assist?
  • Jay (unregistered)

    Wow, in that short code snippet, and three different ways to represent true vs false! ("Y"/"N", "1"/"0", and the boolean) 'Hate to get bogged down in consistency or anything.

    Still, it could have been worse. At my last job I worked on a system where the original developers used the strings "true" and "false". I came across a bug once where they had written, if (flag.equals("flase")), i.e. they had a typo spelling "false". As of course "false".equals("flase") is false, false looked like true, with unproductive results.

    The same programmer stored all his numbers as strings, too. When he had to do arithmetic, he converted to Double, did the arithmetic, then converted the result back to a string.

  • qbolec (unregistered) in reply to SG_01
    SG_01:
    return destination.All(x => char.IsNumber(x));
    Is C# really so bad with eta-reduction that you can't simply write:
    return destination.All(char.IsNumber);
    
    ?

    Anyway, some of you miss that spaces, hash, stars or hypens could be added to the list of legal characters in a phone number at some point in future, and that even O(n*m^2) algorithm is fair enough for n,m < 20, as long as it is easier to write, test, and maintain than O(n) one. But I really doubt this one is one of those.

  • Jay (unregistered)

    When I was in school, I was taught that writing

    if (c>='0' && c<='9')
    

    is wrong. The book said that this relies on the assumption that the character set represents digits with contiguous values, i.e. that if we arranged the characters in order, we wouldn't have letters or punctuation marks interspersed with digits. (This isn't unthinkable. See, "EBCDIC".) The "right" way to do it, the textbook informed us, was to create an array and loop through the elements comparing to each.

    Interesting argument, but it seems to me that any code you write makes assumptions about how the computer works. Putting values in an array assumes that the elements in the array are stored contiguously, so that if I loop through them none will be skipped. It assumes that comparing a character stored in a scalar to a character stored in an array is a valid operation. Etc etc.

    Okay, I'll give that textbook author this: There's a difference between relying on things documented as part of the language spec, and relying on things that could at least theoretically be different if you recompiled the code on a different platform.

  • David (unregistered) in reply to @Deprecated

    You missed one:

    • Uses substring instead of an indexer
  • Daniel (unregistered) in reply to @Deprecated

    You danced around the most important fail: it's an O(n^2) solution that could easily be solved in linear time. Most of the other WTFs in that code stem from that problem.

  • Daniel (unregistered) in reply to Daniel

    Blegh, forgot that both loops aren't working on the same set, so it's actually O(nm), and since the valid character array is a constant size, it technically simplifies to O(n), but it's still extremely inefficient.

  • AGray (unregistered) in reply to PedanticCurmudgeon
    PedanticCurmudgeon:
    not frits at all:
    Are you saying that the "Alex is dead" guy is dead ?
    I would, but I don't have any creative stories about how he died. Maybe one of the others can assist?

    Toothbrushing accident.

    These things happen...

    CAPTCHA: praesent - It's not a present.

  • Zylon (cs)

    I hereby rechristen this site "The Demiweekly WTF".

  • Friedrice the Great (unregistered) in reply to PedanticCurmudgeon
    PedanticCurmudgeon:
    not frits at all:
    Are you saying that the "Alex is dead" guy is dead ?
    I would, but I don't have any creative stories about how he died. Maybe one of the others can assist?

    Well, turns out that the Alex is Dead guy was really a particular Flame infestation on one of the computers in the DailyWTF office. Then a Flame command system sent the instruction to delete itself ...

  • Enterprise Coder (unregistered) in reply to @Deprecated
    Comment held for moderation.
  • Brian (unregistered) in reply to Mathlete
    private static bool ValidateLinq(string str) { return str.All(x => '0' <= x && x <= '9'); }

    I was surprised that this was quite THAT slow. I know it's not exactly cheap to do the invocation of an anonymous function (which is, iirc, what the lambda gets turned into), but that seems extra slow.

    Maybe using Any() with a negated condition would allow for early exit and speed it up a little?

    Captcha: facilisi. Underwater sealab.

  • Mathlete (unregistered) in reply to Brian
    Comment held for moderation.
  • Coyne (cs)

    This is great! I'm gonna run right out and make a StringyString class!

  • hikari (cs) in reply to Steve The Cynic
    Steve The Cynic:
    RaceProUK:
    emaN ruoY:
    First, as stated, sometimes RegEx really is the answer.
    Assuming the code is .NET, I think Int32.TryParse would be quicker.
    Nice try, but you need to exclude strings beginning with "-" in some way, as TryParse would presumably say that e.g. "-456" is valid...

    Because checking the output of TryParse to see if it's negative is so complicated...

    Either int.TryParse() or long.TryParse() was my first thought too; you could always use a lazy loop over the string and use the char.IsDigit() or char.IsNumber() methods, depending on what you were actually trying to achieve (IsDigit() checks that the character is strictly a radix-10 digit, IsNumber() checks whether the character is classified by Unicode as a number).

  • BenJoe (unregistered)

    Public Function IsValid(value as string) as boolean Return IsNumeric(value) End Function

    There, VB.NET fixed it :P

    PS: I Know The Wrapping Function is kinda obsolete, but this way it gives more Context here ;)

  • LarryDavid (cs)

    Having recently worked with a company providing this kind of service ... it is unsurprising...

  • Pero Perić (unregistered) in reply to Jay
    Jay:
    ... Putting values in an array assumes that the elements in the array are stored contiguously, so that if I loop through them none will be skipped. ...
    If by array you mean true array, T[] thingy, than coder has control over where elements are "added" and only assumption is that array filling code works correctly. On the other hand, if by array you mean List<T> than you are talking about interfaces and contracts. List<T> may store elements in any way it sees fit but methods and indexers that expose those elements must work the docs says and as you said, in that case assumption is that docs are reliable.

    This comment assumes that we talk about C# and .Net :)

    captcha: validus - your argument is invalidus

  • TheLurker (unregistered) in reply to @Deprecated

    Ahhh, should've used MUMPS.
    It'd be a one liner..

    ; If there's anything left over it's not an integer. I $L($TR(DEST, "0123456789")) > 0

    I'll get my coat...

  • Slugart (unregistered) in reply to @Deprecated

    Or they decide to start accepting hex...

  • sagaciter (unregistered)

    What? No frist idiots any more? Looks like I cleaned this place of them.

  • mp (unregistered) in reply to @Deprecated
    • Writes any of this when matching against "^[0-9]*$" would do?
  • blagh (unregistered) in reply to @Deprecated
    Ugh. Maybe the array is used in case sometime in the future, a new number is added between '4' and '5'.

    Well, yeah, you wanna make sure that if there's ever a need to accept numbers after the decimal point, you can add those in too.

  • Benjamin Howarth (unregistered) in reply to @Deprecated

    Here's the super-duper-timesaving method of doing this exact same thing (I'm assuming C# here):

    private string checkChars(string destination) { return (destination.ToCharArray().All(d => Char.IsNumeric(d))); }

    You're welcome.

  • drobnox (unregistered)
    public static bool IsNumeric(this string s)
    {
         return s.All(c => Char.IsNumber(c)); 
    }
    
    
  • Todd (unregistered) in reply to @Deprecated

    Looks like C#, so wow, how about just

    function ContainsOnlyNumbers(string input) { foreach(var s in input) { if (!Char.IsNumber(s)) { return false; } } return true; }

    Quits ASAP and uses presumably fast built-in functions. Alternatively a reg-ex that only accepts numbers should be quite smart.

  • Jenda (unregistered) in reply to Zmee
    Zmee:
    Just wow! And replacing this with a single line:
    return java.util.regex.Pattern.matches("^\\d*$", destination);
    (And yes, it is d* rather than d+; there is no requirement for the string to be non-empty.)
    Well if this has changed to a code golf ...

    return $destination =~ /^\d*$/;

    or rather the whole sub

    sub checkChars { $_[0] =~ /^\d*$/; }

    Not that anyone would bother making a function for anything as trivial as this.

  • MyWay (unregistered)
            public bool checkChar(string destination)
            {
                int p;
                for(int i = 0; i<destination.Length;i++)
                {
                    if (!int.TryParse(destination.Substring(i, 1),out p))
                    {
                        return false;
                    }
                }
                return true;
            }
    </pre>
    
  • Ben (unregistered)

    I think it's just the function name that's misleading. Should have been checkCharsAndSpinWait

  • deleto (unregistered)

    Looks like Martin should have no difficulty achieving some stunning performance gains, if management even cares.

  • Fūzetsu (unregistered) in reply to @Deprecated
    Comment held for moderation.
  • Vince (unregistered)

    Hello,

    It is probably an entry for an obfuscation contest ? ;-)

    Never heard of isInt() ?

  • Bozo (unregistered) in reply to @Deprecated

    Well here's a place to start:

    • apparently unaware that character has an isDigit function.
    • apparently unaware that Java has a regex package. People sometimes gripe about the performance of it, but if it's replacing the code above it can't help being an improvement.
  • Craig (unregistered) in reply to @Deprecated
    Comment held for moderation.

Leave a comment on “Stringy Sting Verification”

Log In or post as a guest

Replying to comment #:

« Return to Article