2 * Copyright 2017 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)
22 #include <folly/Portability.h>
23 #include <folly/hash/SpookyHashV2.h>
24 #include <folly/portability/BitsFunctexcept.h>
25 #include <folly/portability/Constexpr.h>
26 #include <folly/portability/String.h>
28 #include <boost/operators.hpp>
29 #include <glog/logging.h>
39 #include <type_traits>
41 #include <folly/CpuId.h>
42 #include <folly/Likely.h>
43 #include <folly/Traits.h>
44 #include <folly/detail/RangeCommon.h>
45 #include <folly/detail/RangeSse42.h>
47 // Ignore shadowing warnings within this file, so includers can use -Wshadow.
49 FOLLY_GCC_DISABLE_WARNING("-Wshadow")
54 * Ubiquitous helper template for knowing what's a string.
57 struct IsSomeString : std::false_type {};
60 struct IsSomeString<std::string> : std::true_type {};
66 * Finds the first occurrence of needle in haystack. The algorithm is on
67 * average faster than O(haystack.size() * needle.size()) but not as fast
68 * as Boyer-Moore. On the upside, it does not do any upfront
69 * preprocessing and does not allocate memory.
73 class Comp = std::equal_to<typename Range<Iter>::value_type>>
75 qfind(const Range<Iter>& haystack, const Range<Iter>& needle, Comp eq = Comp());
78 * Finds the first 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.
84 const Range<Iter>& haystack,
85 const typename Range<Iter>::value_type& needle);
88 * Finds the last occurrence of needle in haystack. The result is the
89 * offset reported to the beginning of haystack, or string::npos if
90 * needle wasn't found.
94 const Range<Iter>& haystack,
95 const typename Range<Iter>::value_type& needle);
98 * Finds the first occurrence of any element of needle in
99 * haystack. The algorithm is O(haystack.size() * needle.size()).
101 template <class Iter>
102 inline size_t qfind_first_of(
103 const Range<Iter>& haystack,
104 const Range<Iter>& needle);
107 * Small internal helper - returns the value just before an iterator.
112 * For random-access iterators, the value before is simply i[-1].
114 template <class Iter>
115 typename std::enable_if<
117 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) {
125 * For all other iterators, we need to use the decrement operator.
127 template <class Iter>
128 typename std::enable_if<
130 typename std::iterator_traits<Iter>::iterator_category,
131 std::random_access_iterator_tag>::value,
132 typename std::iterator_traits<Iter>::reference>::type
133 value_before(Iter i) {
138 * Use IsCharPointer<T>::type to enable const char* or char*.
139 * Use IsCharPointer<T>::const_type to enable only const char*.
142 struct IsCharPointer {};
145 struct IsCharPointer<char*> {
150 struct IsCharPointer<const char*> {
151 typedef int const_type;
155 } // namespace detail
158 * Range abstraction keeping a pair of iterators. We couldn't use
159 * boost's similar range abstraction because we need an API identical
160 * with the former StringPiece class, which is used by a lot of other
161 * code. This abstraction does fulfill the needs of boost's
162 * range-oriented algorithms though.
164 * (Keep memory lifetime in mind when using this class, since it
165 * doesn't manage the data it refers to - just like an iterator
168 template <class Iter>
169 class Range : private boost::totally_ordered<Range<Iter>> {
171 typedef std::size_t size_type;
172 typedef Iter iterator;
173 typedef Iter const_iterator;
174 typedef typename std::remove_reference<
175 typename std::iterator_traits<Iter>::reference>::type value_type;
176 using difference_type = typename std::iterator_traits<Iter>::difference_type;
177 typedef typename std::iterator_traits<Iter>::reference reference;
180 * For MutableStringPiece and MutableByteRange we define StringPiece
181 * and ByteRange as const_range_type (for everything else its just
182 * identity). We do that to enable operations such as find with
183 * args which are const.
185 typedef typename std::conditional<
186 std::is_same<Iter, char*>::value ||
187 std::is_same<Iter, unsigned char*>::value,
188 Range<const value_type*>,
189 Range<Iter>>::type const_range_type;
191 typedef std::char_traits<typename std::remove_const<value_type>::type>
194 static const size_type npos;
196 // Works for all iterators
197 constexpr Range() : b_(), e_() {}
199 constexpr Range(const Range&) = default;
200 constexpr Range(Range&&) = default;
203 // Works for all iterators
204 constexpr Range(Iter start, Iter end) : b_(start), e_(end) {}
206 // Works only for random-access iterators
207 constexpr Range(Iter start, size_t size) : b_(start), e_(start + size) {}
209 #if !__clang__ || __CLANG_PREREQ(3, 7) // Clang 3.6 crashes on this line
210 /* implicit */ Range(std::nullptr_t) = delete;
213 constexpr /* implicit */ Range(Iter str)
214 : b_(str), e_(str + constexpr_strlen(str)) {
216 std::is_same<int, typename detail::IsCharPointer<Iter>::type>::value,
217 "This constructor is only available for character ranges");
220 template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0>
221 /* implicit */ Range(const std::string& str)
222 : b_(str.data()), e_(b_ + str.size()) {}
224 template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0>
225 Range(const std::string& str, std::string::size_type startFrom) {
226 if (UNLIKELY(startFrom > str.size())) {
227 std::__throw_out_of_range("index out of range");
229 b_ = str.data() + startFrom;
230 e_ = str.data() + str.size();
233 template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0>
235 const std::string& str,
236 std::string::size_type startFrom,
237 std::string::size_type size) {
238 if (UNLIKELY(startFrom > str.size())) {
239 std::__throw_out_of_range("index out of range");
241 b_ = str.data() + startFrom;
242 if (str.size() - startFrom < size) {
243 e_ = str.data() + str.size();
249 Range(const Range& other, size_type first, size_type length = npos)
250 : Range(other.subpiece(first, length)) {}
254 class = typename std::enable_if<
255 std::is_same<Iter, typename Container::const_pointer>::value>::type,
257 Iter(std::declval<Container const&>().data()),
259 std::declval<Container const&>().data() +
260 std::declval<Container const&>().size()))>
261 /* implicit */ constexpr Range(Container const& container)
262 : b_(container.data()), e_(b_ + container.size()) {}
266 class = typename std::enable_if<
267 std::is_same<Iter, typename Container::const_pointer>::value>::type,
269 Iter(std::declval<Container const&>().data()),
271 std::declval<Container const&>().data() +
272 std::declval<Container const&>().size()))>
273 Range(Container const& container, typename Container::size_type startFrom) {
274 auto const cdata = container.data();
275 auto const csize = container.size();
276 if (UNLIKELY(startFrom > csize)) {
277 std::__throw_out_of_range("index out of range");
279 b_ = cdata + startFrom;
285 class = typename std::enable_if<
286 std::is_same<Iter, typename Container::const_pointer>::value>::type,
288 Iter(std::declval<Container const&>().data()),
290 std::declval<Container const&>().data() +
291 std::declval<Container const&>().size()))>
293 Container const& container,
294 typename Container::size_type startFrom,
295 typename Container::size_type size) {
296 auto const cdata = container.data();
297 auto const csize = container.size();
298 if (UNLIKELY(startFrom > csize)) {
299 std::__throw_out_of_range("index out of range");
301 b_ = cdata + startFrom;
302 if (csize - startFrom < size) {
309 // Allow implicit conversion from Range<const char*> (aka StringPiece) to
310 // Range<const unsigned char*> (aka ByteRange), as they're both frequently
311 // used to represent ranges of bytes. Allow explicit conversion in the other
315 typename std::enable_if<
316 (std::is_same<Iter, const unsigned char*>::value &&
317 (std::is_same<OtherIter, const char*>::value ||
318 std::is_same<OtherIter, char*>::value)),
320 /* implicit */ Range(const Range<OtherIter>& other)
321 : b_(reinterpret_cast<const unsigned char*>(other.begin())),
322 e_(reinterpret_cast<const unsigned char*>(other.end())) {}
326 typename std::enable_if<
327 (std::is_same<Iter, unsigned char*>::value &&
328 std::is_same<OtherIter, char*>::value),
330 /* implicit */ Range(const Range<OtherIter>& other)
331 : b_(reinterpret_cast<unsigned char*>(other.begin())),
332 e_(reinterpret_cast<unsigned char*>(other.end())) {}
336 typename std::enable_if<
337 (std::is_same<Iter, const char*>::value &&
338 (std::is_same<OtherIter, const unsigned char*>::value ||
339 std::is_same<OtherIter, unsigned char*>::value)),
341 explicit Range(const Range<OtherIter>& other)
342 : b_(reinterpret_cast<const char*>(other.begin())),
343 e_(reinterpret_cast<const char*>(other.end())) {}
347 typename std::enable_if<
348 (std::is_same<Iter, char*>::value &&
349 std::is_same<OtherIter, unsigned char*>::value),
351 explicit Range(const Range<OtherIter>& other)
352 : b_(reinterpret_cast<char*>(other.begin())),
353 e_(reinterpret_cast<char*>(other.end())) {}
355 // Allow implicit conversion from Range<From> to Range<To> if From is
356 // implicitly convertible to To.
359 typename std::enable_if<
360 (!std::is_same<Iter, OtherIter>::value &&
361 std::is_convertible<OtherIter, Iter>::value),
363 constexpr /* implicit */ Range(const Range<OtherIter>& other)
364 : b_(other.begin()), e_(other.end()) {}
366 // Allow explicit conversion from Range<From> to Range<To> if From is
367 // explicitly convertible to To.
370 typename std::enable_if<
371 (!std::is_same<Iter, OtherIter>::value &&
372 !std::is_convertible<OtherIter, Iter>::value &&
373 std::is_constructible<Iter, const OtherIter&>::value),
375 constexpr explicit Range(const Range<OtherIter>& other)
376 : b_(other.begin()), e_(other.end()) {}
379 * Allow explicit construction of Range() from a std::array of a
382 * For instance, this allows constructing StringPiece from a
383 * std::array<char, N> or a std::array<const char, N>
388 typename = typename std::enable_if<
389 std::is_convertible<const T*, Iter>::value>::type>
390 constexpr explicit Range(const std::array<T, N>& array)
391 : b_{array.empty() ? nullptr : &array.at(0)},
392 e_{array.empty() ? nullptr : &array.at(0) + N} {}
397 typename std::enable_if<std::is_convertible<T*, Iter>::value>::type>
398 constexpr explicit Range(std::array<T, N>& array)
399 : b_{array.empty() ? nullptr : &array.at(0)},
400 e_{array.empty() ? nullptr : &array.at(0) + N} {}
402 Range& operator=(const Range& rhs) & = default;
403 Range& operator=(Range&& rhs) & = default;
405 template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0>
406 Range& operator=(std::string&& rhs) = delete;
413 void assign(Iter start, Iter end) {
418 void reset(Iter start, size_type size) {
423 // Works only for Range<const char*>
424 void reset(const std::string& str) {
425 reset(str.data(), str.size());
428 constexpr size_type size() const {
429 // It would be nice to assert(b_ <= e_) here. This can be achieved even
430 // in a C++11 compatible constexpr function:
431 // http://ericniebler.com/2014/09/27/assert-and-constexpr-in-cxx11/
432 // Unfortunately current gcc versions have a bug causing it to reject
433 // this check in a constexpr function:
434 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71448
435 return size_type(e_ - b_);
437 constexpr size_type walk_size() const {
438 return size_type(std::distance(b_, e_));
440 constexpr bool empty() const {
443 constexpr Iter data() const {
446 constexpr Iter start() const {
449 constexpr Iter begin() const {
452 constexpr Iter end() const {
455 constexpr Iter cbegin() const {
458 constexpr Iter cend() const {
461 value_type& front() {
467 return detail::value_before(e_);
469 const value_type& front() const {
473 const value_type& back() const {
475 return detail::value_before(e_);
478 template <typename Tgt>
480 -> decltype(Tgt(std::declval<Iter const&>(), std::declval<size_type>())) {
481 return Tgt(b_, size());
483 // Works only for Range<const char*> and Range<char*>
484 template <typename Tgt = std::string>
486 -> decltype(Tgt(std::declval<Iter const&>(), std::declval<size_type>())) {
489 template <typename Tgt = std::string>
490 auto toString() const
491 -> decltype(Tgt(std::declval<Iter const&>(), std::declval<size_type>())) {
495 const_range_type castToConst() const {
496 return const_range_type(*this);
499 // Works only for Range<const char*> and Range<char*>
500 int compare(const const_range_type& o) const {
501 const size_type tsize = this->size();
502 const size_type osize = o.size();
503 const size_type msize = std::min(tsize, osize);
504 int r = traits_type::compare(data(), o.data(), msize);
505 if (r == 0 && tsize != osize) {
506 // We check the signed bit of the subtraction and bit shift it
507 // to produce either 0 or 2. The subtraction yields the
508 // comparison values of either -1 or 1.
509 r = (static_cast<int>((osize - tsize) >> (CHAR_BIT * sizeof(size_t) - 1))
516 value_type& operator[](size_t i) {
517 DCHECK_GT(size(), i);
521 const value_type& operator[](size_t i) const {
522 DCHECK_GT(size(), i);
526 value_type& at(size_t i) {
528 std::__throw_out_of_range("index out of range");
533 const value_type& at(size_t i) const {
535 std::__throw_out_of_range("index out of range");
540 // Do NOT use this function, which was left behind for backwards
541 // compatibility. Use SpookyHashV2 instead -- it is faster, and produces
542 // a 64-bit hash, which means dramatically fewer collisions in large maps.
543 // (The above advice does not apply if you are targeting a 32-bit system.)
545 // Works only for Range<const char*> and Range<char*>
548 // ** WANT TO GET RID OF THIS LINT? **
550 // A) Use a better hash function (*cough*folly::Hash*cough*), but
551 // only if you don't serialize data in a format that depends on
552 // this formula (ie the writer and reader assume this exact hash
553 // function is used).
555 // B) If you have to use this exact function then make your own hasher
556 // object and copy the body over (see thrift example: D3972362).
557 // https://github.com/facebook/fbthrift/commit/f8ed502e24ab4a32a9d5f266580
558 FOLLY_DEPRECATED("Replace with folly::Hash if the hash is not serialized")
559 uint32_t hash() const {
560 // Taken from fbi/nstring.h:
561 // Quick and dirty bernstein hash...fine for short ascii strings
562 uint32_t hash = 5381;
563 for (size_t ix = 0; ix < size(); ix++) {
564 hash = ((hash << 5) + hash) + b_[ix];
569 void advance(size_type n) {
570 if (UNLIKELY(n > size())) {
571 std::__throw_out_of_range("index out of range");
576 void subtract(size_type n) {
577 if (UNLIKELY(n > size())) {
578 std::__throw_out_of_range("index out of range");
583 Range subpiece(size_type first, size_type length = npos) const {
584 if (UNLIKELY(first > size())) {
585 std::__throw_out_of_range("index out of range");
588 return Range(b_ + first, std::min(length, size() - first));
591 // unchecked versions
592 void uncheckedAdvance(size_type n) {
593 DCHECK_LE(n, size());
597 void uncheckedSubtract(size_type n) {
598 DCHECK_LE(n, size());
602 Range uncheckedSubpiece(size_type first, size_type length = npos) const {
603 DCHECK_LE(first, size());
604 return Range(b_ + first, std::min(length, size() - first));
617 // string work-alike functions
618 size_type find(const_range_type str) const {
619 return qfind(castToConst(), str);
622 size_type find(const_range_type str, size_t pos) const {
624 return std::string::npos;
626 size_t ret = qfind(castToConst().subpiece(pos), str);
627 return ret == npos ? ret : ret + pos;
630 size_type find(Iter s, size_t pos, size_t n) const {
632 return std::string::npos;
634 auto forFinding = castToConst();
636 pos ? forFinding.subpiece(pos) : forFinding, const_range_type(s, n));
637 return ret == npos ? ret : ret + pos;
640 // Works only for Range<(const) (unsigned) char*> which have Range(Iter) ctor
641 size_type find(const Iter s) const {
642 return qfind(castToConst(), const_range_type(s));
645 // Works only for Range<(const) (unsigned) char*> which have Range(Iter) ctor
646 size_type find(const Iter s, size_t pos) const {
648 return std::string::npos;
650 size_type ret = qfind(castToConst().subpiece(pos), const_range_type(s));
651 return ret == npos ? ret : ret + pos;
654 size_type find(value_type c) const {
655 return qfind(castToConst(), c);
658 size_type rfind(value_type c) const {
659 return folly::rfind(castToConst(), c);
662 size_type find(value_type c, size_t pos) const {
664 return std::string::npos;
666 size_type ret = qfind(castToConst().subpiece(pos), c);
667 return ret == npos ? ret : ret + pos;
670 size_type find_first_of(const_range_type needles) const {
671 return qfind_first_of(castToConst(), needles);
674 size_type find_first_of(const_range_type needles, size_t pos) const {
676 return std::string::npos;
678 size_type ret = qfind_first_of(castToConst().subpiece(pos), needles);
679 return ret == npos ? ret : ret + pos;
682 // Works only for Range<(const) (unsigned) char*> which have Range(Iter) ctor
683 size_type find_first_of(Iter needles) const {
684 return find_first_of(const_range_type(needles));
687 // Works only for Range<(const) (unsigned) char*> which have Range(Iter) ctor
688 size_type find_first_of(Iter needles, size_t pos) const {
689 return find_first_of(const_range_type(needles), pos);
692 size_type find_first_of(Iter needles, size_t pos, size_t n) const {
693 return find_first_of(const_range_type(needles, n), pos);
696 size_type find_first_of(value_type c) const {
700 size_type find_first_of(value_type c, size_t pos) const {
705 * Determine whether the range contains the given subrange or item.
707 * Note: Call find() directly if the index is needed.
709 bool contains(const const_range_type& other) const {
710 return find(other) != std::string::npos;
713 bool contains(const value_type& other) const {
714 return find(other) != std::string::npos;
717 void swap(Range& rhs) {
718 std::swap(b_, rhs.b_);
719 std::swap(e_, rhs.e_);
723 * Does this Range start with another range?
725 bool startsWith(const const_range_type& other) const {
726 return size() >= other.size() &&
727 castToConst().subpiece(0, other.size()) == other;
729 bool startsWith(value_type c) const {
730 return !empty() && front() == c;
733 template <class Comp>
734 bool startsWith(const const_range_type& other, Comp&& eq) const {
735 if (size() < other.size()) {
738 auto const trunc = subpiece(0, other.size());
740 trunc.begin(), trunc.end(), other.begin(), std::forward<Comp>(eq));
744 * Does this Range end with another range?
746 bool endsWith(const const_range_type& other) const {
747 return size() >= other.size() &&
748 castToConst().subpiece(size() - other.size()) == other;
750 bool endsWith(value_type c) const {
751 return !empty() && back() == c;
754 template <class Comp>
755 bool endsWith(const const_range_type& other, Comp&& eq) const {
756 if (size() < other.size()) {
759 auto const trunc = subpiece(size() - other.size());
761 trunc.begin(), trunc.end(), other.begin(), std::forward<Comp>(eq));
764 template <class Comp>
765 bool equals(const const_range_type& other, Comp&& eq) const {
766 return size() == other.size() &&
767 std::equal(begin(), end(), other.begin(), std::forward<Comp>(eq));
771 * Remove the items in [b, e), as long as this subrange is at the beginning
772 * or end of the Range.
774 * Required for boost::algorithm::trim()
776 void erase(Iter b, Iter e) {
779 } else if (e == e_) {
782 std::__throw_out_of_range("index out of range");
787 * Remove the given prefix and return true if the range starts with the given
788 * prefix; return false otherwise.
790 bool removePrefix(const const_range_type& prefix) {
791 return startsWith(prefix) && (b_ += prefix.size(), true);
793 bool removePrefix(value_type prefix) {
794 return startsWith(prefix) && (++b_, true);
798 * Remove the given suffix and return true if the range ends with the given
799 * suffix; return false otherwise.
801 bool removeSuffix(const const_range_type& suffix) {
802 return endsWith(suffix) && (e_ -= suffix.size(), true);
804 bool removeSuffix(value_type suffix) {
805 return endsWith(suffix) && (--e_, true);
809 * Replaces the content of the range, starting at position 'pos', with
810 * contents of 'replacement'. Entire 'replacement' must fit into the
811 * range. Returns false if 'replacements' does not fit. Example use:
813 * char in[] = "buffer";
814 * auto msp = MutablesStringPiece(input);
815 * EXPECT_TRUE(msp.replaceAt(2, "tt"));
816 * EXPECT_EQ(msp, "butter");
818 * // not enough space
819 * EXPECT_FALSE(msp.replace(msp.size() - 1, "rr"));
820 * EXPECT_EQ(msp, "butter"); // unchanged
822 bool replaceAt(size_t pos, const_range_type replacement) {
823 if (size() < pos + replacement.size()) {
827 std::copy(replacement.begin(), replacement.end(), begin() + pos);
833 * Replaces all occurences of 'source' with 'dest'. Returns number
834 * of replacements made. Source and dest have to have the same
835 * length. Throws if the lengths are different. If 'source' is a
836 * pattern that is overlapping with itself, we perform sequential
837 * replacement: "aaaaaaa".replaceAll("aa", "ba") --> "bababaa"
841 * char in[] = "buffer";
842 * auto msp = MutablesStringPiece(input);
843 * EXPECT_EQ(msp.replaceAll("ff","tt"), 1);
844 * EXPECT_EQ(msp, "butter");
846 size_t replaceAll(const_range_type source, const_range_type dest) {
847 if (source.size() != dest.size()) {
848 throw std::invalid_argument(
849 "replacement must have the same size as source");
857 size_t num_replaced = 0;
858 size_type found = std::string::npos;
859 while ((found = find(source, pos)) != std::string::npos) {
860 replaceAt(found, dest);
861 pos += source.size();
869 * Splits this `Range` `[b, e)` in the position `i` dictated by the next
870 * occurence of `delimiter`.
872 * Returns a new `Range` `[b, i)` and adjusts this range to start right after
873 * the delimiter's position. This range will be empty if the delimiter is not
874 * found. If called on an empty `Range`, both this and the returned `Range`
879 * folly::StringPiece s("sample string for split_next");
880 * auto p = s.split_step(' ');
882 * // prints "string for split_next"
890 * void tokenize(StringPiece s, char delimiter) {
891 * while (!s.empty()) {
892 * cout << s.split_step(delimiter);
896 * @author: Marcelo Juchem <marcelo@fb.com>
898 Range split_step(value_type delimiter) {
899 auto i = std::find(b_, e_, delimiter);
902 b_ = i == e_ ? e_ : std::next(i);
907 Range split_step(Range delimiter) {
908 auto i = find(delimiter);
909 Range result(b_, i == std::string::npos ? size() : i);
911 b_ = result.end() == e_
915 typename std::iterator_traits<Iter>::difference_type(
922 * Convenience method that calls `split_step()` and passes the result to a
923 * functor, returning whatever the functor does. Any additional arguments
924 * `args` passed to this function are perfectly forwarded to the functor.
926 * Say you have a functor with this signature:
928 * Foo fn(Range r) { }
930 * `split_step()`'s return type will be `Foo`. It works just like:
932 * auto result = fn(myRange.split_step(' '));
934 * A functor returning `void` is also supported.
938 * void do_some_parsing(folly::StringPiece s) {
939 * auto version = s.split_step(' ', [&](folly::StringPiece x) {
941 * throw std::invalid_argument("empty string");
943 * return std::strtoull(x.begin(), x.end(), 16);
950 * void parse(folly::StringPiece s) {
951 * s.split_step(' ', parse_field, bar, 10);
952 * s.split_step('\t', parse_field, baz, 20);
954 * auto const kludge = [](folly::StringPiece x, int &out, int def) {
958 * parse_field(x, out, def);
962 * s.split_step('\t', kludge, gaz);
963 * s.split_step(' ', kludge, foo);
972 * static parse_field(folly::StringPiece s, int &out, int def) {
974 * out = folly::to<int>(s);
975 * } catch (std::exception const &) {
981 * @author: Marcelo Juchem <marcelo@fb.com>
983 template <typename TProcess, typename... Args>
984 auto split_step(value_type delimiter, TProcess&& process, Args&&... args)
985 -> decltype(process(std::declval<Range>(), std::forward<Args>(args)...)) {
986 return process(split_step(delimiter), std::forward<Args>(args)...);
989 template <typename TProcess, typename... Args>
990 auto split_step(Range delimiter, TProcess&& process, Args&&... args)
991 -> decltype(process(std::declval<Range>(), std::forward<Args>(args)...)) {
992 return process(split_step(delimiter), std::forward<Args>(args)...);
999 template <class Iter>
1000 const typename Range<Iter>::size_type Range<Iter>::npos = std::string::npos;
1002 template <class Iter>
1003 void swap(Range<Iter>& lhs, Range<Iter>& rhs) {
1008 * Create a range from two iterators, with type deduction.
1010 template <class Iter>
1011 constexpr Range<Iter> range(Iter first, Iter last) {
1012 return Range<Iter>(first, last);
1016 * Creates a range to reference the contents of a contiguous-storage container.
1018 // Use pointers for types with '.data()' member
1021 class T = typename std::remove_pointer<
1022 decltype(std::declval<Collection>().data())>::type>
1023 constexpr Range<T*> range(Collection&& v) {
1024 return Range<T*>(v.data(), v.data() + v.size());
1027 template <class T, size_t n>
1028 constexpr Range<T*> range(T (&array)[n]) {
1029 return Range<T*>(array, array + n);
1032 template <class T, size_t n>
1033 constexpr Range<const T*> range(const std::array<T, n>& array) {
1034 return Range<const T*>{array};
1037 typedef Range<const char*> StringPiece;
1038 typedef Range<char*> MutableStringPiece;
1039 typedef Range<const unsigned char*> ByteRange;
1040 typedef Range<unsigned char*> MutableByteRange;
1043 std::basic_ostream<C>& operator<<(
1044 std::basic_ostream<C>& os,
1045 Range<C const*> piece) {
1046 using StreamSize = decltype(os.width());
1047 os.write(piece.start(), static_cast<StreamSize>(piece.size()));
1052 std::basic_ostream<C>& operator<<(std::basic_ostream<C>& os, Range<C*> piece) {
1053 using StreamSize = decltype(os.width());
1054 os.write(piece.start(), static_cast<StreamSize>(piece.size()));
1059 * Templated comparison operators
1062 template <class Iter>
1063 inline bool operator==(const Range<Iter>& lhs, const Range<Iter>& rhs) {
1064 return lhs.size() == rhs.size() && lhs.compare(rhs) == 0;
1067 template <class Iter>
1068 inline bool operator<(const Range<Iter>& lhs, const Range<Iter>& rhs) {
1069 return lhs.compare(rhs) < 0;
1073 * Specializations of comparison operators for StringPiece
1078 template <class A, class B>
1079 struct ComparableAsStringPiece {
1081 value = (std::is_convertible<A, StringPiece>::value &&
1082 std::is_same<B, StringPiece>::value) ||
1083 (std::is_convertible<B, StringPiece>::value &&
1084 std::is_same<A, StringPiece>::value)
1088 } // namespace detail
1091 * operator== through conversion for Range<const char*>
1093 template <class T, class U>
1094 _t<std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>>
1095 operator==(const T& lhs, const U& rhs) {
1096 return StringPiece(lhs) == StringPiece(rhs);
1100 * operator< through conversion for Range<const char*>
1102 template <class T, class U>
1103 _t<std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>>
1104 operator<(const T& lhs, const U& rhs) {
1105 return StringPiece(lhs) < StringPiece(rhs);
1109 * operator> through conversion for Range<const char*>
1111 template <class T, class U>
1112 _t<std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>>
1113 operator>(const T& lhs, const U& rhs) {
1114 return StringPiece(lhs) > StringPiece(rhs);
1118 * operator< through conversion for Range<const char*>
1120 template <class T, class U>
1121 _t<std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>>
1122 operator<=(const T& lhs, const U& rhs) {
1123 return StringPiece(lhs) <= StringPiece(rhs);
1127 * operator> through conversion for Range<const char*>
1129 template <class T, class U>
1130 _t<std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>>
1131 operator>=(const T& lhs, const U& rhs) {
1132 return StringPiece(lhs) >= StringPiece(rhs);
1136 * Finds substrings faster than brute force by borrowing from Boyer-Moore
1138 template <class Iter, class Comp>
1139 size_t qfind(const Range<Iter>& haystack, const Range<Iter>& needle, Comp eq) {
1140 // Don't use std::search, use a Boyer-Moore-like trick by comparing
1141 // the last characters first
1142 auto const nsize = needle.size();
1143 if (haystack.size() < nsize) {
1144 return std::string::npos;
1149 auto const nsize_1 = nsize - 1;
1150 auto const lastNeedle = needle[nsize_1];
1152 // Boyer-Moore skip value for the last char in the needle. Zero is
1153 // not a valid value; skip will be computed the first time it's
1155 std::string::size_type skip = 0;
1157 auto i = haystack.begin();
1158 auto iEnd = haystack.end() - nsize_1;
1161 // Boyer-Moore: match the last element in the needle
1162 while (!eq(i[nsize_1], lastNeedle)) {
1165 return std::string::npos;
1168 // Here we know that the last char matches
1169 // Continue in pedestrian mode
1170 for (size_t j = 0;;) {
1172 if (!eq(i[j], needle[j])) {
1173 // Not found, we can skip
1174 // Compute the skip value lazily
1177 while (skip <= nsize_1 && !eq(needle[nsize_1 - skip], lastNeedle)) {
1184 // Check if done searching
1187 return size_t(i - haystack.begin());
1191 return std::string::npos;
1196 inline size_t qfind_first_byte_of(
1197 const StringPiece haystack,
1198 const StringPiece needles) {
1199 static auto const qfind_first_byte_of_fn = folly::CpuId().sse42()
1200 ? qfind_first_byte_of_sse42
1201 : qfind_first_byte_of_nosse;
1202 return qfind_first_byte_of_fn(haystack, needles);
1205 } // namespace detail
1207 template <class Iter, class Comp>
1208 size_t qfind_first_of(
1209 const Range<Iter>& haystack,
1210 const Range<Iter>& needles,
1212 auto ret = std::find_first_of(
1213 haystack.begin(), haystack.end(), needles.begin(), needles.end(), eq);
1214 return ret == haystack.end() ? std::string::npos : ret - haystack.begin();
1217 struct AsciiCaseSensitive {
1218 bool operator()(char lhs, char rhs) const {
1224 * Check if two ascii characters are case insensitive equal.
1225 * The difference between the lower/upper case characters are the 6-th bit.
1226 * We also check they are alpha chars, in case of xor = 32.
1228 struct AsciiCaseInsensitive {
1229 bool operator()(char lhs, char rhs) const {
1238 return (k >= 'a' && k <= 'z');
1242 template <class Iter>
1244 const Range<Iter>& haystack,
1245 const typename Range<Iter>::value_type& needle) {
1246 auto pos = std::find(haystack.begin(), haystack.end(), needle);
1247 return pos == haystack.end() ? std::string::npos : pos - haystack.data();
1250 template <class Iter>
1252 const Range<Iter>& haystack,
1253 const typename Range<Iter>::value_type& needle) {
1254 for (auto i = haystack.size(); i-- > 0;) {
1255 if (haystack[i] == needle) {
1259 return std::string::npos;
1262 // specialization for StringPiece
1264 inline size_t qfind(const Range<const char*>& haystack, const char& needle) {
1265 // memchr expects a not-null pointer, early return if the range is empty.
1266 if (haystack.empty()) {
1267 return std::string::npos;
1269 auto pos = static_cast<const char*>(
1270 ::memchr(haystack.data(), needle, haystack.size()));
1271 return pos == nullptr ? std::string::npos : pos - haystack.data();
1275 inline size_t rfind(const Range<const char*>& haystack, const char& needle) {
1276 // memchr expects a not-null pointer, early return if the range is empty.
1277 if (haystack.empty()) {
1278 return std::string::npos;
1280 auto pos = static_cast<const char*>(
1281 ::memrchr(haystack.data(), needle, haystack.size()));
1282 return pos == nullptr ? std::string::npos : pos - haystack.data();
1285 // specialization for ByteRange
1287 inline size_t qfind(
1288 const Range<const unsigned char*>& haystack,
1289 const unsigned char& needle) {
1290 // memchr expects a not-null pointer, early return if the range is empty.
1291 if (haystack.empty()) {
1292 return std::string::npos;
1294 auto pos = static_cast<const unsigned char*>(
1295 ::memchr(haystack.data(), needle, haystack.size()));
1296 return pos == nullptr ? std::string::npos : pos - haystack.data();
1300 inline size_t rfind(
1301 const Range<const unsigned char*>& haystack,
1302 const unsigned char& needle) {
1303 // memchr expects a not-null pointer, early return if the range is empty.
1304 if (haystack.empty()) {
1305 return std::string::npos;
1307 auto pos = static_cast<const unsigned char*>(
1308 ::memrchr(haystack.data(), needle, haystack.size()));
1309 return pos == nullptr ? std::string::npos : pos - haystack.data();
1312 template <class Iter>
1313 size_t qfind_first_of(const Range<Iter>& haystack, const Range<Iter>& needles) {
1314 return qfind_first_of(haystack, needles, AsciiCaseSensitive());
1317 // specialization for StringPiece
1319 inline size_t qfind_first_of(
1320 const Range<const char*>& haystack,
1321 const Range<const char*>& needles) {
1322 return detail::qfind_first_byte_of(haystack, needles);
1325 // specialization for ByteRange
1327 inline size_t qfind_first_of(
1328 const Range<const unsigned char*>& haystack,
1329 const Range<const unsigned char*>& needles) {
1330 return detail::qfind_first_byte_of(
1331 StringPiece(haystack), StringPiece(needles));
1334 template <class Key, class Enable>
1340 typename std::enable_if<std::is_pod<T>::value, void>::type> {
1341 size_t operator()(folly::Range<T*> r) const {
1342 return hash::SpookyHashV2::Hash64(r.begin(), r.size() * sizeof(T), 0);
1347 * _sp is a user-defined literal suffix to make an appropriate Range
1348 * specialization from a literal string.
1350 * Modeled after C++17's `sv` suffix.
1352 inline namespace literals {
1353 inline namespace string_piece_literals {
1354 constexpr Range<char const*> operator"" _sp(
1356 size_t len) noexcept {
1357 return Range<char const*>(str, len);
1360 constexpr Range<char16_t const*> operator"" _sp(
1361 char16_t const* str,
1362 size_t len) noexcept {
1363 return Range<char16_t const*>(str, len);
1366 constexpr Range<char32_t const*> operator"" _sp(
1367 char32_t const* str,
1368 size_t len) noexcept {
1369 return Range<char32_t const*>(str, len);
1372 constexpr Range<wchar_t const*> operator"" _sp(
1374 size_t len) noexcept {
1375 return Range<wchar_t const*>(str, len);
1377 } // inline namespace string_piece_literals
1378 } // inline namespace literals
1380 } // namespace folly
1384 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(folly::Range);