It's quite common to see maps or sets that make use of a string->object
association, usually as <std::string, T>. However, this can result in
inefficient code when performing lookups or indexing, as something like:
std::map<std::string, T> some_map;
...
auto iter = some_map.find("Name");
...
will result in a std::string instance being constructed around "Name",
which is less than ideal.
With a heterogenous comparator, however (such as std::less<>), like:
std::map<std::string, T, std::less<>>
we allow the container to do automatic type deduction and comparisons.
This allows types comparable to the key type to be used in find calls,
etc, without actually needing to construct the key type around the other
type.
The main way to enable containers to perform such behavior is by
defining a type named is_transparent within the comparator type.
These should be using {}-style formatting specifiers instead of printf
style. While we're at it, std::move the std::string instances where
applicable to potentially avoid reallocations.
These are frequently used within hash implementations, which should be
noexcept by default. Given it doesn't make sense to throw exceptions
from these functions anyways, they can be made noexcept.
Same behavior, but more idiomatic. While we're at it, we can make said
constructor and the conversion operator explicit to make the class a
little less error-prone.
We already construct a std::string instance, so we can just append to
it instead of creating another temporary with std::string's operator+.
We also change this to append using the string view getter functions, as
this allows the appending process to do less work. When a pointer is
passed in, a strlen call would need to be performed in order to
determine the total characters to append. However, we already know the
size (via the string view).
We don't really need to call out to the C functions to perform the
comparison behavior when the views already have a comparison function
as part of their interface.
std::string_view instances can contain character values that lie outside
the range of an unsigned char (negative values). If such a value is
passed into std::isspace, then the behavior of the function is
undefined. To avoid this, we add these casts.
std::string_view instances aren't guaranteed to be null-terminated, so
we shouldn't be treating them as if they are in these functions, and
should instead use a bounded comparison based off their sizes.
This way we prevent an edge-case from ever becoming a problem and also
remove an ifdef, making the code uniform across all implementations.