487bc4edf5ca1f85e2faddda771dc41832cee0e3
[folly.git] / folly / Range.h
1 /*
2  * Copyright 2013 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 // @author Mark Rabkin (mrabkin@fb.com)
18 // @author Andrei Alexandrescu (andrei.alexandrescu@fb.com)
19
20 #ifndef FOLLY_RANGE_H_
21 #define FOLLY_RANGE_H_
22
23 #include "folly/Portability.h"
24 #include "folly/FBString.h"
25 #include <glog/logging.h>
26 #include <algorithm>
27 #include <cstring>
28 #include <iosfwd>
29 #include <string>
30 #include <stdexcept>
31 #include <type_traits>
32 #include <boost/operators.hpp>
33
34 // libc++ doesn't provide this header
35 #if !FOLLY_USE_LIBCPP
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
39 // c++config.h:
40 //   _LIBSTDCXX_FBSTRING - Set inside libstdc++.  This is useful to
41 //      gate use inside fbcode v. libstdc++
42 #include <bits/c++config.h>
43 #endif
44
45 #include "folly/CpuId.h"
46 #include "folly/Traits.h"
47 #include "folly/Likely.h"
48
49 // Ignore shadowing warnings within this file, so includers can use -Wshadow.
50 #pragma GCC diagnostic push
51 #pragma GCC diagnostic ignored "-Wshadow"
52
53 namespace folly {
54
55 template <class T> class Range;
56
57 /**
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.
62  */
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,
66                     Comp eq = Comp());
67
68 /**
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.
72  */
73 template <class T>
74 size_t qfind(const Range<T> & haystack,
75              const typename Range<T>::value_type& needle);
76
77 /**
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.
81  */
82 template <class T>
83 size_t rfind(const Range<T> & haystack,
84              const typename Range<T>::value_type& needle);
85
86
87 /**
88  * Finds the first occurrence of any element of needle in
89  * haystack. The algorithm is O(haystack.size() * needle.size()).
90  */
91 template <class T>
92 inline size_t qfind_first_of(const Range<T> & haystack,
93                              const Range<T> & needle);
94
95 /**
96  * Small internal helper - returns the value just before an iterator.
97  */
98 namespace detail {
99
100 /**
101  * For random-access iterators, the value before is simply i[-1].
102  */
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) {
109   return i[-1];
110 }
111
112 /**
113  * For all other iterators, we need to use the decrement operator.
114  */
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) {
121   return *--i;
122 }
123
124 } // namespace detail
125
126 /**
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.
132  *
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
135  * wouldn't.)
136  */
137 template <class Iter>
138 class Range : private boost::totally_ordered<Range<Iter> > {
139 public:
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
145   value_type;
146   typedef typename std::iterator_traits<Iter>::reference reference;
147   typedef std::char_traits<typename std::remove_const<value_type>::type>
148     traits_type;
149
150   static const size_type npos;
151
152   // Works for all iterators
153   Range() : b_(), e_() {
154   }
155
156 public:
157   // Works for all iterators
158   Range(Iter start, Iter end) : b_(start), e_(end) {
159   }
160
161   // Works only for random-access iterators
162   Range(Iter start, size_t size)
163       : b_(start), e_(start + size) { }
164
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)) {}
169 #else
170   // Works only for Range<const char*>
171   /* implicit */ Range(Iter str)
172       : b_(str), e_(str + strlen(str)) {}
173 #endif
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");
181     }
182     b_ = str.data() + startFrom;
183     e_ = str.data() + str.size();
184   }
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");
191     }
192     b_ = str.data() + startFrom;
193     if (str.size() - startFrom < size) {
194       e_ = str.data() + str.size();
195     } else {
196       e_ = b_ + size;
197     }
198   }
199   Range(const Range<Iter>& str,
200         size_t startFrom,
201         size_t size) {
202     if (UNLIKELY(startFrom > str.size())) {
203       throw std::out_of_range("index out of range");
204     }
205     b_ = str.b_ + startFrom;
206     if (str.size() - startFrom < size) {
207       e_ = str.e_;
208     } else {
209       e_ = b_ + size;
210     }
211   }
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");
219     }
220     b_ = str.data() + startFrom;
221     e_ = str.data() + str.size();
222   }
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");
228     }
229     b_ = str.data() + startFrom;
230     if (str.size() - startFrom < size) {
231       e_ = str.data() + str.size();
232     } else {
233       e_ = b_ + size;
234     }
235   }
236
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
240   // direction.
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), int>::type = 0>
244   /* implicit */ Range(const Range<OtherIter>& other)
245     : b_(reinterpret_cast<const unsigned char*>(other.begin())),
246       e_(reinterpret_cast<const unsigned char*>(other.end())) {
247   }
248
249   template <class OtherIter, typename std::enable_if<
250       (std::is_same<Iter, const char*>::value &&
251        std::is_same<OtherIter, const unsigned char*>::value), int>::type = 0>
252   explicit Range(const Range<OtherIter>& other)
253     : b_(reinterpret_cast<const char*>(other.begin())),
254       e_(reinterpret_cast<const char*>(other.end())) {
255   }
256
257   void clear() {
258     b_ = Iter();
259     e_ = Iter();
260   }
261
262   void assign(Iter start, Iter end) {
263     b_ = start;
264     e_ = end;
265   }
266
267   void reset(Iter start, size_type size) {
268     b_ = start;
269     e_ = start + size;
270   }
271
272   // Works only for Range<const char*>
273   void reset(const std::string& str) {
274     reset(str.data(), str.size());
275   }
276
277   size_type size() const {
278     assert(b_ <= e_);
279     return e_ - b_;
280   }
281   size_type walk_size() const {
282     assert(b_ <= e_);
283     return std::distance(b_, e_);
284   }
285   bool empty() const { return b_ == e_; }
286   Iter data() const { return b_; }
287   Iter start() const { return b_; }
288   Iter begin() const { return b_; }
289   Iter end() const { return e_; }
290   Iter cbegin() const { return b_; }
291   Iter cend() const { return e_; }
292   value_type& front() {
293     assert(b_ < e_);
294     return *b_;
295   }
296   value_type& back() {
297     assert(b_ < e_);
298     return detail::value_before(e_);
299   }
300   const value_type& front() const {
301     assert(b_ < e_);
302     return *b_;
303   }
304   const value_type& back() const {
305     assert(b_ < e_);
306     return detail::value_before(e_);
307   }
308   // Works only for Range<const char*>
309   std::string str() const { return std::string(b_, size()); }
310   std::string toString() const { return str(); }
311   // Works only for Range<const char*>
312   fbstring fbstr() const { return fbstring(b_, size()); }
313   fbstring toFbstring() const { return fbstr(); }
314
315   // Works only for Range<const char*>
316   int compare(const Range& o) const {
317     const size_type tsize = this->size();
318     const size_type osize = o.size();
319     const size_type msize = std::min(tsize, osize);
320     int r = traits_type::compare(data(), o.data(), msize);
321     if (r == 0) r = tsize - osize;
322     return r;
323   }
324
325   value_type& operator[](size_t i) {
326     CHECK_GT(size(), i);
327     return b_[i];
328   }
329
330   const value_type& operator[](size_t i) const {
331     CHECK_GT(size(), i);
332     return b_[i];
333   }
334
335   value_type& at(size_t i) {
336     if (i >= size()) throw std::out_of_range("index out of range");
337     return b_[i];
338   }
339
340   const value_type& at(size_t i) const {
341     if (i >= size()) throw std::out_of_range("index out of range");
342     return b_[i];
343   }
344
345   // Works only for Range<const char*>
346   uint32_t hash() const {
347     // Taken from fbi/nstring.h:
348     //    Quick and dirty bernstein hash...fine for short ascii strings
349     uint32_t hash = 5381;
350     for (size_t ix = 0; ix < size(); ix++) {
351       hash = ((hash << 5) + hash) + b_[ix];
352     }
353     return hash;
354   }
355
356   void advance(size_type n) {
357     if (UNLIKELY(n > size())) {
358       throw std::out_of_range("index out of range");
359     }
360     b_ += n;
361   }
362
363   void subtract(size_type n) {
364     if (UNLIKELY(n > size())) {
365       throw std::out_of_range("index out of range");
366     }
367     e_ -= n;
368   }
369
370   void pop_front() {
371     assert(b_ < e_);
372     ++b_;
373   }
374
375   void pop_back() {
376     assert(b_ < e_);
377     --e_;
378   }
379
380   Range subpiece(size_type first,
381                  size_type length = std::string::npos) const {
382     if (UNLIKELY(first > size())) {
383       throw std::out_of_range("index out of range");
384     }
385     return Range(b_ + first,
386                  std::min<std::string::size_type>(length, size() - first));
387   }
388
389   // string work-alike functions
390   size_type find(Range str) const {
391     return qfind(*this, str);
392   }
393
394   size_type find(Range str, size_t pos) const {
395     if (pos > size()) return std::string::npos;
396     size_t ret = qfind(subpiece(pos), str);
397     return ret == npos ? ret : ret + pos;
398   }
399
400   size_type find(Iter s, size_t pos, size_t n) const {
401     if (pos > size()) return std::string::npos;
402     size_t ret = qfind(pos ? subpiece(pos) : *this, Range(s, n));
403     return ret == npos ? ret : ret + pos;
404   }
405
406   // Works only for Range<const (unsigned) char*> which have Range(Iter) ctor
407   size_type find(const Iter s) const {
408     return qfind(*this, Range(s));
409   }
410
411   // Works only for Range<const (unsigned) char*> which have Range(Iter) ctor
412   size_type find(const Iter s, size_t pos) const {
413     if (pos > size()) return std::string::npos;
414     size_type ret = qfind(subpiece(pos), Range(s));
415     return ret == npos ? ret : ret + pos;
416   }
417
418   size_type find(value_type c) const {
419     return qfind(*this, c);
420   }
421
422   size_type rfind(value_type c) const {
423     return folly::rfind(*this, c);
424   }
425
426   size_type find(value_type c, size_t pos) const {
427     if (pos > size()) return std::string::npos;
428     size_type ret = qfind(subpiece(pos), c);
429     return ret == npos ? ret : ret + pos;
430   }
431
432   size_type find_first_of(Range needles) const {
433     return qfind_first_of(*this, needles);
434   }
435
436   size_type find_first_of(Range needles, size_t pos) const {
437     if (pos > size()) return std::string::npos;
438     size_type ret = qfind_first_of(subpiece(pos), needles);
439     return ret == npos ? ret : ret + pos;
440   }
441
442   // Works only for Range<const (unsigned) char*> which have Range(Iter) ctor
443   size_type find_first_of(Iter needles) const {
444     return find_first_of(Range(needles));
445   }
446
447   // Works only for Range<const (unsigned) char*> which have Range(Iter) ctor
448   size_type find_first_of(Iter needles, size_t pos) const {
449     return find_first_of(Range(needles), pos);
450   }
451
452   size_type find_first_of(Iter needles, size_t pos, size_t n) const {
453     return find_first_of(Range(needles, n), pos);
454   }
455
456   size_type find_first_of(value_type c) const {
457     return find(c);
458   }
459
460   size_type find_first_of(value_type c, size_t pos) const {
461     return find(c, pos);
462   }
463
464   void swap(Range& rhs) {
465     std::swap(b_, rhs.b_);
466     std::swap(e_, rhs.e_);
467   }
468
469 private:
470   Iter b_, e_;
471 };
472
473 template <class Iter>
474 const typename Range<Iter>::size_type Range<Iter>::npos = std::string::npos;
475
476 template <class T>
477 void swap(Range<T>& lhs, Range<T>& rhs) {
478   lhs.swap(rhs);
479 }
480
481 /**
482  * Create a range from two iterators, with type deduction.
483  */
484 template <class Iter>
485 Range<Iter> makeRange(Iter first, Iter last) {
486   return Range<Iter>(first, last);
487 }
488
489 typedef Range<const char*> StringPiece;
490 typedef Range<const unsigned char*> ByteRange;
491
492 std::ostream& operator<<(std::ostream& os, const StringPiece& piece);
493
494 /**
495  * Templated comparison operators
496  */
497
498 template <class T>
499 inline bool operator==(const Range<T>& lhs, const Range<T>& rhs) {
500   return lhs.size() == rhs.size() && lhs.compare(rhs) == 0;
501 }
502
503 template <class T>
504 inline bool operator<(const Range<T>& lhs, const Range<T>& rhs) {
505   return lhs.compare(rhs) < 0;
506 }
507
508 /**
509  * Specializations of comparison operators for StringPiece
510  */
511
512 namespace detail {
513
514 template <class A, class B>
515 struct ComparableAsStringPiece {
516   enum {
517     value =
518     (std::is_convertible<A, StringPiece>::value
519      && std::is_same<B, StringPiece>::value)
520     ||
521     (std::is_convertible<B, StringPiece>::value
522      && std::is_same<A, StringPiece>::value)
523   };
524 };
525
526 } // namespace detail
527
528 /**
529  * operator== through conversion for Range<const char*>
530  */
531 template <class T, class U>
532 typename
533 std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>::type
534 operator==(const T& lhs, const U& rhs) {
535   return StringPiece(lhs) == StringPiece(rhs);
536 }
537
538 /**
539  * operator< through conversion for Range<const char*>
540  */
541 template <class T, class U>
542 typename
543 std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>::type
544 operator<(const T& lhs, const U& rhs) {
545   return StringPiece(lhs) < StringPiece(rhs);
546 }
547
548 /**
549  * operator> through conversion for Range<const char*>
550  */
551 template <class T, class U>
552 typename
553 std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>::type
554 operator>(const T& lhs, const U& rhs) {
555   return StringPiece(lhs) > StringPiece(rhs);
556 }
557
558 /**
559  * operator< through conversion for Range<const char*>
560  */
561 template <class T, class U>
562 typename
563 std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>::type
564 operator<=(const T& lhs, const U& rhs) {
565   return StringPiece(lhs) <= StringPiece(rhs);
566 }
567
568 /**
569  * operator> through conversion for Range<const char*>
570  */
571 template <class T, class U>
572 typename
573 std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>::type
574 operator>=(const T& lhs, const U& rhs) {
575   return StringPiece(lhs) >= StringPiece(rhs);
576 }
577
578 struct StringPieceHash {
579   std::size_t operator()(const StringPiece& str) const {
580     return static_cast<std::size_t>(str.hash());
581   }
582 };
583
584 /**
585  * Finds substrings faster than brute force by borrowing from Boyer-Moore
586  */
587 template <class T, class Comp>
588 size_t qfind(const Range<T>& haystack,
589              const Range<T>& needle,
590              Comp eq) {
591   // Don't use std::search, use a Boyer-Moore-like trick by comparing
592   // the last characters first
593   auto const nsize = needle.size();
594   if (haystack.size() < nsize) {
595     return std::string::npos;
596   }
597   if (!nsize) return 0;
598   auto const nsize_1 = nsize - 1;
599   auto const lastNeedle = needle[nsize_1];
600
601   // Boyer-Moore skip value for the last char in the needle. Zero is
602   // not a valid value; skip will be computed the first time it's
603   // needed.
604   std::string::size_type skip = 0;
605
606   auto i = haystack.begin();
607   auto iEnd = haystack.end() - nsize_1;
608
609   while (i < iEnd) {
610     // Boyer-Moore: match the last element in the needle
611     while (!eq(i[nsize_1], lastNeedle)) {
612       if (++i == iEnd) {
613         // not found
614         return std::string::npos;
615       }
616     }
617     // Here we know that the last char matches
618     // Continue in pedestrian mode
619     for (size_t j = 0; ; ) {
620       assert(j < nsize);
621       if (!eq(i[j], needle[j])) {
622         // Not found, we can skip
623         // Compute the skip value lazily
624         if (skip == 0) {
625           skip = 1;
626           while (skip <= nsize_1 && !eq(needle[nsize_1 - skip], lastNeedle)) {
627             ++skip;
628           }
629         }
630         i += skip;
631         break;
632       }
633       // Check if done searching
634       if (++j == nsize) {
635         // Yay
636         return i - haystack.begin();
637       }
638     }
639   }
640   return std::string::npos;
641 }
642
643 namespace detail {
644
645 size_t qfind_first_byte_of_nosse(const StringPiece& haystack,
646                                  const StringPiece& needles);
647
648 #if FOLLY_HAVE_EMMINTRIN_H && __GNUC_PREREQ(4, 6)
649 size_t qfind_first_byte_of_sse42(const StringPiece& haystack,
650                                  const StringPiece& needles);
651
652 inline size_t qfind_first_byte_of(const StringPiece& haystack,
653                                   const StringPiece& needles) {
654   static auto const qfind_first_byte_of_fn =
655     folly::CpuId().sse42() ? qfind_first_byte_of_sse42
656                            : qfind_first_byte_of_nosse;
657   return qfind_first_byte_of_fn(haystack, needles);
658 }
659
660 #else
661 inline size_t qfind_first_byte_of(const StringPiece& haystack,
662                                   const StringPiece& needles) {
663   return qfind_first_byte_of_nosse(haystack, needles);
664 }
665 #endif // FOLLY_HAVE_EMMINTRIN_H
666
667 } // namespace detail
668
669 template <class T, class Comp>
670 size_t qfind_first_of(const Range<T> & haystack,
671                       const Range<T> & needles,
672                       Comp eq) {
673   auto ret = std::find_first_of(haystack.begin(), haystack.end(),
674                                 needles.begin(), needles.end(),
675                                 eq);
676   return ret == haystack.end() ? std::string::npos : ret - haystack.begin();
677 }
678
679 struct AsciiCaseSensitive {
680   bool operator()(char lhs, char rhs) const {
681     return lhs == rhs;
682   }
683 };
684
685 struct AsciiCaseInsensitive {
686   bool operator()(char lhs, char rhs) const {
687     return toupper(lhs) == toupper(rhs);
688   }
689 };
690
691 extern const AsciiCaseSensitive asciiCaseSensitive;
692 extern const AsciiCaseInsensitive asciiCaseInsensitive;
693
694 template <class T>
695 size_t qfind(const Range<T>& haystack,
696              const typename Range<T>::value_type& needle) {
697   auto pos = std::find(haystack.begin(), haystack.end(), needle);
698   return pos == haystack.end() ? std::string::npos : pos - haystack.data();
699 }
700
701 template <class T>
702 size_t rfind(const Range<T>& haystack,
703              const typename Range<T>::value_type& needle) {
704   for (auto i = haystack.size(); i-- > 0; ) {
705     if (haystack[i] == needle) {
706       return i;
707     }
708   }
709   return std::string::npos;
710 }
711
712 // specialization for StringPiece
713 template <>
714 inline size_t qfind(const Range<const char*>& haystack, const char& needle) {
715   auto pos = static_cast<const char*>(
716     ::memchr(haystack.data(), needle, haystack.size()));
717   return pos == nullptr ? std::string::npos : pos - haystack.data();
718 }
719
720 #if FOLLY_HAVE_MEMRCHR
721 template <>
722 inline size_t rfind(const Range<const char*>& haystack, const char& needle) {
723   auto pos = static_cast<const char*>(
724     ::memrchr(haystack.data(), needle, haystack.size()));
725   return pos == nullptr ? std::string::npos : pos - haystack.data();
726 }
727 #endif
728
729 // specialization for ByteRange
730 template <>
731 inline size_t qfind(const Range<const unsigned char*>& haystack,
732                     const unsigned char& needle) {
733   auto pos = static_cast<const unsigned char*>(
734     ::memchr(haystack.data(), needle, haystack.size()));
735   return pos == nullptr ? std::string::npos : pos - haystack.data();
736 }
737
738 #if FOLLY_HAVE_MEMRCHR
739 template <>
740 inline size_t rfind(const Range<const unsigned char*>& haystack,
741                     const unsigned char& needle) {
742   auto pos = static_cast<const unsigned char*>(
743     ::memrchr(haystack.data(), needle, haystack.size()));
744   return pos == nullptr ? std::string::npos : pos - haystack.data();
745 }
746 #endif
747
748 template <class T>
749 size_t qfind_first_of(const Range<T>& haystack,
750                       const Range<T>& needles) {
751   return qfind_first_of(haystack, needles, asciiCaseSensitive);
752 }
753
754 // specialization for StringPiece
755 template <>
756 inline size_t qfind_first_of(const Range<const char*>& haystack,
757                              const Range<const char*>& needles) {
758   return detail::qfind_first_byte_of(haystack, needles);
759 }
760
761 // specialization for ByteRange
762 template <>
763 inline size_t qfind_first_of(const Range<const unsigned char*>& haystack,
764                              const Range<const unsigned char*>& needles) {
765   return detail::qfind_first_byte_of(StringPiece(haystack),
766                                      StringPiece(needles));
767 }
768 }  // !namespace folly
769
770 #pragma GCC diagnostic pop
771
772 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(folly::Range);
773
774 #endif // FOLLY_RANGE_H_