2 * Copyright 2014 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 // @author Mark Rabkin (mrabkin@fb.com)
18 // @author Andrei Alexandrescu (andrei.alexandrescu@fb.com)
20 #ifndef FOLLY_RANGE_H_
21 #define FOLLY_RANGE_H_
23 #include "folly/Portability.h"
24 #include "folly/FBString.h"
25 #include <glog/logging.h>
31 #include <type_traits>
32 #include <boost/operators.hpp>
34 // libc++ doesn't provide this header
36 // This file appears in two locations: inside fbcode and in the
37 // libstdc++ source code (when embedding fbstring as std::string).
38 // To aid in this schizophrenic use, two macros are defined in
40 // _LIBSTDCXX_FBSTRING - Set inside libstdc++. This is useful to
41 // gate use inside fbcode v. libstdc++
42 #include <bits/c++config.h>
45 #include "folly/CpuId.h"
46 #include "folly/Traits.h"
47 #include "folly/Likely.h"
49 // Ignore shadowing warnings within this file, so includers can use -Wshadow.
50 #pragma GCC diagnostic push
51 #pragma GCC diagnostic ignored "-Wshadow"
55 template <class T> class Range;
58 * Finds the first occurrence of needle in haystack. The algorithm is on
59 * average faster than O(haystack.size() * needle.size()) but not as fast
60 * as Boyer-Moore. On the upside, it does not do any upfront
61 * preprocessing and does not allocate memory.
63 template <class T, class Comp = std::equal_to<typename Range<T>::value_type>>
64 inline size_t qfind(const Range<T> & haystack,
65 const Range<T> & needle,
69 * Finds the first occurrence of needle in haystack. The result is the
70 * offset reported to the beginning of haystack, or string::npos if
71 * needle wasn't found.
74 size_t qfind(const Range<T> & haystack,
75 const typename Range<T>::value_type& needle);
78 * Finds the last occurrence of needle in haystack. The result is the
79 * offset reported to the beginning of haystack, or string::npos if
80 * needle wasn't found.
83 size_t rfind(const Range<T> & haystack,
84 const typename Range<T>::value_type& needle);
88 * Finds the first occurrence of any element of needle in
89 * haystack. The algorithm is O(haystack.size() * needle.size()).
92 inline size_t qfind_first_of(const Range<T> & haystack,
93 const Range<T> & needle);
96 * Small internal helper - returns the value just before an iterator.
101 * For random-access iterators, the value before is simply i[-1].
103 template <class Iter>
104 typename std::enable_if<
105 std::is_same<typename std::iterator_traits<Iter>::iterator_category,
106 std::random_access_iterator_tag>::value,
107 typename std::iterator_traits<Iter>::reference>::type
108 value_before(Iter i) {
113 * For all other iterators, we need to use the decrement operator.
115 template <class Iter>
116 typename std::enable_if<
117 !std::is_same<typename std::iterator_traits<Iter>::iterator_category,
118 std::random_access_iterator_tag>::value,
119 typename std::iterator_traits<Iter>::reference>::type
120 value_before(Iter i) {
124 } // namespace detail
127 * Range abstraction keeping a pair of iterators. We couldn't use
128 * boost's similar range abstraction because we need an API identical
129 * with the former StringPiece class, which is used by a lot of other
130 * code. This abstraction does fulfill the needs of boost's
131 * range-oriented algorithms though.
133 * (Keep memory lifetime in mind when using this class, since it
134 * doesn't manage the data it refers to - just like an iterator
137 template <class Iter>
138 class Range : private boost::totally_ordered<Range<Iter> > {
140 typedef std::size_t size_type;
141 typedef Iter iterator;
142 typedef Iter const_iterator;
143 typedef typename std::remove_reference<
144 typename std::iterator_traits<Iter>::reference>::type
146 typedef typename std::iterator_traits<Iter>::reference reference;
147 typedef std::char_traits<typename std::remove_const<value_type>::type>
150 static const size_type npos;
152 // Works for all iterators
153 Range() : b_(), e_() {
157 // Works for all iterators
158 Range(Iter start, Iter end) : b_(start), e_(end) {
161 // Works only for random-access iterators
162 Range(Iter start, size_t size)
163 : b_(start), e_(start + size) { }
165 #if FOLLY_HAVE_CONSTEXPR_STRLEN
166 // Works only for Range<const char*>
167 /* implicit */ constexpr Range(Iter str)
168 : b_(str), e_(str + strlen(str)) {}
170 // Works only for Range<const char*>
171 /* implicit */ Range(Iter str)
172 : b_(str), e_(str + strlen(str)) {}
174 // Works only for Range<const char*>
175 /* implicit */ Range(const std::string& str)
176 : b_(str.data()), e_(b_ + str.size()) {}
177 // Works only for Range<const char*>
178 Range(const std::string& str, std::string::size_type startFrom) {
179 if (UNLIKELY(startFrom > str.size())) {
180 throw std::out_of_range("index out of range");
182 b_ = str.data() + startFrom;
183 e_ = str.data() + str.size();
185 // Works only for Range<const char*>
186 Range(const std::string& str,
187 std::string::size_type startFrom,
188 std::string::size_type size) {
189 if (UNLIKELY(startFrom > str.size())) {
190 throw std::out_of_range("index out of range");
192 b_ = str.data() + startFrom;
193 if (str.size() - startFrom < size) {
194 e_ = str.data() + str.size();
199 Range(const Range<Iter>& str,
202 if (UNLIKELY(startFrom > str.size())) {
203 throw std::out_of_range("index out of range");
205 b_ = str.b_ + startFrom;
206 if (str.size() - startFrom < size) {
212 // Works only for Range<const char*>
213 /* implicit */ Range(const fbstring& str)
214 : b_(str.data()), e_(b_ + str.size()) { }
215 // Works only for Range<const char*>
216 Range(const fbstring& str, fbstring::size_type startFrom) {
217 if (UNLIKELY(startFrom > str.size())) {
218 throw std::out_of_range("index out of range");
220 b_ = str.data() + startFrom;
221 e_ = str.data() + str.size();
223 // Works only for Range<const char*>
224 Range(const fbstring& str, fbstring::size_type startFrom,
225 fbstring::size_type size) {
226 if (UNLIKELY(startFrom > str.size())) {
227 throw std::out_of_range("index out of range");
229 b_ = str.data() + startFrom;
230 if (str.size() - startFrom < size) {
231 e_ = str.data() + str.size();
237 // Allow implicit conversion from Range<const char*> (aka StringPiece) to
238 // Range<const unsigned char*> (aka ByteRange), as they're both frequently
239 // used to represent ranges of bytes. Allow explicit conversion in the other
241 template <class OtherIter, typename std::enable_if<
242 (std::is_same<Iter, const unsigned char*>::value &&
243 (std::is_same<OtherIter, const char*>::value ||
244 std::is_same<OtherIter, char*>::value)), int>::type = 0>
245 /* implicit */ Range(const Range<OtherIter>& other)
246 : b_(reinterpret_cast<const unsigned char*>(other.begin())),
247 e_(reinterpret_cast<const unsigned char*>(other.end())) {
250 template <class OtherIter, typename std::enable_if<
251 (std::is_same<Iter, unsigned char*>::value &&
252 std::is_same<OtherIter, char*>::value), int>::type = 0>
253 /* implicit */ Range(const Range<OtherIter>& other)
254 : b_(reinterpret_cast<unsigned char*>(other.begin())),
255 e_(reinterpret_cast<unsigned char*>(other.end())) {
258 template <class OtherIter, typename std::enable_if<
259 (std::is_same<Iter, const char*>::value &&
260 (std::is_same<OtherIter, const unsigned char*>::value ||
261 std::is_same<OtherIter, unsigned char*>::value)), int>::type = 0>
262 explicit Range(const Range<OtherIter>& other)
263 : b_(reinterpret_cast<const char*>(other.begin())),
264 e_(reinterpret_cast<const char*>(other.end())) {
267 template <class OtherIter, typename std::enable_if<
268 (std::is_same<Iter, char*>::value &&
269 std::is_same<OtherIter, unsigned char*>::value), int>::type = 0>
270 explicit Range(const Range<OtherIter>& other)
271 : b_(reinterpret_cast<char*>(other.begin())),
272 e_(reinterpret_cast<char*>(other.end())) {
275 // Allow implicit conversion from Range<From> to Range<To> if From is
276 // implicitly convertible to To.
277 template <class OtherIter, typename std::enable_if<
278 (!std::is_same<Iter, OtherIter>::value &&
279 std::is_convertible<OtherIter, Iter>::value), int>::type = 0>
280 /* implicit */ Range(const Range<OtherIter>& other)
285 // Allow explicit conversion from Range<From> to Range<To> if From is
286 // explicitly convertible to To.
287 template <class OtherIter, typename std::enable_if<
288 (!std::is_same<Iter, OtherIter>::value &&
289 !std::is_convertible<OtherIter, Iter>::value &&
290 std::is_constructible<Iter, const OtherIter&>::value), int>::type = 0>
291 explicit Range(const Range<OtherIter>& other)
301 void assign(Iter start, Iter end) {
306 void reset(Iter start, size_type size) {
311 // Works only for Range<const char*>
312 void reset(const std::string& str) {
313 reset(str.data(), str.size());
316 size_type size() const {
320 size_type walk_size() const {
322 return std::distance(b_, e_);
324 bool empty() const { return b_ == e_; }
325 Iter data() const { return b_; }
326 Iter start() const { return b_; }
327 Iter begin() const { return b_; }
328 Iter end() const { return e_; }
329 Iter cbegin() const { return b_; }
330 Iter cend() const { return e_; }
331 value_type& front() {
337 return detail::value_before(e_);
339 const value_type& front() const {
343 const value_type& back() const {
345 return detail::value_before(e_);
347 // Works only for Range<const char*>
348 std::string str() const { return std::string(b_, size()); }
349 std::string toString() const { return str(); }
350 // Works only for Range<const char*>
351 fbstring fbstr() const { return fbstring(b_, size()); }
352 fbstring toFbstring() const { return fbstr(); }
354 // Works only for Range<const char*>
355 int compare(const Range& o) const {
356 const size_type tsize = this->size();
357 const size_type osize = o.size();
358 const size_type msize = std::min(tsize, osize);
359 int r = traits_type::compare(data(), o.data(), msize);
360 if (r == 0) r = tsize - osize;
364 value_type& operator[](size_t i) {
365 DCHECK_GT(size(), i);
369 const value_type& operator[](size_t i) const {
370 DCHECK_GT(size(), i);
374 value_type& at(size_t i) {
375 if (i >= size()) throw std::out_of_range("index out of range");
379 const value_type& at(size_t i) const {
380 if (i >= size()) throw std::out_of_range("index out of range");
384 // Works only for Range<const char*>
385 uint32_t hash() const {
386 // Taken from fbi/nstring.h:
387 // Quick and dirty bernstein hash...fine for short ascii strings
388 uint32_t hash = 5381;
389 for (size_t ix = 0; ix < size(); ix++) {
390 hash = ((hash << 5) + hash) + b_[ix];
395 void advance(size_type n) {
396 if (UNLIKELY(n > size())) {
397 throw std::out_of_range("index out of range");
402 void subtract(size_type n) {
403 if (UNLIKELY(n > size())) {
404 throw std::out_of_range("index out of range");
419 Range subpiece(size_type first,
420 size_type length = std::string::npos) const {
421 if (UNLIKELY(first > size())) {
422 throw std::out_of_range("index out of range");
424 return Range(b_ + first,
425 std::min<std::string::size_type>(length, size() - first));
428 // string work-alike functions
429 size_type find(Range str) const {
430 return qfind(*this, str);
433 size_type find(Range str, size_t pos) const {
434 if (pos > size()) return std::string::npos;
435 size_t ret = qfind(subpiece(pos), str);
436 return ret == npos ? ret : ret + pos;
439 size_type find(Iter s, size_t pos, size_t n) const {
440 if (pos > size()) return std::string::npos;
441 size_t ret = qfind(pos ? subpiece(pos) : *this, Range(s, n));
442 return ret == npos ? ret : ret + pos;
445 // Works only for Range<const (unsigned) char*> which have Range(Iter) ctor
446 size_type find(const Iter s) const {
447 return qfind(*this, Range(s));
450 // Works only for Range<const (unsigned) char*> which have Range(Iter) ctor
451 size_type find(const Iter s, size_t pos) const {
452 if (pos > size()) return std::string::npos;
453 size_type ret = qfind(subpiece(pos), Range(s));
454 return ret == npos ? ret : ret + pos;
457 size_type find(value_type c) const {
458 return qfind(*this, c);
461 size_type rfind(value_type c) const {
462 return folly::rfind(*this, c);
465 size_type find(value_type c, size_t pos) const {
466 if (pos > size()) return std::string::npos;
467 size_type ret = qfind(subpiece(pos), c);
468 return ret == npos ? ret : ret + pos;
471 size_type find_first_of(Range needles) const {
472 return qfind_first_of(*this, needles);
475 size_type find_first_of(Range needles, size_t pos) const {
476 if (pos > size()) return std::string::npos;
477 size_type ret = qfind_first_of(subpiece(pos), needles);
478 return ret == npos ? ret : ret + pos;
481 // Works only for Range<const (unsigned) char*> which have Range(Iter) ctor
482 size_type find_first_of(Iter needles) const {
483 return find_first_of(Range(needles));
486 // Works only for Range<const (unsigned) char*> which have Range(Iter) ctor
487 size_type find_first_of(Iter needles, size_t pos) const {
488 return find_first_of(Range(needles), pos);
491 size_type find_first_of(Iter needles, size_t pos, size_t n) const {
492 return find_first_of(Range(needles, n), pos);
495 size_type find_first_of(value_type c) const {
499 size_type find_first_of(value_type c, size_t pos) const {
503 void swap(Range& rhs) {
504 std::swap(b_, rhs.b_);
505 std::swap(e_, rhs.e_);
509 * Does this Range start with another range?
511 bool startsWith(const Range& other) const {
512 return size() >= other.size() && subpiece(0, other.size()) == other;
514 bool startsWith(value_type c) const {
515 return !empty() && front() == c;
519 * Does this Range end with another range?
521 bool endsWith(const Range& other) const {
522 return size() >= other.size() && subpiece(size() - other.size()) == other;
524 bool endsWith(value_type c) const {
525 return !empty() && back() == c;
529 * Remove the given prefix and return true if the range starts with the given
530 * prefix; return false otherwise.
532 bool removePrefix(const Range& prefix) {
533 return startsWith(prefix) && (b_ += prefix.size(), true);
535 bool removePrefix(value_type prefix) {
536 return startsWith(prefix) && (++b_, true);
540 * Remove the given suffix and return true if the range ends with the given
541 * suffix; return false otherwise.
543 bool removeSuffix(const Range& suffix) {
544 return endsWith(suffix) && (e_ -= suffix.size(), true);
546 bool removeSuffix(value_type suffix) {
547 return endsWith(suffix) && (--e_, true);
551 * Splits this `Range` `[b, e)` in the position `i` dictated by the next
552 * occurence of `delimiter`.
554 * Returns a new `Range` `[b, i)` and adjusts this range to start right after
555 * the delimiter's position. This range will be empty if the delimiter is not
556 * found. If called on an empty `Range`, both this and the returned `Range`
561 * folly::StringPiece s("sample string for split_next");
562 * auto p = s.split_step(' ');
567 * // prints "string for split_next"
572 * void tokenize(StringPiece s, char delimiter) {
573 * while (!s.empty()) {
574 * cout << s.split_step(delimiter);
578 * @author: Marcelo Juchem <marcelo@fb.com>
580 Range split_step(value_type delimiter) {
581 auto i = std::find(b_, e_, delimiter);
584 b_ = i == e_ ? e_ : std::next(i);
589 Range split_step(Range delimiter) {
590 auto i = find(delimiter);
591 Range result(b_, i == std::string::npos ? size() : i);
593 b_ = result.end() == e_ ? e_ : std::next(result.end(), delimiter.size());
599 * Convenience method that calls `split_step()` and passes the result to a
600 * functor, returning whatever the functor does.
602 * Say you have a functor with this signature:
604 * Foo fn(Range r) { }
606 * `split_step()`'s return type will be `Foo`. It works just like:
608 * auto result = fn(myRange.split_step(' '));
610 * A functor returning `void` is also supported.
614 * void do_some_parsing(folly::StringPiece s) {
615 * auto version = s.split_step(' ', [&](folly::StringPiece x) {
617 * throw std::invalid_argument("empty string");
619 * return std::strtoull(x.begin(), x.end(), 16);
625 * @author: Marcelo Juchem <marcelo@fb.com>
627 template <typename TProcess>
628 auto split_step(value_type delimiter, TProcess &&process)
629 -> decltype(process(std::declval<Range>()))
630 { return process(split_step(delimiter)); }
632 template <typename TProcess>
633 auto split_step(Range delimiter, TProcess &&process)
634 -> decltype(process(std::declval<Range>()))
635 { return process(split_step(delimiter)); }
641 template <class Iter>
642 const typename Range<Iter>::size_type Range<Iter>::npos = std::string::npos;
645 void swap(Range<T>& lhs, Range<T>& rhs) {
650 * Create a range from two iterators, with type deduction.
652 template <class Iter>
653 Range<Iter> makeRange(Iter first, Iter last) {
654 return Range<Iter>(first, last);
657 typedef Range<const char*> StringPiece;
658 typedef Range<char*> MutableStringPiece;
659 typedef Range<const unsigned char*> ByteRange;
660 typedef Range<unsigned char*> MutableByteRange;
662 std::ostream& operator<<(std::ostream& os, const StringPiece& piece);
665 * Templated comparison operators
669 inline bool operator==(const Range<T>& lhs, const Range<T>& rhs) {
670 return lhs.size() == rhs.size() && lhs.compare(rhs) == 0;
674 inline bool operator<(const Range<T>& lhs, const Range<T>& rhs) {
675 return lhs.compare(rhs) < 0;
679 * Specializations of comparison operators for StringPiece
684 template <class A, class B>
685 struct ComparableAsStringPiece {
688 (std::is_convertible<A, StringPiece>::value
689 && std::is_same<B, StringPiece>::value)
691 (std::is_convertible<B, StringPiece>::value
692 && std::is_same<A, StringPiece>::value)
696 } // namespace detail
699 * operator== through conversion for Range<const char*>
701 template <class T, class U>
703 std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>::type
704 operator==(const T& lhs, const U& rhs) {
705 return StringPiece(lhs) == StringPiece(rhs);
709 * operator< through conversion for Range<const char*>
711 template <class T, class U>
713 std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>::type
714 operator<(const T& lhs, const U& rhs) {
715 return StringPiece(lhs) < StringPiece(rhs);
719 * operator> through conversion for Range<const char*>
721 template <class T, class U>
723 std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>::type
724 operator>(const T& lhs, const U& rhs) {
725 return StringPiece(lhs) > StringPiece(rhs);
729 * operator< through conversion for Range<const char*>
731 template <class T, class U>
733 std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>::type
734 operator<=(const T& lhs, const U& rhs) {
735 return StringPiece(lhs) <= StringPiece(rhs);
739 * operator> through conversion for Range<const char*>
741 template <class T, class U>
743 std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>::type
744 operator>=(const T& lhs, const U& rhs) {
745 return StringPiece(lhs) >= StringPiece(rhs);
748 struct StringPieceHash {
749 std::size_t operator()(const StringPiece& str) const {
750 return static_cast<std::size_t>(str.hash());
755 * Finds substrings faster than brute force by borrowing from Boyer-Moore
757 template <class T, class Comp>
758 size_t qfind(const Range<T>& haystack,
759 const Range<T>& needle,
761 // Don't use std::search, use a Boyer-Moore-like trick by comparing
762 // the last characters first
763 auto const nsize = needle.size();
764 if (haystack.size() < nsize) {
765 return std::string::npos;
767 if (!nsize) return 0;
768 auto const nsize_1 = nsize - 1;
769 auto const lastNeedle = needle[nsize_1];
771 // Boyer-Moore skip value for the last char in the needle. Zero is
772 // not a valid value; skip will be computed the first time it's
774 std::string::size_type skip = 0;
776 auto i = haystack.begin();
777 auto iEnd = haystack.end() - nsize_1;
780 // Boyer-Moore: match the last element in the needle
781 while (!eq(i[nsize_1], lastNeedle)) {
784 return std::string::npos;
787 // Here we know that the last char matches
788 // Continue in pedestrian mode
789 for (size_t j = 0; ; ) {
791 if (!eq(i[j], needle[j])) {
792 // Not found, we can skip
793 // Compute the skip value lazily
796 while (skip <= nsize_1 && !eq(needle[nsize_1 - skip], lastNeedle)) {
803 // Check if done searching
806 return i - haystack.begin();
810 return std::string::npos;
815 size_t qfind_first_byte_of_nosse(const StringPiece& haystack,
816 const StringPiece& needles);
818 #if FOLLY_HAVE_EMMINTRIN_H && __GNUC_PREREQ(4, 6)
819 size_t qfind_first_byte_of_sse42(const StringPiece& haystack,
820 const StringPiece& needles);
822 inline size_t qfind_first_byte_of(const StringPiece& haystack,
823 const StringPiece& needles) {
824 static auto const qfind_first_byte_of_fn =
825 folly::CpuId().sse42() ? qfind_first_byte_of_sse42
826 : qfind_first_byte_of_nosse;
827 return qfind_first_byte_of_fn(haystack, needles);
831 inline size_t qfind_first_byte_of(const StringPiece& haystack,
832 const StringPiece& needles) {
833 return qfind_first_byte_of_nosse(haystack, needles);
835 #endif // FOLLY_HAVE_EMMINTRIN_H
837 } // namespace detail
839 template <class T, class Comp>
840 size_t qfind_first_of(const Range<T> & haystack,
841 const Range<T> & needles,
843 auto ret = std::find_first_of(haystack.begin(), haystack.end(),
844 needles.begin(), needles.end(),
846 return ret == haystack.end() ? std::string::npos : ret - haystack.begin();
849 struct AsciiCaseSensitive {
850 bool operator()(char lhs, char rhs) const {
855 struct AsciiCaseInsensitive {
856 bool operator()(char lhs, char rhs) const {
857 return toupper(lhs) == toupper(rhs);
861 extern const AsciiCaseSensitive asciiCaseSensitive;
862 extern const AsciiCaseInsensitive asciiCaseInsensitive;
865 size_t qfind(const Range<T>& haystack,
866 const typename Range<T>::value_type& needle) {
867 auto pos = std::find(haystack.begin(), haystack.end(), needle);
868 return pos == haystack.end() ? std::string::npos : pos - haystack.data();
872 size_t rfind(const Range<T>& haystack,
873 const typename Range<T>::value_type& needle) {
874 for (auto i = haystack.size(); i-- > 0; ) {
875 if (haystack[i] == needle) {
879 return std::string::npos;
882 // specialization for StringPiece
884 inline size_t qfind(const Range<const char*>& haystack, const char& needle) {
885 auto pos = static_cast<const char*>(
886 ::memchr(haystack.data(), needle, haystack.size()));
887 return pos == nullptr ? std::string::npos : pos - haystack.data();
890 #if FOLLY_HAVE_MEMRCHR
892 inline size_t rfind(const Range<const char*>& haystack, const char& needle) {
893 auto pos = static_cast<const char*>(
894 ::memrchr(haystack.data(), needle, haystack.size()));
895 return pos == nullptr ? std::string::npos : pos - haystack.data();
899 // specialization for ByteRange
901 inline size_t qfind(const Range<const unsigned char*>& haystack,
902 const unsigned char& needle) {
903 auto pos = static_cast<const unsigned char*>(
904 ::memchr(haystack.data(), needle, haystack.size()));
905 return pos == nullptr ? std::string::npos : pos - haystack.data();
908 #if FOLLY_HAVE_MEMRCHR
910 inline size_t rfind(const Range<const unsigned char*>& haystack,
911 const unsigned char& needle) {
912 auto pos = static_cast<const unsigned char*>(
913 ::memrchr(haystack.data(), needle, haystack.size()));
914 return pos == nullptr ? std::string::npos : pos - haystack.data();
919 size_t qfind_first_of(const Range<T>& haystack,
920 const Range<T>& needles) {
921 return qfind_first_of(haystack, needles, asciiCaseSensitive);
924 // specialization for StringPiece
926 inline size_t qfind_first_of(const Range<const char*>& haystack,
927 const Range<const char*>& needles) {
928 return detail::qfind_first_byte_of(haystack, needles);
931 // specialization for ByteRange
933 inline size_t qfind_first_of(const Range<const unsigned char*>& haystack,
934 const Range<const unsigned char*>& needles) {
935 return detail::qfind_first_byte_of(StringPiece(haystack),
936 StringPiece(needles));
938 } // !namespace folly
940 #pragma GCC diagnostic pop
942 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(folly::Range);
944 #endif // FOLLY_RANGE_H_