Bug 1535487 - determine rootUrl directly in buglist creator r=tomprince
[gecko.git] / mfbt / Span.h
blob5ca9e60509446f41c5e10fad78491b1ac81a1081
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
18 // https://github.com/Microsoft/GSL/blob/3819df6e378ffccf0e29465afe99c3b324c2aa70/include/gsl/span
19 // and
20 // https://github.com/Microsoft/GSL/blob/3819df6e378ffccf0e29465afe99c3b324c2aa70/include/gsl/gsl_util
22 #ifndef mozilla_Span_h
23 # define mozilla_Span_h
25 # include "mozilla/Array.h"
26 # include "mozilla/Assertions.h"
27 # include "mozilla/Casting.h"
28 # include "mozilla/IntegerTypeTraits.h"
29 # include "mozilla/Move.h"
30 # include "mozilla/TypeTraits.h"
31 # include "mozilla/UniquePtr.h"
33 # include <algorithm>
34 # include <array>
35 # include <cstring>
36 # include <iterator>
38 # ifdef _MSC_VER
39 # pragma warning(push)
41 // turn off some warnings that are noisy about our MOZ_RELEASE_ASSERT statements
42 # pragma warning(disable : 4127) // conditional expression is constant
44 // blanket turn off warnings from CppCoreCheck for now
45 // so people aren't annoyed by them when running the tool.
46 // more targeted suppressions will be added in a future update to the GSL
47 # pragma warning( \
48 disable : 26481 26482 26483 26485 26490 26491 26492 26493 26495)
50 # if _MSC_VER < 1910
51 # pragma push_macro("constexpr")
52 # define constexpr /*constexpr*/
54 # endif // _MSC_VER < 1910
55 # endif // _MSC_VER
57 namespace mozilla {
59 // Stuff from gsl_util
61 // narrow_cast(): a searchable way to do narrowing casts of values
62 template <class T, class U>
63 inline constexpr T narrow_cast(U&& u) {
64 return static_cast<T>(std::forward<U>(u));
67 // end gsl_util
69 // [views.constants], constants
70 // This was -1 in gsl::span, but using size_t for sizes instead of ptrdiff_t
71 // and reserving a magic value that realistically doesn't occur in
72 // compile-time-constant Span sizes makes things a lot less messy in terms of
73 // comparison between signed and unsigned.
74 constexpr const size_t dynamic_extent = mozilla::MaxValue<size_t>::value;
76 template <class ElementType, size_t Extent = dynamic_extent>
77 class Span;
79 // implementation details
80 namespace span_details {
82 inline size_t strlen16(const char16_t* aZeroTerminated) {
83 size_t len = 0;
84 while (*(aZeroTerminated++)) {
85 len++;
87 return len;
90 // C++14 types that we don't have because we build as C++11.
91 template <class T>
92 using remove_cv_t = typename mozilla::RemoveCV<T>::Type;
93 template <class T>
94 using remove_const_t = typename mozilla::RemoveConst<T>::Type;
95 template <bool B, class T, class F>
96 using conditional_t = typename mozilla::Conditional<B, T, F>::Type;
97 template <class T>
98 using add_pointer_t = typename mozilla::AddPointer<T>::Type;
99 template <bool B, class T = void>
100 using enable_if_t = typename mozilla::EnableIf<B, T>::Type;
102 template <class T>
103 struct is_span_oracle : mozilla::FalseType {};
105 template <class ElementType, size_t Extent>
106 struct is_span_oracle<mozilla::Span<ElementType, Extent>> : mozilla::TrueType {
109 template <class T>
110 struct is_span : public is_span_oracle<remove_cv_t<T>> {};
112 template <class T>
113 struct is_std_array_oracle : mozilla::FalseType {};
115 template <class ElementType, size_t Extent>
116 struct is_std_array_oracle<std::array<ElementType, Extent>>
117 : mozilla::TrueType {};
119 template <class T>
120 struct is_std_array : public is_std_array_oracle<remove_cv_t<T>> {};
122 template <size_t From, size_t To>
123 struct is_allowed_extent_conversion
124 : public mozilla::IntegralConstant<
125 bool, From == To || From == mozilla::dynamic_extent ||
126 To == mozilla::dynamic_extent> {};
128 template <class From, class To>
129 struct is_allowed_element_type_conversion
130 : public mozilla::IntegralConstant<
131 bool, mozilla::IsConvertible<From (*)[], To (*)[]>::value> {};
133 template <class Span, bool IsConst>
134 class span_iterator {
135 using element_type_ = typename Span::element_type;
137 public:
138 using iterator_category = std::random_access_iterator_tag;
139 using value_type = remove_const_t<element_type_>;
140 using difference_type = typename Span::index_type;
142 using reference = conditional_t<IsConst, const element_type_, element_type_>&;
143 using pointer = add_pointer_t<reference>;
145 constexpr span_iterator() : span_iterator(nullptr, 0) {}
147 constexpr span_iterator(const Span* span, typename Span::index_type index)
148 : span_(span), index_(index) {
149 MOZ_RELEASE_ASSERT(span == nullptr ||
150 (index_ >= 0 && index <= span_->Length()));
153 friend class span_iterator<Span, true>;
154 constexpr MOZ_IMPLICIT span_iterator(const span_iterator<Span, false>& other)
155 : span_iterator(other.span_, other.index_) {}
157 constexpr span_iterator<Span, IsConst>& operator=(
158 const span_iterator<Span, IsConst>&) = default;
160 constexpr reference operator*() const {
161 MOZ_RELEASE_ASSERT(span_);
162 return (*span_)[index_];
165 constexpr pointer operator->() const {
166 MOZ_RELEASE_ASSERT(span_);
167 return &((*span_)[index_]);
170 constexpr span_iterator& operator++() {
171 MOZ_RELEASE_ASSERT(span_ && index_ >= 0 && index_ < span_->Length());
172 ++index_;
173 return *this;
176 constexpr span_iterator operator++(int) {
177 auto ret = *this;
178 ++(*this);
179 return ret;
182 constexpr span_iterator& operator--() {
183 MOZ_RELEASE_ASSERT(span_ && index_ > 0 && index_ <= span_->Length());
184 --index_;
185 return *this;
188 constexpr span_iterator operator--(int) {
189 auto ret = *this;
190 --(*this);
191 return ret;
194 constexpr span_iterator operator+(difference_type n) const {
195 auto ret = *this;
196 return ret += n;
199 constexpr span_iterator& operator+=(difference_type n) {
200 MOZ_RELEASE_ASSERT(span_ && (index_ + n) >= 0 &&
201 (index_ + n) <= span_->Length());
202 index_ += n;
203 return *this;
206 constexpr span_iterator operator-(difference_type n) const {
207 auto ret = *this;
208 return ret -= n;
211 constexpr span_iterator& operator-=(difference_type n)
214 return *this += -n;
217 constexpr difference_type operator-(const span_iterator& rhs) const {
218 MOZ_RELEASE_ASSERT(span_ == rhs.span_);
219 return index_ - rhs.index_;
222 constexpr reference operator[](difference_type n) const {
223 return *(*this + n);
226 constexpr friend bool operator==(const span_iterator& lhs,
227 const span_iterator& rhs) {
228 return lhs.span_ == rhs.span_ && lhs.index_ == rhs.index_;
231 constexpr friend bool operator!=(const span_iterator& lhs,
232 const span_iterator& rhs) {
233 return !(lhs == rhs);
236 constexpr friend bool operator<(const span_iterator& lhs,
237 const span_iterator& rhs) {
238 MOZ_RELEASE_ASSERT(lhs.span_ == rhs.span_);
239 return lhs.index_ < rhs.index_;
242 constexpr friend bool operator<=(const span_iterator& lhs,
243 const span_iterator& rhs) {
244 return !(rhs < lhs);
247 constexpr friend bool operator>(const span_iterator& lhs,
248 const span_iterator& rhs) {
249 return rhs < lhs;
252 constexpr friend bool operator>=(const span_iterator& lhs,
253 const span_iterator& rhs) {
254 return !(rhs > lhs);
257 void swap(span_iterator& rhs) {
258 std::swap(index_, rhs.index_);
259 std::swap(span_, rhs.span_);
262 protected:
263 const Span* span_;
264 size_t index_;
267 template <class Span, bool IsConst>
268 inline constexpr span_iterator<Span, IsConst> operator+(
269 typename span_iterator<Span, IsConst>::difference_type n,
270 const span_iterator<Span, IsConst>& rhs) {
271 return rhs + n;
274 template <size_t Ext>
275 class extent_type {
276 public:
277 using index_type = size_t;
279 static_assert(Ext >= 0, "A fixed-size Span must be >= 0 in size.");
281 constexpr extent_type() {}
283 template <index_type Other>
284 constexpr MOZ_IMPLICIT extent_type(extent_type<Other> ext) {
285 static_assert(
286 Other == Ext || Other == dynamic_extent,
287 "Mismatch between fixed-size extent and size of initializing data.");
288 MOZ_RELEASE_ASSERT(ext.size() == Ext);
291 constexpr MOZ_IMPLICIT extent_type(index_type length) {
292 MOZ_RELEASE_ASSERT(length == Ext);
295 constexpr index_type size() const { return Ext; }
298 template <>
299 class extent_type<dynamic_extent> {
300 public:
301 using index_type = size_t;
303 template <index_type Other>
304 explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size()) {}
306 explicit constexpr extent_type(index_type length) : size_(length) {}
308 constexpr index_type size() const { return size_; }
310 private:
311 index_type size_;
313 } // namespace span_details
316 * Span - slices for C++
318 * Span implements Rust's slice concept for C++. It's called "Span" instead of
319 * "Slice" to follow the naming used in C++ Core Guidelines.
321 * A Span wraps a pointer and a length that identify a non-owning view to a
322 * contiguous block of memory of objects of the same type. Various types,
323 * including (pre-decay) C arrays, XPCOM strings, nsTArray, mozilla::Array,
324 * mozilla::Range and contiguous standard-library containers, auto-convert
325 * into Spans when attempting to pass them as arguments to methods that take
326 * Spans. MakeSpan() functions can be used for explicit conversion in other
327 * contexts. (Span itself autoconverts into mozilla::Range.)
329 * Like Rust's slices, Span provides safety against out-of-bounds access by
330 * performing run-time bound checks. However, unlike Rust's slices, Span
331 * cannot provide safety against use-after-free.
333 * (Note: Span is like Rust's slice only conceptually. Due to the lack of
334 * ABI guarantees, you should still decompose spans/slices to raw pointer
335 * and length parts when crossing the FFI. The Elements() and data() methods
336 * are guaranteed to return a non-null pointer even for zero-length spans,
337 * so the pointer can be used as a raw part of a Rust slice without further
338 * checks.)
340 * In addition to having constructors and MakeSpan() functions that take
341 * various well-known types, a Span for an arbitrary type can be constructed
342 * (via constructor or MakeSpan()) from a pointer and a length or a pointer
343 * and another pointer pointing just past the last element.
345 * A Span<const char> or Span<const char16_t> can be obtained for const char*
346 * or const char16_t pointing to a zero-terminated string using the
347 * MakeStringSpan() function (which treats a nullptr argument equivalently
348 * to the empty string). Corresponding implicit constructor does not exist
349 * in order to avoid accidental construction in cases where const char* or
350 * const char16_t* do not point to a zero-terminated string.
352 * Span has methods that follow the Mozilla naming style and methods that
353 * don't. The methods that follow the Mozilla naming style are meant to be
354 * used directly from Mozilla code. The methods that don't are meant for
355 * integration with C++11 range-based loops and with meta-programming that
356 * expects the same methods that are found on the standard-library
357 * containers. For example, to decompose a Span into its parts in Mozilla
358 * code, use Elements() and Length() (as with nsTArray) instead of data()
359 * and size() (as with std::vector).
361 * The pointer and length wrapped by a Span cannot be changed after a Span has
362 * been created. When new values are required, simply create a new Span. Span
363 * has a method called Subspan() that works analogously to the Substring()
364 * method of XPCOM strings taking a start index and an optional length. As a
365 * Mozilla extension (relative to Microsoft's gsl::span that mozilla::Span is
366 * based on), Span has methods From(start), To(end) and FromTo(start, end)
367 * that correspond to Rust's &slice[start..], &slice[..end] and
368 * &slice[start..end], respectively. (That is, the end index is the index of
369 * the first element not to be included in the new subspan.)
371 * When indicating a Span that's only read from, const goes inside the type
372 * parameter. Don't put const in front of Span. That is:
373 * size_t ReadsFromOneSpanAndWritesToAnother(Span<const uint8_t> aReadFrom,
374 * Span<uint8_t> aWrittenTo);
376 * Any Span<const T> can be viewed as Span<const uint8_t> using the function
377 * AsBytes(). Any Span<T> can be viewed as Span<uint8_t> using the function
378 * AsWritableBytes().
380 template <class ElementType, size_t Extent>
381 class Span {
382 public:
383 // constants and types
384 using element_type = ElementType;
385 using index_type = size_t;
386 using pointer = element_type*;
387 using reference = element_type&;
389 using iterator =
390 span_details::span_iterator<Span<ElementType, Extent>, false>;
391 using const_iterator =
392 span_details::span_iterator<Span<ElementType, Extent>, true>;
393 using reverse_iterator = std::reverse_iterator<iterator>;
394 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
396 constexpr static const index_type extent = Extent;
398 // [Span.cons], Span constructors, copy, assignment, and destructor
399 // "Dependent" is needed to make "span_details::enable_if_t<(Dependent ||
400 // Extent == 0 || Extent == mozilla::MaxValue<size_t>::value)>" SFINAE,
401 // since "span_details::enable_if_t<(Extent == 0 || Extent ==
402 // mozilla::MaxValue<size_t>::value)>" is ill-formed when Extent is neither
403 // of the extreme values.
405 * Constructor with no args.
407 template <bool Dependent = false,
408 class = span_details::enable_if_t<
409 (Dependent || Extent == 0 ||
410 Extent == mozilla::MaxValue<size_t>::value)>>
411 constexpr Span() : storage_(nullptr, span_details::extent_type<0>()) {}
414 * Constructor for nullptr.
416 constexpr MOZ_IMPLICIT Span(std::nullptr_t) : Span() {}
419 * Constructor for pointer and length.
421 constexpr Span(pointer aPtr, index_type aLength) : storage_(aPtr, aLength) {}
424 * Constructor for start pointer and pointer past end.
426 constexpr Span(pointer aStartPtr, pointer aEndPtr)
427 : storage_(aStartPtr, std::distance(aStartPtr, aEndPtr)) {}
430 * Constructor for C array.
432 template <size_t N>
433 constexpr MOZ_IMPLICIT Span(element_type (&aArr)[N])
434 : storage_(&aArr[0], span_details::extent_type<N>()) {}
436 // Implicit constructors for char* and char16_t* pointers are deleted in order
437 // to avoid accidental construction in cases where a pointer does not point to
438 // a zero-terminated string. A Span<const char> or Span<const char16_t> can be
439 // obtained for const char* or const char16_t pointing to a zero-terminated
440 // string using the MakeStringSpan() function.
441 Span(char* aStr) = delete;
442 Span(const char* aStr) = delete;
443 Span(char16_t* aStr) = delete;
444 Span(const char16_t* aStr) = delete;
447 * Constructor for std::array.
449 template <size_t N,
450 class ArrayElementType = span_details::remove_const_t<element_type>>
451 constexpr MOZ_IMPLICIT Span(std::array<ArrayElementType, N>& aArr)
452 : storage_(&aArr[0], span_details::extent_type<N>()) {}
455 * Constructor for const std::array.
457 template <size_t N>
458 constexpr MOZ_IMPLICIT Span(
459 const std::array<span_details::remove_const_t<element_type>, N>& aArr)
460 : storage_(&aArr[0], span_details::extent_type<N>()) {}
463 * Constructor for mozilla::Array.
465 template <size_t N,
466 class ArrayElementType = span_details::remove_const_t<element_type>>
467 constexpr MOZ_IMPLICIT Span(mozilla::Array<ArrayElementType, N>& aArr)
468 : storage_(&aArr[0], span_details::extent_type<N>()) {}
471 * Constructor for const mozilla::Array.
473 template <size_t N>
474 constexpr MOZ_IMPLICIT Span(
475 const mozilla::Array<span_details::remove_const_t<element_type>, N>& aArr)
476 : storage_(&aArr[0], span_details::extent_type<N>()) {}
479 * Constructor for mozilla::UniquePtr holding an array and length.
481 template <class ArrayElementType = std::add_pointer<element_type>>
482 constexpr Span(const mozilla::UniquePtr<ArrayElementType>& aPtr,
483 index_type aLength)
484 : storage_(aPtr.get(), aLength) {}
486 // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the
487 // requirement on Container to be a contiguous sequence container.
489 * Constructor for standard-library containers.
491 template <
492 class Container,
493 class = span_details::enable_if_t<
494 !span_details::is_span<Container>::value &&
495 !span_details::is_std_array<Container>::value &&
496 mozilla::IsConvertible<typename Container::pointer, pointer>::value &&
497 mozilla::IsConvertible<
498 typename Container::pointer,
499 decltype(mozilla::DeclVal<Container>().data())>::value>>
500 constexpr MOZ_IMPLICIT Span(Container& cont)
501 : Span(cont.data(), ReleaseAssertedCast<index_type>(cont.size())) {}
504 * Constructor for standard-library containers (const version).
506 template <
507 class Container,
508 class = span_details::enable_if_t<
509 mozilla::IsConst<element_type>::value &&
510 !span_details::is_span<Container>::value &&
511 mozilla::IsConvertible<typename Container::pointer, pointer>::value &&
512 mozilla::IsConvertible<
513 typename Container::pointer,
514 decltype(mozilla::DeclVal<Container>().data())>::value>>
515 constexpr MOZ_IMPLICIT Span(const Container& cont)
516 : Span(cont.data(), ReleaseAssertedCast<index_type>(cont.size())) {}
519 * Constructor from other Span.
521 constexpr Span(const Span& other) = default;
524 * Constructor from other Span.
526 constexpr Span(Span&& other) = default;
529 * Constructor from other Span with conversion of element type.
531 template <class OtherElementType, size_t OtherExtent,
532 class = span_details::enable_if_t<
533 span_details::is_allowed_extent_conversion<OtherExtent,
534 Extent>::value &&
535 span_details::is_allowed_element_type_conversion<
536 OtherElementType, element_type>::value>>
537 constexpr MOZ_IMPLICIT Span(const Span<OtherElementType, OtherExtent>& other)
538 : storage_(other.data(),
539 span_details::extent_type<OtherExtent>(other.size())) {}
542 * Constructor from other Span with conversion of element type.
544 template <class OtherElementType, size_t OtherExtent,
545 class = span_details::enable_if_t<
546 span_details::is_allowed_extent_conversion<OtherExtent,
547 Extent>::value &&
548 span_details::is_allowed_element_type_conversion<
549 OtherElementType, element_type>::value>>
550 constexpr MOZ_IMPLICIT Span(Span<OtherElementType, OtherExtent>&& other)
551 : storage_(other.data(),
552 span_details::extent_type<OtherExtent>(other.size())) {}
554 ~Span() = default;
555 constexpr Span& operator=(const Span& other) = default;
557 constexpr Span& operator=(Span&& other) = default;
559 // [Span.sub], Span subviews
561 * Subspan with first N elements with compile-time N.
563 template <size_t Count>
564 constexpr Span<element_type, Count> First() const {
565 MOZ_RELEASE_ASSERT(Count <= size());
566 return {data(), Count};
570 * Subspan with last N elements with compile-time N.
572 template <size_t Count>
573 constexpr Span<element_type, Count> Last() const {
574 const size_t len = size();
575 MOZ_RELEASE_ASSERT(Count <= len);
576 return {data() + (len - Count), Count};
580 * Subspan with compile-time start index and length.
582 template <size_t Offset, size_t Count = dynamic_extent>
583 constexpr Span<element_type, Count> Subspan() const {
584 const size_t len = size();
585 MOZ_RELEASE_ASSERT(Offset <= len &&
586 (Count == dynamic_extent || (Offset + Count <= len)));
587 return {data() + Offset, Count == dynamic_extent ? len - Offset : Count};
591 * Subspan with first N elements with run-time N.
593 constexpr Span<element_type, dynamic_extent> First(index_type aCount) const {
594 MOZ_RELEASE_ASSERT(aCount <= size());
595 return {data(), aCount};
599 * Subspan with last N elements with run-time N.
601 constexpr Span<element_type, dynamic_extent> Last(index_type aCount) const {
602 const size_t len = size();
603 MOZ_RELEASE_ASSERT(aCount <= len);
604 return {data() + (len - aCount), aCount};
608 * Subspan with run-time start index and length.
610 constexpr Span<element_type, dynamic_extent> Subspan(
611 index_type aStart, index_type aLength = dynamic_extent) const {
612 const size_t len = size();
613 MOZ_RELEASE_ASSERT(aStart <= len && (aLength == dynamic_extent ||
614 (aStart + aLength <= len)));
615 return {data() + aStart,
616 aLength == dynamic_extent ? len - aStart : aLength};
620 * Subspan with run-time start index. (Rust's &foo[start..])
622 constexpr Span<element_type, dynamic_extent> From(index_type aStart) const {
623 return Subspan(aStart);
627 * Subspan with run-time exclusive end index. (Rust's &foo[..end])
629 constexpr Span<element_type, dynamic_extent> To(index_type aEnd) const {
630 return Subspan(0, aEnd);
634 * Subspan with run-time start index and exclusive end index.
635 * (Rust's &foo[start..end])
637 constexpr Span<element_type, dynamic_extent> FromTo(index_type aStart,
638 index_type aEnd) const {
639 MOZ_RELEASE_ASSERT(aStart <= aEnd);
640 return Subspan(aStart, aEnd - aStart);
643 // [Span.obs], Span observers
645 * Number of elements in the span.
647 constexpr index_type Length() const { return size(); }
650 * Number of elements in the span (standard-libray duck typing version).
652 constexpr index_type size() const { return storage_.size(); }
655 * Size of the span in bytes.
657 constexpr index_type LengthBytes() const { return size_bytes(); }
660 * Size of the span in bytes (standard-library naming style version).
662 constexpr index_type size_bytes() const {
663 return size() * narrow_cast<index_type>(sizeof(element_type));
667 * Checks if the the length of the span is zero.
669 constexpr bool IsEmpty() const { return empty(); }
672 * Checks if the the length of the span is zero (standard-libray duck
673 * typing version).
675 constexpr bool empty() const { return size() == 0; }
677 // [Span.elem], Span element access
678 constexpr reference operator[](index_type idx) const {
679 MOZ_RELEASE_ASSERT(idx < storage_.size());
680 return data()[idx];
684 * Access element of span by index (standard-library duck typing version).
686 constexpr reference at(index_type idx) const { return this->operator[](idx); }
688 constexpr reference operator()(index_type idx) const {
689 return this->operator[](idx);
693 * Pointer to the first element of the span. The return value is never
694 * nullptr, not ever for zero-length spans, so it can be passed as-is
695 * to std::slice::from_raw_parts() in Rust.
697 constexpr pointer Elements() const { return data(); }
700 * Pointer to the first element of the span (standard-libray duck typing
701 * version). The return value is never nullptr, not ever for zero-length
702 * spans, so it can be passed as-is to std::slice::from_raw_parts() in Rust.
704 constexpr pointer data() const { return storage_.data(); }
706 // [Span.iter], Span iterator support
707 iterator begin() const { return {this, 0}; }
708 iterator end() const { return {this, Length()}; }
710 const_iterator cbegin() const { return {this, 0}; }
711 const_iterator cend() const { return {this, Length()}; }
713 reverse_iterator rbegin() const { return reverse_iterator{end()}; }
714 reverse_iterator rend() const { return reverse_iterator{begin()}; }
716 const_reverse_iterator crbegin() const {
717 return const_reverse_iterator{cend()};
719 const_reverse_iterator crend() const {
720 return const_reverse_iterator{cbegin()};
723 private:
724 // this implementation detail class lets us take advantage of the
725 // empty base class optimization to pay for only storage of a single
726 // pointer in the case of fixed-size Spans
727 template <class ExtentType>
728 class storage_type : public ExtentType {
729 public:
730 template <class OtherExtentType>
731 constexpr storage_type(pointer elements, OtherExtentType ext)
732 : ExtentType(ext)
733 // Replace nullptr with aligned bogus pointer for Rust slice
734 // compatibility. See
735 // https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html
737 data_(elements ? elements
738 : reinterpret_cast<pointer>(alignof(element_type))) {
739 const size_t extentSize = ExtentType::size();
740 MOZ_RELEASE_ASSERT(
741 (!elements && extentSize == 0) ||
742 (elements && extentSize != mozilla::MaxValue<size_t>::value));
745 constexpr pointer data() const { return data_; }
747 private:
748 pointer data_;
751 storage_type<span_details::extent_type<Extent>> storage_;
754 // [Span.comparison], Span comparison operators
755 template <class ElementType, size_t FirstExtent, size_t SecondExtent>
756 inline constexpr bool operator==(const Span<ElementType, FirstExtent>& l,
757 const Span<ElementType, SecondExtent>& r) {
758 return (l.size() == r.size()) && std::equal(l.begin(), l.end(), r.begin());
761 template <class ElementType, size_t Extent>
762 inline constexpr bool operator!=(const Span<ElementType, Extent>& l,
763 const Span<ElementType, Extent>& r) {
764 return !(l == r);
767 template <class ElementType, size_t Extent>
768 inline constexpr bool operator<(const Span<ElementType, Extent>& l,
769 const Span<ElementType, Extent>& r) {
770 return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
773 template <class ElementType, size_t Extent>
774 inline constexpr bool operator<=(const Span<ElementType, Extent>& l,
775 const Span<ElementType, Extent>& r) {
776 return !(l > r);
779 template <class ElementType, size_t Extent>
780 inline constexpr bool operator>(const Span<ElementType, Extent>& l,
781 const Span<ElementType, Extent>& r) {
782 return r < l;
785 template <class ElementType, size_t Extent>
786 inline constexpr bool operator>=(const Span<ElementType, Extent>& l,
787 const Span<ElementType, Extent>& r) {
788 return !(l < r);
791 namespace span_details {
792 // if we only supported compilers with good constexpr support then
793 // this pair of classes could collapse down to a constexpr function
795 // we should use a narrow_cast<> to go to size_t, but older compilers may not
796 // see it as constexpr and so will fail compilation of the template
797 template <class ElementType, size_t Extent>
798 struct calculate_byte_size
799 : mozilla::IntegralConstant<size_t, static_cast<size_t>(
800 sizeof(ElementType) *
801 static_cast<size_t>(Extent))> {};
803 template <class ElementType>
804 struct calculate_byte_size<ElementType, dynamic_extent>
805 : mozilla::IntegralConstant<size_t, dynamic_extent> {};
806 } // namespace span_details
808 // [Span.objectrep], views of object representation
810 * View span as Span<const uint8_t>.
812 template <class ElementType, size_t Extent>
813 Span<const uint8_t,
814 span_details::calculate_byte_size<ElementType, Extent>::value>
815 AsBytes(Span<ElementType, Extent> s) {
816 return {reinterpret_cast<const uint8_t*>(s.data()), s.size_bytes()};
820 * View span as Span<uint8_t>.
822 template <
823 class ElementType, size_t Extent,
824 class = span_details::enable_if_t<!mozilla::IsConst<ElementType>::value>>
825 Span<uint8_t, span_details::calculate_byte_size<ElementType, Extent>::value>
826 AsWritableBytes(Span<ElementType, Extent> s) {
827 return {reinterpret_cast<uint8_t*>(s.data()), s.size_bytes()};
831 // MakeSpan() - Utility functions for creating Spans
834 * Create span from pointer and length.
836 template <class ElementType>
837 Span<ElementType> MakeSpan(ElementType* aPtr,
838 typename Span<ElementType>::index_type aLength) {
839 return Span<ElementType>(aPtr, aLength);
843 * Create span from start pointer and pointer past end.
845 template <class ElementType>
846 Span<ElementType> MakeSpan(ElementType* aStartPtr, ElementType* aEndPtr) {
847 return Span<ElementType>(aStartPtr, aEndPtr);
851 * Create span from C array.
852 * MakeSpan() does not permit creating Span objects from string literals (const
853 * char or char16_t arrays) because the Span length would include the zero
854 * terminator, which may surprise callers. Use MakeStringSpan() to create a
855 * Span whose length that excludes the string literal's zero terminator or use
856 * the MakeSpan() overload that accepts a pointer and length and specify the
857 * string literal's full length.
859 template <class ElementType, size_t N,
860 class = span_details::enable_if_t<
861 !IsSame<ElementType, const char>::value &&
862 !IsSame<ElementType, const char16_t>::value>>
863 Span<ElementType> MakeSpan(ElementType (&aArr)[N]) {
864 return Span<ElementType>(aArr, N);
868 * Create span from mozilla::Array.
870 template <class ElementType, size_t N>
871 Span<ElementType> MakeSpan(mozilla::Array<ElementType, N>& aArr) {
872 return aArr;
876 * Create span from const mozilla::Array.
878 template <class ElementType, size_t N>
879 Span<const ElementType> MakeSpan(const mozilla::Array<ElementType, N>& arr) {
880 return arr;
884 * Create span from standard-library container.
886 template <class Container>
887 Span<typename Container::value_type> MakeSpan(Container& cont) {
888 return Span<typename Container::value_type>(cont);
892 * Create span from standard-library container (const version).
894 template <class Container>
895 Span<const typename Container::value_type> MakeSpan(const Container& cont) {
896 return Span<const typename Container::value_type>(cont);
900 * Create span from smart pointer and length.
902 template <class Ptr>
903 Span<typename Ptr::element_type> MakeSpan(Ptr& aPtr, size_t aLength) {
904 return Span<typename Ptr::element_type>(aPtr, aLength);
908 * Create span from a zero-terminated C string. nullptr is
909 * treated as the empty string.
911 inline Span<const char> MakeStringSpan(const char* aZeroTerminated) {
912 if (!aZeroTerminated) {
913 return Span<const char>();
915 return Span<const char>(aZeroTerminated, std::strlen(aZeroTerminated));
919 * Create span from a zero-terminated UTF-16 C string. nullptr is
920 * treated as the empty string.
922 inline Span<const char16_t> MakeStringSpan(const char16_t* aZeroTerminated) {
923 if (!aZeroTerminated) {
924 return Span<const char16_t>();
926 return Span<const char16_t>(aZeroTerminated,
927 span_details::strlen16(aZeroTerminated));
930 } // namespace mozilla
932 # ifdef _MSC_VER
933 # if _MSC_VER < 1910
934 # undef constexpr
935 # pragma pop_macro("constexpr")
937 # endif // _MSC_VER < 1910
939 # pragma warning(pop)
940 # endif // _MSC_VER
942 #endif // mozilla_Span_h