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
23 public:
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(noexcept((*this)(std::string_view{s.GetUnderlying()})),
39 "GetUnderlying() should not throw as this affects efficiency "
40 "on some platforms");
41
42 return (*this)(std::string_view{s.GetUnderlying()});
43 }
44
45 HashSeed GetSeed() const noexcept { return seed_; }
46
47 private:
48 HashSeed seed_;
49};
50
51/// @brief Case insensitive ASCII hashing functor
53 public:
54 using is_transparent [[maybe_unused]] = void;
55
56 /// Generates a new random hash seed for each hasher instance
58
59 /// Uses the provided seed. Use with caution: a constant seed makes the hasher
60 /// vulnerable to HashDOS attacks when arbitrary keys are allowed.
61 explicit StrIcaseHash(HashSeed seed) noexcept;
62
63 std::size_t operator()(std::string_view s) const& noexcept;
64
65 private:
66 HashSeed seed_;
67};
68
69/// Case insensitive ASCII 3-way comparison functor
71 public:
72 // TODO: std::weak_ordering
73
74 /// @returns integer <0 when `lhs < rhs`, >0 when `lhs > rhs` and 0 otherwise
75 int operator()(std::string_view lhs, std::string_view rhs) const noexcept;
76};
77
78/// Case insensitive ASCII equality comparison functor
80 public:
81 using is_transparent [[maybe_unused]] = void;
82
83 bool operator()(std::string_view lhs, std::string_view rhs) const noexcept;
84};
85
86/// Case insensitive ASCII less comparison functor
88 public:
89 using is_transparent [[maybe_unused]] = void;
90
91 bool operator()(std::string_view lhs, std::string_view rhs) const noexcept;
92};
93
94} // namespace utils
95
96USERVER_NAMESPACE_END