diff --git a/include/tscpp/util/TextView.h b/include/tscpp/util/TextView.h index 637dfa10d8b..1a56ad07e3d 100644 --- a/include/tscpp/util/TextView.h +++ b/include/tscpp/util/TextView.h @@ -1273,21 +1273,47 @@ namespace literals * std::set strings; * @endcode */ -struct caseless_compare { +struct caseless_less_than { bool operator()(std::string_view const &lhs, std::string_view const &rhs) const { - return strcasecmp(lhs, rhs); + return strcasecmp(lhs, rhs) < 0; } bool operator()(TextView const &lhs, TextView const &rhs) const { - return strcasecmp(lhs, rhs); + return strcasecmp(lhs, rhs) < 0; } bool operator()(std::string const &lhs, std::string const &rhs) const { - return strcasecmp(lhs, rhs); + return strcasecmp(lhs, rhs) < 0; + } +}; + +/** Functor for STL containers that need caseless equality of standard string types. + * + * For example a @c std::set of strings with caseless comparison would be + * + * @code + * std::set strings; + * @endcode + */ +struct caseless_equal { + bool + operator()(std::string_view const &lhs, std::string_view const &rhs) const + { + return strcasecmp(lhs, rhs) == 0; + } + bool + operator()(TextView const &lhs, TextView const &rhs) const + { + return strcasecmp(lhs, rhs) == 0; + } + bool + operator()(std::string const &lhs, std::string const &rhs) const + { + return strcasecmp(lhs, rhs) == 0; } }; diff --git a/iocore/utils/I_Machine.h b/iocore/utils/I_Machine.h index dbfe34df7df..13e47e078cf 100644 --- a/iocore/utils/I_Machine.h +++ b/iocore/utils/I_Machine.h @@ -88,6 +88,6 @@ struct Machine { Machine(char const *hostname, sockaddr const *addr); static self_type *_instance; ///< Singleton for the class. - std::unordered_set, ts::caseless_compare> machine_id_strings; + std::unordered_set, ts::caseless_equal> machine_id_strings; std::unordered_set machine_id_ipaddrs; }; diff --git a/iocore/utils/Machine.cc b/iocore/utils/Machine.cc index 01854673bef..2697e040d90 100644 --- a/iocore/utils/Machine.cc +++ b/iocore/utils/Machine.cc @@ -65,7 +65,7 @@ Machine::Machine(char const *the_hostname, sockaddr const *addr) // limit is 63, or 255 for a FQDN. auto result = gethostname(localhost, sizeof(localhost)); ink_release_assert(result == 0); - host_name.assign(localhost, result); + host_name.assign(localhost, strlen(localhost)); } #if HAVE_IFADDRS_H