Bug 1456906 [wpt PR 10641] - Subtract scrollbar in PerpendicularContainingBlockLogica...
[gecko.git] / mfbt / Span.h
blob5a94ab471a73d9cc42631469a5addea7205f0a75
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
4 //
5 // This code is licensed under the MIT License (MIT).
6 //
7 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
10 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
12 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
13 // THE SOFTWARE.
15 ///////////////////////////////////////////////////////////////////////////////
17 // Adapted from https://github.com/Microsoft/GSL/blob/3819df6e378ffccf0e29465afe99c3b324c2aa70/include/gsl/span
18 // and https://github.com/Microsoft/GSL/blob/3819df6e378ffccf0e29465afe99c3b324c2aa70/include/gsl/gsl_util
20 #ifndef mozilla_Span_h
21 #define mozilla_Span_h
23 #include "mozilla/Array.h"
24 #include "mozilla/Assertions.h"
25 #include "mozilla/Casting.h"
26 #include "mozilla/IntegerTypeTraits.h"
27 #include "mozilla/Move.h"
28 #include "mozilla/TypeTraits.h"
29 #include "mozilla/UniquePtr.h"
31 #include <algorithm>
32 #include <array>
33 #include <cstring>
34 #include <iterator>
36 #ifdef _MSC_VER
37 #pragma warning(push)
39 // turn off some warnings that are noisy about our MOZ_RELEASE_ASSERT statements
40 #pragma warning(disable : 4127) // conditional expression is constant
42 // blanket turn off warnings from CppCoreCheck for now
43 // so people aren't annoyed by them when running the tool.
44 // more targeted suppressions will be added in a future update to the GSL
45 #pragma warning(disable : 26481 26482 26483 26485 26490 26491 26492 26493 26495)
47 #if _MSC_VER < 1910
48 #pragma push_macro("constexpr")
49 #define constexpr /*constexpr*/
51 #endif // _MSC_VER < 1910
52 #endif // _MSC_VER
54 namespace mozilla {
56 // Stuff from gsl_util
58 // narrow_cast(): a searchable way to do narrowing casts of values
59 template<class T, class U>
60 inline constexpr T
61 narrow_cast(U&& u)
63 return static_cast<T>(mozilla::Forward<U>(u));
66 // end gsl_util
68 // [views.constants], constants
69 // This was -1 in gsl::span, but using size_t for sizes instead of ptrdiff_t
70 // and reserving a magic value that realistically doesn't occur in
71 // compile-time-constant Span sizes makes things a lot less messy in terms of
72 // comparison between signed and unsigned.
73 constexpr const size_t dynamic_extent = mozilla::MaxValue<size_t>::value;
75 template<class ElementType, size_t Extent = dynamic_extent>
76 class Span;
78 // implementation details
79 namespace span_details {
81 inline size_t strlen16(const char16_t* aZeroTerminated) {
82 size_t len = 0;
83 while (*(aZeroTerminated++)) {
84 len++;
86 return len;
89 // C++14 types that we don't have because we build as C++11.
90 template<class T>
91 using remove_cv_t = typename mozilla::RemoveCV<T>::Type;
92 template<class T>
93 using remove_const_t = typename mozilla::RemoveConst<T>::Type;
94 template<bool B, class T, class F>
95 using conditional_t = typename mozilla::Conditional<B, T, F>::Type;
96 template<class T>
97 using add_pointer_t = typename mozilla::AddPointer<T>::Type;
98 template<bool B, class T = void>
99 using enable_if_t = typename mozilla::EnableIf<B, T>::Type;
101 template<class T>
102 struct is_span_oracle : mozilla::FalseType
106 template<class ElementType, size_t Extent>
107 struct is_span_oracle<mozilla::Span<ElementType, Extent>> : mozilla::TrueType
111 template<class T>
112 struct is_span : public is_span_oracle<remove_cv_t<T>>
116 template<class T>
117 struct is_std_array_oracle : mozilla::FalseType
121 template<class ElementType, size_t Extent>
122 struct is_std_array_oracle<std::array<ElementType, Extent>> : mozilla::TrueType
126 template<class T>
127 struct is_std_array : public is_std_array_oracle<remove_cv_t<T>>
131 template<size_t From, size_t To>
132 struct is_allowed_extent_conversion
133 : public mozilla::IntegralConstant<bool,
134 From == To ||
135 From == mozilla::dynamic_extent ||
136 To == mozilla::dynamic_extent>
140 template<class From, class To>
141 struct is_allowed_element_type_conversion
142 : public mozilla::IntegralConstant<bool, mozilla::IsConvertible<From (*)[], To (*)[]>::value>
146 template<class Span, bool IsConst>
147 class span_iterator
149 using element_type_ = typename Span::element_type;
151 public:
152 using iterator_category = std::random_access_iterator_tag;
153 using value_type = remove_const_t<element_type_>;
154 using difference_type = typename Span::index_type;
156 using reference = conditional_t<IsConst, const element_type_, element_type_>&;
157 using pointer = add_pointer_t<reference>;
159 constexpr span_iterator() : span_iterator(nullptr, 0) {}
161 constexpr span_iterator(const Span* span,
162 typename Span::index_type index)
163 : span_(span)
164 , index_(index)
166 MOZ_RELEASE_ASSERT(span == nullptr ||
167 (index_ >= 0 && index <= span_->Length()));
170 friend class span_iterator<Span, true>;
171 constexpr MOZ_IMPLICIT span_iterator(const span_iterator<Span, false>& other)
172 : span_iterator(other.span_, other.index_)
176 constexpr span_iterator<Span, IsConst>&
177 operator=(const span_iterator<Span, IsConst>&) = default;
179 constexpr reference operator*() const
181 MOZ_RELEASE_ASSERT(span_);
182 return (*span_)[index_];
185 constexpr pointer operator->() const
187 MOZ_RELEASE_ASSERT(span_);
188 return &((*span_)[index_]);
191 constexpr span_iterator& operator++()
193 MOZ_RELEASE_ASSERT(span_ && index_ >= 0 && index_ < span_->Length());
194 ++index_;
195 return *this;
198 constexpr span_iterator operator++(int)
200 auto ret = *this;
201 ++(*this);
202 return ret;
205 constexpr span_iterator& operator--()
207 MOZ_RELEASE_ASSERT(span_ && index_ > 0 && index_ <= span_->Length());
208 --index_;
209 return *this;
212 constexpr span_iterator operator--(int)
214 auto ret = *this;
215 --(*this);
216 return ret;
219 constexpr span_iterator
220 operator+(difference_type n) const
222 auto ret = *this;
223 return ret += n;
226 constexpr span_iterator& operator+=(difference_type n)
228 MOZ_RELEASE_ASSERT(span_ && (index_ + n) >= 0 &&
229 (index_ + n) <= span_->Length());
230 index_ += n;
231 return *this;
234 constexpr span_iterator
235 operator-(difference_type n) const
237 auto ret = *this;
238 return ret -= n;
241 constexpr span_iterator& operator-=(difference_type n)
244 return *this += -n;
247 constexpr difference_type
248 operator-(const span_iterator& rhs) const
250 MOZ_RELEASE_ASSERT(span_ == rhs.span_);
251 return index_ - rhs.index_;
254 constexpr reference operator[](difference_type n) const
256 return *(*this + n);
259 constexpr friend bool operator==(const span_iterator& lhs,
260 const span_iterator& rhs)
262 return lhs.span_ == rhs.span_ && lhs.index_ == rhs.index_;
265 constexpr friend bool operator!=(const span_iterator& lhs,
266 const span_iterator& rhs)
268 return !(lhs == rhs);
271 constexpr friend bool operator<(const span_iterator& lhs,
272 const span_iterator& rhs)
274 MOZ_RELEASE_ASSERT(lhs.span_ == rhs.span_);
275 return lhs.index_ < rhs.index_;
278 constexpr friend bool operator<=(const span_iterator& lhs,
279 const span_iterator& rhs)
281 return !(rhs < lhs);
284 constexpr friend bool operator>(const span_iterator& lhs,
285 const span_iterator& rhs)
287 return rhs < lhs;
290 constexpr friend bool operator>=(const span_iterator& lhs,
291 const span_iterator& rhs)
293 return !(rhs > lhs);
296 void swap(span_iterator& rhs)
298 std::swap(index_, rhs.index_);
299 std::swap(span_, rhs.span_);
302 protected:
303 const Span* span_;
304 size_t index_;
307 template<class Span, bool IsConst>
308 inline constexpr span_iterator<Span, IsConst>
309 operator+(typename span_iterator<Span, IsConst>::difference_type n,
310 const span_iterator<Span, IsConst>& rhs)
312 return rhs + n;
315 template<size_t Ext>
316 class extent_type
318 public:
319 using index_type = size_t;
321 static_assert(Ext >= 0, "A fixed-size Span must be >= 0 in size.");
323 constexpr extent_type() {}
325 template<index_type Other>
326 constexpr MOZ_IMPLICIT extent_type(extent_type<Other> ext)
328 static_assert(
329 Other == Ext || Other == dynamic_extent,
330 "Mismatch between fixed-size extent and size of initializing data.");
331 MOZ_RELEASE_ASSERT(ext.size() == Ext);
334 constexpr MOZ_IMPLICIT extent_type(index_type length)
336 MOZ_RELEASE_ASSERT(length == Ext);
339 constexpr index_type size() const { return Ext; }
342 template<>
343 class extent_type<dynamic_extent>
345 public:
346 using index_type = size_t;
348 template<index_type Other>
349 explicit constexpr extent_type(extent_type<Other> ext)
350 : size_(ext.size())
354 explicit constexpr extent_type(index_type length)
355 : size_(length)
359 constexpr index_type size() const { return size_; }
361 private:
362 index_type size_;
364 } // namespace span_details
367 * Span - slices for C++
369 * Span implements Rust's slice concept for C++. It's called "Span" instead of
370 * "Slice" to follow the naming used in C++ Core Guidelines.
372 * A Span wraps a pointer and a length that identify a non-owning view to a
373 * contiguous block of memory of objects of the same type. Various types,
374 * including (pre-decay) C arrays, XPCOM strings, nsTArray, mozilla::Array,
375 * mozilla::Range and contiguous standard-library containers, auto-convert
376 * into Spans when attempting to pass them as arguments to methods that take
377 * Spans. MakeSpan() functions can be used for explicit conversion in other
378 * contexts. (Span itself autoconverts into mozilla::Range.)
380 * Like Rust's slices, Span provides safety against out-of-bounds access by
381 * performing run-time bound checks. However, unlike Rust's slices, Span
382 * cannot provide safety against use-after-free.
384 * (Note: Span is like Rust's slice only conceptually. Due to the lack of
385 * ABI guarantees, you should still decompose spans/slices to raw pointer
386 * and length parts when crossing the FFI. The Elements() and data() methods
387 * are guaranteed to return a non-null pointer even for zero-length spans,
388 * so the pointer can be used as a raw part of a Rust slice without further
389 * checks.)
391 * In addition to having constructors and MakeSpan() functions that take
392 * various well-known types, a Span for an arbitrary type can be constructed
393 * (via constructor or MakeSpan()) from a pointer and a length or a pointer
394 * and another pointer pointing just past the last element.
396 * A Span<const char> or Span<const char16_t> can be obtained for const char*
397 * or const char16_t pointing to a zero-terminated string using the
398 * MakeStringSpan() function (which treats a nullptr argument equivalently
399 * to the empty string). Corresponding implicit constructor does not exist
400 * in order to avoid accidental construction in cases where const char* or
401 * const char16_t* do not point to a zero-terminated string.
403 * Span has methods that follow the Mozilla naming style and methods that
404 * don't. The methods that follow the Mozilla naming style are meant to be
405 * used directly from Mozilla code. The methods that don't are meant for
406 * integration with C++11 range-based loops and with meta-programming that
407 * expects the same methods that are found on the standard-library
408 * containers. For example, to decompose a Span into its parts in Mozilla
409 * code, use Elements() and Length() (as with nsTArray) instead of data()
410 * and size() (as with std::vector).
412 * The pointer and length wrapped by a Span cannot be changed after a Span has
413 * been created. When new values are required, simply create a new Span. Span
414 * has a method called Subspan() that works analogously to the Substring()
415 * method of XPCOM strings taking a start index and an optional length. As a
416 * Mozilla extension (relative to Microsoft's gsl::span that mozilla::Span is
417 * based on), Span has methods From(start), To(end) and FromTo(start, end)
418 * that correspond to Rust's &slice[start..], &slice[..end] and
419 * &slice[start..end], respectively. (That is, the end index is the index of
420 * the first element not to be included in the new subspan.)
422 * When indicating a Span that's only read from, const goes inside the type
423 * parameter. Don't put const in front of Span. That is:
424 * size_t ReadsFromOneSpanAndWritesToAnother(Span<const uint8_t> aReadFrom,
425 * Span<uint8_t> aWrittenTo);
427 * Any Span<const T> can be viewed as Span<const uint8_t> using the function
428 * AsBytes(). Any Span<T> can be viewed as Span<uint8_t> using the function
429 * AsWritableBytes().
431 template<class ElementType, size_t Extent>
432 class Span
434 public:
435 // constants and types
436 using element_type = ElementType;
437 using index_type = size_t;
438 using pointer = element_type*;
439 using reference = element_type&;
441 using iterator =
442 span_details::span_iterator<Span<ElementType, Extent>, false>;
443 using const_iterator =
444 span_details::span_iterator<Span<ElementType, Extent>, true>;
445 using reverse_iterator = std::reverse_iterator<iterator>;
446 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
448 constexpr static const index_type extent = Extent;
450 // [Span.cons], Span constructors, copy, assignment, and destructor
451 // "Dependent" is needed to make "span_details::enable_if_t<(Dependent || Extent == 0 || Extent == mozilla::MaxValue<size_t>::value)>" SFINAE,
452 // since "span_details::enable_if_t<(Extent == 0 || Extent == mozilla::MaxValue<size_t>::value)>" is ill-formed when Extent is neither of the extreme values.
454 * Constructor with no args.
456 template<
457 bool Dependent = false,
458 class = span_details::enable_if_t<
459 (Dependent || Extent == 0 || Extent == mozilla::MaxValue<size_t>::value)>>
460 constexpr Span()
461 : storage_(nullptr, span_details::extent_type<0>())
466 * Constructor for nullptr.
468 constexpr MOZ_IMPLICIT Span(std::nullptr_t) : Span() {}
471 * Constructor for pointer and length.
473 constexpr Span(pointer aPtr, index_type aLength)
474 : storage_(aPtr, aLength)
479 * Constructor for start pointer and pointer past end.
481 constexpr Span(pointer aStartPtr, pointer aEndPtr)
482 : storage_(aStartPtr, std::distance(aStartPtr, aEndPtr))
487 * Constructor for C array.
489 template<size_t N>
490 constexpr MOZ_IMPLICIT Span(element_type (&aArr)[N])
491 : storage_(&aArr[0], span_details::extent_type<N>())
495 // Implicit constructors for char* and char16_t* pointers are deleted in order
496 // to avoid accidental construction in cases where a pointer does not point to
497 // a zero-terminated string. A Span<const char> or Span<const char16_t> can be
498 // obtained for const char* or const char16_t pointing to a zero-terminated
499 // string using the MakeStringSpan() function.
500 Span(char* aStr) = delete;
501 Span(const char* aStr) = delete;
502 Span(char16_t* aStr) = delete;
503 Span(const char16_t* aStr) = delete;
506 * Constructor for std::array.
508 template<size_t N,
509 class ArrayElementType = span_details::remove_const_t<element_type>>
510 constexpr MOZ_IMPLICIT Span(std::array<ArrayElementType, N>& aArr)
511 : storage_(&aArr[0], span_details::extent_type<N>())
516 * Constructor for const std::array.
518 template<size_t N>
519 constexpr MOZ_IMPLICIT Span(
520 const std::array<span_details::remove_const_t<element_type>, N>& aArr)
521 : storage_(&aArr[0], span_details::extent_type<N>())
526 * Constructor for mozilla::Array.
528 template<size_t N,
529 class ArrayElementType = span_details::remove_const_t<element_type>>
530 constexpr MOZ_IMPLICIT Span(mozilla::Array<ArrayElementType, N>& aArr)
531 : storage_(&aArr[0], span_details::extent_type<N>())
536 * Constructor for const mozilla::Array.
538 template<size_t N>
539 constexpr MOZ_IMPLICIT Span(
540 const mozilla::Array<span_details::remove_const_t<element_type>, N>& aArr)
541 : storage_(&aArr[0], span_details::extent_type<N>())
546 * Constructor for mozilla::UniquePtr holding an array and length.
548 template<class ArrayElementType = std::add_pointer<element_type>>
549 constexpr Span(const mozilla::UniquePtr<ArrayElementType>& aPtr,
550 index_type aLength)
551 : storage_(aPtr.get(), aLength)
555 // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement
556 // on Container to be a contiguous sequence container.
558 * Constructor for standard-library containers.
560 template<
561 class Container,
562 class = span_details::enable_if_t<
563 !span_details::is_span<Container>::value &&
564 !span_details::is_std_array<Container>::value &&
565 mozilla::IsConvertible<typename Container::pointer, pointer>::value &&
566 mozilla::IsConvertible<typename Container::pointer,
567 decltype(mozilla::DeclVal<Container>().data())>::value>>
568 constexpr MOZ_IMPLICIT Span(Container& cont)
569 : Span(cont.data(), ReleaseAssertedCast<index_type>(cont.size()))
574 * Constructor for standard-library containers (const version).
576 template<
577 class Container,
578 class = span_details::enable_if_t<
579 mozilla::IsConst<element_type>::value &&
580 !span_details::is_span<Container>::value &&
581 mozilla::IsConvertible<typename Container::pointer, pointer>::value &&
582 mozilla::IsConvertible<typename Container::pointer,
583 decltype(mozilla::DeclVal<Container>().data())>::value>>
584 constexpr MOZ_IMPLICIT Span(const Container& cont)
585 : Span(cont.data(), ReleaseAssertedCast<index_type>(cont.size()))
590 * Constructor from other Span.
592 constexpr Span(const Span& other) = default;
595 * Constructor from other Span.
597 constexpr Span(Span&& other) = default;
600 * Constructor from other Span with conversion of element type.
602 template<
603 class OtherElementType,
604 size_t OtherExtent,
605 class = span_details::enable_if_t<
606 span_details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
607 span_details::is_allowed_element_type_conversion<OtherElementType,
608 element_type>::value>>
609 constexpr MOZ_IMPLICIT Span(const Span<OtherElementType, OtherExtent>& other)
610 : storage_(other.data(),
611 span_details::extent_type<OtherExtent>(other.size()))
616 * Constructor from other Span with conversion of element type.
618 template<
619 class OtherElementType,
620 size_t OtherExtent,
621 class = span_details::enable_if_t<
622 span_details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
623 span_details::is_allowed_element_type_conversion<OtherElementType,
624 element_type>::value>>
625 constexpr MOZ_IMPLICIT Span(Span<OtherElementType, OtherExtent>&& other)
626 : storage_(other.data(),
627 span_details::extent_type<OtherExtent>(other.size()))
631 ~Span() = default;
632 constexpr Span& operator=(const Span& other)
633 = default;
635 constexpr Span& operator=(Span&& other)
636 = default;
638 // [Span.sub], Span subviews
640 * Subspan with first N elements with compile-time N.
642 template<size_t Count>
643 constexpr Span<element_type, Count> First() const
645 MOZ_RELEASE_ASSERT(Count <= size());
646 return { data(), Count };
650 * Subspan with last N elements with compile-time N.
652 template<size_t Count>
653 constexpr Span<element_type, Count> Last() const
655 const size_t len = size();
656 MOZ_RELEASE_ASSERT(Count <= len);
657 return { data() + (len - Count), Count };
661 * Subspan with compile-time start index and length.
663 template<size_t Offset, size_t Count = dynamic_extent>
664 constexpr Span<element_type, Count> Subspan() const
666 const size_t len = size();
667 MOZ_RELEASE_ASSERT(Offset <= len &&
668 (Count == dynamic_extent || (Offset + Count <= len)));
669 return { data() + Offset,
670 Count == dynamic_extent ? len - Offset : Count };
674 * Subspan with first N elements with run-time N.
676 constexpr Span<element_type, dynamic_extent> First(
677 index_type aCount) const
679 MOZ_RELEASE_ASSERT(aCount <= size());
680 return { data(), aCount };
684 * Subspan with last N elements with run-time N.
686 constexpr Span<element_type, dynamic_extent> Last(
687 index_type aCount) const
689 const size_t len = size();
690 MOZ_RELEASE_ASSERT(aCount <= len);
691 return { data() + (len - aCount), aCount };
695 * Subspan with run-time start index and length.
697 constexpr Span<element_type, dynamic_extent> Subspan(
698 index_type aStart,
699 index_type aLength = dynamic_extent) const
701 const size_t len = size();
702 MOZ_RELEASE_ASSERT(aStart <= len &&
703 (aLength == dynamic_extent ||
704 (aStart + aLength <= len)));
705 return { data() + aStart,
706 aLength == dynamic_extent ? len - aStart : aLength };
710 * Subspan with run-time start index. (Rust's &foo[start..])
712 constexpr Span<element_type, dynamic_extent> From(
713 index_type aStart) const
715 return Subspan(aStart);
719 * Subspan with run-time exclusive end index. (Rust's &foo[..end])
721 constexpr Span<element_type, dynamic_extent> To(
722 index_type aEnd) const
724 return Subspan(0, aEnd);
728 * Subspan with run-time start index and exclusive end index.
729 * (Rust's &foo[start..end])
731 constexpr Span<element_type, dynamic_extent> FromTo(
732 index_type aStart,
733 index_type aEnd) const
735 MOZ_RELEASE_ASSERT(aStart <= aEnd);
736 return Subspan(aStart, aEnd - aStart);
739 // [Span.obs], Span observers
741 * Number of elements in the span.
743 constexpr index_type Length() const { return size(); }
746 * Number of elements in the span (standard-libray duck typing version).
748 constexpr index_type size() const { return storage_.size(); }
751 * Size of the span in bytes.
753 constexpr index_type LengthBytes() const { return size_bytes(); }
756 * Size of the span in bytes (standard-library naming style version).
758 constexpr index_type size_bytes() const
760 return size() * narrow_cast<index_type>(sizeof(element_type));
764 * Checks if the the length of the span is zero.
766 constexpr bool IsEmpty() const { return empty(); }
769 * Checks if the the length of the span is zero (standard-libray duck
770 * typing version).
772 constexpr bool empty() const { return size() == 0; }
774 // [Span.elem], Span element access
775 constexpr reference operator[](index_type idx) const
777 MOZ_RELEASE_ASSERT(idx < storage_.size());
778 return data()[idx];
782 * Access element of span by index (standard-library duck typing version).
784 constexpr reference at(index_type idx) const { return this->operator[](idx); }
786 constexpr reference operator()(index_type idx) const
788 return this->operator[](idx);
792 * Pointer to the first element of the span. The return value is never
793 * nullptr, not ever for zero-length spans, so it can be passed as-is
794 * to std::slice::from_raw_parts() in Rust.
796 constexpr pointer Elements() const { return data(); }
799 * Pointer to the first element of the span (standard-libray duck typing version).
800 * The return value is never nullptr, not ever for zero-length spans, so it can
801 * be passed as-is to std::slice::from_raw_parts() in Rust.
803 constexpr pointer data() const { return storage_.data(); }
805 // [Span.iter], Span iterator support
806 iterator begin() const { return { this, 0 }; }
807 iterator end() const { return { this, Length() }; }
809 const_iterator cbegin() const { return { this, 0 }; }
810 const_iterator cend() const { return { this, Length() }; }
812 reverse_iterator rbegin() const
814 return reverse_iterator{ end() };
816 reverse_iterator rend() const
818 return reverse_iterator{ begin() };
821 const_reverse_iterator crbegin() const
823 return const_reverse_iterator{ cend() };
825 const_reverse_iterator crend() const
827 return const_reverse_iterator{ cbegin() };
830 private:
831 // this implementation detail class lets us take advantage of the
832 // empty base class optimization to pay for only storage of a single
833 // pointer in the case of fixed-size Spans
834 template<class ExtentType>
835 class storage_type : public ExtentType
837 public:
838 template<class OtherExtentType>
839 constexpr storage_type(pointer elements,
840 OtherExtentType ext)
841 : ExtentType(ext)
842 // Replace nullptr with 0x1 for Rust slice compatibility. See
843 // https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html
844 , data_(elements ? elements : reinterpret_cast<pointer>(0x1))
846 const size_t extentSize = ExtentType::size();
847 MOZ_RELEASE_ASSERT(
848 (!elements && extentSize == 0) ||
849 (elements && extentSize != mozilla::MaxValue<size_t>::value));
852 constexpr pointer data() const { return data_; }
854 private:
855 pointer data_;
858 storage_type<span_details::extent_type<Extent>> storage_;
861 // [Span.comparison], Span comparison operators
862 template<class ElementType, size_t FirstExtent, size_t SecondExtent>
863 inline constexpr bool
864 operator==(const Span<ElementType, FirstExtent>& l,
865 const Span<ElementType, SecondExtent>& r)
867 return (l.size() == r.size()) && std::equal(l.begin(), l.end(), r.begin());
870 template<class ElementType, size_t Extent>
871 inline constexpr bool
872 operator!=(const Span<ElementType, Extent>& l,
873 const Span<ElementType, Extent>& r)
875 return !(l == r);
878 template<class ElementType, size_t Extent>
879 inline constexpr bool
880 operator<(const Span<ElementType, Extent>& l,
881 const Span<ElementType, Extent>& r)
883 return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
886 template<class ElementType, size_t Extent>
887 inline constexpr bool
888 operator<=(const Span<ElementType, Extent>& l,
889 const Span<ElementType, Extent>& r)
891 return !(l > r);
894 template<class ElementType, size_t Extent>
895 inline constexpr bool
896 operator>(const Span<ElementType, Extent>& l,
897 const Span<ElementType, Extent>& r)
899 return r < l;
902 template<class ElementType, size_t Extent>
903 inline constexpr bool
904 operator>=(const Span<ElementType, Extent>& l,
905 const Span<ElementType, Extent>& r)
907 return !(l < r);
910 namespace span_details {
911 // if we only supported compilers with good constexpr support then
912 // this pair of classes could collapse down to a constexpr function
914 // we should use a narrow_cast<> to go to size_t, but older compilers may not see it as
915 // constexpr
916 // and so will fail compilation of the template
917 template<class ElementType, size_t Extent>
918 struct calculate_byte_size
919 : mozilla::IntegralConstant<size_t,
920 static_cast<size_t>(sizeof(ElementType) *
921 static_cast<size_t>(Extent))>
925 template<class ElementType>
926 struct calculate_byte_size<ElementType, dynamic_extent>
927 : mozilla::IntegralConstant<size_t, dynamic_extent>
932 // [Span.objectrep], views of object representation
934 * View span as Span<const uint8_t>.
936 template<class ElementType, size_t Extent>
937 Span<const uint8_t,
938 span_details::calculate_byte_size<ElementType, Extent>::value>
939 AsBytes(Span<ElementType, Extent> s)
941 return { reinterpret_cast<const uint8_t*>(s.data()), s.size_bytes() };
945 * View span as Span<uint8_t>.
947 template<class ElementType,
948 size_t Extent,
949 class = span_details::enable_if_t<!mozilla::IsConst<ElementType>::value>>
950 Span<uint8_t, span_details::calculate_byte_size<ElementType, Extent>::value>
951 AsWritableBytes(Span<ElementType, Extent> s)
953 return { reinterpret_cast<uint8_t*>(s.data()), s.size_bytes() };
957 // MakeSpan() - Utility functions for creating Spans
960 * Create span from pointer and length.
962 template<class ElementType>
963 Span<ElementType>
964 MakeSpan(ElementType* aPtr, typename Span<ElementType>::index_type aLength)
966 return Span<ElementType>(aPtr, aLength);
970 * Create span from start pointer and pointer past end.
972 template<class ElementType>
973 Span<ElementType>
974 MakeSpan(ElementType* aStartPtr, ElementType* aEndPtr)
976 return Span<ElementType>(aStartPtr, aEndPtr);
980 * Create span from C array.
981 * MakeSpan() does not permit creating Span objects from string literals (const
982 * char or char16_t arrays) because the Span length would include the zero
983 * terminator, which may surprise callers. Use MakeStringSpan() to create a
984 * Span whose length that excludes the string literal's zero terminator or use
985 * the MakeSpan() overload that accepts a pointer and length and specify the
986 * string literal's full length.
988 template<class ElementType, size_t N,
989 class = span_details::enable_if_t<
990 !IsSame<ElementType, const char>::value &&
991 !IsSame<ElementType, const char16_t>::value>>
992 Span<ElementType> MakeSpan(ElementType (&aArr)[N])
994 return Span<ElementType>(aArr, N);
998 * Create span from mozilla::Array.
1000 template<class ElementType, size_t N>
1001 Span<ElementType>
1002 MakeSpan(mozilla::Array<ElementType, N>& aArr)
1004 return aArr;
1008 * Create span from const mozilla::Array.
1010 template<class ElementType, size_t N>
1011 Span<const ElementType>
1012 MakeSpan(const mozilla::Array<ElementType, N>& arr)
1014 return arr;
1018 * Create span from standard-library container.
1020 template<class Container>
1021 Span<typename Container::value_type>
1022 MakeSpan(Container& cont)
1024 return Span<typename Container::value_type>(cont);
1028 * Create span from standard-library container (const version).
1030 template<class Container>
1031 Span<const typename Container::value_type>
1032 MakeSpan(const Container& cont)
1034 return Span<const typename Container::value_type>(cont);
1038 * Create span from smart pointer and length.
1040 template<class Ptr>
1041 Span<typename Ptr::element_type>
1042 MakeSpan(Ptr& aPtr, size_t aLength)
1044 return Span<typename Ptr::element_type>(aPtr, aLength);
1048 * Create span from a zero-terminated C string. nullptr is
1049 * treated as the empty string.
1051 inline Span<const char>
1052 MakeStringSpan(const char* aZeroTerminated)
1054 if (!aZeroTerminated) {
1055 return Span<const char>();
1057 return Span<const char>(aZeroTerminated, std::strlen(aZeroTerminated));
1061 * Create span from a zero-terminated UTF-16 C string. nullptr is
1062 * treated as the empty string.
1064 inline Span<const char16_t>
1065 MakeStringSpan(const char16_t* aZeroTerminated)
1067 if (!aZeroTerminated) {
1068 return Span<const char16_t>();
1070 return Span<const char16_t>(aZeroTerminated, span_details::strlen16(aZeroTerminated));
1073 } // namespace mozilla
1075 #ifdef _MSC_VER
1076 #if _MSC_VER < 1910
1077 #undef constexpr
1078 #pragma pop_macro("constexpr")
1080 #endif // _MSC_VER < 1910
1082 #pragma warning(pop)
1083 #endif // _MSC_VER
1085 #endif // mozilla_Span_h