string: add AsciiCaseInsensitive{KeyEqual, Hash} for unordered map

https://httpwg.org/specs/rfc9110.html#rfc.section.5.1
Field names in HTTP headers are case-insensitive. These
structs will be used in the headers map to search by key.

In libevent field names are also converted to lowercase for comparison:
  evhttp_find_header()
  evutil_ascii_strcasecmp()
  EVUTIL_TOLOWER_()
This commit is contained in:
Matthew Zipkin
2024-05-30 15:34:58 -04:00
committed by Matthew Zipkin
parent 4e300df712
commit eea38787b9
2 changed files with 50 additions and 0 deletions

View File

@@ -13,6 +13,7 @@
#include <span.h>
#include <util/string.h>
#include <algorithm>
#include <array>
#include <bit>
#include <charconv>
@@ -353,6 +354,20 @@ struct Hex {
};
} // namespace detail
struct AsciiCaseInsensitiveKeyEqual {
bool operator()(std::string_view s1, std::string_view s2) const
{
return ToLower(s1) == ToLower(s2);
}
};
struct AsciiCaseInsensitiveHash {
size_t operator()(std::string_view s) const
{
return std::hash<std::string>{}(ToLower(s));
}
};
/**
* ""_hex is a compile-time user-defined literal returning a
* `std::array<std::byte>`, equivalent to ParseHex(). Variants provided: