How not to find a trailing substring match
-
This code is paraphrased from some open source:
// Consider host.domain.invalid and *.domain.invalid: // "host.domain.invalid".Find(".domain.invalid") is 4 // 4 + strlen(".domain.invalid") == 4 + 15 == 19 == strlen("host.domain.invalid") // Prerequiste: wildcardString begins with "*.". static bool HostMatchesWildcard(const char* host, string wildcard) { if (!host) return false;
string wildcardSubstr(wildcard.c_str() + 1, wildcard.length() - 1);
string hostString(host, strlen(host));
size_t index = hostString.find(wildcardSubstr);
return index != string::npos &&
index + wildcardSubstr.length() == hostString.length();
}(Names have been changed to protect the guilty.)
-