]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
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> | |
b32b8144 | 15 | #include <boost/utility/string_view.hpp> |
b32b8144 FG |
16 | #include <algorithm> |
17 | ||
18 | namespace boost { | |
19 | namespace beast { | |
20 | ||
b32b8144 FG |
21 | /// The type of string view used by the library |
22 | using string_view = boost::string_view; | |
23 | ||
24 | /// The type of basic string view used by the library | |
25 | template<class CharT, class Traits> | |
26 | using basic_string_view = | |
27 | boost::basic_string_view<CharT, Traits>; | |
b32b8144 FG |
28 | |
29 | namespace detail { | |
30 | ||
31 | inline | |
32 | char | |
33 | ascii_tolower(char c) | |
34 | { | |
11fdf7f2 TL |
35 | return ((static_cast<unsigned>(c) - 65U) < 26) ? |
36 | c + 'a' - 'A' : c; | |
b32b8144 FG |
37 | } |
38 | ||
39 | template<class = void> | |
40 | bool | |
41 | iequals( | |
42 | beast::string_view lhs, | |
43 | beast::string_view rhs) | |
44 | { | |
45 | auto n = lhs.size(); | |
46 | if(rhs.size() != n) | |
47 | return false; | |
48 | auto p1 = lhs.data(); | |
49 | auto p2 = rhs.data(); | |
50 | char a, b; | |
51 | while(n--) | |
52 | { | |
53 | a = *p1++; | |
54 | b = *p2++; | |
55 | if(a != b) | |
11fdf7f2 TL |
56 | { |
57 | // slow loop | |
58 | do | |
59 | { | |
60 | if(ascii_tolower(a) != ascii_tolower(b)) | |
61 | return false; | |
62 | a = *p1++; | |
63 | b = *p2++; | |
64 | } | |
65 | while(n--); | |
66 | return true; | |
67 | } | |
b32b8144 FG |
68 | } |
69 | return true; | |
70 | } | |
71 | ||
72 | } // detail | |
73 | ||
74 | /** Returns `true` if two strings are equal, using a case-insensitive comparison. | |
75 | ||
76 | The case-comparison operation is defined only for low-ASCII characters. | |
77 | ||
78 | @param lhs The string on the left side of the equality | |
79 | ||
80 | @param rhs The string on the right side of the equality | |
81 | */ | |
82 | inline | |
83 | bool | |
84 | iequals( | |
85 | beast::string_view lhs, | |
86 | beast::string_view rhs) | |
87 | { | |
88 | return detail::iequals(lhs, rhs); | |
89 | } | |
90 | ||
91 | /** A case-insensitive less predicate for strings. | |
92 | ||
93 | The case-comparison operation is defined only for low-ASCII characters. | |
94 | */ | |
95 | struct iless | |
96 | { | |
97 | bool | |
98 | operator()( | |
99 | string_view lhs, | |
100 | string_view rhs) const | |
101 | { | |
102 | using std::begin; | |
103 | using std::end; | |
104 | return std::lexicographical_compare( | |
105 | begin(lhs), end(lhs), begin(rhs), end(rhs), | |
106 | [](char c1, char c2) | |
107 | { | |
108 | return detail::ascii_tolower(c1) < detail::ascii_tolower(c2); | |
109 | } | |
110 | ); | |
111 | } | |
112 | }; | |
113 | ||
114 | /** A case-insensitive equality predicate for strings. | |
115 | ||
116 | The case-comparison operation is defined only for low-ASCII characters. | |
117 | */ | |
118 | struct iequal | |
119 | { | |
120 | bool | |
121 | operator()( | |
122 | string_view lhs, | |
123 | string_view rhs) const | |
124 | { | |
125 | return iequals(lhs, rhs); | |
126 | } | |
127 | }; | |
128 | ||
129 | } // beast | |
130 | } // boost | |
131 | ||
132 | #endif |