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