{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreidlumwl4d2mz447ucwtioo55utynfh57mk3p7wf3c5cfrsdtuhe6i",
"uri": "at://did:plc:vyjlfm46mfv6u4vjp6qtrfx2/app.bsky.feed.post/3ml7csolcgls2"
},
"coverImage": {
"$type": "blob",
"ref": {
"$link": "bafkreidg2k33tiuupnstmiowh7hyyvtsbe3gyhfygiytrzynysnhoctrxi"
},
"mimeType": "image/jpeg",
"size": 81260
},
"path": "/articles/please-find-rewind",
"publishedAt": "2026-05-06T06:30:00.000Z",
"site": "https://thedailywtf.com",
"tags": [
"CodeSOD",
"previously discussed",
"a real gem",
"Download Free Guide Now!"
],
"textContent": "As previously discussed, C++ took a surprisingly long time to get a \"starts with\" function for strings. It took even longer to get a function called \"contains\". In part, that's simply because `string::find` solves that problem.\n\n**Nancy** sends us a… different approach to solving this problem.\n\n\n bool substringInString(string str, string::iterator &it)\n {\n string tmp;\n bool result = false;\n int size = str.length();\n\n int count = 0;\n while (count < size)\n {\n tmp += *it;\n it++;\n count++;\n if (tmp.find(str) != string::npos)\n {\n result = true;\n it -= size;\n break;\n }\n }\n\n if ( !result)\n {\n it -= size;\n }\n\n return result;\n }\n\n\nThis function iterates across a string, character by character. In this iteration, we copy one character at a time into `tmp`. Then we see if `tmp` contains our search `str`. If it does, we break out of the loop after rewinding the iterator. Outside of the loop, we check if we found the substring, and if we did, we rewind the iterator. Then we return true or false based on whether on not we found the substring.\n\nSo wait a second. `str` is our search string. `it` is where we're searching. And we copy from `it` up to our search string's `length` into a temporary string. We then do a `find` in that temporary string- hey! This is just a `startsWith` check written in the most insane way possible.\n\nWhy even bother with the while loop? While `tmp` is shorter than the search string, the answer is always \"no, we haven't found it\". And the developers knew that- that's why they always rewind `size` characters on the iterator. They're always searching exactly that many characters. Of course, since we _always_ rewind the same amount, we can also just move the `it -= size` statement out of the loop and out of the `if` statement and do it once.\n\nNancy calls this \"a little gem\" in a \"large codebase\". Yeah, a real gem.\n\n[Advertisement] **Plan Your .NET 9 Migration with Confidence**\nYour journey to .NET 9 is more than just one decision.Avoid migration migraines with the advice in this free guide. **Download Free Guide Now!**",
"title": "CodeSOD: Please Find, Rewind"
}