- 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
So many pointlessly complicated ways to reinvent the wheel. Like, ok, I get it. Maybe your boss really is that stupid and makes you work with pre 98 C++ standards so you have to do everything by hand. But if that is the case why not just do the simple things? Why bother with itterators and stuff and make your life miserable?
Admin
Wait... this isn't even a "startsWith". It's just "contains" with extra steps. Like, it will find "aaa" inside "bbb aaa bbb". And the rewind will be off too.
Admin
No, wait, you're right. It's because it only checks up to "str.length" first characters, where "str" is the needle we're looking for.
Admin
find( std::string const & haystack, std::string const & needle) { return strpos( needle.c_str(), haystack.c_str() ) != -1; }
Edit Admin
Unfortunately, in the completely general case, this doesn't work because a
std::stringcan contain embedded NUL characters, whichstd::string::find()will cope with just fine, but the C functions will most definitely not cope with at all.Edit Admin
If your boss is this stupid, you should urgently look for either (a) a job with a diffferent boss in the same company, or (b) a job in a different company, or (c) a hitman to rid you of your boss.
Also, you can eliminate the goto by replacing it with a break and make the return conditional on whether you looked at the entire length of
needle.Admin
Also, rewinding the iterator is unnecessary if they didn't pass it by reference in the first place, which there's no reason to. The whole thing boils down to:
(With somewhat recent versions, you can use string_view instead of string to avoid copying.)
At least, this could be a teachable moment in code review (if the organization permits that, which I doubt here), or rather a lot of teachable points:
Admin
On the first point I agree with you. Well, I agree with you now. I remember the days when you didn't have anything but ye old C to work with. xP But that's a story for another day.
On the second though I actually prefer the goto. It's cleaner and more old school. The later of which is what I was going for with this code.
This said, it is a pet peeve of mine when I see people use advanced and complex language constructs to do simple things. If you look at code written these days it some times feel like people are afraid of the basics. And the code in this article is a prime example of that sort of behavior.
You don't need 3 layers of interfaces, LINQ, smart pointers and generics to write hello world. And yet people keep doing that with such a consistency that I am unsure whether they are showing off or just so ignorant of the basics.
Edit Admin
What I had in mind for getting rid of the goto was more like this:
It removes a "continue before closing brace of loop", about which some static analysis tools will complain.
Edit Admin
If you're stuck with pre-98 C++, you use the strstr() function in the C library to do the same thing. I think most of the C++ string functions were implemented using the C library functions.
Admin
All this talk of C and basic coding really made me feel nostalgic for the good old days. Thanks you guys.
Edit Admin
In fact, you can't or at least shouldn't, because C++ string objects can contain embedded NUL characters. Well, that's true for
std::stringbut might not be for pre-STL string objects, which makes the concept of passing some equivalent ofsomevar.c_str()to a C library function highly questionable.Admin
Dad joke time - C++ Edition:
How do you know your programming language is all grown up? It got an STD.