]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/beast/core/string.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / beast / core / string.hpp
1 //
2 // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/beast
8 //
9
10 #ifndef BOOST_BEAST_STRING_HPP
11 #define BOOST_BEAST_STRING_HPP
12
13 #include <boost/beast/core/detail/config.hpp>
14 #include <boost/version.hpp>
15 #ifndef BOOST_BEAST_NO_BOOST_STRING_VIEW
16 # if BOOST_VERSION >= 106400
17 # define BOOST_BEAST_NO_BOOST_STRING_VIEW 0
18 # else
19 # define BOOST_BEAST_NO_BOOST_STRING_VIEW 1
20 # endif
21 #endif
22
23 #if BOOST_BEAST_NO_BOOST_STRING_VIEW
24 #include <boost/utility/string_ref.hpp>
25 #else
26 #include <boost/utility/string_view.hpp>
27 #endif
28
29 #include <algorithm>
30
31 namespace boost {
32 namespace beast {
33
34 #if BOOST_BEAST_NO_BOOST_STRING_VIEW
35 /// The type of string view used by the library
36 using string_view = boost::string_ref;
37
38 /// The type of basic string view used by the library
39 template<class CharT, class Traits>
40 using basic_string_view =
41 boost::basic_string_ref<CharT, Traits>;
42 #else
43 /// The type of string view used by the library
44 using string_view = boost::string_view;
45
46 /// The type of basic string view used by the library
47 template<class CharT, class Traits>
48 using basic_string_view =
49 boost::basic_string_view<CharT, Traits>;
50 #endif
51
52 namespace detail {
53
54 inline
55 char
56 ascii_tolower(char c)
57 {
58 if(c >= 'A' && c <= 'Z')
59 c += 'a' - 'A';
60 return c;
61 }
62
63 template<class = void>
64 bool
65 iequals(
66 beast::string_view lhs,
67 beast::string_view rhs)
68 {
69 auto n = lhs.size();
70 if(rhs.size() != n)
71 return false;
72 auto p1 = lhs.data();
73 auto p2 = rhs.data();
74 char a, b;
75 while(n--)
76 {
77 a = *p1++;
78 b = *p2++;
79 if(a != b)
80 goto slow;
81 }
82 return true;
83
84 while(n--)
85 {
86 slow:
87 if(ascii_tolower(a) != ascii_tolower(b))
88 return false;
89 a = *p1++;
90 b = *p2++;
91 }
92 return true;
93 }
94
95 } // detail
96
97 /** Returns `true` if two strings are equal, using a case-insensitive comparison.
98
99 The case-comparison operation is defined only for low-ASCII characters.
100
101 @param lhs The string on the left side of the equality
102
103 @param rhs The string on the right side of the equality
104 */
105 inline
106 bool
107 iequals(
108 beast::string_view lhs,
109 beast::string_view rhs)
110 {
111 return detail::iequals(lhs, rhs);
112 }
113
114 /** A case-insensitive less predicate for strings.
115
116 The case-comparison operation is defined only for low-ASCII characters.
117 */
118 struct iless
119 {
120 bool
121 operator()(
122 string_view lhs,
123 string_view rhs) const
124 {
125 using std::begin;
126 using std::end;
127 return std::lexicographical_compare(
128 begin(lhs), end(lhs), begin(rhs), end(rhs),
129 [](char c1, char c2)
130 {
131 return detail::ascii_tolower(c1) < detail::ascii_tolower(c2);
132 }
133 );
134 }
135 };
136
137 /** A case-insensitive equality predicate for strings.
138
139 The case-comparison operation is defined only for low-ASCII characters.
140 */
141 struct iequal
142 {
143 bool
144 operator()(
145 string_view lhs,
146 string_view rhs) const
147 {
148 return iequals(lhs, rhs);
149 }
150 };
151
152 } // beast
153 } // boost
154
155 #endif