userver
C++ Async Framework
Toggle main menu visibility
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
11
USERVER_NAMESPACE_BEGIN
12
13
namespace
utils
{
14
15
/// The seed structure used by underlying hashing implementation
16
struct
HashSeed
final
{
17
std::uint64_t k0;
18
std::uint64_t k1;
19
};
20
21
/// @brief Case sensitive ASCII hashing functor
22
class
StrCaseHash
{
23
public
:
24
using
is_transparent [[maybe_unused]] =
void
;
25
26
/// Generates a new random hash seed for each hasher instance
27
StrCaseHash
();
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
37
)
const
&
noexcept
->
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
49
private
:
50
HashSeed seed_;
51
};
52
53
/// @brief Case insensitive ASCII hashing functor
54
class
StrIcaseHash
{
55
public
:
56
using
is_transparent [[maybe_unused]] =
void
;
57
58
/// Generates a new random hash seed for each hasher instance
59
StrIcaseHash
();
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
67
private
:
68
HashSeed seed_;
69
};
70
71
/// Case insensitive ASCII 3-way comparison functor
72
class
StrIcaseCompareThreeWay
{
73
public
:
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
81
class
StrIcaseEqual
{
82
public
:
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
89
class
StrIcaseLess
{
90
public
:
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
98
USERVER_NAMESPACE_END
userver
utils
str_icase.hpp
Generated on
for userver by
Doxygen
1.17.0