From: Miod Vallat Date: Mon, 13 Jul 2026 07:03:56 +0000 (+0200) Subject: Move pdns_i*() from misc.hh to dnsname.hh. NFCI X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=32b78fde7205e768d018e9aede9f3a939521060d;p=thirdparty%2Fpdns.git Move pdns_i*() from misc.hh to dnsname.hh. NFCI This will avoid future chicken-and-egg problems. Signed-off-by: Miod Vallat --- diff --git a/pdns/dnsname.hh b/pdns/dnsname.hh index fae6f1f8ae..5f8bc17066 100644 --- a/pdns/dnsname.hh +++ b/pdns/dnsname.hh @@ -59,6 +59,58 @@ inline unsigned char dns_tolower(unsigned char chr) return dns_tolower_table[chr]; } +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.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; + } + } + aPtr++; + bPtr++; + } + // At this point, one of the strings has been completely processed. + // Either both have the same length, and they are equal, or one of them + // is larger, and compares as higher. + 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)); +inline bool pdns_iequals(const std::string& a, const std::string& b) +{ + if (a.length() != b.length()) + return false; + + return pdns_ilexicographical_compare_three_way(a, b) == 0; +} + +inline bool pdns_iequals_ch(const char a, const char b) __attribute__((pure)); +inline bool pdns_iequals_ch(const char a, const char b) +{ + if ((a != b) && (dns_tolower(a) != dns_tolower(b))) + return false; + + return true; +} + #include "burtle.hh" #include "views.hh" diff --git a/pdns/misc.hh b/pdns/misc.hh index de314d1e68..f488e04afa 100644 --- a/pdns/misc.hh +++ b/pdns/misc.hh @@ -373,59 +373,6 @@ inline bool operator<(const struct timespec& lhs, const struct timespec& rhs) } -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.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; - } - } - aPtr++; - bPtr++; - } - // At this point, one of the strings has been completely processed. - // Either both have the same length, and they are equal, or one of them - // is larger, and compares as higher. - 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)); -inline bool pdns_iequals(const std::string& a, const std::string& b) -{ - if (a.length() != b.length()) - return false; - - return pdns_ilexicographical_compare_three_way(a, b) == 0; -} - -inline bool pdns_iequals_ch(const char a, const char b) __attribute__((pure)); -inline bool pdns_iequals_ch(const char a, const char b) -{ - if ((a != b) && (dns_tolower(a) != dns_tolower(b))) - return false; - - return true; -} - - typedef unsigned long AtomicCounterInner; typedef std::atomic AtomicCounter ; diff --git a/pdns/test-dnsname_cc.cc b/pdns/test-dnsname_cc.cc index 0aafae488b..5b17e28dab 100644 --- a/pdns/test-dnsname_cc.cc +++ b/pdns/test-dnsname_cc.cc @@ -5,6 +5,8 @@ #define BOOST_TEST_NO_MAIN #include +#include +#include #include #include @@ -1107,4 +1109,50 @@ BOOST_AUTO_TEST_CASE(test_variantnames) { } #endif +BOOST_AUTO_TEST_CASE(test_pdns_ilexicographical_compare) { + typedef boost::tuple case_t; + typedef std::list cases_t; + + cases_t cases = boost::assign::list_of + (case_t(std::string(""), std::string(""), false)) + (case_t(std::string(""), std::string("abc"), true)) + (case_t(std::string("abc"), std::string(""), false)) + (case_t(std::string("abc"), std::string("abcd"), true)) + (case_t(std::string("abcd"), std::string("abc"), false)) + (case_t(std::string("abd"), std::string("abc"), false)) + (case_t(std::string("abc"), std::string("abd"), true)) + (case_t(std::string("abc"), std::string("Abc"), false)) + (case_t(std::string("Abc"), std::string("abc"), false)) + ; + + for(const case_t& val : cases) { + bool res; + res = pdns_ilexicographical_compare(val.get<0>(), val.get<1>()); + BOOST_CHECK_EQUAL(res, val.get<2>()); + } +} + +BOOST_AUTO_TEST_CASE(test_pdns_iequals) { + typedef boost::tuple case_t; + typedef std::list cases_t; + + cases_t cases = boost::assign::list_of + (case_t(std::string(""), std::string(""), true)) + (case_t(std::string(""), std::string("abc"), false)) + (case_t(std::string("abc"), std::string(""), false)) + (case_t(std::string("abc"), std::string("abcd"), false)) + (case_t(std::string("abcd"), std::string("abc"), false)) + (case_t(std::string("abd"), std::string("abc"), false)) + (case_t(std::string("abc"), std::string("abd"), false)) + (case_t(std::string("abc"), std::string("Abc"), true)) + (case_t(std::string("Abc"), std::string("abc"), true)) + ; + + for(const case_t& val : cases) { + bool res; + res = pdns_iequals(val.get<0>(), val.get<1>()); + BOOST_CHECK_EQUAL(res, val.get<2>()); + } +} + BOOST_AUTO_TEST_SUITE_END() diff --git a/pdns/test-misc_hh.cc b/pdns/test-misc_hh.cc index 27a77de427..bd5a8eff2c 100644 --- a/pdns/test-misc_hh.cc +++ b/pdns/test-misc_hh.cc @@ -12,8 +12,6 @@ #include #include -#include - #include #include "dns.hh" @@ -63,52 +61,6 @@ BOOST_AUTO_TEST_CASE(test_CIStringPairCompare) { BOOST_CHECK_EQUAL(s.str(), "(|1)(abc|1)(abc|2)(def|1)(ns.example.com|0)(ns.example.com|1)"); } -BOOST_AUTO_TEST_CASE(test_pdns_ilexicographical_compare) { - typedef boost::tuple case_t; - typedef std::list cases_t; - - cases_t cases = boost::assign::list_of - (case_t(std::string(""), std::string(""), false)) - (case_t(std::string(""), std::string("abc"), true)) - (case_t(std::string("abc"), std::string(""), false)) - (case_t(std::string("abc"), std::string("abcd"), true)) - (case_t(std::string("abcd"), std::string("abc"), false)) - (case_t(std::string("abd"), std::string("abc"), false)) - (case_t(std::string("abc"), std::string("abd"), true)) - (case_t(std::string("abc"), std::string("Abc"), false)) - (case_t(std::string("Abc"), std::string("abc"), false)) - ; - - for(const case_t& val : cases) { - bool res; - res = pdns_ilexicographical_compare(val.get<0>(), val.get<1>()); - BOOST_CHECK_EQUAL(res, val.get<2>()); - } -} - -BOOST_AUTO_TEST_CASE(test_pdns_iequals) { - typedef boost::tuple case_t; - typedef std::list cases_t; - - cases_t cases = boost::assign::list_of - (case_t(std::string(""), std::string(""), true)) - (case_t(std::string(""), std::string("abc"), false)) - (case_t(std::string("abc"), std::string(""), false)) - (case_t(std::string("abc"), std::string("abcd"), false)) - (case_t(std::string("abcd"), std::string("abc"), false)) - (case_t(std::string("abd"), std::string("abc"), false)) - (case_t(std::string("abc"), std::string("abd"), false)) - (case_t(std::string("abc"), std::string("Abc"), true)) - (case_t(std::string("Abc"), std::string("abc"), true)) - ; - - for(const case_t& val : cases) { - bool res; - res = pdns_iequals(val.get<0>(), val.get<1>()); - BOOST_CHECK_EQUAL(res, val.get<2>()); - } -} - BOOST_AUTO_TEST_CASE(test_stripDot) { BOOST_CHECK_EQUAL(stripDot("."), ""); BOOST_CHECK_EQUAL(stripDot(""), "");