userver: userver/utils/str_icase.hpp Source File
Loading...
Searching...
No Matches
str_icase.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utils/str_icase.hpp
4/// @brief Case insensitive ASCII comparators and hashers
5/// @ingroup userver_universal
6
7#include <cstdint>
8#include <string_view>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace utils {
13
14/// The seed structure used by underlying hashing implementation
15struct HashSeed final {
16 std::uint64_t k0;
17 std::uint64_t k1;
18};
19
20/// @brief Case sensitive ASCII hashing functor
22public:
23 using is_transparent [[maybe_unused]] = void;
24
25 /// Generates a new random hash seed for each hasher instance
27
28 /// Uses the provided seed. Use with caution: a constant seed makes the hasher
29 /// vulnerable to HashDOS attacks when arbitrary keys are allowed.
30 explicit StrCaseHash(HashSeed seed) noexcept;
31
32 std::size_t operator()(std::string_view s) const& noexcept;
33
34 template <class StringStrongTypedef>
35 auto operator()(const StringStrongTypedef& s
36 ) const& noexcept -> decltype(operator()(std::string_view{s.GetUnderlying()})) {
37 static_assert(
38 noexcept((*this)(std::string_view{s.GetUnderlying()})),
39 "GetUnderlying() should not throw as this affects efficiency "
40 "on some platforms"
41 );
42
43 return (*this)(std::string_view{s.GetUnderlying()});
44 }
45
46 HashSeed GetSeed() const noexcept { return seed_; }
47
48private:
49 HashSeed seed_;
50};
51
52/// @brief Case insensitive ASCII hashing functor
54public:
55 using is_transparent [[maybe_unused]] = void;
56
57 /// Generates a new random hash seed for each hasher instance
59
60 /// Uses the provided seed. Use with caution: a constant seed makes the hasher
61 /// vulnerable to HashDOS attacks when arbitrary keys are allowed.
62 explicit StrIcaseHash(HashSeed seed) noexcept;
63
64 std::size_t operator()(std::string_view s) const& noexcept;
65
66private:
67 HashSeed seed_;
68};
69
70/// Case insensitive ASCII 3-way comparison functor
72public:
73 // TODO: std::weak_ordering
74
75 /// @returns integer <0 when `lhs < rhs`, >0 when `lhs > rhs` and 0 otherwise
76 int operator()(std::string_view lhs, std::string_view rhs) const noexcept;
77};
78
79/// Case insensitive ASCII equality comparison functor
81public:
82 using is_transparent [[maybe_unused]] = void;
83
84 bool operator()(std::string_view lhs, std::string_view rhs) const noexcept;
85};
86
87/// Case insensitive ASCII less comparison functor
89public:
90 using is_transparent [[maybe_unused]] = void;
91
92 bool operator()(std::string_view lhs, std::string_view rhs) const noexcept;
93};
94
95} // namespace utils
96
97USERVER_NAMESPACE_END