2017-06-16 09:38:53 -07:00
|
|
|
//
|
2019-02-21 07:00:31 -08:00
|
|
|
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
|
2017-06-16 09:38:53 -07:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
2017-07-20 13:40:34 -07:00
|
|
|
// Official repository: https://github.com/boostorg/beast
|
|
|
|
//
|
2017-06-16 09:38:53 -07:00
|
|
|
|
2017-07-20 13:40:34 -07:00
|
|
|
#ifndef BOOST_BEAST_STRING_HPP
|
|
|
|
#define BOOST_BEAST_STRING_HPP
|
2017-06-16 09:38:53 -07:00
|
|
|
|
2017-10-10 07:49:03 -07:00
|
|
|
#include <boost/beast/core/detail/config.hpp>
|
2017-06-26 09:00:15 -07:00
|
|
|
#include <boost/version.hpp>
|
2018-09-05 21:17:53 +02:00
|
|
|
|
|
|
|
#if defined(BOOST_BEAST_USE_STD_STRING_VIEW)
|
|
|
|
#include <string_view>
|
|
|
|
#else
|
2017-06-24 11:33:04 -07:00
|
|
|
#include <boost/utility/string_view.hpp>
|
2018-09-05 21:17:53 +02:00
|
|
|
#endif
|
|
|
|
|
2017-06-16 21:38:17 -07:00
|
|
|
#include <algorithm>
|
2017-06-16 09:38:53 -07:00
|
|
|
|
2017-07-20 13:40:34 -07:00
|
|
|
namespace boost {
|
2017-06-16 09:38:53 -07:00
|
|
|
namespace beast {
|
|
|
|
|
2018-09-05 21:17:53 +02:00
|
|
|
#if defined(BOOST_BEAST_USE_STD_STRING_VIEW)
|
|
|
|
/// The type of string view used by the library
|
|
|
|
using string_view = std::string_view;
|
|
|
|
|
|
|
|
/// The type of basic string view used by the library
|
|
|
|
template<class CharT, class Traits>
|
|
|
|
using basic_string_view =
|
|
|
|
std::basic_string_view<CharT, Traits>;
|
|
|
|
#else
|
|
|
|
/// The type of string view used by the library
|
|
|
|
using string_view = boost::string_view;
|
2017-06-16 09:38:53 -07:00
|
|
|
|
2018-09-05 21:17:53 +02:00
|
|
|
/// The type of basic string view used by the library
|
|
|
|
template<class CharT, class Traits>
|
|
|
|
using basic_string_view =
|
|
|
|
boost::basic_string_view<CharT, Traits>;
|
|
|
|
#endif
|
2017-06-16 09:38:53 -07:00
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
2017-06-16 21:38:17 -07:00
|
|
|
inline
|
|
|
|
char
|
|
|
|
ascii_tolower(char c)
|
|
|
|
{
|
2017-11-21 09:55:48 -08:00
|
|
|
return ((static_cast<unsigned>(c) - 65U) < 26) ?
|
|
|
|
c + 'a' - 'A' : c;
|
2017-06-16 21:38:17 -07:00
|
|
|
}
|
|
|
|
|
2017-06-16 09:38:53 -07:00
|
|
|
template<class = void>
|
|
|
|
bool
|
|
|
|
iequals(
|
2017-07-12 16:35:52 -07:00
|
|
|
beast::string_view lhs,
|
|
|
|
beast::string_view rhs)
|
2017-06-16 09:38:53 -07:00
|
|
|
{
|
|
|
|
auto n = lhs.size();
|
|
|
|
if(rhs.size() != n)
|
|
|
|
return false;
|
|
|
|
auto p1 = lhs.data();
|
|
|
|
auto p2 = rhs.data();
|
2017-06-26 10:46:17 -07:00
|
|
|
char a, b;
|
2018-12-09 09:19:01 -08:00
|
|
|
// fast loop
|
2017-06-16 09:38:53 -07:00
|
|
|
while(n--)
|
2017-06-26 10:46:17 -07:00
|
|
|
{
|
|
|
|
a = *p1++;
|
|
|
|
b = *p2++;
|
|
|
|
if(a != b)
|
2018-12-09 09:19:01 -08:00
|
|
|
goto slow;
|
2017-06-26 10:46:17 -07:00
|
|
|
}
|
2017-06-16 09:38:53 -07:00
|
|
|
return true;
|
2018-12-09 09:19:01 -08:00
|
|
|
slow:
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if(ascii_tolower(a) != ascii_tolower(b))
|
|
|
|
return false;
|
|
|
|
a = *p1++;
|
|
|
|
b = *p2++;
|
|
|
|
}
|
|
|
|
while(n--);
|
|
|
|
return true;
|
2017-06-16 09:38:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} // detail
|
|
|
|
|
|
|
|
/** Returns `true` if two strings are equal, using a case-insensitive comparison.
|
|
|
|
|
|
|
|
The case-comparison operation is defined only for low-ASCII characters.
|
|
|
|
|
|
|
|
@param lhs The string on the left side of the equality
|
|
|
|
|
|
|
|
@param rhs The string on the right side of the equality
|
|
|
|
*/
|
|
|
|
inline
|
|
|
|
bool
|
|
|
|
iequals(
|
2017-07-12 16:35:52 -07:00
|
|
|
beast::string_view lhs,
|
|
|
|
beast::string_view rhs)
|
2017-06-16 09:38:53 -07:00
|
|
|
{
|
|
|
|
return detail::iequals(lhs, rhs);
|
|
|
|
}
|
|
|
|
|
2017-06-26 10:46:17 -07:00
|
|
|
/** A case-insensitive less predicate for strings.
|
2017-06-16 09:38:53 -07:00
|
|
|
|
|
|
|
The case-comparison operation is defined only for low-ASCII characters.
|
|
|
|
*/
|
|
|
|
struct iless
|
|
|
|
{
|
|
|
|
bool
|
|
|
|
operator()(
|
2017-07-12 16:35:52 -07:00
|
|
|
string_view lhs,
|
|
|
|
string_view rhs) const
|
2017-06-16 09:38:53 -07:00
|
|
|
{
|
|
|
|
using std::begin;
|
|
|
|
using std::end;
|
|
|
|
return std::lexicographical_compare(
|
|
|
|
begin(lhs), end(lhs), begin(rhs), end(rhs),
|
2017-06-18 14:57:32 -07:00
|
|
|
[](char c1, char c2)
|
2017-06-16 09:38:53 -07:00
|
|
|
{
|
2017-06-18 14:57:32 -07:00
|
|
|
return detail::ascii_tolower(c1) < detail::ascii_tolower(c2);
|
2017-06-16 09:38:53 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-26 10:46:17 -07:00
|
|
|
/** A case-insensitive equality predicate for strings.
|
2017-06-16 09:38:53 -07:00
|
|
|
|
|
|
|
The case-comparison operation is defined only for low-ASCII characters.
|
|
|
|
*/
|
|
|
|
struct iequal
|
|
|
|
{
|
|
|
|
bool
|
|
|
|
operator()(
|
2017-07-12 16:35:52 -07:00
|
|
|
string_view lhs,
|
|
|
|
string_view rhs) const
|
2017-06-16 09:38:53 -07:00
|
|
|
{
|
|
|
|
return iequals(lhs, rhs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // beast
|
2017-07-20 13:40:34 -07:00
|
|
|
} // boost
|
2017-06-16 09:38:53 -07:00
|
|
|
|
|
|
|
#endif
|