1 ///////////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
5 // This code is licensed under the MIT License (MIT).
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
15 ///////////////////////////////////////////////////////////////////////////////
18 // https://github.com/Microsoft/GSL/blob/3819df6e378ffccf0e29465afe99c3b324c2aa70/include/gsl/span
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"
40 // Stuff from gsl_util
42 // narrow_cast(): a searchable way to do narrowing casts of values
43 template <class T
, class U
>
44 inline constexpr T
narrow_cast(U
&& u
) {
45 return static_cast<T
>(std::forward
<U
>(u
));
50 // [views.constants], constants
51 // This was -1 in gsl::span, but using size_t for sizes instead of ptrdiff_t
52 // and reserving a magic value that realistically doesn't occur in
53 // compile-time-constant Span sizes makes things a lot less messy in terms of
54 // comparison between signed and unsigned.
55 constexpr const size_t dynamic_extent
= mozilla::MaxValue
<size_t>::value
;
57 template <class ElementType
, size_t Extent
= dynamic_extent
>
60 // implementation details
61 namespace span_details
{
63 inline size_t strlen16(const char16_t
* aZeroTerminated
) {
65 while (*(aZeroTerminated
++)) {
71 // C++14 types that we don't have because we build as C++11.
73 using remove_cv_t
= typename
mozilla::RemoveCV
<T
>::Type
;
75 using remove_const_t
= typename
mozilla::RemoveConst
<T
>::Type
;
76 template <bool B
, class T
, class F
>
77 using conditional_t
= typename
mozilla::Conditional
<B
, T
, F
>::Type
;
79 using add_pointer_t
= typename
mozilla::AddPointer
<T
>::Type
;
80 template <bool B
, class T
= void>
81 using enable_if_t
= typename
mozilla::EnableIf
<B
, T
>::Type
;
84 struct is_span_oracle
: mozilla::FalseType
{};
86 template <class ElementType
, size_t Extent
>
87 struct is_span_oracle
<mozilla::Span
<ElementType
, Extent
>> : mozilla::TrueType
{
91 struct is_span
: public is_span_oracle
<remove_cv_t
<T
>> {};
94 struct is_std_array_oracle
: mozilla::FalseType
{};
96 template <class ElementType
, size_t Extent
>
97 struct is_std_array_oracle
<std::array
<ElementType
, Extent
>>
98 : mozilla::TrueType
{};
101 struct is_std_array
: public is_std_array_oracle
<remove_cv_t
<T
>> {};
103 template <size_t From
, size_t To
>
104 struct is_allowed_extent_conversion
105 : public mozilla::IntegralConstant
<
106 bool, From
== To
|| From
== mozilla::dynamic_extent
||
107 To
== mozilla::dynamic_extent
> {};
109 template <class From
, class To
>
110 struct is_allowed_element_type_conversion
111 : public mozilla::IntegralConstant
<
112 bool, mozilla::IsConvertible
<From (*)[], To (*)[]>::value
> {};
114 template <class Span
, bool IsConst
>
115 class span_iterator
{
116 using element_type_
= typename
Span::element_type
;
119 using iterator_category
= std::random_access_iterator_tag
;
120 using value_type
= remove_const_t
<element_type_
>;
121 using difference_type
= typename
Span::index_type
;
123 using reference
= conditional_t
<IsConst
, const element_type_
, element_type_
>&;
124 using pointer
= add_pointer_t
<reference
>;
126 constexpr span_iterator() : span_iterator(nullptr, 0) {}
128 constexpr span_iterator(const Span
* span
, typename
Span::index_type index
)
129 : span_(span
), index_(index
) {
130 MOZ_RELEASE_ASSERT(span
== nullptr ||
131 (index_
>= 0 && index
<= span_
->Length()));
134 friend class span_iterator
<Span
, true>;
135 constexpr MOZ_IMPLICIT
span_iterator(const span_iterator
<Span
, false>& other
)
136 : span_iterator(other
.span_
, other
.index_
) {}
138 constexpr span_iterator
<Span
, IsConst
>& operator=(
139 const span_iterator
<Span
, IsConst
>&) = default;
141 constexpr reference
operator*() const {
142 MOZ_RELEASE_ASSERT(span_
);
143 return (*span_
)[index_
];
146 constexpr pointer
operator->() const {
147 MOZ_RELEASE_ASSERT(span_
);
148 return &((*span_
)[index_
]);
151 constexpr span_iterator
& operator++() {
152 MOZ_RELEASE_ASSERT(span_
&& index_
>= 0 && index_
< span_
->Length());
157 constexpr span_iterator
operator++(int) {
163 constexpr span_iterator
& operator--() {
164 MOZ_RELEASE_ASSERT(span_
&& index_
> 0 && index_
<= span_
->Length());
169 constexpr span_iterator
operator--(int) {
175 constexpr span_iterator
operator+(difference_type n
) const {
180 constexpr span_iterator
& operator+=(difference_type n
) {
181 MOZ_RELEASE_ASSERT(span_
&& (index_
+ n
) >= 0 &&
182 (index_
+ n
) <= span_
->Length());
187 constexpr span_iterator
operator-(difference_type n
) const {
192 constexpr span_iterator
& operator-=(difference_type n
)
198 constexpr difference_type
operator-(const span_iterator
& rhs
) const {
199 MOZ_RELEASE_ASSERT(span_
== rhs
.span_
);
200 return index_
- rhs
.index_
;
203 constexpr reference
operator[](difference_type n
) const {
207 constexpr friend bool operator==(const span_iterator
& lhs
,
208 const span_iterator
& rhs
) {
209 return lhs
.span_
== rhs
.span_
&& lhs
.index_
== rhs
.index_
;
212 constexpr friend bool operator!=(const span_iterator
& lhs
,
213 const span_iterator
& rhs
) {
214 return !(lhs
== rhs
);
217 constexpr friend bool operator<(const span_iterator
& lhs
,
218 const span_iterator
& rhs
) {
219 MOZ_RELEASE_ASSERT(lhs
.span_
== rhs
.span_
);
220 return lhs
.index_
< rhs
.index_
;
223 constexpr friend bool operator<=(const span_iterator
& lhs
,
224 const span_iterator
& rhs
) {
228 constexpr friend bool operator>(const span_iterator
& lhs
,
229 const span_iterator
& rhs
) {
233 constexpr friend bool operator>=(const span_iterator
& lhs
,
234 const span_iterator
& rhs
) {
238 void swap(span_iterator
& rhs
) {
239 std::swap(index_
, rhs
.index_
);
240 std::swap(span_
, rhs
.span_
);
248 template <class Span
, bool IsConst
>
249 inline constexpr span_iterator
<Span
, IsConst
> operator+(
250 typename span_iterator
<Span
, IsConst
>::difference_type n
,
251 const span_iterator
<Span
, IsConst
>& rhs
) {
255 template <size_t Ext
>
258 using index_type
= size_t;
260 static_assert(Ext
>= 0, "A fixed-size Span must be >= 0 in size.");
262 constexpr extent_type() {}
264 template <index_type Other
>
265 constexpr MOZ_IMPLICIT
extent_type(extent_type
<Other
> ext
) {
267 Other
== Ext
|| Other
== dynamic_extent
,
268 "Mismatch between fixed-size extent and size of initializing data.");
269 MOZ_RELEASE_ASSERT(ext
.size() == Ext
);
272 constexpr MOZ_IMPLICIT
extent_type(index_type length
) {
273 MOZ_RELEASE_ASSERT(length
== Ext
);
276 constexpr index_type
size() const { return Ext
; }
280 class extent_type
<dynamic_extent
> {
282 using index_type
= size_t;
284 template <index_type Other
>
285 explicit constexpr extent_type(extent_type
<Other
> ext
) : size_(ext
.size()) {}
287 explicit constexpr extent_type(index_type length
) : size_(length
) {}
289 constexpr index_type
size() const { return size_
; }
294 } // namespace span_details
297 * Span - slices for C++
299 * Span implements Rust's slice concept for C++. It's called "Span" instead of
300 * "Slice" to follow the naming used in C++ Core Guidelines.
302 * A Span wraps a pointer and a length that identify a non-owning view to a
303 * contiguous block of memory of objects of the same type. Various types,
304 * including (pre-decay) C arrays, XPCOM strings, nsTArray, mozilla::Array,
305 * mozilla::Range and contiguous standard-library containers, auto-convert
306 * into Spans when attempting to pass them as arguments to methods that take
307 * Spans. MakeSpan() functions can be used for explicit conversion in other
308 * contexts. (Span itself autoconverts into mozilla::Range.)
310 * Like Rust's slices, Span provides safety against out-of-bounds access by
311 * performing run-time bound checks. However, unlike Rust's slices, Span
312 * cannot provide safety against use-after-free.
314 * (Note: Span is like Rust's slice only conceptually. Due to the lack of
315 * ABI guarantees, you should still decompose spans/slices to raw pointer
316 * and length parts when crossing the FFI. The Elements() and data() methods
317 * are guaranteed to return a non-null pointer even for zero-length spans,
318 * so the pointer can be used as a raw part of a Rust slice without further
321 * In addition to having constructors and MakeSpan() functions that take
322 * various well-known types, a Span for an arbitrary type can be constructed
323 * (via constructor or MakeSpan()) from a pointer and a length or a pointer
324 * and another pointer pointing just past the last element.
326 * A Span<const char> or Span<const char16_t> can be obtained for const char*
327 * or const char16_t pointing to a zero-terminated string using the
328 * MakeStringSpan() function (which treats a nullptr argument equivalently
329 * to the empty string). Corresponding implicit constructor does not exist
330 * in order to avoid accidental construction in cases where const char* or
331 * const char16_t* do not point to a zero-terminated string.
333 * Span has methods that follow the Mozilla naming style and methods that
334 * don't. The methods that follow the Mozilla naming style are meant to be
335 * used directly from Mozilla code. The methods that don't are meant for
336 * integration with C++11 range-based loops and with meta-programming that
337 * expects the same methods that are found on the standard-library
338 * containers. For example, to decompose a Span into its parts in Mozilla
339 * code, use Elements() and Length() (as with nsTArray) instead of data()
340 * and size() (as with std::vector).
342 * The pointer and length wrapped by a Span cannot be changed after a Span has
343 * been created. When new values are required, simply create a new Span. Span
344 * has a method called Subspan() that works analogously to the Substring()
345 * method of XPCOM strings taking a start index and an optional length. As a
346 * Mozilla extension (relative to Microsoft's gsl::span that mozilla::Span is
347 * based on), Span has methods From(start), To(end) and FromTo(start, end)
348 * that correspond to Rust's &slice[start..], &slice[..end] and
349 * &slice[start..end], respectively. (That is, the end index is the index of
350 * the first element not to be included in the new subspan.)
352 * When indicating a Span that's only read from, const goes inside the type
353 * parameter. Don't put const in front of Span. That is:
354 * size_t ReadsFromOneSpanAndWritesToAnother(Span<const uint8_t> aReadFrom,
355 * Span<uint8_t> aWrittenTo);
357 * Any Span<const T> can be viewed as Span<const uint8_t> using the function
358 * AsBytes(). Any Span<T> can be viewed as Span<uint8_t> using the function
361 template <class ElementType
, size_t Extent
>
364 // constants and types
365 using element_type
= ElementType
;
366 using index_type
= size_t;
367 using pointer
= element_type
*;
368 using reference
= element_type
&;
371 span_details::span_iterator
<Span
<ElementType
, Extent
>, false>;
372 using const_iterator
=
373 span_details::span_iterator
<Span
<ElementType
, Extent
>, true>;
374 using reverse_iterator
= std::reverse_iterator
<iterator
>;
375 using const_reverse_iterator
= std::reverse_iterator
<const_iterator
>;
377 constexpr static const index_type extent
= Extent
;
379 // [Span.cons], Span constructors, copy, assignment, and destructor
380 // "Dependent" is needed to make "span_details::enable_if_t<(Dependent ||
381 // Extent == 0 || Extent == mozilla::MaxValue<size_t>::value)>" SFINAE,
382 // since "span_details::enable_if_t<(Extent == 0 || Extent ==
383 // mozilla::MaxValue<size_t>::value)>" is ill-formed when Extent is neither
384 // of the extreme values.
386 * Constructor with no args.
388 template <bool Dependent
= false,
389 class = span_details::enable_if_t
<
390 (Dependent
|| Extent
== 0 ||
391 Extent
== mozilla::MaxValue
<size_t>::value
)>>
392 constexpr Span() : storage_(nullptr, span_details::extent_type
<0>()) {}
395 * Constructor for nullptr.
397 constexpr MOZ_IMPLICIT
Span(std::nullptr_t
) : Span() {}
400 * Constructor for pointer and length.
402 constexpr Span(pointer aPtr
, index_type aLength
) : storage_(aPtr
, aLength
) {}
405 * Constructor for start pointer and pointer past end.
407 constexpr Span(pointer aStartPtr
, pointer aEndPtr
)
408 : storage_(aStartPtr
, std::distance(aStartPtr
, aEndPtr
)) {}
411 * Constructor for C array.
414 constexpr MOZ_IMPLICIT
Span(element_type (&aArr
)[N
])
415 : storage_(&aArr
[0], span_details::extent_type
<N
>()) {}
417 // Implicit constructors for char* and char16_t* pointers are deleted in order
418 // to avoid accidental construction in cases where a pointer does not point to
419 // a zero-terminated string. A Span<const char> or Span<const char16_t> can be
420 // obtained for const char* or const char16_t pointing to a zero-terminated
421 // string using the MakeStringSpan() function.
422 Span(char* aStr
) = delete;
423 Span(const char* aStr
) = delete;
424 Span(char16_t
* aStr
) = delete;
425 Span(const char16_t
* aStr
) = delete;
428 * Constructor for std::array.
431 class ArrayElementType
= span_details::remove_const_t
<element_type
>>
432 constexpr MOZ_IMPLICIT
Span(std::array
<ArrayElementType
, N
>& aArr
)
433 : storage_(&aArr
[0], span_details::extent_type
<N
>()) {}
436 * Constructor for const std::array.
439 constexpr MOZ_IMPLICIT
Span(
440 const std::array
<span_details::remove_const_t
<element_type
>, N
>& aArr
)
441 : storage_(&aArr
[0], span_details::extent_type
<N
>()) {}
444 * Constructor for mozilla::Array.
447 class ArrayElementType
= span_details::remove_const_t
<element_type
>>
448 constexpr MOZ_IMPLICIT
Span(mozilla::Array
<ArrayElementType
, N
>& aArr
)
449 : storage_(&aArr
[0], span_details::extent_type
<N
>()) {}
452 * Constructor for const mozilla::Array.
455 constexpr MOZ_IMPLICIT
Span(
456 const mozilla::Array
<span_details::remove_const_t
<element_type
>, N
>& aArr
)
457 : storage_(&aArr
[0], span_details::extent_type
<N
>()) {}
460 * Constructor for mozilla::UniquePtr holding an array and length.
462 template <class ArrayElementType
= std::add_pointer
<element_type
>>
463 constexpr Span(const mozilla::UniquePtr
<ArrayElementType
>& aPtr
,
465 : storage_(aPtr
.get(), aLength
) {}
467 // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the
468 // requirement on Container to be a contiguous sequence container.
470 * Constructor for standard-library containers.
474 class = span_details::enable_if_t
<
475 !span_details::is_span
<Container
>::value
&&
476 !span_details::is_std_array
<Container
>::value
&&
477 mozilla::IsConvertible
<typename
Container::pointer
, pointer
>::value
&&
478 mozilla::IsConvertible
<
479 typename
Container::pointer
,
480 decltype(mozilla::DeclVal
<Container
>().data())>::value
>>
481 constexpr MOZ_IMPLICIT
Span(Container
& cont
)
482 : Span(cont
.data(), ReleaseAssertedCast
<index_type
>(cont
.size())) {}
485 * Constructor for standard-library containers (const version).
489 class = span_details::enable_if_t
<
490 mozilla::IsConst
<element_type
>::value
&&
491 !span_details::is_span
<Container
>::value
&&
492 mozilla::IsConvertible
<typename
Container::pointer
, pointer
>::value
&&
493 mozilla::IsConvertible
<
494 typename
Container::pointer
,
495 decltype(mozilla::DeclVal
<Container
>().data())>::value
>>
496 constexpr MOZ_IMPLICIT
Span(const Container
& cont
)
497 : Span(cont
.data(), ReleaseAssertedCast
<index_type
>(cont
.size())) {}
500 * Constructor from other Span.
502 constexpr Span(const Span
& other
) = default;
505 * Constructor from other Span.
507 constexpr Span(Span
&& other
) = default;
510 * Constructor from other Span with conversion of element type.
512 template <class OtherElementType
, size_t OtherExtent
,
513 class = span_details::enable_if_t
<
514 span_details::is_allowed_extent_conversion
<OtherExtent
,
516 span_details::is_allowed_element_type_conversion
<
517 OtherElementType
, element_type
>::value
>>
518 constexpr MOZ_IMPLICIT
Span(const Span
<OtherElementType
, OtherExtent
>& other
)
519 : storage_(other
.data(),
520 span_details::extent_type
<OtherExtent
>(other
.size())) {}
523 * Constructor from other Span with conversion of element type.
525 template <class OtherElementType
, size_t OtherExtent
,
526 class = span_details::enable_if_t
<
527 span_details::is_allowed_extent_conversion
<OtherExtent
,
529 span_details::is_allowed_element_type_conversion
<
530 OtherElementType
, element_type
>::value
>>
531 constexpr MOZ_IMPLICIT
Span(Span
<OtherElementType
, OtherExtent
>&& other
)
532 : storage_(other
.data(),
533 span_details::extent_type
<OtherExtent
>(other
.size())) {}
536 constexpr Span
& operator=(const Span
& other
) = default;
538 constexpr Span
& operator=(Span
&& other
) = default;
540 // [Span.sub], Span subviews
542 * Subspan with first N elements with compile-time N.
544 template <size_t Count
>
545 constexpr Span
<element_type
, Count
> First() const {
546 MOZ_RELEASE_ASSERT(Count
<= size());
547 return {data(), Count
};
551 * Subspan with last N elements with compile-time N.
553 template <size_t Count
>
554 constexpr Span
<element_type
, Count
> Last() const {
555 const size_t len
= size();
556 MOZ_RELEASE_ASSERT(Count
<= len
);
557 return {data() + (len
- Count
), Count
};
561 * Subspan with compile-time start index and length.
563 template <size_t Offset
, size_t Count
= dynamic_extent
>
564 constexpr Span
<element_type
, Count
> Subspan() const {
565 const size_t len
= size();
566 MOZ_RELEASE_ASSERT(Offset
<= len
&&
567 (Count
== dynamic_extent
|| (Offset
+ Count
<= len
)));
568 return {data() + Offset
, Count
== dynamic_extent
? len
- Offset
: Count
};
572 * Subspan with first N elements with run-time N.
574 constexpr Span
<element_type
, dynamic_extent
> First(index_type aCount
) const {
575 MOZ_RELEASE_ASSERT(aCount
<= size());
576 return {data(), aCount
};
580 * Subspan with last N elements with run-time N.
582 constexpr Span
<element_type
, dynamic_extent
> Last(index_type aCount
) const {
583 const size_t len
= size();
584 MOZ_RELEASE_ASSERT(aCount
<= len
);
585 return {data() + (len
- aCount
), aCount
};
589 * Subspan with run-time start index and length.
591 constexpr Span
<element_type
, dynamic_extent
> Subspan(
592 index_type aStart
, index_type aLength
= dynamic_extent
) const {
593 const size_t len
= size();
594 MOZ_RELEASE_ASSERT(aStart
<= len
&& (aLength
== dynamic_extent
||
595 (aStart
+ aLength
<= len
)));
596 return {data() + aStart
,
597 aLength
== dynamic_extent
? len
- aStart
: aLength
};
601 * Subspan with run-time start index. (Rust's &foo[start..])
603 constexpr Span
<element_type
, dynamic_extent
> From(index_type aStart
) const {
604 return Subspan(aStart
);
608 * Subspan with run-time exclusive end index. (Rust's &foo[..end])
610 constexpr Span
<element_type
, dynamic_extent
> To(index_type aEnd
) const {
611 return Subspan(0, aEnd
);
615 * Subspan with run-time start index and exclusive end index.
616 * (Rust's &foo[start..end])
618 constexpr Span
<element_type
, dynamic_extent
> FromTo(index_type aStart
,
619 index_type aEnd
) const {
620 MOZ_RELEASE_ASSERT(aStart
<= aEnd
);
621 return Subspan(aStart
, aEnd
- aStart
);
624 // [Span.obs], Span observers
626 * Number of elements in the span.
628 constexpr index_type
Length() const { return size(); }
631 * Number of elements in the span (standard-libray duck typing version).
633 constexpr index_type
size() const { return storage_
.size(); }
636 * Size of the span in bytes.
638 constexpr index_type
LengthBytes() const { return size_bytes(); }
641 * Size of the span in bytes (standard-library naming style version).
643 constexpr index_type
size_bytes() const {
644 return size() * narrow_cast
<index_type
>(sizeof(element_type
));
648 * Checks if the the length of the span is zero.
650 constexpr bool IsEmpty() const { return empty(); }
653 * Checks if the the length of the span is zero (standard-libray duck
656 constexpr bool empty() const { return size() == 0; }
658 // [Span.elem], Span element access
659 constexpr reference
operator[](index_type idx
) const {
660 MOZ_RELEASE_ASSERT(idx
< storage_
.size());
665 * Access element of span by index (standard-library duck typing version).
667 constexpr reference
at(index_type idx
) const { return this->operator[](idx
); }
669 constexpr reference
operator()(index_type idx
) const {
670 return this->operator[](idx
);
674 * Pointer to the first element of the span. The return value is never
675 * nullptr, not ever for zero-length spans, so it can be passed as-is
676 * to std::slice::from_raw_parts() in Rust.
678 constexpr pointer
Elements() const { return data(); }
681 * Pointer to the first element of the span (standard-libray duck typing
682 * version). The return value is never nullptr, not ever for zero-length
683 * spans, so it can be passed as-is to std::slice::from_raw_parts() in Rust.
685 constexpr pointer
data() const { return storage_
.data(); }
687 // [Span.iter], Span iterator support
688 iterator
begin() const { return {this, 0}; }
689 iterator
end() const { return {this, Length()}; }
691 const_iterator
cbegin() const { return {this, 0}; }
692 const_iterator
cend() const { return {this, Length()}; }
694 reverse_iterator
rbegin() const { return reverse_iterator
{end()}; }
695 reverse_iterator
rend() const { return reverse_iterator
{begin()}; }
697 const_reverse_iterator
crbegin() const {
698 return const_reverse_iterator
{cend()};
700 const_reverse_iterator
crend() const {
701 return const_reverse_iterator
{cbegin()};
705 // this implementation detail class lets us take advantage of the
706 // empty base class optimization to pay for only storage of a single
707 // pointer in the case of fixed-size Spans
708 template <class ExtentType
>
709 class storage_type
: public ExtentType
{
711 template <class OtherExtentType
>
712 constexpr storage_type(pointer elements
, OtherExtentType ext
)
714 // Replace nullptr with aligned bogus pointer for Rust slice
715 // compatibility. See
716 // https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html
718 data_(elements
? elements
719 : reinterpret_cast<pointer
>(alignof(element_type
))) {
720 const size_t extentSize
= ExtentType::size();
722 (!elements
&& extentSize
== 0) ||
723 (elements
&& extentSize
!= mozilla::MaxValue
<size_t>::value
));
726 constexpr pointer
data() const { return data_
; }
732 storage_type
<span_details::extent_type
<Extent
>> storage_
;
735 // [Span.comparison], Span comparison operators
736 template <class ElementType
, size_t FirstExtent
, size_t SecondExtent
>
737 inline constexpr bool operator==(const Span
<ElementType
, FirstExtent
>& l
,
738 const Span
<ElementType
, SecondExtent
>& r
) {
739 return (l
.size() == r
.size()) &&
740 std::equal(l
.data(), l
.data() + l
.size(), r
.data());
743 template <class ElementType
, size_t Extent
>
744 inline constexpr bool operator!=(const Span
<ElementType
, Extent
>& l
,
745 const Span
<ElementType
, Extent
>& r
) {
749 template <class ElementType
, size_t Extent
>
750 inline constexpr bool operator<(const Span
<ElementType
, Extent
>& l
,
751 const Span
<ElementType
, Extent
>& r
) {
752 return std::lexicographical_compare(l
.data(), l
.data() + l
.size(), r
.data(),
753 r
.data() + r
.size());
756 template <class ElementType
, size_t Extent
>
757 inline constexpr bool operator<=(const Span
<ElementType
, Extent
>& l
,
758 const Span
<ElementType
, Extent
>& r
) {
762 template <class ElementType
, size_t Extent
>
763 inline constexpr bool operator>(const Span
<ElementType
, Extent
>& l
,
764 const Span
<ElementType
, Extent
>& r
) {
768 template <class ElementType
, size_t Extent
>
769 inline constexpr bool operator>=(const Span
<ElementType
, Extent
>& l
,
770 const Span
<ElementType
, Extent
>& r
) {
774 namespace span_details
{
775 // if we only supported compilers with good constexpr support then
776 // this pair of classes could collapse down to a constexpr function
778 // we should use a narrow_cast<> to go to size_t, but older compilers may not
779 // see it as constexpr and so will fail compilation of the template
780 template <class ElementType
, size_t Extent
>
781 struct calculate_byte_size
782 : mozilla::IntegralConstant
<size_t, static_cast<size_t>(
783 sizeof(ElementType
) *
784 static_cast<size_t>(Extent
))> {};
786 template <class ElementType
>
787 struct calculate_byte_size
<ElementType
, dynamic_extent
>
788 : mozilla::IntegralConstant
<size_t, dynamic_extent
> {};
789 } // namespace span_details
791 // [Span.objectrep], views of object representation
793 * View span as Span<const uint8_t>.
795 template <class ElementType
, size_t Extent
>
797 span_details::calculate_byte_size
<ElementType
, Extent
>::value
>
798 AsBytes(Span
<ElementType
, Extent
> s
) {
799 return {reinterpret_cast<const uint8_t*>(s
.data()), s
.size_bytes()};
803 * View span as Span<uint8_t>.
806 class ElementType
, size_t Extent
,
807 class = span_details::enable_if_t
<!mozilla::IsConst
<ElementType
>::value
>>
808 Span
<uint8_t, span_details::calculate_byte_size
<ElementType
, Extent
>::value
>
809 AsWritableBytes(Span
<ElementType
, Extent
> s
) {
810 return {reinterpret_cast<uint8_t*>(s
.data()), s
.size_bytes()};
814 * View a span of uint8_t as a span of char.
816 inline Span
<const char> AsChars(Span
<const uint8_t> s
) {
817 return {reinterpret_cast<const char*>(s
.data()), s
.size()};
821 * View a writable span of uint8_t as a span of char.
823 inline Span
<char> AsWritableChars(Span
<uint8_t> s
) {
824 return {reinterpret_cast<char*>(s
.data()), s
.size()};
828 // MakeSpan() - Utility functions for creating Spans
831 * Create span from pointer and length.
833 template <class ElementType
>
834 Span
<ElementType
> MakeSpan(ElementType
* aPtr
,
835 typename Span
<ElementType
>::index_type aLength
) {
836 return Span
<ElementType
>(aPtr
, aLength
);
840 * Create span from start pointer and pointer past end.
842 template <class ElementType
>
843 Span
<ElementType
> MakeSpan(ElementType
* aStartPtr
, ElementType
* aEndPtr
) {
844 return Span
<ElementType
>(aStartPtr
, aEndPtr
);
848 * Create span from C array.
849 * MakeSpan() does not permit creating Span objects from string literals (const
850 * char or char16_t arrays) because the Span length would include the zero
851 * terminator, which may surprise callers. Use MakeStringSpan() to create a
852 * Span whose length that excludes the string literal's zero terminator or use
853 * the MakeSpan() overload that accepts a pointer and length and specify the
854 * string literal's full length.
856 template <class ElementType
, size_t N
,
857 class = span_details::enable_if_t
<
858 !IsSame
<ElementType
, const char>::value
&&
859 !IsSame
<ElementType
, const char16_t
>::value
>>
860 Span
<ElementType
> MakeSpan(ElementType (&aArr
)[N
]) {
861 return Span
<ElementType
>(aArr
, N
);
865 * Create span from mozilla::Array.
867 template <class ElementType
, size_t N
>
868 Span
<ElementType
> MakeSpan(mozilla::Array
<ElementType
, N
>& aArr
) {
873 * Create span from const mozilla::Array.
875 template <class ElementType
, size_t N
>
876 Span
<const ElementType
> MakeSpan(const mozilla::Array
<ElementType
, N
>& arr
) {
881 * Create span from standard-library container.
883 template <class Container
>
884 Span
<typename
Container::value_type
> MakeSpan(Container
& cont
) {
885 return Span
<typename
Container::value_type
>(cont
);
889 * Create span from standard-library container (const version).
891 template <class Container
>
892 Span
<const typename
Container::value_type
> MakeSpan(const Container
& cont
) {
893 return Span
<const typename
Container::value_type
>(cont
);
897 * Create span from smart pointer and length.
900 Span
<typename
Ptr::element_type
> MakeSpan(Ptr
& aPtr
, size_t aLength
) {
901 return Span
<typename
Ptr::element_type
>(aPtr
, aLength
);
905 * Create span from a zero-terminated C string. nullptr is
906 * treated as the empty string.
908 inline Span
<const char> MakeStringSpan(const char* aZeroTerminated
) {
909 if (!aZeroTerminated
) {
910 return Span
<const char>();
912 return Span
<const char>(aZeroTerminated
, std::strlen(aZeroTerminated
));
916 * Create span from a zero-terminated UTF-16 C string. nullptr is
917 * treated as the empty string.
919 inline Span
<const char16_t
> MakeStringSpan(const char16_t
* aZeroTerminated
) {
920 if (!aZeroTerminated
) {
921 return Span
<const char16_t
>();
923 return Span
<const char16_t
>(aZeroTerminated
,
924 span_details::strlen16(aZeroTerminated
));
927 } // namespace mozilla
929 #endif // mozilla_Span_h