}
-inline bool pdns_ilexicographical_compare(const std::string& a, const std::string& b) __attribute__((pure));
-inline bool pdns_ilexicographical_compare(const std::string& a, const std::string& b)
+inline int pdns_ilexicographical_compare_three_way(std::string_view a, std::string_view b) __attribute__((pure));
+inline int pdns_ilexicographical_compare_three_way(std::string_view a, std::string_view b)
{
- const unsigned char *aPtr = (const unsigned char*)a.c_str(), *bPtr = (const unsigned char*)b.c_str();
+ const unsigned char *aPtr = (const unsigned char*)a.data(), *bPtr = (const unsigned char*)b.data();
const unsigned char *aEptr = aPtr + a.length(), *bEptr = bPtr + b.length();
while(aPtr != aEptr && bPtr != bEptr) {
if (*aPtr != *bPtr) {
if (int rc = dns_tolower(*aPtr) - dns_tolower(*bPtr); rc != 0) {
- return rc < 0;
+ return rc;
}
}
aPtr++;
bPtr++;
}
- if(aPtr == aEptr && bPtr == bEptr) // strings are equal (in length)
- return false;
- return aPtr == aEptr; // true if first string was shorter
+ if (aPtr == aEptr) {
+ if (bPtr != bEptr) {
+ return -1; // a < b
+ }
+ }
+ else {
+ return 1; // a > b
+ }
+ return 0; // a == b
+}
+
+inline bool pdns_ilexicographical_compare(const std::string& a, const std::string& b) __attribute__((pure));
+inline bool pdns_ilexicographical_compare(const std::string& a, const std::string& b)
+{
+ return pdns_ilexicographical_compare_three_way(a, b) < 0;
}
inline bool pdns_iequals(const std::string& a, const std::string& b) __attribute__((pure));
if (a.length() != b.length())
return false;
- const char *aPtr = a.c_str(), *bPtr = b.c_str();
- const char *aEptr = aPtr + a.length();
- while(aPtr != aEptr) {
- if((*aPtr != *bPtr) && (dns_tolower(*aPtr) != dns_tolower(*bPtr)))
- return false;
- aPtr++;
- bPtr++;
- }
- return true;
+ return pdns_ilexicographical_compare_three_way(a, b) == 0;
}
inline bool pdns_iequals_ch(const char a, const char b) __attribute__((pure));