From the old Sidebar: How not to find a trailing substring match
-
@NeilRashbrook said:
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.)
Filed under: Reinventing square wheels
-
So does that mean that C# really is the least bad?
-
It's not like they did it with PHP...