- 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
Admin
I love me some recursion too, but recursion is slow; is that really the best way to validate the string?
I think that would be a bit cleaner.
CAPTCHA: Persto - Presto when said frist!
Admin
Such as Derf...
Admin
Looks like a good place to use Regular Expressions (try http://www.ultrapico.com/Expresso.htm).
Additionally it validates an empty destination of string.empty since validChars defaults to true, which probably isn't the intended behavior and would help your case for cleaning it up.
Admin
Admin
return destination.All(x => char.IsNumber(x));
Was that so hard? (Note that this allows numbers that cannot be parsed on the system due to them being too large)
Admin
Fair point.
CAPTCHA: consequat - a Sasquatch-sized consequence of writing bad code!
Admin
AGray, you realize your method is completely wrong, right?
Admin
Hrmm, this looks amazing, no idea that existed grabs
Admin
Your input and pattern parameters are in the wrong order. The pattern comes second.
Compiler error: Unrecognized escape sequence. Put @ in front of the pattern or double the backslash.
Your pattern will match any string with a number in it anywhere.
Admin
Hard? No. Correct? Also No.
These calls will both return true with your version: checkChars("ⅮⅠⅯⅱ"); checkChars("⅞፼൱");
IsDigit is what you wanted here.
Admin
Oops..I got it wrong too. There are 460 digit chars in unicode.
Guess we gotta go with: destination.All(x => x >= '0' && x <= '9');
Admin
Oh I see the problem, he didn't use xml.
Admin
I tried texting 1-800-suicide and my phone told me my very character is invalid. _esteem--;
Admin
The only requirement stated was that the thing only contained numbers, which is exactly what it does. I never said it matched what the code does :D
Admin
Getting paid by the line surely...
Admin
Well, if you were parsing phone numbers, you'd have to at least have something along the lines of:
^( *+ *[0-9]+)?( *( *[0-9]+ *))( *[0-9]+| *- [0-9]+) *$
Of course in this, the characters (, ), - and space get removed, + gets replaced by whatever the international dialling code is, however before that you might want to remove the entire international block all together if it's for the current country. However if this is the case, you will need to insert an extra 0 for some countries. Also, if you want to do this, you'll need to figure out the exact length of the country code, making it become something like:
*+ *(1|[2-4][0-9]|...)
Then you get the locational area, this is usually the first block after the internal code, and may be omitted to dial local... This is usually the part between the parentheses, however as users tend to get this part wrong. In the end, this requires a whole separate parsing round, so you may as well have been writing your own parser for this.
If you want to support letters, you can do that in the last couple of blocks as well, but then you'll need to convert them to numbers as well.
TL;DR: Phone numbers: A whole lot harder than your simple 0-9 digit parsing.
Admin
Damn.... that's pretty fkn bad..
Admin
Yeah, because you couldn't just have used regex
Admin
Does it work? Yes.
Admin
Admin
TRWTF is that almost every "fixed" version in this comments thread is wrong! You could get at least a week's worth of articles from it.
This will maintain the current logging and return type while removing the stupid:
Of course if you just want a boolean, you could use any of the following:
Admin
In many "modern" languages this could be simplified basically to something like: return (string)(int)destination === destination
E.g. in JavaScript: return String(parseInt(destination, 10)) === destination
Admin
Admin
.. if instead of boolean you want "1" or "0", why not just add ?"1":"0" or whichever way they wanted the return value ..
Admin
Oh, du-uh! I'll pick one for you, then you work out whether you can do likewise. Ready? Set? Go!
4.5
Admin
Reminds me of someone's FORTRAN program when I was considerably younger: dSylelixc colleague wanted to use NCOUNT as a variable but instead transposed the 2nd and 3rd letters. Oh how we laughed.
Admin
Because the equivalent version I posted has multiple return statements? If it had a valid flag and a single return then obviously I would have used
Also out of curiosity I also ran the following benchmark code:
The results were interesting:
ValidateLinq: 1239 ValidateRegex: 6241 ValidatePrecompiledRegex: 5449 ValidateForLoop: 171
Admin
Actually, a new integer between 3 and 4 have recently been found in Denmark.
http://translate.google.com/translate?sl=auto&tl=en&js=n&prev=_t&hl=en&ie=UTF-8&layout=2&eotf=1&u=http%3A%2F%2Frokokoposten.dk%2F2011%2F10%2F14%2Fbreaking-heltal-mellem-3-og-4-fundet-naer-silkeborg%2F&act=url
Admin
not to mention the == equality checks with Strings.
Admin
You laugh now, but just wait I tell you, you'll see, you'll all see!
Admin
I guess a company selling text-based services like to use a text-based approach when coding...
Admin
And the award for TRWTF goes to...
Admin
Don't worry, Moore's law will soon take up the slack.
Admin
And I thought MY code was bad.
Admin
This is correct. In fact this is about a thousand times faster than Regex.IsMatch(string, @"\d+");
Admin
There should be a international law against using non-boolean values for clearly boolean output in high level languages (C and higher despite C being considered low level these days). It's not likely the method in question would be required to return "X" (undefined) or "Z" (high impedance) in future :). Using 0/1 and Y/N is not all that can be encountered in this kind of string abuse, toss in case variation (y/n, y/N, Y/n), language variation (D/N, J/N), full words (yes/now, da/ne, ja/nein...) and you be running a freak show circus. Using ints instead of bools should be also prohibited by that law.
Anyway, regex that passes Turkish test would suffice in this case. There is a nice example in one comment how to find illegal chars using .Net Regex class. Turkish test is important, don't skip that part.
Admin
So, lowest level language (looks like C to me!) is best. Again, OOP is the real WTF!
Admin
Hey, wait a second. I'm a "linguistic major" and I've been coding for over 30 years... and... I've... written a lot of crappy code in those years.
Yeah, okay, I see your point.
Admin
Admin
Apparently, you did know where to start! :-)
And, how to finish!
Good one.
Admin
ctype_digit()
Admin
No no no! You just don't get it! He was getting ready for when all the kids grow up and "L33T Speak" is all they can write! This guy was a really forward thinking Brillant Suuuuuuuuuupppperrrrr Geeeeeeeeennniuuuuusssss.
Admin
I get the joke but does "L33T Speak" even exist outside of ridicule anymore?
persto: too fast to check splelign
Admin
To be fair, this is true for pretty much everything, but trying to do the same logic with 'a' thru 'z' could fail horribly if EBCDIC got involved...
Admin
Damn those futurians and their weird maths!
Admin
This depends on an arbitrary sort order.
Admin
At what time does Alex post articles on this so-called "daily" site?
Admin
At what time does Alex post articles on this so-called "daily" site?
Admin
There is a wheel that once, a long and long forgotten time ago, was invented and they called it Regex. It was a good wheel and it rolls on to this very day. Programmers like these shun this wheel though as complicated and slow and rather work with wheels that are cut in two, have not less than 3 not more than 5 edges and preferable an axle attached to more than one of these edges. If they didn't invent it, it can't be good.