First off, GetFirstSubstring should take a const char *, not a char *, as it isn't modifying its argument.
As for your question, the first warning sign should have been the static variable. Any time you see something like that you run into problems like this, where you're not reentrant and multiple calls into the function result in weirdness. The solutions that come to mind are either dynamically allocating your return string, or returning by reference the length, something like:
This doesn't make your CaselessCompare call any easier, but at least it avoids having to remember to delete the return value of GetFirstSubstring.
Of course, if you're using more modern C++ and not C++ that looks like it wants to be C you could just return a std::string, which avoids both the dynamic allocation memory management issues and the annoyance of having to return the length of the result.
Comments 2
As for your question, the first warning sign should have been the static variable. Any time you see something like that you run into problems like this, where you're not reentrant and multiple calls into the function result in weirdness. The solutions that come to mind are either dynamically allocating your return string, or returning by reference the length, something like:
const char *GetFirstSubstring(const char *str, size_t *len);
This doesn't make your CaselessCompare call any easier, but at least it avoids having to remember to delete the return value of GetFirstSubstring.
Of course, if you're using more modern C++ and not C++ that looks like it wants to be C you could just return a std::string, which avoids both the dynamic allocation memory management issues and the annoyance of having to return the length of the result.
Reply
That is to say, Substring now takes two params, the output destination and the input string to search.
Reply
Leave a comment