Last active
August 29, 2015 14:04
-
-
Save tonisuter/c29228ead18c3b330121 to your computer and use it in GitHub Desktop.
Possible Refactorings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool MatchDnsName(const char* hostname, const char* dns_name) { | |
// NULL or empty names don't match anything | |
if (hostname == NULL || *hostname == '\0') return false; | |
if (dns_name == NULL || *dns_name == '\0') return false; | |
// check for wildcards */ | |
if (dns_name[0] == '*') { | |
// wildcard match, expect '*.' at the start, we don't match '*foo.com' | |
if (dns_name[1] != '.') return false; | |
// skip the first component of the hostname | |
while (hostname[0] != '\0' && hostname[0] != '.') { | |
++hostname; | |
} | |
if (hostname[0] == '.') ++hostname; | |
// compare the tails | |
return NPT_String::Compare(hostname, dns_name+2, true) == 0; | |
} | |
else { | |
// full match | |
return NPT_String::Compare(hostname, dns_name, true) == 0; | |
} | |
} | |
//==> | |
bool MatchDnsName(const char* hostname, const char* dns_name) { | |
if (hostname == NULL) return false; | |
if (dns_name == NULL) return false; | |
return MatchDnsName(std::string(hostname), std::string(dns_name)); | |
} | |
bool MatchDnsName(const std::string& hostname, const std::string& dns_name) { | |
if (hostname.empty()) return false; | |
if (dns_name.empty()) return false; | |
if (dns_name[0] == '*') { | |
if (dns_name[1] != '.') return false; | |
std::string::size_type hostname_pos = 0; | |
while (hostname[hostname_pos] != '\0' && hostname[hostname_pos] != '.') { | |
++hostname_pos; | |
} | |
if (hostname[hostname_pos] == '.') ++hostname_pos; | |
// compare the tails | |
return NPT_String::Compare(hostname.c_str()+hostname_pos, dns_name.c_str()+2, true) == 0; | |
} | |
else { | |
// full match | |
return NPT_String::Compare(hostname.c_str(), dns_name.c_str(), true) == 0; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const char *str = "Testtexte"; | |
const char *ptr = str; | |
while((ptr = strchr(ptr, 'e')) != NULL) { | |
std::cout << (ptr - str) << std::endl; | |
ptr++; | |
} | |
//=> | |
std::string str = "Testtexte"; | |
std::string::size_type ptr = 0; | |
while((ptr = str.find('e', ptr)) != std::string::npos) { | |
std::cout << ptr << std::endl; | |
ptr++; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
std::string getCommand(const char* serverObject) { | |
assert(serverObject); | |
if (!*serverObject) { | |
return ""; | |
} | |
while (*serverObject == '/') { | |
++serverObject; | |
} | |
const char *v = strchr(serverObject, '?'); | |
if (v) { | |
return std::string(serverObject, v - serverObject); | |
} | |
return serverObject; | |
} | |
//==> | |
std::string getCommand(const char* serverObject) { | |
assert(serverObject); | |
return getCommand(std::string(serverObject)); | |
} | |
std::string getCommand(const std::string& serverObject) { | |
if (serverObject.empty()) { | |
return ""; | |
} | |
std::string::const_iterator serverObject_it = serverObject.cbegin(); | |
while (*serverObject_it == '/') { | |
++serverObject_it; | |
} | |
std::string::const_iterator v_it = std::find(serverObject_it, serverObject.cend(), '?'); | |
if (v_it != serverObject.end()) { | |
return std::string(serverObject_it, v_it); | |
} | |
return std::string(serverObject_it, serverObject.end()); | |
} | |
//alternative: size_type instead of const_iterator | |
std::string getCommand(const std::string& serverObject) { | |
if (serverObject.empty()) { | |
return ""; | |
} | |
std::string::size_type serverObject_pos = 0; | |
while (serverObject[serverObject_pos] == '/') { | |
++serverObject_pos; | |
} | |
std::string::size_type v_pos = serverObject.find('?', serverObject_pos); | |
if (v_pos != std::string::npos) { | |
return serverObject.substr(serverObject_pos, v_pos - serverObject_pos); | |
} | |
return serverObject.substr(serverObject_pos); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const char *getServerObject(const char* url) { | |
assert(url); | |
int strip = 0; | |
if (strncmp(url, "http://", 7) == 0) { | |
strip = 7; | |
} else if (strncmp(url, "https://", 8) == 0) { | |
strip = 8; | |
} | |
const char *p = strchr(url + strip, '/'); | |
if (p) { | |
while (*(p + 1) == '/') p++; | |
return p; | |
} | |
if (strip == 0) return url; | |
return ""; | |
} | |
//==> | |
const char *getServerObject(const char* url) { | |
assert(url); | |
return getServerObject2(std::string(url)); | |
} | |
const char *getServerObject(const std::string& url) { | |
int strip = 0; | |
if (url.compare(0, 7, "http://", 0, 7) == 0) { | |
strip = 7; | |
} else if (url.compare(0, 8, "https://", 0, 8) == 0) { | |
strip = 8; | |
} | |
std::string::const_iterator p = std::find(url.begin() + strip, url.end(), '/'); | |
if (p != url.end()) { | |
while (*(p + 1) == '/') p++; | |
return std::string(p, url.end()).c_str(); | |
} | |
if (strip == 0) return url.c_str(); | |
return ""; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment