I'm supposed to implement a function which compares two strings simliar so strcmp
but ignoring whitespace characters, so
strcmpignorews("abc ", " a b c")
should give the same result.
Here's my implementation:
namespace {
void SkipWhitespace(const char *&s) {
for (; std::isspace(*s, std::locale::classic); ++s);
}
}
int strcmpignorews(const char *s1, const char *s2) {
for (; *s1 != '\0' && *s2 != '\0'; ++s1, ++s2) {
SkipWhitespace(s1);
SkipWhitespace(s2);
if (*s1 != *s2) {
break;
}
}
return (*s1 < *s2) ? -1 : ((*s1 == *s2) ? 0 : 1);
}
Now, the question is, would it make sense to inline the SkipWhitespace function? I think I've read somewhere that inline
should not be used for functions which contain loops or switches but I can't remember where and why.
Historically, Inline has been an indication to the compiler that it should insert the function body into the call site. However, that is no longer a meaningful annotation. Modern compilers will inline a function or not regardless of the presence or absence of inline
qualification.
To emphasize, whether compiler will perform inline optimization is completely out of your hands.
In modern use, inline has only one function. It can be used to get the linker to ignore multiple symbols, as when a function is defined in multiple compilation units. This technique can be used to break circular dependencies. Use inline for no other purpose.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With