Commit fc10d0b8 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Apply clang-format to folly/**/*Range*.*

Summary: [Folly] Apply `clang-format` to `folly/**/*Range*.*`.

Reviewed By: meyering

Differential Revision: D5570553

fbshipit-source-id: 230aa8557c2d402d741aaa541f96f4634dd13d3b
parent e4c892c7
...@@ -50,8 +50,8 @@ ...@@ -50,8 +50,8 @@
#endif #endif
#include <folly/CpuId.h> #include <folly/CpuId.h>
#include <folly/Traits.h>
#include <folly/Likely.h> #include <folly/Likely.h>
#include <folly/Traits.h>
#include <folly/detail/RangeCommon.h> #include <folly/detail/RangeCommon.h>
#include <folly/detail/RangeSse42.h> #include <folly/detail/RangeSse42.h>
...@@ -61,7 +61,8 @@ FOLLY_GCC_DISABLE_WARNING("-Wshadow") ...@@ -61,7 +61,8 @@ FOLLY_GCC_DISABLE_WARNING("-Wshadow")
namespace folly { namespace folly {
template <class Iter> class Range; template <class Iter>
class Range;
/** /**
* Finds the first occurrence of needle in haystack. The algorithm is on * Finds the first occurrence of needle in haystack. The algorithm is on
...@@ -72,9 +73,8 @@ template <class Iter> class Range; ...@@ -72,9 +73,8 @@ template <class Iter> class Range;
template < template <
class Iter, class Iter,
class Comp = std::equal_to<typename Range<Iter>::value_type>> class Comp = std::equal_to<typename Range<Iter>::value_type>>
inline size_t qfind(const Range<Iter> & haystack, inline size_t
const Range<Iter> & needle, qfind(const Range<Iter>& haystack, const Range<Iter>& needle, Comp eq = Comp());
Comp eq = Comp());
/** /**
* Finds the first occurrence of needle in haystack. The result is the * Finds the first occurrence of needle in haystack. The result is the
...@@ -82,8 +82,9 @@ inline size_t qfind(const Range<Iter> & haystack, ...@@ -82,8 +82,9 @@ inline size_t qfind(const Range<Iter> & haystack,
* needle wasn't found. * needle wasn't found.
*/ */
template <class Iter> template <class Iter>
size_t qfind(const Range<Iter> & haystack, size_t qfind(
const typename Range<Iter>::value_type& needle); const Range<Iter>& haystack,
const typename Range<Iter>::value_type& needle);
/** /**
* Finds the last occurrence of needle in haystack. The result is the * Finds the last occurrence of needle in haystack. The result is the
...@@ -91,17 +92,18 @@ size_t qfind(const Range<Iter> & haystack, ...@@ -91,17 +92,18 @@ size_t qfind(const Range<Iter> & haystack,
* needle wasn't found. * needle wasn't found.
*/ */
template <class Iter> template <class Iter>
size_t rfind(const Range<Iter> & haystack, size_t rfind(
const typename Range<Iter>::value_type& needle); const Range<Iter>& haystack,
const typename Range<Iter>::value_type& needle);
/** /**
* Finds the first occurrence of any element of needle in * Finds the first occurrence of any element of needle in
* haystack. The algorithm is O(haystack.size() * needle.size()). * haystack. The algorithm is O(haystack.size() * needle.size()).
*/ */
template <class Iter> template <class Iter>
inline size_t qfind_first_of(const Range<Iter> & haystack, inline size_t qfind_first_of(
const Range<Iter> & needle); const Range<Iter>& haystack,
const Range<Iter>& needle);
/** /**
* Small internal helper - returns the value just before an iterator. * Small internal helper - returns the value just before an iterator.
...@@ -113,9 +115,10 @@ namespace detail { ...@@ -113,9 +115,10 @@ namespace detail {
*/ */
template <class Iter> template <class Iter>
typename std::enable_if< typename std::enable_if<
std::is_same<typename std::iterator_traits<Iter>::iterator_category, std::is_same<
std::random_access_iterator_tag>::value, typename std::iterator_traits<Iter>::iterator_category,
typename std::iterator_traits<Iter>::reference>::type std::random_access_iterator_tag>::value,
typename std::iterator_traits<Iter>::reference>::type
value_before(Iter i) { value_before(Iter i) {
return i[-1]; return i[-1];
} }
...@@ -125,9 +128,10 @@ value_before(Iter i) { ...@@ -125,9 +128,10 @@ value_before(Iter i) {
*/ */
template <class Iter> template <class Iter>
typename std::enable_if< typename std::enable_if<
!std::is_same<typename std::iterator_traits<Iter>::iterator_category, !std::is_same<
std::random_access_iterator_tag>::value, typename std::iterator_traits<Iter>::iterator_category,
typename std::iterator_traits<Iter>::reference>::type std::random_access_iterator_tag>::value,
typename std::iterator_traits<Iter>::reference>::type
value_before(Iter i) { value_before(Iter i) {
return *--i; return *--i;
} }
...@@ -136,7 +140,8 @@ value_before(Iter i) { ...@@ -136,7 +140,8 @@ value_before(Iter i) {
* Use IsCharPointer<T>::type to enable const char* or char*. * Use IsCharPointer<T>::type to enable const char* or char*.
* Use IsCharPointer<T>::const_type to enable only const char*. * Use IsCharPointer<T>::const_type to enable only const char*.
*/ */
template <class T> struct IsCharPointer {}; template <class T>
struct IsCharPointer {};
template <> template <>
struct IsCharPointer<char*> { struct IsCharPointer<char*> {
...@@ -163,14 +168,13 @@ struct IsCharPointer<const char*> { ...@@ -163,14 +168,13 @@ struct IsCharPointer<const char*> {
* wouldn't.) * wouldn't.)
*/ */
template <class Iter> template <class Iter>
class Range : private boost::totally_ordered<Range<Iter> > { class Range : private boost::totally_ordered<Range<Iter>> {
public: public:
typedef std::size_t size_type; typedef std::size_t size_type;
typedef Iter iterator; typedef Iter iterator;
typedef Iter const_iterator; typedef Iter const_iterator;
typedef typename std::remove_reference< typedef typename std::remove_reference<
typename std::iterator_traits<Iter>::reference>::type typename std::iterator_traits<Iter>::reference>::type value_type;
value_type;
using difference_type = typename std::iterator_traits<Iter>::difference_type; using difference_type = typename std::iterator_traits<Iter>::difference_type;
typedef typename std::iterator_traits<Iter>::reference reference; typedef typename std::iterator_traits<Iter>::reference reference;
...@@ -181,35 +185,32 @@ class Range : private boost::totally_ordered<Range<Iter> > { ...@@ -181,35 +185,32 @@ class Range : private boost::totally_ordered<Range<Iter> > {
* args which are const. * args which are const.
*/ */
typedef typename std::conditional< typedef typename std::conditional<
std::is_same<Iter, char*>::value std::is_same<Iter, char*>::value ||
|| std::is_same<Iter, unsigned char*>::value, std::is_same<Iter, unsigned char*>::value,
Range<const value_type*>, Range<const value_type*>,
Range<Iter>>::type const_range_type; Range<Iter>>::type const_range_type;
typedef std::char_traits<typename std::remove_const<value_type>::type> typedef std::char_traits<typename std::remove_const<value_type>::type>
traits_type; traits_type;
static const size_type npos; static const size_type npos;
// Works for all iterators // Works for all iterators
constexpr Range() : b_(), e_() { constexpr Range() : b_(), e_() {}
}
constexpr Range(const Range&) = default; constexpr Range(const Range&) = default;
constexpr Range(Range&&) = default; constexpr Range(Range&&) = default;
public: public:
// Works for all iterators // Works for all iterators
constexpr Range(Iter start, Iter end) : b_(start), e_(end) { constexpr Range(Iter start, Iter end) : b_(start), e_(end) {}
}
// Works only for random-access iterators // Works only for random-access iterators
constexpr Range(Iter start, size_t size) constexpr Range(Iter start, size_t size) : b_(start), e_(start + size) {}
: b_(start), e_(start + size) { }
# if !__clang__ || __CLANG_PREREQ(3, 7) // Clang 3.6 crashes on this line #if !__clang__ || __CLANG_PREREQ(3, 7) // Clang 3.6 crashes on this line
/* implicit */ Range(std::nullptr_t) = delete; /* implicit */ Range(std::nullptr_t) = delete;
# endif #endif
template <class T = Iter, typename detail::IsCharPointer<T>::type = 0> template <class T = Iter, typename detail::IsCharPointer<T>::type = 0>
constexpr /* implicit */ Range(Iter str) constexpr /* implicit */ Range(Iter str)
...@@ -229,9 +230,10 @@ class Range : private boost::totally_ordered<Range<Iter> > { ...@@ -229,9 +230,10 @@ class Range : private boost::totally_ordered<Range<Iter> > {
} }
template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0> template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0>
Range(const std::string& str, Range(
std::string::size_type startFrom, const std::string& str,
std::string::size_type size) { std::string::size_type startFrom,
std::string::size_type size) {
if (UNLIKELY(startFrom > str.size())) { if (UNLIKELY(startFrom > str.size())) {
std::__throw_out_of_range("index out of range"); std::__throw_out_of_range("index out of range");
} }
...@@ -243,15 +245,12 @@ class Range : private boost::totally_ordered<Range<Iter> > { ...@@ -243,15 +245,12 @@ class Range : private boost::totally_ordered<Range<Iter> > {
} }
} }
Range(const Range& other, Range(const Range& other, size_type first, size_type length = npos)
size_type first, : Range(other.subpiece(first, length)) {}
size_type length = npos)
: Range(other.subpiece(first, length))
{ }
template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0> template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0>
/* implicit */ Range(const fbstring& str) /* implicit */ Range(const fbstring& str)
: b_(str.data()), e_(b_ + str.size()) { } : b_(str.data()), e_(b_ + str.size()) {}
template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0> template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0>
Range(const fbstring& str, fbstring::size_type startFrom) { Range(const fbstring& str, fbstring::size_type startFrom) {
...@@ -263,8 +262,10 @@ class Range : private boost::totally_ordered<Range<Iter> > { ...@@ -263,8 +262,10 @@ class Range : private boost::totally_ordered<Range<Iter> > {
} }
template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0> template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0>
Range(const fbstring& str, fbstring::size_type startFrom, Range(
fbstring::size_type size) { const fbstring& str,
fbstring::size_type startFrom,
fbstring::size_type size) {
if (UNLIKELY(startFrom > str.size())) { if (UNLIKELY(startFrom > str.size())) {
std::__throw_out_of_range("index out of range"); std::__throw_out_of_range("index out of range");
} }
...@@ -280,60 +281,70 @@ class Range : private boost::totally_ordered<Range<Iter> > { ...@@ -280,60 +281,70 @@ class Range : private boost::totally_ordered<Range<Iter> > {
// Range<const unsigned char*> (aka ByteRange), as they're both frequently // Range<const unsigned char*> (aka ByteRange), as they're both frequently
// used to represent ranges of bytes. Allow explicit conversion in the other // used to represent ranges of bytes. Allow explicit conversion in the other
// direction. // direction.
template <class OtherIter, typename std::enable_if< template <
(std::is_same<Iter, const unsigned char*>::value && class OtherIter,
(std::is_same<OtherIter, const char*>::value || typename std::enable_if<
std::is_same<OtherIter, char*>::value)), int>::type = 0> (std::is_same<Iter, const unsigned char*>::value &&
(std::is_same<OtherIter, const char*>::value ||
std::is_same<OtherIter, char*>::value)),
int>::type = 0>
/* implicit */ Range(const Range<OtherIter>& other) /* implicit */ Range(const Range<OtherIter>& other)
: b_(reinterpret_cast<const unsigned char*>(other.begin())), : b_(reinterpret_cast<const unsigned char*>(other.begin())),
e_(reinterpret_cast<const unsigned char*>(other.end())) { e_(reinterpret_cast<const unsigned char*>(other.end())) {}
}
template <class OtherIter, typename std::enable_if< template <
(std::is_same<Iter, unsigned char*>::value && class OtherIter,
std::is_same<OtherIter, char*>::value), int>::type = 0> typename std::enable_if<
(std::is_same<Iter, unsigned char*>::value &&
std::is_same<OtherIter, char*>::value),
int>::type = 0>
/* implicit */ Range(const Range<OtherIter>& other) /* implicit */ Range(const Range<OtherIter>& other)
: b_(reinterpret_cast<unsigned char*>(other.begin())), : b_(reinterpret_cast<unsigned char*>(other.begin())),
e_(reinterpret_cast<unsigned char*>(other.end())) { e_(reinterpret_cast<unsigned char*>(other.end())) {}
}
template <class OtherIter, typename std::enable_if< template <
(std::is_same<Iter, const char*>::value && class OtherIter,
(std::is_same<OtherIter, const unsigned char*>::value || typename std::enable_if<
std::is_same<OtherIter, unsigned char*>::value)), int>::type = 0> (std::is_same<Iter, const char*>::value &&
(std::is_same<OtherIter, const unsigned char*>::value ||
std::is_same<OtherIter, unsigned char*>::value)),
int>::type = 0>
explicit Range(const Range<OtherIter>& other) explicit Range(const Range<OtherIter>& other)
: b_(reinterpret_cast<const char*>(other.begin())), : b_(reinterpret_cast<const char*>(other.begin())),
e_(reinterpret_cast<const char*>(other.end())) { e_(reinterpret_cast<const char*>(other.end())) {}
}
template <class OtherIter, typename std::enable_if< template <
(std::is_same<Iter, char*>::value && class OtherIter,
std::is_same<OtherIter, unsigned char*>::value), int>::type = 0> typename std::enable_if<
(std::is_same<Iter, char*>::value &&
std::is_same<OtherIter, unsigned char*>::value),
int>::type = 0>
explicit Range(const Range<OtherIter>& other) explicit Range(const Range<OtherIter>& other)
: b_(reinterpret_cast<char*>(other.begin())), : b_(reinterpret_cast<char*>(other.begin())),
e_(reinterpret_cast<char*>(other.end())) { e_(reinterpret_cast<char*>(other.end())) {}
}
// Allow implicit conversion from Range<From> to Range<To> if From is // Allow implicit conversion from Range<From> to Range<To> if From is
// implicitly convertible to To. // implicitly convertible to To.
template <class OtherIter, typename std::enable_if< template <
(!std::is_same<Iter, OtherIter>::value && class OtherIter,
std::is_convertible<OtherIter, Iter>::value), int>::type = 0> typename std::enable_if<
(!std::is_same<Iter, OtherIter>::value &&
std::is_convertible<OtherIter, Iter>::value),
int>::type = 0>
constexpr /* implicit */ Range(const Range<OtherIter>& other) constexpr /* implicit */ Range(const Range<OtherIter>& other)
: b_(other.begin()), : b_(other.begin()), e_(other.end()) {}
e_(other.end()) {
}
// Allow explicit conversion from Range<From> to Range<To> if From is // Allow explicit conversion from Range<From> to Range<To> if From is
// explicitly convertible to To. // explicitly convertible to To.
template <class OtherIter, typename std::enable_if< template <
(!std::is_same<Iter, OtherIter>::value && class OtherIter,
!std::is_convertible<OtherIter, Iter>::value && typename std::enable_if<
std::is_constructible<Iter, const OtherIter&>::value), int>::type = 0> (!std::is_same<Iter, OtherIter>::value &&
!std::is_convertible<OtherIter, Iter>::value &&
std::is_constructible<Iter, const OtherIter&>::value),
int>::type = 0>
constexpr explicit Range(const Range<OtherIter>& other) constexpr explicit Range(const Range<OtherIter>& other)
: b_(other.begin()), : b_(other.begin()), e_(other.end()) {}
e_(other.end()) {
}
/** /**
* Allow explicit construction of Range() from a std::array of a * Allow explicit construction of Range() from a std::array of a
...@@ -435,11 +446,19 @@ class Range : private boost::totally_ordered<Range<Iter> > { ...@@ -435,11 +446,19 @@ class Range : private boost::totally_ordered<Range<Iter> > {
return detail::value_before(e_); return detail::value_before(e_);
} }
// Works only for Range<const char*> and Range<char*> // Works only for Range<const char*> and Range<char*>
std::string str() const { return std::string(b_, size()); } std::string str() const {
std::string toString() const { return str(); } return std::string(b_, size());
}
std::string toString() const {
return str();
}
// Works only for Range<const char*> and Range<char*> // Works only for Range<const char*> and Range<char*>
fbstring fbstr() const { return fbstring(b_, size()); } fbstring fbstr() const {
fbstring toFbstring() const { return fbstr(); } return fbstring(b_, size());
}
fbstring toFbstring() const {
return fbstr();
}
const_range_type castToConst() const { const_range_type castToConst() const {
return const_range_type(*this); return const_range_type(*this);
...@@ -455,8 +474,9 @@ class Range : private boost::totally_ordered<Range<Iter> > { ...@@ -455,8 +474,9 @@ class Range : private boost::totally_ordered<Range<Iter> > {
// We check the signed bit of the subtraction and bit shift it // We check the signed bit of the subtraction and bit shift it
// to produce either 0 or 2. The subtraction yields the // to produce either 0 or 2. The subtraction yields the
// comparison values of either -1 or 1. // comparison values of either -1 or 1.
r = (static_cast<int>( r = (static_cast<int>((osize - tsize) >> (CHAR_BIT * sizeof(size_t) - 1))
(osize - tsize) >> (CHAR_BIT * sizeof(size_t) - 1)) << 1) - 1; << 1) -
1;
} }
return r; return r;
} }
...@@ -472,12 +492,16 @@ class Range : private boost::totally_ordered<Range<Iter> > { ...@@ -472,12 +492,16 @@ class Range : private boost::totally_ordered<Range<Iter> > {
} }
value_type& at(size_t i) { value_type& at(size_t i) {
if (i >= size()) std::__throw_out_of_range("index out of range"); if (i >= size()) {
std::__throw_out_of_range("index out of range");
}
return b_[i]; return b_[i];
} }
const value_type& at(size_t i) const { const value_type& at(size_t i) const {
if (i >= size()) std::__throw_out_of_range("index out of range"); if (i >= size()) {
std::__throw_out_of_range("index out of range");
}
return b_[i]; return b_[i];
} }
...@@ -564,13 +588,17 @@ class Range : private boost::totally_ordered<Range<Iter> > { ...@@ -564,13 +588,17 @@ class Range : private boost::totally_ordered<Range<Iter> > {
} }
size_type find(const_range_type str, size_t pos) const { size_type find(const_range_type str, size_t pos) const {
if (pos > size()) return std::string::npos; if (pos > size()) {
return std::string::npos;
}
size_t ret = qfind(castToConst().subpiece(pos), str); size_t ret = qfind(castToConst().subpiece(pos), str);
return ret == npos ? ret : ret + pos; return ret == npos ? ret : ret + pos;
} }
size_type find(Iter s, size_t pos, size_t n) const { size_type find(Iter s, size_t pos, size_t n) const {
if (pos > size()) return std::string::npos; if (pos > size()) {
return std::string::npos;
}
auto forFinding = castToConst(); auto forFinding = castToConst();
size_t ret = qfind( size_t ret = qfind(
pos ? forFinding.subpiece(pos) : forFinding, const_range_type(s, n)); pos ? forFinding.subpiece(pos) : forFinding, const_range_type(s, n));
...@@ -584,7 +612,9 @@ class Range : private boost::totally_ordered<Range<Iter> > { ...@@ -584,7 +612,9 @@ class Range : private boost::totally_ordered<Range<Iter> > {
// Works only for Range<(const) (unsigned) char*> which have Range(Iter) ctor // Works only for Range<(const) (unsigned) char*> which have Range(Iter) ctor
size_type find(const Iter s, size_t pos) const { size_type find(const Iter s, size_t pos) const {
if (pos > size()) return std::string::npos; if (pos > size()) {
return std::string::npos;
}
size_type ret = qfind(castToConst().subpiece(pos), const_range_type(s)); size_type ret = qfind(castToConst().subpiece(pos), const_range_type(s));
return ret == npos ? ret : ret + pos; return ret == npos ? ret : ret + pos;
} }
...@@ -598,7 +628,9 @@ class Range : private boost::totally_ordered<Range<Iter> > { ...@@ -598,7 +628,9 @@ class Range : private boost::totally_ordered<Range<Iter> > {
} }
size_type find(value_type c, size_t pos) const { size_type find(value_type c, size_t pos) const {
if (pos > size()) return std::string::npos; if (pos > size()) {
return std::string::npos;
}
size_type ret = qfind(castToConst().subpiece(pos), c); size_type ret = qfind(castToConst().subpiece(pos), c);
return ret == npos ? ret : ret + pos; return ret == npos ? ret : ret + pos;
} }
...@@ -608,7 +640,9 @@ class Range : private boost::totally_ordered<Range<Iter> > { ...@@ -608,7 +640,9 @@ class Range : private boost::totally_ordered<Range<Iter> > {
} }
size_type find_first_of(const_range_type needles, size_t pos) const { size_type find_first_of(const_range_type needles, size_t pos) const {
if (pos > size()) return std::string::npos; if (pos > size()) {
return std::string::npos;
}
size_type ret = qfind_first_of(castToConst().subpiece(pos), needles); size_type ret = qfind_first_of(castToConst().subpiece(pos), needles);
return ret == npos ? ret : ret + pos; return ret == npos ? ret : ret + pos;
} }
...@@ -657,8 +691,8 @@ class Range : private boost::totally_ordered<Range<Iter> > { ...@@ -657,8 +691,8 @@ class Range : private boost::totally_ordered<Range<Iter> > {
* Does this Range start with another range? * Does this Range start with another range?
*/ */
bool startsWith(const const_range_type& other) const { bool startsWith(const const_range_type& other) const {
return size() >= other.size() return size() >= other.size() &&
&& castToConst().subpiece(0, other.size()) == other; castToConst().subpiece(0, other.size()) == other;
} }
bool startsWith(value_type c) const { bool startsWith(value_type c) const {
return !empty() && front() == c; return !empty() && front() == c;
...@@ -678,8 +712,8 @@ class Range : private boost::totally_ordered<Range<Iter> > { ...@@ -678,8 +712,8 @@ class Range : private boost::totally_ordered<Range<Iter> > {
* Does this Range end with another range? * Does this Range end with another range?
*/ */
bool endsWith(const const_range_type& other) const { bool endsWith(const const_range_type& other) const {
return size() >= other.size() return size() >= other.size() &&
&& castToConst().subpiece(size() - other.size()) == other; castToConst().subpiece(size() - other.size()) == other;
} }
bool endsWith(value_type c) const { bool endsWith(value_type c) const {
return !empty() && back() == c; return !empty() && back() == c;
...@@ -915,14 +949,16 @@ class Range : private boost::totally_ordered<Range<Iter> > { ...@@ -915,14 +949,16 @@ class Range : private boost::totally_ordered<Range<Iter> > {
* @author: Marcelo Juchem <marcelo@fb.com> * @author: Marcelo Juchem <marcelo@fb.com>
*/ */
template <typename TProcess, typename... Args> template <typename TProcess, typename... Args>
auto split_step(value_type delimiter, TProcess &&process, Args &&...args) auto split_step(value_type delimiter, TProcess&& process, Args&&... args)
-> decltype(process(std::declval<Range>(), std::forward<Args>(args)...)) -> decltype(process(std::declval<Range>(), std::forward<Args>(args)...)) {
{ return process(split_step(delimiter), std::forward<Args>(args)...); } return process(split_step(delimiter), std::forward<Args>(args)...);
}
template <typename TProcess, typename... Args> template <typename TProcess, typename... Args>
auto split_step(Range delimiter, TProcess &&process, Args &&...args) auto split_step(Range delimiter, TProcess&& process, Args&&... args)
-> decltype(process(std::declval<Range>(), std::forward<Args>(args)...)) -> decltype(process(std::declval<Range>(), std::forward<Args>(args)...)) {
{ return process(split_step(delimiter), std::forward<Args>(args)...); } return process(split_step(delimiter), std::forward<Args>(args)...);
}
private: private:
Iter b_, e_; Iter b_, e_;
...@@ -1010,12 +1046,10 @@ namespace detail { ...@@ -1010,12 +1046,10 @@ namespace detail {
template <class A, class B> template <class A, class B>
struct ComparableAsStringPiece { struct ComparableAsStringPiece {
enum { enum {
value = value = (std::is_convertible<A, StringPiece>::value &&
(std::is_convertible<A, StringPiece>::value std::is_same<B, StringPiece>::value) ||
&& std::is_same<B, StringPiece>::value) (std::is_convertible<B, StringPiece>::value &&
|| std::is_same<A, StringPiece>::value)
(std::is_convertible<B, StringPiece>::value
&& std::is_same<A, StringPiece>::value)
}; };
}; };
...@@ -1025,8 +1059,7 @@ struct ComparableAsStringPiece { ...@@ -1025,8 +1059,7 @@ struct ComparableAsStringPiece {
* operator== through conversion for Range<const char*> * operator== through conversion for Range<const char*>
*/ */
template <class T, class U> template <class T, class U>
typename _t<std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>>
std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>::type
operator==(const T& lhs, const U& rhs) { operator==(const T& lhs, const U& rhs) {
return StringPiece(lhs) == StringPiece(rhs); return StringPiece(lhs) == StringPiece(rhs);
} }
...@@ -1035,8 +1068,7 @@ operator==(const T& lhs, const U& rhs) { ...@@ -1035,8 +1068,7 @@ operator==(const T& lhs, const U& rhs) {
* operator< through conversion for Range<const char*> * operator< through conversion for Range<const char*>
*/ */
template <class T, class U> template <class T, class U>
typename _t<std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>>
std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>::type
operator<(const T& lhs, const U& rhs) { operator<(const T& lhs, const U& rhs) {
return StringPiece(lhs) < StringPiece(rhs); return StringPiece(lhs) < StringPiece(rhs);
} }
...@@ -1045,8 +1077,7 @@ operator<(const T& lhs, const U& rhs) { ...@@ -1045,8 +1077,7 @@ operator<(const T& lhs, const U& rhs) {
* operator> through conversion for Range<const char*> * operator> through conversion for Range<const char*>
*/ */
template <class T, class U> template <class T, class U>
typename _t<std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>>
std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>::type
operator>(const T& lhs, const U& rhs) { operator>(const T& lhs, const U& rhs) {
return StringPiece(lhs) > StringPiece(rhs); return StringPiece(lhs) > StringPiece(rhs);
} }
...@@ -1055,8 +1086,7 @@ operator>(const T& lhs, const U& rhs) { ...@@ -1055,8 +1086,7 @@ operator>(const T& lhs, const U& rhs) {
* operator< through conversion for Range<const char*> * operator< through conversion for Range<const char*>
*/ */
template <class T, class U> template <class T, class U>
typename _t<std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>>
std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>::type
operator<=(const T& lhs, const U& rhs) { operator<=(const T& lhs, const U& rhs) {
return StringPiece(lhs) <= StringPiece(rhs); return StringPiece(lhs) <= StringPiece(rhs);
} }
...@@ -1065,8 +1095,7 @@ operator<=(const T& lhs, const U& rhs) { ...@@ -1065,8 +1095,7 @@ operator<=(const T& lhs, const U& rhs) {
* operator> through conversion for Range<const char*> * operator> through conversion for Range<const char*>
*/ */
template <class T, class U> template <class T, class U>
typename _t<std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>>
std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>::type
operator>=(const T& lhs, const U& rhs) { operator>=(const T& lhs, const U& rhs) {
return StringPiece(lhs) >= StringPiece(rhs); return StringPiece(lhs) >= StringPiece(rhs);
} }
...@@ -1075,16 +1104,16 @@ operator>=(const T& lhs, const U& rhs) { ...@@ -1075,16 +1104,16 @@ operator>=(const T& lhs, const U& rhs) {
* Finds substrings faster than brute force by borrowing from Boyer-Moore * Finds substrings faster than brute force by borrowing from Boyer-Moore
*/ */
template <class Iter, class Comp> template <class Iter, class Comp>
size_t qfind(const Range<Iter>& haystack, size_t qfind(const Range<Iter>& haystack, const Range<Iter>& needle, Comp eq) {
const Range<Iter>& needle,
Comp eq) {
// Don't use std::search, use a Boyer-Moore-like trick by comparing // Don't use std::search, use a Boyer-Moore-like trick by comparing
// the last characters first // the last characters first
auto const nsize = needle.size(); auto const nsize = needle.size();
if (haystack.size() < nsize) { if (haystack.size() < nsize) {
return std::string::npos; return std::string::npos;
} }
if (!nsize) return 0; if (!nsize) {
return 0;
}
auto const nsize_1 = nsize - 1; auto const nsize_1 = nsize - 1;
auto const lastNeedle = needle[nsize_1]; auto const lastNeedle = needle[nsize_1];
...@@ -1106,7 +1135,7 @@ size_t qfind(const Range<Iter>& haystack, ...@@ -1106,7 +1135,7 @@ size_t qfind(const Range<Iter>& haystack,
} }
// Here we know that the last char matches // Here we know that the last char matches
// Continue in pedestrian mode // Continue in pedestrian mode
for (size_t j = 0; ; ) { for (size_t j = 0;;) {
assert(j < nsize); assert(j < nsize);
if (!eq(i[j], needle[j])) { if (!eq(i[j], needle[j])) {
// Not found, we can skip // Not found, we can skip
...@@ -1132,23 +1161,24 @@ size_t qfind(const Range<Iter>& haystack, ...@@ -1132,23 +1161,24 @@ size_t qfind(const Range<Iter>& haystack,
namespace detail { namespace detail {
inline size_t qfind_first_byte_of(const StringPiece haystack, inline size_t qfind_first_byte_of(
const StringPiece needles) { const StringPiece haystack,
static auto const qfind_first_byte_of_fn = const StringPiece needles) {
folly::CpuId().sse42() ? qfind_first_byte_of_sse42 static auto const qfind_first_byte_of_fn = folly::CpuId().sse42()
: qfind_first_byte_of_nosse; ? qfind_first_byte_of_sse42
: qfind_first_byte_of_nosse;
return qfind_first_byte_of_fn(haystack, needles); return qfind_first_byte_of_fn(haystack, needles);
} }
} // namespace detail } // namespace detail
template <class Iter, class Comp> template <class Iter, class Comp>
size_t qfind_first_of(const Range<Iter> & haystack, size_t qfind_first_of(
const Range<Iter> & needles, const Range<Iter>& haystack,
Comp eq) { const Range<Iter>& needles,
auto ret = std::find_first_of(haystack.begin(), haystack.end(), Comp eq) {
needles.begin(), needles.end(), auto ret = std::find_first_of(
eq); haystack.begin(), haystack.end(), needles.begin(), needles.end(), eq);
return ret == haystack.end() ? std::string::npos : ret - haystack.begin(); return ret == haystack.end() ? std::string::npos : ret - haystack.begin();
} }
...@@ -1166,24 +1196,30 @@ struct AsciiCaseSensitive { ...@@ -1166,24 +1196,30 @@ struct AsciiCaseSensitive {
struct AsciiCaseInsensitive { struct AsciiCaseInsensitive {
bool operator()(char lhs, char rhs) const { bool operator()(char lhs, char rhs) const {
char k = lhs ^ rhs; char k = lhs ^ rhs;
if (k == 0) return true; if (k == 0) {
if (k != 32) return false; return true;
}
if (k != 32) {
return false;
}
k = lhs | rhs; k = lhs | rhs;
return (k >= 'a' && k <= 'z'); return (k >= 'a' && k <= 'z');
} }
}; };
template <class Iter> template <class Iter>
size_t qfind(const Range<Iter>& haystack, size_t qfind(
const typename Range<Iter>::value_type& needle) { const Range<Iter>& haystack,
const typename Range<Iter>::value_type& needle) {
auto pos = std::find(haystack.begin(), haystack.end(), needle); auto pos = std::find(haystack.begin(), haystack.end(), needle);
return pos == haystack.end() ? std::string::npos : pos - haystack.data(); return pos == haystack.end() ? std::string::npos : pos - haystack.data();
} }
template <class Iter> template <class Iter>
size_t rfind(const Range<Iter>& haystack, size_t rfind(
const typename Range<Iter>::value_type& needle) { const Range<Iter>& haystack,
for (auto i = haystack.size(); i-- > 0; ) { const typename Range<Iter>::value_type& needle) {
for (auto i = haystack.size(); i-- > 0;) {
if (haystack[i] == needle) { if (haystack[i] == needle) {
return i; return i;
} }
...@@ -1199,7 +1235,7 @@ inline size_t qfind(const Range<const char*>& haystack, const char& needle) { ...@@ -1199,7 +1235,7 @@ inline size_t qfind(const Range<const char*>& haystack, const char& needle) {
return std::string::npos; return std::string::npos;
} }
auto pos = static_cast<const char*>( auto pos = static_cast<const char*>(
::memchr(haystack.data(), needle, haystack.size())); ::memchr(haystack.data(), needle, haystack.size()));
return pos == nullptr ? std::string::npos : pos - haystack.data(); return pos == nullptr ? std::string::npos : pos - haystack.data();
} }
...@@ -1210,62 +1246,66 @@ inline size_t rfind(const Range<const char*>& haystack, const char& needle) { ...@@ -1210,62 +1246,66 @@ inline size_t rfind(const Range<const char*>& haystack, const char& needle) {
return std::string::npos; return std::string::npos;
} }
auto pos = static_cast<const char*>( auto pos = static_cast<const char*>(
::memrchr(haystack.data(), needle, haystack.size())); ::memrchr(haystack.data(), needle, haystack.size()));
return pos == nullptr ? std::string::npos : pos - haystack.data(); return pos == nullptr ? std::string::npos : pos - haystack.data();
} }
// specialization for ByteRange // specialization for ByteRange
template <> template <>
inline size_t qfind(const Range<const unsigned char*>& haystack, inline size_t qfind(
const unsigned char& needle) { const Range<const unsigned char*>& haystack,
const unsigned char& needle) {
// memchr expects a not-null pointer, early return if the range is empty. // memchr expects a not-null pointer, early return if the range is empty.
if (haystack.empty()) { if (haystack.empty()) {
return std::string::npos; return std::string::npos;
} }
auto pos = static_cast<const unsigned char*>( auto pos = static_cast<const unsigned char*>(
::memchr(haystack.data(), needle, haystack.size())); ::memchr(haystack.data(), needle, haystack.size()));
return pos == nullptr ? std::string::npos : pos - haystack.data(); return pos == nullptr ? std::string::npos : pos - haystack.data();
} }
template <> template <>
inline size_t rfind(const Range<const unsigned char*>& haystack, inline size_t rfind(
const unsigned char& needle) { const Range<const unsigned char*>& haystack,
const unsigned char& needle) {
// memchr expects a not-null pointer, early return if the range is empty. // memchr expects a not-null pointer, early return if the range is empty.
if (haystack.empty()) { if (haystack.empty()) {
return std::string::npos; return std::string::npos;
} }
auto pos = static_cast<const unsigned char*>( auto pos = static_cast<const unsigned char*>(
::memrchr(haystack.data(), needle, haystack.size())); ::memrchr(haystack.data(), needle, haystack.size()));
return pos == nullptr ? std::string::npos : pos - haystack.data(); return pos == nullptr ? std::string::npos : pos - haystack.data();
} }
template <class Iter> template <class Iter>
size_t qfind_first_of(const Range<Iter>& haystack, size_t qfind_first_of(const Range<Iter>& haystack, const Range<Iter>& needles) {
const Range<Iter>& needles) {
return qfind_first_of(haystack, needles, AsciiCaseSensitive()); return qfind_first_of(haystack, needles, AsciiCaseSensitive());
} }
// specialization for StringPiece // specialization for StringPiece
template <> template <>
inline size_t qfind_first_of(const Range<const char*>& haystack, inline size_t qfind_first_of(
const Range<const char*>& needles) { const Range<const char*>& haystack,
const Range<const char*>& needles) {
return detail::qfind_first_byte_of(haystack, needles); return detail::qfind_first_byte_of(haystack, needles);
} }
// specialization for ByteRange // specialization for ByteRange
template <> template <>
inline size_t qfind_first_of(const Range<const unsigned char*>& haystack, inline size_t qfind_first_of(
const Range<const unsigned char*>& needles) { const Range<const unsigned char*>& haystack,
return detail::qfind_first_byte_of(StringPiece(haystack), const Range<const unsigned char*>& needles) {
StringPiece(needles)); return detail::qfind_first_byte_of(
StringPiece(haystack), StringPiece(needles));
} }
template <class Key, class Enable> template <class Key, class Enable>
struct hasher; struct hasher;
template <class T> template <class T>
struct hasher<folly::Range<T*>, struct hasher<
typename std::enable_if<std::is_pod<T>::value, void>::type> { folly::Range<T*>,
typename std::enable_if<std::is_pod<T>::value, void>::type> {
size_t operator()(folly::Range<T*> r) const { size_t operator()(folly::Range<T*> r) const {
return hash::SpookyHashV2::Hash64(r.begin(), r.size() * sizeof(T), 0); return hash::SpookyHashV2::Hash64(r.begin(), r.size() * sizeof(T), 0);
} }
...@@ -1274,12 +1314,15 @@ struct hasher<folly::Range<T*>, ...@@ -1274,12 +1314,15 @@ struct hasher<folly::Range<T*>,
/** /**
* Ubiquitous helper template for knowing what's a string * Ubiquitous helper template for knowing what's a string
*/ */
template <class T> struct IsSomeString { template <class T>
enum { value = std::is_same<T, std::string>::value struct IsSomeString {
|| std::is_same<T, fbstring>::value }; enum {
value =
std::is_same<T, std::string>::value || std::is_same<T, fbstring>::value
};
}; };
} // !namespace folly } // namespace folly
FOLLY_POP_WARNING FOLLY_POP_WARNING
......
...@@ -24,8 +24,9 @@ namespace folly { ...@@ -24,8 +24,9 @@ namespace folly {
namespace detail { namespace detail {
size_t qfind_first_byte_of_bitset(const StringPieceLite haystack, size_t qfind_first_byte_of_bitset(
const StringPieceLite needles) { const StringPieceLite haystack,
const StringPieceLite needles) {
std::bitset<256> s; std::bitset<256> s;
for (auto needle : needles) { for (auto needle : needles) {
s[(uint8_t)needle] = true; s[(uint8_t)needle] = true;
...@@ -38,10 +39,11 @@ size_t qfind_first_byte_of_bitset(const StringPieceLite haystack, ...@@ -38,10 +39,11 @@ size_t qfind_first_byte_of_bitset(const StringPieceLite haystack,
return std::string::npos; return std::string::npos;
} }
size_t qfind_first_byte_of_byteset(const StringPieceLite haystack, size_t qfind_first_byte_of_byteset(
const StringPieceLite needles) { const StringPieceLite haystack,
const StringPieceLite needles) {
SparseByteSet s; SparseByteSet s;
for (auto needle: needles) { for (auto needle : needles) {
s.add(uint8_t(needle)); s.add(uint8_t(needle));
} }
for (size_t index = 0; index < haystack.size(); ++index) { for (size_t index = 0; index < haystack.size(); ++index) {
...@@ -51,7 +53,5 @@ size_t qfind_first_byte_of_byteset(const StringPieceLite haystack, ...@@ -51,7 +53,5 @@ size_t qfind_first_byte_of_byteset(const StringPieceLite haystack,
} }
return std::string::npos; return std::string::npos;
} }
} }
} }
...@@ -38,38 +38,60 @@ class StringPieceLite { ...@@ -38,38 +38,60 @@ class StringPieceLite {
public: public:
StringPieceLite(const char* b, const char* e) : b_(b), e_(e) {} StringPieceLite(const char* b, const char* e) : b_(b), e_(e) {}
template <typename Range> template <typename Range>
/* implicit */ StringPieceLite(const Range& r) : /* implicit */ StringPieceLite(const Range& r)
StringPieceLite(r.data(), r.data() + r.size()) {} : StringPieceLite(r.data(), r.data() + r.size()) {}
const char* data() const { return b_; } const char* data() const {
const char* begin() const { return b_; } return b_;
const char* end() const { return e_; } }
size_t size() const { return size_t(e_ - b_); } const char* begin() const {
bool empty() const { return size() == 0; } return b_;
const char& operator[](size_t i) const { DCHECK_GT(size(), i); return b_[i]; } }
const char* end() const {
return e_;
}
size_t size() const {
return size_t(e_ - b_);
}
bool empty() const {
return size() == 0;
}
const char& operator[](size_t i) const {
DCHECK_GT(size(), i);
return b_[i];
}
template <typename Range> template <typename Range>
explicit operator Range() const { return Range(begin(), end()); } explicit operator Range() const {
return Range(begin(), end());
}
private: private:
const char* b_; const char* b_;
const char* e_; const char* e_;
}; };
inline size_t qfind_first_byte_of_std(const StringPieceLite haystack, inline size_t qfind_first_byte_of_std(
const StringPieceLite needles) { const StringPieceLite haystack,
auto ret = std::find_first_of(haystack.begin(), haystack.end(), const StringPieceLite needles) {
needles.begin(), needles.end(), auto ret = std::find_first_of(
[](char a, char b) { return a == b; }); haystack.begin(),
haystack.end(),
needles.begin(),
needles.end(),
[](char a, char b) { return a == b; });
return ret == haystack.end() ? std::string::npos : ret - haystack.begin(); return ret == haystack.end() ? std::string::npos : ret - haystack.begin();
} }
size_t qfind_first_byte_of_bitset(
const StringPieceLite haystack,
const StringPieceLite needles);
size_t qfind_first_byte_of_bitset(const StringPieceLite haystack, size_t qfind_first_byte_of_byteset(
const StringPieceLite needles); const StringPieceLite haystack,
const StringPieceLite needles);
size_t qfind_first_byte_of_byteset(const StringPieceLite haystack,
const StringPieceLite needles);
inline size_t qfind_first_byte_of_nosse(const StringPieceLite haystack, inline size_t qfind_first_byte_of_nosse(
const StringPieceLite needles) { const StringPieceLite haystack,
const StringPieceLite needles) {
if (UNLIKELY(needles.empty() || haystack.empty())) { if (UNLIKELY(needles.empty() || haystack.empty())) {
return std::string::npos; return std::string::npos;
} }
...@@ -77,13 +99,10 @@ inline size_t qfind_first_byte_of_nosse(const StringPieceLite haystack, ...@@ -77,13 +99,10 @@ inline size_t qfind_first_byte_of_nosse(const StringPieceLite haystack,
// This is not an exact science since it depends on the CPU, the size of // This is not an exact science since it depends on the CPU, the size of
// needles, and the size of haystack. // needles, and the size of haystack.
if ((needles.size() >= 4 && haystack.size() <= 10) || if ((needles.size() >= 4 && haystack.size() <= 10) ||
(needles.size() >= 16 && haystack.size() <= 64) || (needles.size() >= 16 && haystack.size() <= 64) || needles.size() >= 32) {
needles.size() >= 32) {
return qfind_first_byte_of_byteset(haystack, needles); return qfind_first_byte_of_byteset(haystack, needles);
} }
return qfind_first_byte_of_std(haystack, needles); return qfind_first_byte_of_std(haystack, needles);
} }
} }
} }
...@@ -28,13 +28,14 @@ ...@@ -28,13 +28,14 @@
#if !FOLLY_SSE_PREREQ(4, 2) #if !FOLLY_SSE_PREREQ(4, 2)
namespace folly { namespace folly {
namespace detail { namespace detail {
size_t qfind_first_byte_of_sse42(const StringPieceLite haystack, size_t qfind_first_byte_of_sse42(
const StringPieceLite needles) { const StringPieceLite haystack,
const StringPieceLite needles) {
return qfind_first_byte_of_nosse(haystack, needles); return qfind_first_byte_of_nosse(haystack, needles);
} }
} }
} }
# else #else
#include <cstdint> #include <cstdint>
#include <limits> #include <limits>
#include <string> #include <string>
...@@ -49,15 +50,14 @@ size_t qfind_first_byte_of_sse42(const StringPieceLite haystack, ...@@ -49,15 +50,14 @@ size_t qfind_first_byte_of_sse42(const StringPieceLite haystack,
// a function with always_inline fails to build. The _mm_* functions are marked // a function with always_inline fails to build. The _mm_* functions are marked
// always_inline. // always_inline.
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
#if defined FOLLY_SANITIZE_ADDRESS && \ #if defined FOLLY_SANITIZE_ADDRESS && FOLLY_SANITIZE_ADDRESS == 1 && \
FOLLY_SANITIZE_ADDRESS == 1 && \
__GNUC_PREREQ(4, 9) __GNUC_PREREQ(4, 9)
# define _mm_load_si128(p) (*(p)) #define _mm_load_si128(p) (*(p))
# define _mm_loadu_si128(p) ((__m128i)__builtin_ia32_loaddqu((const char*)(p))) #define _mm_loadu_si128(p) ((__m128i)__builtin_ia32_loaddqu((const char*)(p)))
# ifdef _mm_cmpestri #ifdef _mm_cmpestri
# undef _mm_cmpestri #undef _mm_cmpestri
# endif #endif
# define _mm_cmpestri(a, b, c, d, e) \ #define _mm_cmpestri(a, b, c, d, e) \
__builtin_ia32_pcmpestri128((__v16qi)(a), b, (__v16qi)(c), d, e) __builtin_ia32_pcmpestri128((__v16qi)(a), b, (__v16qi)(c), d, e)
#endif #endif
...@@ -67,8 +67,9 @@ namespace detail { ...@@ -67,8 +67,9 @@ namespace detail {
// It's okay if pages are bigger than this (as powers of two), but they should // It's okay if pages are bigger than this (as powers of two), but they should
// not be smaller. // not be smaller.
static constexpr size_t kMinPageSize = 4096; static constexpr size_t kMinPageSize = 4096;
static_assert(kMinPageSize >= 16, static_assert(
"kMinPageSize must be at least SSE register size"); kMinPageSize >= 16,
"kMinPageSize must be at least SSE register size");
template <typename T> template <typename T>
static inline uintptr_t page_for(T* addr) { static inline uintptr_t page_for(T* addr) {
...@@ -76,19 +77,20 @@ static inline uintptr_t page_for(T* addr) { ...@@ -76,19 +77,20 @@ static inline uintptr_t page_for(T* addr) {
} }
static inline size_t nextAlignedIndex(const char* arr) { static inline size_t nextAlignedIndex(const char* arr) {
auto firstPossible = reinterpret_cast<uintptr_t>(arr) + 1; auto firstPossible = reinterpret_cast<uintptr_t>(arr) + 1;
return 1 + // add 1 because the index starts at 'arr' return 1 + // add 1 because the index starts at 'arr'
((firstPossible + 15) & ~0xF) // round up to next multiple of 16 ((firstPossible + 15) & ~0xF) // round up to next multiple of 16
- firstPossible; - firstPossible;
} }
static size_t qfind_first_byte_of_needles16(const StringPieceLite haystack, static size_t qfind_first_byte_of_needles16(
const StringPieceLite needles) const StringPieceLite haystack,
FOLLY_DISABLE_ADDRESS_SANITIZER; const StringPieceLite needles) FOLLY_DISABLE_ADDRESS_SANITIZER;
// helper method for case where needles.size() <= 16 // helper method for case where needles.size() <= 16
size_t qfind_first_byte_of_needles16(const StringPieceLite haystack, size_t qfind_first_byte_of_needles16(
const StringPieceLite needles) { const StringPieceLite haystack,
const StringPieceLite needles) {
DCHECK_GT(haystack.size(), 0u); DCHECK_GT(haystack.size(), 0u);
DCHECK_GT(needles.size(), 0u); DCHECK_GT(needles.size(), 0u);
DCHECK_LE(needles.size(), 16u); DCHECK_LE(needles.size(), 16u);
...@@ -101,11 +103,10 @@ size_t qfind_first_byte_of_needles16(const StringPieceLite haystack, ...@@ -101,11 +103,10 @@ size_t qfind_first_byte_of_needles16(const StringPieceLite haystack,
return detail::qfind_first_byte_of_nosse(haystack, needles); return detail::qfind_first_byte_of_nosse(haystack, needles);
} }
auto arr2 = _mm_loadu_si128( auto arr2 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(needles.data()));
reinterpret_cast<const __m128i*>(needles.data()));
// do an unaligned load for first block of haystack // do an unaligned load for first block of haystack
auto arr1 = _mm_loadu_si128( auto arr1 =
reinterpret_cast<const __m128i*>(haystack.data())); _mm_loadu_si128(reinterpret_cast<const __m128i*>(haystack.data()));
auto index = auto index =
_mm_cmpestri(arr2, int(needles.size()), arr1, int(haystack.size()), 0); _mm_cmpestri(arr2, int(needles.size()), arr1, int(haystack.size()), 0);
if (index < 16) { if (index < 16) {
...@@ -114,7 +115,7 @@ size_t qfind_first_byte_of_needles16(const StringPieceLite haystack, ...@@ -114,7 +115,7 @@ size_t qfind_first_byte_of_needles16(const StringPieceLite haystack,
// Now, we can do aligned loads hereafter... // Now, we can do aligned loads hereafter...
size_t i = nextAlignedIndex(haystack.data()); size_t i = nextAlignedIndex(haystack.data());
for (; i < haystack.size(); i+= 16) { for (; i < haystack.size(); i += 16) {
arr1 = arr1 =
_mm_load_si128(reinterpret_cast<const __m128i*>(haystack.data() + i)); _mm_load_si128(reinterpret_cast<const __m128i*>(haystack.data() + i));
index = _mm_cmpestri( index = _mm_cmpestri(
...@@ -127,27 +128,30 @@ size_t qfind_first_byte_of_needles16(const StringPieceLite haystack, ...@@ -127,27 +128,30 @@ size_t qfind_first_byte_of_needles16(const StringPieceLite haystack,
} }
template <bool HAYSTACK_ALIGNED> template <bool HAYSTACK_ALIGNED>
size_t scanHaystackBlock(const StringPieceLite haystack, size_t scanHaystackBlock(
const StringPieceLite needles, const StringPieceLite haystack,
uint64_t idx) const StringPieceLite needles,
// Turn off ASAN because the "arr2 = ..." assignment in the loop below reads uint64_t idx)
// up to 15 bytes beyond end of the buffer in #needles#. That is ok because // Turn off ASAN because the "arr2 = ..." assignment in the loop below reads
// ptr2 is always 16-byte aligned, so the read can never span a page boundary. // up to 15 bytes beyond end of the buffer in #needles#. That is ok because
// Also, the extra data that may be read is never actually used. // ptr2 is always 16-byte aligned, so the read can never span a page
FOLLY_DISABLE_ADDRESS_SANITIZER; // boundary. Also, the extra data that may be read is never actually used.
FOLLY_DISABLE_ADDRESS_SANITIZER;
// Scans a 16-byte block of haystack (starting at blockStartIdx) to find first // Scans a 16-byte block of haystack (starting at blockStartIdx) to find first
// needle. If HAYSTACK_ALIGNED, then haystack must be 16byte aligned. // needle. If HAYSTACK_ALIGNED, then haystack must be 16byte aligned.
// If !HAYSTACK_ALIGNED, then caller must ensure that it is safe to load the // If !HAYSTACK_ALIGNED, then caller must ensure that it is safe to load the
// block. // block.
template <bool HAYSTACK_ALIGNED> template <bool HAYSTACK_ALIGNED>
size_t scanHaystackBlock(const StringPieceLite haystack, size_t scanHaystackBlock(
const StringPieceLite needles, const StringPieceLite haystack,
uint64_t blockStartIdx) { const StringPieceLite needles,
uint64_t blockStartIdx) {
DCHECK_GT(needles.size(), 16u); // should handled by *needles16() method DCHECK_GT(needles.size(), 16u); // should handled by *needles16() method
DCHECK(blockStartIdx + 16 <= haystack.size() || DCHECK(
(page_for(haystack.data() + blockStartIdx) == blockStartIdx + 16 <= haystack.size() ||
page_for(haystack.data() + blockStartIdx + 15))); (page_for(haystack.data() + blockStartIdx) ==
page_for(haystack.data() + blockStartIdx + 15)));
__m128i arr1; __m128i arr1;
if (HAYSTACK_ALIGNED) { if (HAYSTACK_ALIGNED) {
...@@ -159,15 +163,13 @@ size_t scanHaystackBlock(const StringPieceLite haystack, ...@@ -159,15 +163,13 @@ size_t scanHaystackBlock(const StringPieceLite haystack,
} }
// This load is safe because needles.size() >= 16 // This load is safe because needles.size() >= 16
auto arr2 = _mm_loadu_si128( auto arr2 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(needles.data()));
reinterpret_cast<const __m128i*>(needles.data()));
auto b = auto b =
_mm_cmpestri(arr2, 16, arr1, int(haystack.size() - blockStartIdx), 0); _mm_cmpestri(arr2, 16, arr1, int(haystack.size() - blockStartIdx), 0);
size_t j = nextAlignedIndex(needles.data()); size_t j = nextAlignedIndex(needles.data());
for (; j < needles.size(); j += 16) { for (; j < needles.size(); j += 16) {
arr2 = _mm_load_si128( arr2 = _mm_load_si128(reinterpret_cast<const __m128i*>(needles.data() + j));
reinterpret_cast<const __m128i*>(needles.data() + j));
auto index = _mm_cmpestri( auto index = _mm_cmpestri(
arr2, arr2,
...@@ -184,11 +186,13 @@ size_t scanHaystackBlock(const StringPieceLite haystack, ...@@ -184,11 +186,13 @@ size_t scanHaystackBlock(const StringPieceLite haystack,
return std::string::npos; return std::string::npos;
} }
size_t qfind_first_byte_of_sse42(const StringPieceLite haystack, size_t qfind_first_byte_of_sse42(
const StringPieceLite needles); const StringPieceLite haystack,
const StringPieceLite needles);
size_t qfind_first_byte_of_sse42(const StringPieceLite haystack, size_t qfind_first_byte_of_sse42(
const StringPieceLite needles) { const StringPieceLite haystack,
const StringPieceLite needles) {
if (UNLIKELY(needles.empty() || haystack.empty())) { if (UNLIKELY(needles.empty() || haystack.empty())) {
return std::string::npos; return std::string::npos;
} else if (needles.size() <= 16) { } else if (needles.size() <= 16) {
......
...@@ -24,9 +24,8 @@ namespace folly { ...@@ -24,9 +24,8 @@ namespace folly {
namespace detail { namespace detail {
size_t qfind_first_byte_of_sse42(const StringPieceLite haystack, size_t qfind_first_byte_of_sse42(
const StringPieceLite needles); const StringPieceLite haystack,
const StringPieceLite needles);
} }
} }
...@@ -80,9 +80,8 @@ void initFile(int len) { ...@@ -80,9 +80,8 @@ void initFile(int len) {
} }
} }
string generateString(int len) { string generateString(int len) {
std::uniform_int_distribution<uint32_t> validChar(1, 255); // no null-char std::uniform_int_distribution<uint32_t> validChar(1, 255); // no null-char
string ret; string ret;
while (len--) { while (len--) {
ret.push_back(validChar(rnd)); ret.push_back(validChar(rnd));
...@@ -93,7 +92,7 @@ string generateString(int len) { ...@@ -93,7 +92,7 @@ string generateString(int len) {
void initDelims(int len) { void initDelims(int len) {
ffoDelim.clear(); ffoDelim.clear();
string s(len - 1, '\0'); // find_first_of won't finish until last char string s(len - 1, '\0'); // find_first_of won't finish until last char
s.push_back('a'); s.push_back('a');
ffoTestString = s; ffoTestString = s;
...@@ -107,7 +106,7 @@ void initDelims(int len) { ...@@ -107,7 +106,7 @@ void initDelims(int len) {
auto s = generateString(n); auto s = generateString(n);
if (rnd() % 2) { if (rnd() % 2) {
// ~half of tests will find a hit // ~half of tests will find a hit
s[rnd() % s.size()] = 'a'; // yes, this could mean 'a' is a duplicate s[rnd() % s.size()] = 'a'; // yes, this could mean 'a' is a duplicate
} }
ffoDelim.push_back(s); ffoDelim.push_back(s);
} }
...@@ -305,8 +304,9 @@ BENCHMARK_RELATIVE(FindFirstOf32NeedlesBitSet, n) { ...@@ -305,8 +304,9 @@ BENCHMARK_RELATIVE(FindFirstOf32NeedlesBitSet, n) {
BENCHMARK_DRAW_LINE(); BENCHMARK_DRAW_LINE();
const string delims64 = "!bcdefghijklmnopqrstuvwxyz_" const string delims64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789$"; "!bcdefghijklmnopqrstuvwxyz_"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789$";
BENCHMARK(FindFirstOf64NeedlesBase, n) { BENCHMARK(FindFirstOf64NeedlesBase, n) {
findFirstOfRange(delims64, detail::qfind_first_byte_of, n); findFirstOfRange(delims64, detail::qfind_first_byte_of, n);
...@@ -400,7 +400,7 @@ BENCHMARK_DRAW_LINE(); ...@@ -400,7 +400,7 @@ BENCHMARK_DRAW_LINE();
int main(int argc, char** argv) { int main(int argc, char** argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true); gflags::ParseCommandLineFlags(&argc, &argv, true);
for (int len : {1, 8, 10, 16, 32, 64, 128, 256, 10*1024, 1024*1024}) { for (int len : {1, 8, 10, 16, 32, 64, 128, 256, 10 * 1024, 1024 * 1024}) {
initStr(len); initStr(len);
initDelims(len); initDelims(len);
initFile(len); initFile(len);
......
...@@ -50,8 +50,8 @@ TEST(StringPiece, All) { ...@@ -50,8 +50,8 @@ TEST(StringPiece, All) {
// we expect the compiler to optimize things so that there's only one copy // we expect the compiler to optimize things so that there's only one copy
// of the string literal "foo", even though we've got it in multiple places // of the string literal "foo", even though we've got it in multiple places
EXPECT_EQ(foo, foo2); // remember, this uses ==, not strcmp, so it's a ptr EXPECT_EQ(foo, foo2); // remember, this uses ==, not strcmp, so it's a ptr
// comparison rather than lexical // comparison rather than lexical
// the string object creates copies though, so the c_str of these should be // the string object creates copies though, so the c_str of these should be
// distinct // distinct
...@@ -61,17 +61,17 @@ TEST(StringPiece, All) { ...@@ -61,17 +61,17 @@ TEST(StringPiece, All) {
StringPiece s(foo); StringPiece s(foo);
EXPECT_EQ(s.size(), 3); EXPECT_EQ(s.size(), 3);
EXPECT_EQ(s.start(), foo); // ptr comparison EXPECT_EQ(s.start(), foo); // ptr comparison
EXPECT_NE(s.start(), fooStr.c_str()); // ptr comparison EXPECT_NE(s.start(), fooStr.c_str()); // ptr comparison
EXPECT_NE(s.start(), foo2Str.c_str()); // ptr comparison EXPECT_NE(s.start(), foo2Str.c_str()); // ptr comparison
EXPECT_EQ(s.toString(), foo); // lexical comparison EXPECT_EQ(s.toString(), foo); // lexical comparison
EXPECT_EQ(s.toString(), fooStr.c_str()); // lexical comparison EXPECT_EQ(s.toString(), fooStr.c_str()); // lexical comparison
EXPECT_EQ(s.toString(), foo2Str.c_str()); // lexical comparison EXPECT_EQ(s.toString(), foo2Str.c_str()); // lexical comparison
EXPECT_EQ(s, foo); // lexical comparison EXPECT_EQ(s, foo); // lexical comparison
EXPECT_EQ(s, fooStr); // lexical comparison EXPECT_EQ(s, fooStr); // lexical comparison
EXPECT_EQ(s, foo2Str); // lexical comparison EXPECT_EQ(s, foo2Str); // lexical comparison
EXPECT_EQ(foo, s); EXPECT_EQ(foo, s);
// check using StringPiece to reference substrings // check using StringPiece to reference substrings
...@@ -96,7 +96,7 @@ TEST(StringPiece, All) { ...@@ -96,7 +96,7 @@ TEST(StringPiece, All) {
EXPECT_EQ(s.find("ba", 4), 6); EXPECT_EQ(s.find("ba", 4), 6);
EXPECT_EQ(s.find("notfound"), StringPiece::npos); EXPECT_EQ(s.find("notfound"), StringPiece::npos);
EXPECT_EQ(s.find("notfound", 1), StringPiece::npos); EXPECT_EQ(s.find("notfound", 1), StringPiece::npos);
EXPECT_EQ(s.find("bar", 4), StringPiece::npos); // starting position too far EXPECT_EQ(s.find("bar", 4), StringPiece::npos); // starting position too far
// starting pos that is obviously past the end -- This works for std::string // starting pos that is obviously past the end -- This works for std::string
EXPECT_EQ(s.toString().find("notfound", 55), StringPiece::npos); EXPECT_EQ(s.toString().find("notfound", 55), StringPiece::npos);
EXPECT_EQ(s.find("z", s.size()), StringPiece::npos); EXPECT_EQ(s.find("z", s.size()), StringPiece::npos);
...@@ -112,7 +112,7 @@ TEST(StringPiece, All) { ...@@ -112,7 +112,7 @@ TEST(StringPiece, All) {
EXPECT_EQ(s.find('o', 2), 2); EXPECT_EQ(s.find('o', 2), 2);
EXPECT_EQ(s.find('y'), StringPiece::npos); EXPECT_EQ(s.find('y'), StringPiece::npos);
EXPECT_EQ(s.find('y', 1), StringPiece::npos); EXPECT_EQ(s.find('y', 1), StringPiece::npos);
EXPECT_EQ(s.find('o', 4), StringPiece::npos); // starting position too far EXPECT_EQ(s.find('o', 4), StringPiece::npos); // starting position too far
EXPECT_TRUE(s.contains('z')); EXPECT_TRUE(s.contains('z'));
// starting pos that is obviously past the end -- This works for std::string // starting pos that is obviously past the end -- This works for std::string
EXPECT_EQ(s.toString().find('y', 55), StringPiece::npos); EXPECT_EQ(s.toString().find('y', 55), StringPiece::npos);
...@@ -265,10 +265,10 @@ TEST(StringPiece, EightBitComparisons) { ...@@ -265,10 +265,10 @@ TEST(StringPiece, EightBitComparisons) {
TEST(StringPiece, ToByteRange) { TEST(StringPiece, ToByteRange) {
StringPiece a("hello"); StringPiece a("hello");
ByteRange b(a); ByteRange b(a);
EXPECT_EQ(static_cast<const void*>(a.begin()), EXPECT_EQ(
static_cast<const void*>(b.begin())); static_cast<const void*>(a.begin()), static_cast<const void*>(b.begin()));
EXPECT_EQ(static_cast<const void*>(a.end()), EXPECT_EQ(
static_cast<const void*>(b.end())); static_cast<const void*>(a.end()), static_cast<const void*>(b.end()));
// and convert back again // and convert back again
StringPiece c(b); StringPiece c(b);
...@@ -625,72 +625,72 @@ TEST(StringPiece, split_step_with_process_char_delimiter) { ...@@ -625,72 +625,72 @@ TEST(StringPiece, split_step_with_process_char_delimiter) {
EXPECT_EQ(s, p); EXPECT_EQ(s, p);
EXPECT_EQ(1, (p.split_step(' ', [&](folly::StringPiece x) { EXPECT_EQ(1, (p.split_step(' ', [&](folly::StringPiece x) {
EXPECT_EQ(std::next(s, 5), p.begin()); EXPECT_EQ(std::next(s, 5), p.begin());
EXPECT_EQ(e, p.end()); EXPECT_EQ(e, p.end());
EXPECT_EQ("this", x); EXPECT_EQ("this", x);
return 1; return 1;
}))); })));
EXPECT_EQ(2, (p.split_step(' ', [&](folly::StringPiece x) { EXPECT_EQ(2, (p.split_step(' ', [&](folly::StringPiece x) {
EXPECT_EQ(std::next(s, 8), p.begin()); EXPECT_EQ(std::next(s, 8), p.begin());
EXPECT_EQ(e, p.end()); EXPECT_EQ(e, p.end());
EXPECT_EQ("is", x); EXPECT_EQ("is", x);
return 2; return 2;
}))); })));
EXPECT_EQ(3, (p.split_step('u', [&](folly::StringPiece x) { EXPECT_EQ(3, (p.split_step('u', [&](folly::StringPiece x) {
EXPECT_EQ(std::next(s, 10), p.begin()); EXPECT_EQ(std::next(s, 10), p.begin());
EXPECT_EQ(e, p.end()); EXPECT_EQ(e, p.end());
EXPECT_EQ("j", x); EXPECT_EQ("j", x);
return 3; return 3;
}))); })));
EXPECT_EQ(4, (p.split_step(' ', [&](folly::StringPiece x) { EXPECT_EQ(4, (p.split_step(' ', [&](folly::StringPiece x) {
EXPECT_EQ(std::next(s, 13), p.begin()); EXPECT_EQ(std::next(s, 13), p.begin());
EXPECT_EQ(e, p.end()); EXPECT_EQ(e, p.end());
EXPECT_EQ("st", x); EXPECT_EQ("st", x);
return 4; return 4;
}))); })));
EXPECT_EQ(5, (p.split_step(' ', [&](folly::StringPiece x) { EXPECT_EQ(5, (p.split_step(' ', [&](folly::StringPiece x) {
EXPECT_EQ(std::next(s, 14), p.begin()); EXPECT_EQ(std::next(s, 14), p.begin());
EXPECT_EQ(e, p.end()); EXPECT_EQ(e, p.end());
EXPECT_EQ("", x); EXPECT_EQ("", x);
return 5; return 5;
}))); })));
EXPECT_EQ(6, (p.split_step(' ', [&](folly::StringPiece x) { EXPECT_EQ(6, (p.split_step(' ', [&](folly::StringPiece x) {
EXPECT_EQ(std::next(s, 16), p.begin()); EXPECT_EQ(std::next(s, 16), p.begin());
EXPECT_EQ(e, p.end()); EXPECT_EQ(e, p.end());
EXPECT_EQ("a", x); EXPECT_EQ("a", x);
return 6; return 6;
}))); })));
EXPECT_EQ(7, (p.split_step(' ', [&](folly::StringPiece x) { EXPECT_EQ(7, (p.split_step(' ', [&](folly::StringPiece x) {
EXPECT_EQ(std::next(s, 21), p.begin()); EXPECT_EQ(std::next(s, 21), p.begin());
EXPECT_EQ(e, p.end()); EXPECT_EQ(e, p.end());
EXPECT_EQ("test", x); EXPECT_EQ("test", x);
return 7; return 7;
}))); })));
EXPECT_EQ(8, (p.split_step(' ', [&](folly::StringPiece x) { EXPECT_EQ(8, (p.split_step(' ', [&](folly::StringPiece x) {
EXPECT_EQ(e, p.begin()); EXPECT_EQ(e, p.begin());
EXPECT_EQ(e, p.end()); EXPECT_EQ(e, p.end());
EXPECT_EQ("string", x); EXPECT_EQ("string", x);
return 8; return 8;
}))); })));
EXPECT_EQ(9, (p.split_step(' ', [&](folly::StringPiece x) { EXPECT_EQ(9, (p.split_step(' ', [&](folly::StringPiece x) {
EXPECT_EQ(e, p.begin()); EXPECT_EQ(e, p.begin());
EXPECT_EQ(e, p.end()); EXPECT_EQ(e, p.end());
EXPECT_EQ("", x); EXPECT_EQ("", x);
return 9; return 9;
}))); })));
EXPECT_TRUE((std::is_same< EXPECT_TRUE(
void, (std::is_same<
decltype(p.split_step(' ', split_step_with_process_noop)) void,
>::value)); decltype(p.split_step(' ', split_step_with_process_noop))>::value));
EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop)); EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
} }
...@@ -708,79 +708,79 @@ TEST(StringPiece, split_step_with_process_range_delimiter) { ...@@ -708,79 +708,79 @@ TEST(StringPiece, split_step_with_process_range_delimiter) {
EXPECT_EQ(s, p); EXPECT_EQ(s, p);
EXPECT_EQ(1, (p.split_step(" ", [&](folly::StringPiece x) { EXPECT_EQ(1, (p.split_step(" ", [&](folly::StringPiece x) {
EXPECT_EQ(std::next(s, 6), p.begin()); EXPECT_EQ(std::next(s, 6), p.begin());
EXPECT_EQ(e, p.end()); EXPECT_EQ(e, p.end());
EXPECT_EQ("this", x); EXPECT_EQ("this", x);
return 1; return 1;
}))); })));
EXPECT_EQ(2, (p.split_step(" ", [&](folly::StringPiece x) { EXPECT_EQ(2, (p.split_step(" ", [&](folly::StringPiece x) {
EXPECT_EQ(std::next(s, 10), p.begin()); EXPECT_EQ(std::next(s, 10), p.begin());
EXPECT_EQ(e, p.end()); EXPECT_EQ(e, p.end());
EXPECT_EQ("is", x); EXPECT_EQ("is", x);
return 2; return 2;
}))); })));
EXPECT_EQ(3, (p.split_step("u", [&](folly::StringPiece x) { EXPECT_EQ(3, (p.split_step("u", [&](folly::StringPiece x) {
EXPECT_EQ(std::next(s, 12), p.begin()); EXPECT_EQ(std::next(s, 12), p.begin());
EXPECT_EQ(e, p.end()); EXPECT_EQ(e, p.end());
EXPECT_EQ("j", x); EXPECT_EQ("j", x);
return 3; return 3;
}))); })));
EXPECT_EQ(4, (p.split_step(" ", [&](folly::StringPiece x) { EXPECT_EQ(4, (p.split_step(" ", [&](folly::StringPiece x) {
EXPECT_EQ(std::next(s, 16), p.begin()); EXPECT_EQ(std::next(s, 16), p.begin());
EXPECT_EQ(e, p.end()); EXPECT_EQ(e, p.end());
EXPECT_EQ("st", x); EXPECT_EQ("st", x);
return 4; return 4;
}))); })));
EXPECT_EQ(5, (p.split_step(" ", [&](folly::StringPiece x) { EXPECT_EQ(5, (p.split_step(" ", [&](folly::StringPiece x) {
EXPECT_EQ(std::next(s, 18), p.begin()); EXPECT_EQ(std::next(s, 18), p.begin());
EXPECT_EQ(e, p.end()); EXPECT_EQ(e, p.end());
EXPECT_EQ("", x); EXPECT_EQ("", x);
return 5; return 5;
}))); })));
EXPECT_EQ(6, (p.split_step(" ", [&](folly::StringPiece x) { EXPECT_EQ(6, (p.split_step(" ", [&](folly::StringPiece x) {
EXPECT_EQ(std::next(s, 21), p.begin()); EXPECT_EQ(std::next(s, 21), p.begin());
EXPECT_EQ(e, p.end()); EXPECT_EQ(e, p.end());
EXPECT_EQ("a", x); EXPECT_EQ("a", x);
return 6; return 6;
}))); })));
EXPECT_EQ(7, (p.split_step(" ", [&](folly::StringPiece x) { EXPECT_EQ(7, (p.split_step(" ", [&](folly::StringPiece x) {
EXPECT_EQ(std::next(s, 28), p.begin()); EXPECT_EQ(std::next(s, 28), p.begin());
EXPECT_EQ(e, p.end()); EXPECT_EQ(e, p.end());
EXPECT_EQ(" test", x); EXPECT_EQ(" test", x);
return 7; return 7;
}))); })));
EXPECT_EQ(8, (p.split_step(" ", [&](folly::StringPiece x) { EXPECT_EQ(8, (p.split_step(" ", [&](folly::StringPiece x) {
EXPECT_EQ(e, p.begin()); EXPECT_EQ(e, p.begin());
EXPECT_EQ(e, p.end()); EXPECT_EQ(e, p.end());
EXPECT_EQ("string", x); EXPECT_EQ("string", x);
return 8; return 8;
}))); })));
EXPECT_EQ(9, (p.split_step(" ", [&](folly::StringPiece x) { EXPECT_EQ(9, (p.split_step(" ", [&](folly::StringPiece x) {
EXPECT_EQ(e, p.begin()); EXPECT_EQ(e, p.begin());
EXPECT_EQ(e, p.end()); EXPECT_EQ(e, p.end());
EXPECT_EQ("", x); EXPECT_EQ("", x);
return 9; return 9;
}))); })));
EXPECT_EQ(10, (p.split_step(" ", [&](folly::StringPiece x) { EXPECT_EQ(10, (p.split_step(" ", [&](folly::StringPiece x) {
EXPECT_EQ(e, p.begin()); EXPECT_EQ(e, p.begin());
EXPECT_EQ(e, p.end()); EXPECT_EQ(e, p.end());
EXPECT_EQ("", x); EXPECT_EQ("", x);
return 10; return 10;
}))); })));
EXPECT_TRUE((std::is_same< EXPECT_TRUE(
void, (std::is_same<
decltype(p.split_step(' ', split_step_with_process_noop)) void,
>::value)); decltype(p.split_step(' ', split_step_with_process_noop))>::value));
EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop)); EXPECT_NO_THROW(p.split_step(' ', split_step_with_process_noop));
} }
...@@ -798,10 +798,7 @@ TEST(StringPiece, split_step_with_process_char_delimiter_additional_args) { ...@@ -798,10 +798,7 @@ TEST(StringPiece, split_step_with_process_char_delimiter_additional_args) {
EXPECT_EQ(e, p.end()); EXPECT_EQ(e, p.end());
EXPECT_EQ(s, p); EXPECT_EQ(s, p);
auto const functor = []( auto const functor = [](folly::StringPiece s, folly::StringPiece expected) {
folly::StringPiece s,
folly::StringPiece expected
) {
EXPECT_EQ(expected, s); EXPECT_EQ(expected, s);
return expected; return expected;
}; };
...@@ -836,10 +833,7 @@ TEST(StringPiece, split_step_with_process_range_delimiter_additional_args) { ...@@ -836,10 +833,7 @@ TEST(StringPiece, split_step_with_process_range_delimiter_additional_args) {
EXPECT_EQ(e, p.end()); EXPECT_EQ(e, p.end());
EXPECT_EQ(s, p); EXPECT_EQ(s, p);
auto const functor = []( auto const functor = [](folly::StringPiece s, folly::StringPiece expected) {
folly::StringPiece s,
folly::StringPiece expected
) {
EXPECT_EQ(expected, s); EXPECT_EQ(expected, s);
return expected; return expected;
}; };
...@@ -863,8 +857,12 @@ TEST(StringPiece, split_step_with_process_range_delimiter_additional_args) { ...@@ -863,8 +857,12 @@ TEST(StringPiece, split_step_with_process_range_delimiter_additional_args) {
TEST(StringPiece, NoInvalidImplicitConversions) { TEST(StringPiece, NoInvalidImplicitConversions) {
struct IsString { struct IsString {
bool operator()(folly::Range<int*>) { return false; } bool operator()(folly::Range<int*>) {
bool operator()(folly::StringPiece) { return true; } return false;
}
bool operator()(folly::StringPiece) {
return true;
}
}; };
std::string s = "hello"; std::string s = "hello";
...@@ -912,9 +910,8 @@ struct ByteSetNeedleFinder { ...@@ -912,9 +910,8 @@ struct ByteSetNeedleFinder {
} }
}; };
typedef ::testing::Types<SseNeedleFinder, using NeedleFinders =
NoSseNeedleFinder, ::testing::Types<SseNeedleFinder, NoSseNeedleFinder, ByteSetNeedleFinder>;
ByteSetNeedleFinder> NeedleFinders;
TYPED_TEST_CASE(NeedleFinderTest, NeedleFinders); TYPED_TEST_CASE(NeedleFinderTest, NeedleFinders);
TYPED_TEST(NeedleFinderTest, Null) { TYPED_TEST(NeedleFinderTest, Null) {
...@@ -1047,13 +1044,13 @@ TYPED_TEST(NeedleFinderTest, NoSegFault) { ...@@ -1047,13 +1044,13 @@ TYPED_TEST(NeedleFinderTest, NoSegFault) {
// string(s1.data(), s1.size()).c_str(), s1.size(), // string(s1.data(), s1.size()).c_str(), s1.size(),
// string(s2.data(), s2.size()).c_str(), s2.size()); // string(s2.data(), s2.size()).c_str(), s2.size());
auto r1 = this->find_first_byte_of(s1, s2); auto r1 = this->find_first_byte_of(s1, s2);
auto f1 = std::find_first_of(s1.begin(), s1.end(), auto f1 =
s2.begin(), s2.end()); std::find_first_of(s1.begin(), s1.end(), s2.begin(), s2.end());
auto e1 = (f1 == s1.end()) ? StringPiece::npos : f1 - s1.begin(); auto e1 = (f1 == s1.end()) ? StringPiece::npos : f1 - s1.begin();
EXPECT_EQ(r1, e1); EXPECT_EQ(r1, e1);
auto r2 = this->find_first_byte_of(s2, s1); auto r2 = this->find_first_byte_of(s2, s1);
auto f2 = std::find_first_of(s2.begin(), s2.end(), auto f2 =
s1.begin(), s1.end()); std::find_first_of(s2.begin(), s2.end(), s1.begin(), s1.end());
auto e2 = (f2 == s2.end()) ? StringPiece::npos : f2 - s2.begin(); auto e2 = (f2 == s2.end()) ? StringPiece::npos : f2 - s2.begin();
EXPECT_EQ(r2, e2); EXPECT_EQ(r2, e2);
freeProtectedBuf(buf1); freeProtectedBuf(buf1);
...@@ -1121,7 +1118,7 @@ TEST(RangeFunc, Array) { ...@@ -1121,7 +1118,7 @@ TEST(RangeFunc, Array) {
} }
TEST(RangeFunc, CArray) { TEST(RangeFunc, CArray) {
int x[] {1, 2, 3, 4}; int x[]{1, 2, 3, 4};
testRangeFunc(x, 4); testRangeFunc(x, 4);
} }
...@@ -1181,9 +1178,10 @@ TEST(RangeFunc, ConstexprCollection) { ...@@ -1181,9 +1178,10 @@ TEST(RangeFunc, ConstexprCollection) {
EXPECT_EQ(2, numCollRangeSize); EXPECT_EQ(2, numCollRangeSize);
} }
std::string get_rand_str(size_t size, std::string get_rand_str(
std::uniform_int_distribution<>& dist, size_t size,
std::mt19937& gen) { std::uniform_int_distribution<>& dist,
std::mt19937& gen) {
std::string ret(size, '\0'); std::string ret(size, '\0');
for (size_t i = 0; i < size; ++i) { for (size_t i = 0; i < size; ++i) {
ret[i] = static_cast<char>(dist(gen)); ret[i] = static_cast<char>(dist(gen));
...@@ -1210,7 +1208,7 @@ TEST(ReplaceAt, exhaustiveTest) { ...@@ -1210,7 +1208,7 @@ TEST(ReplaceAt, exhaustiveTest) {
std::mt19937 gen(rd()); std::mt19937 gen(rd());
std::uniform_int_distribution<> dist('a', 'z'); std::uniform_int_distribution<> dist('a', 'z');
for (int i=0; i < 100; ++i) { for (int i = 0; i < 100; ++i) {
for (size_t j = 1; j <= msp.size(); ++j) { for (size_t j = 1; j <= msp.size(); ++j) {
auto replacement = get_rand_str(j, dist, gen); auto replacement = get_rand_str(j, dist, gen);
for (size_t pos = 0; pos < msp.size() - j; ++pos) { for (size_t pos = 0; pos < msp.size() - j; ++pos) {
...@@ -1266,7 +1264,7 @@ TEST(ReplaceAll, randomTest) { ...@@ -1266,7 +1264,7 @@ TEST(ReplaceAll, randomTest) {
std::mt19937 gen(rd()); std::mt19937 gen(rd());
std::uniform_int_distribution<> dist('A', 'Z'); std::uniform_int_distribution<> dist('A', 'Z');
for (int i=0; i < 100; ++i) { for (int i = 0; i < 100; ++i) {
for (size_t j = 1; j <= orig.size(); ++j) { for (size_t j = 1; j <= orig.size(); ++j) {
auto replacement = get_rand_str(j, dist, gen); auto replacement = get_rand_str(j, dist, gen);
for (size_t pos = 0; pos < msp.size() - j; ++pos) { for (size_t pos = 0; pos < msp.size() - j; ++pos) {
...@@ -1285,7 +1283,7 @@ TEST(ReplaceAll, BadArg) { ...@@ -1285,7 +1283,7 @@ TEST(ReplaceAll, BadArg) {
auto fst = "longer"; auto fst = "longer";
auto snd = "small"; auto snd = "small";
char input[] = "meh meh meh"; char input[] = "meh meh meh";
auto all = MutableStringPiece(input); auto all = MutableStringPiece(input);
try { try {
all.replaceAll(fst, snd); all.replaceAll(fst, snd);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment