forked from boostorg/beast
Improve performance of http::string_to_verb
`operator==` performs better than the hand-written comparison routine. fix #1636 Signed-off-by: Damian Jarek <damian.jarek93@gmail.com>
This commit is contained in:
@@ -10,82 +10,17 @@
|
||||
#ifndef BOOST_BEAST_STRING_HPP
|
||||
#define BOOST_BEAST_STRING_HPP
|
||||
|
||||
#include <boost/beast/core/detail/config.hpp>
|
||||
#include <boost/version.hpp>
|
||||
|
||||
#if defined(BOOST_BEAST_USE_STD_STRING_VIEW)
|
||||
#include <string_view>
|
||||
#else
|
||||
#include <boost/utility/string_view.hpp>
|
||||
#endif
|
||||
|
||||
#include <algorithm>
|
||||
#include <boost/beast/core/detail/string.hpp>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
|
||||
#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 string view used by the library
|
||||
using detail::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;
|
||||
|
||||
/// 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
|
||||
|
||||
namespace detail {
|
||||
|
||||
inline
|
||||
char
|
||||
ascii_tolower(char c)
|
||||
{
|
||||
return ((static_cast<unsigned>(c) - 65U) < 26) ?
|
||||
c + 'a' - 'A' : c;
|
||||
}
|
||||
|
||||
template<class = void>
|
||||
bool
|
||||
iequals(
|
||||
beast::string_view lhs,
|
||||
beast::string_view rhs)
|
||||
{
|
||||
auto n = lhs.size();
|
||||
if(rhs.size() != n)
|
||||
return false;
|
||||
auto p1 = lhs.data();
|
||||
auto p2 = rhs.data();
|
||||
char a, b;
|
||||
// fast loop
|
||||
while(n--)
|
||||
{
|
||||
a = *p1++;
|
||||
b = *p2++;
|
||||
if(a != b)
|
||||
goto slow;
|
||||
}
|
||||
return true;
|
||||
slow:
|
||||
do
|
||||
{
|
||||
if(ascii_tolower(a) != ascii_tolower(b))
|
||||
return false;
|
||||
a = *p1++;
|
||||
b = *p2++;
|
||||
}
|
||||
while(n--);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // detail
|
||||
/// The type of basic string view used by the library
|
||||
template<class CharT, class Traits>
|
||||
using basic_string_view = detail::basic_string_view<CharT, Traits>;
|
||||
|
||||
/** Returns `true` if two strings are equal, using a case-insensitive comparison.
|
||||
|
||||
@@ -95,14 +30,11 @@ slow:
|
||||
|
||||
@param rhs The string on the right side of the equality
|
||||
*/
|
||||
inline
|
||||
BOOST_BEAST_DECL
|
||||
bool
|
||||
iequals(
|
||||
beast::string_view lhs,
|
||||
beast::string_view rhs)
|
||||
{
|
||||
return detail::iequals(lhs, rhs);
|
||||
}
|
||||
beast::string_view rhs);
|
||||
|
||||
/** A case-insensitive less predicate for strings.
|
||||
|
||||
@@ -110,21 +42,11 @@ iequals(
|
||||
*/
|
||||
struct iless
|
||||
{
|
||||
BOOST_BEAST_DECL
|
||||
bool
|
||||
operator()(
|
||||
string_view lhs,
|
||||
string_view rhs) const
|
||||
{
|
||||
using std::begin;
|
||||
using std::end;
|
||||
return std::lexicographical_compare(
|
||||
begin(lhs), end(lhs), begin(rhs), end(rhs),
|
||||
[](char c1, char c2)
|
||||
{
|
||||
return detail::ascii_tolower(c1) < detail::ascii_tolower(c2);
|
||||
}
|
||||
);
|
||||
}
|
||||
string_view rhs) const;
|
||||
};
|
||||
|
||||
/** A case-insensitive equality predicate for strings.
|
||||
@@ -145,4 +67,8 @@ struct iequal
|
||||
} // beast
|
||||
} // boost
|
||||
|
||||
#ifdef BOOST_BEAST_HEADER_ONLY
|
||||
#include <boost/beast/core/impl/string.ipp>
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user