String is only digits in convoluted c#



  • <FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    Came across the following crap while making changes to a module written by one of the devs. Called him right and gave him the 'shame on you for writing this crap as production code!' -- 'but it works!' -- 'it's f***in unmaintainable.'

    Bonus: it was obviously inline code sprinkled somewhere in the module.

    </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>var</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> digits = </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>new</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>List</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas><</FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>string</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>>() { </FONT></FONT><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas>"0"</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>, </FONT></FONT><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas>"1"</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>, </FONT></FONT><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas>"2"</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>, </FONT></FONT><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas>"3"</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>, </FONT></FONT><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas>"4"</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>, </FONT></FONT><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas>"5"</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>, </FONT></FONT><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas>"6"</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>, </FONT></FONT><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas>"7"</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>, </FONT></FONT><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas>"8"</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>, </FONT></FONT><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas>"9"</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> };
    </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>int</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> pos = 0;
    </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>bool</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> b = </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>false</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>;
    </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>for</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> (</FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>int</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> i = 0; i < fphone.Length; i++)
    {
      </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>string</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> s = fphone.Substring(pos, 1);
    </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>  bool</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> x = </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>false</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>;
      </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>foreach</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> (</FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>string</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> c </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>in</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> digits)
      </FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>{
        </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>if</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> (s == c)
        {
          pos = pos + 1;
          x = </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>true</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>;
          </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>break</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>;
        </FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>}
      }
      </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>if</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> (x == </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>false</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>)
      {
        b = </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>true</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>;
        </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>break</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>;
      }
    }

    </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>if</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> (b == </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>true</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>)
      fphone = </FONT></FONT><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas><FONT color=#a31515 size=2 face=Consolas>""</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>;

    </FONT></FONT>


  •  That's dumb.  Everybody knows you're supposed to convert it to a number and make sure it's between 100000000 and 9999999999.



  • /^\d+$/.test(fphone)




  • He is checking if the string contains only digits, and it seems like a regex like "[0-9]" was too much for him.



  • Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.



  • @pbean said:

    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    Some people, in an effort to sound intelligent, quote other people. Now they look retarded.



  • @dhromed said:

    /^\d+$/.test(fphone)
     

    for (double i = Math.pow(10, fphone.length() - 1); i < Math.pow(10, fphone.length()); ++i) {
    if (fphone.equals(Double.toString(i)) {
    return true;
    }
    }
    return false;



  • Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have /([2-9]|[1-9][0-9]*)/ problems.



  • @Welbog said:

    @pbean said:
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    Some people, in an effort to sound intelligent, quote other people. Now they look retarded.
    Agreed.  TRWTF is not precompiling the regex to enable efficient reuse.



  • @Welbog said:

    @pbean said:
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    Some people, in an effort to sound intelligent, quote other people. Now they look retarded.

    Some people, when--

    oh fuck that.

    I'm going to grab a sandwich.



  • @dhromed said:

    I'm going to grab a sandwich.
    I already have a sandwich. I picked it up at Canterbury Commons.



  • @dhromed said:

    @Welbog said:

    @pbean said:
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    Some people, in an effort to sound intelligent, quote other people. Now they look retarded.

    Some people, when--

    oh fuck that.

    I'm going to grab a sandwich.

    Some people, when confronted with a problem, think "I'm going to grab a sandwich."  Now they have two problems because the sandwich is poisoned.



  • You regex maniacs! I have a lot of more ways to do this:

    //use your favorite type of data, probably integer will not work
    integer p 
    if (integer.tryparse(fphone,p))
    {
    //it's a phone number!!!
    }
    
    
    //another
    integer p
    Try
    {
    p = integer.parse(fphone)
    }
    

    [Fixed your lack of markup. -bs]



  • @bstorer said:

    @dhromed said:

    @Welbog said:

    @pbean said:
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    Some people, in an effort to sound intelligent, quote other people. Now they look retarded.

    Some people, when--

    oh fuck that.

    I'm going to grab a sandwich.

    Some people, when confronted with a problem, think "I'm going to grab a sandwich."  Now they have two problems because the sandwich is poisoned.

    Some people, when confronted with a dhromed, think "I'm going to use a poisoned sandwich."  Now they have two problems because dhromed is going to "love" the sandwich, not eat it.



  • @Welbog said:

    @pbean said:
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    Some people, in an effort to sound intelligent, quote other people. Now they look retarded.

    Some people, when confronted with an over-used Zawinksi quote, think "I know, I'll use flaming."  Now they have two problems.



  • @morbiuswilters said:

    @bstorer said:

    @dhromed said:

    @Welbog said:

    @pbean said:
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    Some people, in an effort to sound intelligent, quote other people. Now they look retarded.

    Some people, when--

    oh fuck that.

    I'm going to grab a sandwich.

    Some people, when confronted with a problem, think "I'm going to grab a sandwich."  Now they have two problems because the sandwich is poisoned.

    Some people, when confronted with a dhromed, think "I'm going to use a poisoned sandwich."  Now they have two problems because dhromed is going to "love" the sandwich, not eat it.

    Some people, when confronted with a dhromed making love to a sandwich, think "I know, I'll eat that sandwich."  Now they have two problems because it turns out that they're btk.  Also the poison; so I guess it's actually 3 problems.



  • ok i'm not coming back to this forum



  • @adi_drumea said:

    ok i'm not coming back to this forum
    Good going, Welbog.  Way to scare off another one.  Jerk.



  • @bstorer said:

    @morbiuswilters said:

    @bstorer said:

    @dhromed said:

    @Welbog said:

    @pbean said:
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    Some people, in an effort to sound intelligent, quote other people. Now they look retarded.

    Some people, when--

    oh fuck that.

    I'm going to grab a sandwich.

    Some people, when confronted with a problem, think "I'm going to grab a sandwich."  Now they have two problems because the sandwich is poisoned.

    Some people, when confronted with a dhromed, think "I'm going to use a poisoned sandwich."  Now they have two problems because dhromed is going to "love" the sandwich, not eat it.

    Some people, when confronted with a dhromed making love to a sandwich, think "I know, I'll eat that sandwich."  Now they have two problems because it turns out that they're btk.  Also the poison; so I guess it's actually 3 problems.
     

    Some people, when observing a quote pyramid forming, add to it.



  • @adi_drumea said:

    ok i'm not coming back to this forum

     

    WHY DO YOU HATE FUN


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.