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