Add startsWith, endsWith, removePrefix, removeSuffix to folly::Range
[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 #include <bits/c++config.h>
34 #include "folly/CpuId.h"
35 #include "folly/Traits.h"
36 #include "folly/Likely.h"
37
38 // Ignore shadowing warnings within this file, so includers can use -Wshadow.
39 #pragma GCC diagnostic push
40 #pragma GCC diagnostic ignored "-Wshadow"
41
42 namespace folly {
43
44 template <class T> class Range;
45
46 /**
47  * Finds the first occurrence of needle in haystack. The algorithm is on
48  * average faster than O(haystack.size() * needle.size()) but not as fast
49  * as Boyer-Moore. On the upside, it does not do any upfront
50  * preprocessing and does not allocate memory.
51  */
52 template <class T, class Comp = std::equal_to<typename Range<T>::value_type>>
53 inline size_t qfind(const Range<T> & haystack,
54                     const Range<T> & needle,
55                     Comp eq = Comp());
56
57 /**
58  * Finds the first occurrence of needle in haystack. The result is the
59  * offset reported to the beginning of haystack, or string::npos if
60  * needle wasn't found.
61  */
62 template <class T>
63 size_t qfind(const Range<T> & haystack,
64              const typename Range<T>::value_type& needle);
65
66 /**
67  * Finds the last occurrence of needle in haystack. The result is the
68  * offset reported to the beginning of haystack, or string::npos if
69  * needle wasn't found.
70  */
71 template <class T>
72 size_t rfind(const Range<T> & haystack,
73              const typename Range<T>::value_type& needle);
74
75
76 /**
77  * Finds the first occurrence of any element of needle in
78  * haystack. The algorithm is O(haystack.size() * needle.size()).
79  */
80 template <class T>
81 inline size_t qfind_first_of(const Range<T> & haystack,
82                              const Range<T> & needle);
83
84 /**
85  * Small internal helper - returns the value just before an iterator.
86  */
87 namespace detail {
88
89 /**
90  * For random-access iterators, the value before is simply i[-1].
91  */
92 template <class Iter>
93 typename std::enable_if<
94   std::is_same<typename std::iterator_traits<Iter>::iterator_category,
95                std::random_access_iterator_tag>::value,
96   typename std::iterator_traits<Iter>::reference>::type
97 value_before(Iter i) {
98   return i[-1];
99 }
100
101 /**
102  * For all other iterators, we need to use the decrement operator.
103  */
104 template <class Iter>
105 typename std::enable_if<
106   !std::is_same<typename std::iterator_traits<Iter>::iterator_category,
107                 std::random_access_iterator_tag>::value,
108   typename std::iterator_traits<Iter>::reference>::type
109 value_before(Iter i) {
110   return *--i;
111 }
112
113 } // namespace detail
114
115 /**
116  * Range abstraction keeping a pair of iterators. We couldn't use
117  * boost's similar range abstraction because we need an API identical
118  * with the former StringPiece class, which is used by a lot of other
119  * code. This abstraction does fulfill the needs of boost's
120  * range-oriented algorithms though.
121  *
122  * (Keep memory lifetime in mind when using this class, since it
123  * doesn't manage the data it refers to - just like an iterator
124  * wouldn't.)
125  */
126 template <class Iter>
127 class Range : private boost::totally_ordered<Range<Iter> > {
128 public:
129   typedef std::size_t size_type;
130   typedef Iter iterator;
131   typedef Iter const_iterator;
132   typedef typename std::remove_reference<
133     typename std::iterator_traits<Iter>::reference>::type
134   value_type;
135   typedef typename std::iterator_traits<Iter>::reference reference;
136   typedef std::char_traits<typename std::remove_const<value_type>::type>
137     traits_type;
138
139   static const size_type npos;
140
141   // Works for all iterators
142   Range() : b_(), e_() {
143   }
144
145 public:
146   // Works for all iterators
147   Range(Iter start, Iter end) : b_(start), e_(end) {
148   }
149
150   // Works only for random-access iterators
151   Range(Iter start, size_t size)
152       : b_(start), e_(start + size) { }
153
154 #if FOLLY_HAVE_CONSTEXPR_STRLEN
155   // Works only for Range<const char*>
156   /* implicit */ constexpr Range(Iter str)
157       : b_(str), e_(str + strlen(str)) {}
158 #else
159   // Works only for Range<const char*>
160   /* implicit */ Range(Iter str)
161       : b_(str), e_(str + strlen(str)) {}
162 #endif
163   // Works only for Range<const char*>
164   /* implicit */ Range(const std::string& str)
165       : b_(str.data()), e_(b_ + str.size()) {}
166   // Works only for Range<const char*>
167   Range(const std::string& str, std::string::size_type startFrom) {
168     if (UNLIKELY(startFrom > str.size())) {
169       throw std::out_of_range("index out of range");
170     }
171     b_ = str.data() + startFrom;
172     e_ = str.data() + str.size();
173   }
174   // Works only for Range<const char*>
175   Range(const std::string& str,
176         std::string::size_type startFrom,
177         std::string::size_type size) {
178     if (UNLIKELY(startFrom > str.size())) {
179       throw std::out_of_range("index out of range");
180     }
181     b_ = str.data() + startFrom;
182     if (str.size() - startFrom < size) {
183       e_ = str.data() + str.size();
184     } else {
185       e_ = b_ + size;
186     }
187   }
188   Range(const Range<Iter>& str,
189         size_t startFrom,
190         size_t size) {
191     if (UNLIKELY(startFrom > str.size())) {
192       throw std::out_of_range("index out of range");
193     }
194     b_ = str.b_ + startFrom;
195     if (str.size() - startFrom < size) {
196       e_ = str.e_;
197     } else {
198       e_ = b_ + size;
199     }
200   }
201   // Works only for Range<const char*>
202   /* implicit */ Range(const fbstring& str)
203     : b_(str.data()), e_(b_ + str.size()) { }
204   // Works only for Range<const char*>
205   Range(const fbstring& str, fbstring::size_type startFrom) {
206     if (UNLIKELY(startFrom > str.size())) {
207       throw std::out_of_range("index out of range");
208     }
209     b_ = str.data() + startFrom;
210     e_ = str.data() + str.size();
211   }
212   // Works only for Range<const char*>
213   Range(const fbstring& str, fbstring::size_type startFrom,
214         fbstring::size_type size) {
215     if (UNLIKELY(startFrom > str.size())) {
216       throw std::out_of_range("index out of range");
217     }
218     b_ = str.data() + startFrom;
219     if (str.size() - startFrom < size) {
220       e_ = str.data() + str.size();
221     } else {
222       e_ = b_ + size;
223     }
224   }
225
226   // Allow implicit conversion from Range<const char*> (aka StringPiece) to
227   // Range<const unsigned char*> (aka ByteRange), as they're both frequently
228   // used to represent ranges of bytes.  Allow explicit conversion in the other
229   // direction.
230   template <class OtherIter, typename std::enable_if<
231       (std::is_same<Iter, const unsigned char*>::value &&
232        std::is_same<OtherIter, const char*>::value), int>::type = 0>
233   /* implicit */ Range(const Range<OtherIter>& other)
234     : b_(reinterpret_cast<const unsigned char*>(other.begin())),
235       e_(reinterpret_cast<const unsigned char*>(other.end())) {
236   }
237
238   template <class OtherIter, typename std::enable_if<
239       (std::is_same<Iter, const char*>::value &&
240        std::is_same<OtherIter, const unsigned char*>::value), int>::type = 0>
241   explicit Range(const Range<OtherIter>& other)
242     : b_(reinterpret_cast<const char*>(other.begin())),
243       e_(reinterpret_cast<const char*>(other.end())) {
244   }
245
246   void clear() {
247     b_ = Iter();
248     e_ = Iter();
249   }
250
251   void assign(Iter start, Iter end) {
252     b_ = start;
253     e_ = end;
254   }
255
256   void reset(Iter start, size_type size) {
257     b_ = start;
258     e_ = start + size;
259   }
260
261   // Works only for Range<const char*>
262   void reset(const std::string& str) {
263     reset(str.data(), str.size());
264   }
265
266   size_type size() const {
267     assert(b_ <= e_);
268     return e_ - b_;
269   }
270   size_type walk_size() const {
271     assert(b_ <= e_);
272     return std::distance(b_, e_);
273   }
274   bool empty() const { return b_ == e_; }
275   Iter data() const { return b_; }
276   Iter start() const { return b_; }
277   Iter begin() const { return b_; }
278   Iter end() const { return e_; }
279   Iter cbegin() const { return b_; }
280   Iter cend() const { return e_; }
281   value_type& front() {
282     assert(b_ < e_);
283     return *b_;
284   }
285   value_type& back() {
286     assert(b_ < e_);
287     return detail::value_before(e_);
288   }
289   const value_type& front() const {
290     assert(b_ < e_);
291     return *b_;
292   }
293   const value_type& back() const {
294     assert(b_ < e_);
295     return detail::value_before(e_);
296   }
297   // Works only for Range<const char*>
298   std::string str() const { return std::string(b_, size()); }
299   std::string toString() const { return str(); }
300   // Works only for Range<const char*>
301   fbstring fbstr() const { return fbstring(b_, size()); }
302   fbstring toFbstring() const { return fbstr(); }
303
304   // Works only for Range<const char*>
305   int compare(const Range& o) const {
306     const size_type tsize = this->size();
307     const size_type osize = o.size();
308     const size_type msize = std::min(tsize, osize);
309     int r = traits_type::compare(data(), o.data(), msize);
310     if (r == 0) r = tsize - osize;
311     return r;
312   }
313
314   value_type& operator[](size_t i) {
315     CHECK_GT(size(), i);
316     return b_[i];
317   }
318
319   const value_type& operator[](size_t i) const {
320     CHECK_GT(size(), i);
321     return b_[i];
322   }
323
324   value_type& at(size_t i) {
325     if (i >= size()) throw std::out_of_range("index out of range");
326     return b_[i];
327   }
328
329   const value_type& at(size_t i) const {
330     if (i >= size()) throw std::out_of_range("index out of range");
331     return b_[i];
332   }
333
334   // Works only for Range<const char*>
335   uint32_t hash() const {
336     // Taken from fbi/nstring.h:
337     //    Quick and dirty bernstein hash...fine for short ascii strings
338     uint32_t hash = 5381;
339     for (size_t ix = 0; ix < size(); ix++) {
340       hash = ((hash << 5) + hash) + b_[ix];
341     }
342     return hash;
343   }
344
345   void advance(size_type n) {
346     if (UNLIKELY(n > size())) {
347       throw std::out_of_range("index out of range");
348     }
349     b_ += n;
350   }
351
352   void subtract(size_type n) {
353     if (UNLIKELY(n > size())) {
354       throw std::out_of_range("index out of range");
355     }
356     e_ -= n;
357   }
358
359   void pop_front() {
360     assert(b_ < e_);
361     ++b_;
362   }
363
364   void pop_back() {
365     assert(b_ < e_);
366     --e_;
367   }
368
369   Range subpiece(size_type first,
370                  size_type length = std::string::npos) const {
371     if (UNLIKELY(first > size())) {
372       throw std::out_of_range("index out of range");
373     }
374     return Range(b_ + first,
375                  std::min<std::string::size_type>(length, size() - first));
376   }
377
378   // string work-alike functions
379   size_type find(Range str) const {
380     return qfind(*this, str);
381   }
382
383   size_type find(Range str, size_t pos) const {
384     if (pos > size()) return std::string::npos;
385     size_t ret = qfind(subpiece(pos), str);
386     return ret == npos ? ret : ret + pos;
387   }
388
389   size_type find(Iter s, size_t pos, size_t n) const {
390     if (pos > size()) return std::string::npos;
391     size_t ret = qfind(pos ? subpiece(pos) : *this, Range(s, n));
392     return ret == npos ? ret : ret + pos;
393   }
394
395   // Works only for Range<const (unsigned) char*> which have Range(Iter) ctor
396   size_type find(const Iter s) const {
397     return qfind(*this, Range(s));
398   }
399
400   // Works only for Range<const (unsigned) char*> which have Range(Iter) ctor
401   size_type find(const Iter s, size_t pos) const {
402     if (pos > size()) return std::string::npos;
403     size_type ret = qfind(subpiece(pos), Range(s));
404     return ret == npos ? ret : ret + pos;
405   }
406
407   size_type find(value_type c) const {
408     return qfind(*this, c);
409   }
410
411   size_type rfind(value_type c) const {
412     return folly::rfind(*this, c);
413   }
414
415   size_type find(value_type c, size_t pos) const {
416     if (pos > size()) return std::string::npos;
417     size_type ret = qfind(subpiece(pos), c);
418     return ret == npos ? ret : ret + pos;
419   }
420
421   size_type find_first_of(Range needles) const {
422     return qfind_first_of(*this, needles);
423   }
424
425   size_type find_first_of(Range needles, size_t pos) const {
426     if (pos > size()) return std::string::npos;
427     size_type ret = qfind_first_of(subpiece(pos), needles);
428     return ret == npos ? ret : ret + pos;
429   }
430
431   // Works only for Range<const (unsigned) char*> which have Range(Iter) ctor
432   size_type find_first_of(Iter needles) const {
433     return find_first_of(Range(needles));
434   }
435
436   // Works only for Range<const (unsigned) char*> which have Range(Iter) ctor
437   size_type find_first_of(Iter needles, size_t pos) const {
438     return find_first_of(Range(needles), pos);
439   }
440
441   size_type find_first_of(Iter needles, size_t pos, size_t n) const {
442     return find_first_of(Range(needles, n), pos);
443   }
444
445   size_type find_first_of(value_type c) const {
446     return find(c);
447   }
448
449   size_type find_first_of(value_type c, size_t pos) const {
450     return find(c, pos);
451   }
452
453   void swap(Range& rhs) {
454     std::swap(b_, rhs.b_);
455     std::swap(e_, rhs.e_);
456   }
457
458   /**
459    * Does this Range start with another range?
460    */
461   bool startsWith(const Range& other) const {
462     return size() >= other.size() && subpiece(0, other.size()) == other;
463   }
464   bool startsWith(value_type c) const {
465     return !empty() && front() == c;
466   }
467
468   /**
469    * Does this Range end with another range?
470    */
471   bool endsWith(const Range& other) const {
472     return size() >= other.size() && subpiece(size() - other.size()) == other;
473   }
474   bool endsWith(value_type c) const {
475     return !empty() && back() == c;
476   }
477
478   /**
479    * Remove the given prefix and return true if the range starts with the given
480    * prefix; return false otherwise.
481    */
482   bool removePrefix(const Range& prefix) {
483     return startsWith(prefix) && (b_ += prefix.size(), true);
484   }
485   bool removePrefix(value_type prefix) {
486     return startsWith(prefix) && (++b_, true);
487   }
488
489   /**
490    * Remove the given suffix and return true if the range ends with the given
491    * suffix; return false otherwise.
492    */
493   bool removeSuffix(const Range& suffix) {
494     return endsWith(suffix) && (e_ -= suffix.size(), true);
495   }
496   bool removeSuffix(value_type suffix) {
497     return endsWith(suffix) && (--e_, true);
498   }
499
500 private:
501   Iter b_, e_;
502 };
503
504 template <class Iter>
505 const typename Range<Iter>::size_type Range<Iter>::npos = std::string::npos;
506
507 template <class T>
508 void swap(Range<T>& lhs, Range<T>& rhs) {
509   lhs.swap(rhs);
510 }
511
512 /**
513  * Create a range from two iterators, with type deduction.
514  */
515 template <class Iter>
516 Range<Iter> makeRange(Iter first, Iter last) {
517   return Range<Iter>(first, last);
518 }
519
520 typedef Range<const char*> StringPiece;
521 typedef Range<const unsigned char*> ByteRange;
522
523 std::ostream& operator<<(std::ostream& os, const StringPiece& piece);
524
525 /**
526  * Templated comparison operators
527  */
528
529 template <class T>
530 inline bool operator==(const Range<T>& lhs, const Range<T>& rhs) {
531   return lhs.size() == rhs.size() && lhs.compare(rhs) == 0;
532 }
533
534 template <class T>
535 inline bool operator<(const Range<T>& lhs, const Range<T>& rhs) {
536   return lhs.compare(rhs) < 0;
537 }
538
539 /**
540  * Specializations of comparison operators for StringPiece
541  */
542
543 namespace detail {
544
545 template <class A, class B>
546 struct ComparableAsStringPiece {
547   enum {
548     value =
549     (std::is_convertible<A, StringPiece>::value
550      && std::is_same<B, StringPiece>::value)
551     ||
552     (std::is_convertible<B, StringPiece>::value
553      && std::is_same<A, StringPiece>::value)
554   };
555 };
556
557 } // namespace detail
558
559 /**
560  * operator== through conversion for Range<const char*>
561  */
562 template <class T, class U>
563 typename
564 std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>::type
565 operator==(const T& lhs, const U& rhs) {
566   return StringPiece(lhs) == StringPiece(rhs);
567 }
568
569 /**
570  * operator< through conversion for Range<const char*>
571  */
572 template <class T, class U>
573 typename
574 std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>::type
575 operator<(const T& lhs, const U& rhs) {
576   return StringPiece(lhs) < StringPiece(rhs);
577 }
578
579 /**
580  * operator> through conversion for Range<const char*>
581  */
582 template <class T, class U>
583 typename
584 std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>::type
585 operator>(const T& lhs, const U& rhs) {
586   return StringPiece(lhs) > StringPiece(rhs);
587 }
588
589 /**
590  * operator< through conversion for Range<const char*>
591  */
592 template <class T, class U>
593 typename
594 std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>::type
595 operator<=(const T& lhs, const U& rhs) {
596   return StringPiece(lhs) <= StringPiece(rhs);
597 }
598
599 /**
600  * operator> through conversion for Range<const char*>
601  */
602 template <class T, class U>
603 typename
604 std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>::type
605 operator>=(const T& lhs, const U& rhs) {
606   return StringPiece(lhs) >= StringPiece(rhs);
607 }
608
609 struct StringPieceHash {
610   std::size_t operator()(const StringPiece& str) const {
611     return static_cast<std::size_t>(str.hash());
612   }
613 };
614
615 /**
616  * Finds substrings faster than brute force by borrowing from Boyer-Moore
617  */
618 template <class T, class Comp>
619 size_t qfind(const Range<T>& haystack,
620              const Range<T>& needle,
621              Comp eq) {
622   // Don't use std::search, use a Boyer-Moore-like trick by comparing
623   // the last characters first
624   auto const nsize = needle.size();
625   if (haystack.size() < nsize) {
626     return std::string::npos;
627   }
628   if (!nsize) return 0;
629   auto const nsize_1 = nsize - 1;
630   auto const lastNeedle = needle[nsize_1];
631
632   // Boyer-Moore skip value for the last char in the needle. Zero is
633   // not a valid value; skip will be computed the first time it's
634   // needed.
635   std::string::size_type skip = 0;
636
637   auto i = haystack.begin();
638   auto iEnd = haystack.end() - nsize_1;
639
640   while (i < iEnd) {
641     // Boyer-Moore: match the last element in the needle
642     while (!eq(i[nsize_1], lastNeedle)) {
643       if (++i == iEnd) {
644         // not found
645         return std::string::npos;
646       }
647     }
648     // Here we know that the last char matches
649     // Continue in pedestrian mode
650     for (size_t j = 0; ; ) {
651       assert(j < nsize);
652       if (!eq(i[j], needle[j])) {
653         // Not found, we can skip
654         // Compute the skip value lazily
655         if (skip == 0) {
656           skip = 1;
657           while (skip <= nsize_1 && !eq(needle[nsize_1 - skip], lastNeedle)) {
658             ++skip;
659           }
660         }
661         i += skip;
662         break;
663       }
664       // Check if done searching
665       if (++j == nsize) {
666         // Yay
667         return i - haystack.begin();
668       }
669     }
670   }
671   return std::string::npos;
672 }
673
674 namespace detail {
675
676 size_t qfind_first_byte_of_nosse(const StringPiece& haystack,
677                                  const StringPiece& needles);
678
679 #if FOLLY_HAVE_EMMINTRIN_H && __GNUC_PREREQ(4, 6)
680 size_t qfind_first_byte_of_sse42(const StringPiece& haystack,
681                                  const StringPiece& needles);
682
683 inline size_t qfind_first_byte_of(const StringPiece& haystack,
684                                   const StringPiece& needles) {
685   static auto const qfind_first_byte_of_fn =
686     folly::CpuId().sse42() ? qfind_first_byte_of_sse42
687                            : qfind_first_byte_of_nosse;
688   return qfind_first_byte_of_fn(haystack, needles);
689 }
690
691 #else
692 inline size_t qfind_first_byte_of(const StringPiece& haystack,
693                                   const StringPiece& needles) {
694   return qfind_first_byte_of_nosse(haystack, needles);
695 }
696 #endif // FOLLY_HAVE_EMMINTRIN_H
697
698 } // namespace detail
699
700 template <class T, class Comp>
701 size_t qfind_first_of(const Range<T> & haystack,
702                       const Range<T> & needles,
703                       Comp eq) {
704   auto ret = std::find_first_of(haystack.begin(), haystack.end(),
705                                 needles.begin(), needles.end(),
706                                 eq);
707   return ret == haystack.end() ? std::string::npos : ret - haystack.begin();
708 }
709
710 struct AsciiCaseSensitive {
711   bool operator()(char lhs, char rhs) const {
712     return lhs == rhs;
713   }
714 };
715
716 struct AsciiCaseInsensitive {
717   bool operator()(char lhs, char rhs) const {
718     return toupper(lhs) == toupper(rhs);
719   }
720 };
721
722 extern const AsciiCaseSensitive asciiCaseSensitive;
723 extern const AsciiCaseInsensitive asciiCaseInsensitive;
724
725 template <class T>
726 size_t qfind(const Range<T>& haystack,
727              const typename Range<T>::value_type& needle) {
728   auto pos = std::find(haystack.begin(), haystack.end(), needle);
729   return pos == haystack.end() ? std::string::npos : pos - haystack.data();
730 }
731
732 template <class T>
733 size_t rfind(const Range<T>& haystack,
734              const typename Range<T>::value_type& needle) {
735   for (auto i = haystack.size(); i-- > 0; ) {
736     if (haystack[i] == needle) {
737       return i;
738     }
739   }
740   return std::string::npos;
741 }
742
743 // specialization for StringPiece
744 template <>
745 inline size_t qfind(const Range<const char*>& haystack, const char& needle) {
746   auto pos = static_cast<const char*>(
747     ::memchr(haystack.data(), needle, haystack.size()));
748   return pos == nullptr ? std::string::npos : pos - haystack.data();
749 }
750
751 #if FOLLY_HAVE_MEMRCHR
752 template <>
753 inline size_t rfind(const Range<const char*>& haystack, const char& needle) {
754   auto pos = static_cast<const char*>(
755     ::memrchr(haystack.data(), needle, haystack.size()));
756   return pos == nullptr ? std::string::npos : pos - haystack.data();
757 }
758 #endif
759
760 // specialization for ByteRange
761 template <>
762 inline size_t qfind(const Range<const unsigned char*>& haystack,
763                     const unsigned char& needle) {
764   auto pos = static_cast<const unsigned char*>(
765     ::memchr(haystack.data(), needle, haystack.size()));
766   return pos == nullptr ? std::string::npos : pos - haystack.data();
767 }
768
769 #if FOLLY_HAVE_MEMRCHR
770 template <>
771 inline size_t rfind(const Range<const unsigned char*>& haystack,
772                     const unsigned char& needle) {
773   auto pos = static_cast<const unsigned char*>(
774     ::memrchr(haystack.data(), needle, haystack.size()));
775   return pos == nullptr ? std::string::npos : pos - haystack.data();
776 }
777 #endif
778
779 template <class T>
780 size_t qfind_first_of(const Range<T>& haystack,
781                       const Range<T>& needles) {
782   return qfind_first_of(haystack, needles, asciiCaseSensitive);
783 }
784
785 // specialization for StringPiece
786 template <>
787 inline size_t qfind_first_of(const Range<const char*>& haystack,
788                              const Range<const char*>& needles) {
789   return detail::qfind_first_byte_of(haystack, needles);
790 }
791
792 // specialization for ByteRange
793 template <>
794 inline size_t qfind_first_of(const Range<const unsigned char*>& haystack,
795                              const Range<const unsigned char*>& needles) {
796   return detail::qfind_first_byte_of(StringPiece(haystack),
797                                      StringPiece(needles));
798 }
799 }  // !namespace folly
800
801 #pragma GCC diagnostic pop
802
803 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(folly::Range);
804
805 #endif // FOLLY_RANGE_H_