- 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
There's also the delightfully readable version
someString.rfind(substring, 0) == fristthat doesn't waste time looking at any position other than the start! (assume a variablefristwith the value zero)Admin
This, in and of itself, is substantial wrongness, because you should make it more readable by comparing the result of the funtion to zero :
But in 2007-era C++, probably mostly C++98, it should be
if ( pdf->symTabName().substr(0, prefix.length()) == prefix )Admin
Anyone who fails to put a space between the if and the mandatory opening parenthesis should be banned from touching a keyboard ever again
Admin
Are you suggesting to search the whole string to see if it's the actual start? I don't know how big these strings are, but performance-wise that is not great, for something that is hardly more readable. Better to write a stringStartsWith() utility function.
Admin
rfindis (supposed to) be in C++98 and it doesn't need to allocate/copy a substringAdmin
The
::rfind()method is the correct answer.The one in the text:
if(pdf->symTabName().find(prefix) != 0UL)is, I think, wrong as that's only going to work if the prefix isn't at the start [when it does==0] and that includes not existing at all as::find()will returnnposif the prefix isn't in the string. And because that's astd::size_tit'll be comparable to 0...Admin
I am always amazed at how important the original C standards body thought that strings being "greater" or "lesser" was.
Admin
I consider none of the code-examples, including the ones suggested in the comments so far, readable. Readable would be to give it a speaking name. For example, if starts_with_prefix(pdf->symTabName(), prefix). I see no upside in calling .c_str() multiple times outside the function-call or in keeping the line that long. One of the great benefit of functions is the possibility to give non-interacting code-sections a name.
Admin
You are absolutely right! (omg, I swear I'm not a robot >.<) Calling "using the search function that starts at the end to check for 'starts with'" readable was with my tongue fimly planted in cheek.
Admin
There's lots and lots of bad code posted here in the TDWTF comments due to sloppy reading, sloppy thinking, and sloppy typing (with no edit function). I'm certainly guilty of all three at one time or another.
If you're going to deliberately write bad code for humor, it probably needs some kind of sarcasm indicator. e.g.
Admin
I'm guessing there was a PDP-11 instruction that did that.
Admin
In 1999, I was probably my city's leading expert in C++. I taught on-site C++ courses to a few large companies, including a major airline. 27 years later, I'm working for one of those companies full-time, but I went cold turkey in 2000 and became a C# expert instead. Last year, I had to do a presentation of identical code done in JavaScript, C#, and C++. In the 25+ years I had been away from C++, I came back to find a lot of radical changes. But the changes to the string libraries that I found in every other language since that time simply were not there. I can't do x.trim().tolower(). And I can't get good answers to this, even though I work with a member of the C++ standards committee. To me, C++ is starting to feel like it's aged to the point that it's reactionary, not progressive.
Admin
Sorry, but that is only your opinion that C++ is regressive.
As if other languages don't suffer from massive bloat of various non-core elements…. and still missing useful found in C++.
Admin
I like the std::string::operator<(), because it allows std::string to be used in std::map and others as key. Makes the handling of strings feel much more like first class citizens.
Admin
If you are using strings as the keys in a map, there better be only six or seven entries because that does NOT scale well.
Admin
It's almost as if they knew people would want to sort them in alphabetical order.
Edit Admin
The reason that it's wrong with .find() != 0UL is merely that the sense of the test is inverted (change "!=" to "==", and you're good).
And find() is better than rfind in an important way, especially if the names are long, because find() begins searching at the place where the prefix is, i.e. at the beginning. rfind() begins at the other end. If it's common that the names are long and the prefix is not, you'll spend time looking at places where the prefix isn't.
But most of all, rfind is absolutely wrong because e.g. "abcdabcd"s.rfind("a") also returns the wrong thing (4uz) if the prefix appears twice, once at the beginning and once somewhere else. (It returns the second, but not the first, and the code decides that the prefix isn't there.) "abcdabcd"s.find("a") correctly returns 0uz because the prefix is there.
Admin
::rfind("a")would indeed start from the end, but that's not what was said. The optionalposparam defaults tonposbut by specifying0, we ensure the search starts at the first character. If that fails, we don't look up the string, as we're already there.Edit Admin
Sorting lists of strings alphabetically has always been a useful function to make it easier for humans to find items in them.
If there's a defined collation order of strings (in whatever language you use and whatever alphabet) then there's a well defined "greater than" function that is quite useful for sorting lists of strings.
When
strcmpwas thought of, the only sort order available was by using the ASCII values. It worked reasonably well until non Americans wanted sorted string lists.Edit Admin
Unless the keys are quite long making the comparison expensive, it scales as well as using pretty much anything as the keys.
Admin
Hehe, you're exactly proving my point about the readability. As PlasticHound points out, the zero
posargument is essential and what makes it the correct solution, but it is a real headscratcher and should definitely be wrapped in astartsWithfunction.Edit Admin
C++ is the only language I've tried to learn and gave up. This reminds me why.
Edit Admin
I completely forgot I submitted this one. I've seen so much worse now that I'd barely stop to give something like this a second thought.
P.S. It's not company code. It's academic FOSS.