Source code

Revision control

Other Tools

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.
14
//
15
///////////////////////////////////////////////////////////////////////////////
16
17
// Adapted from
19
// and
21
22
#ifndef mozilla_Span_h
23
#define mozilla_Span_h
24
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"
32
33
#include <algorithm>
34
#include <array>
35
#include <cstring>
36
#include <iterator>
37
38
namespace mozilla {
39
40
// Stuff from gsl_util
41
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));
46
}
47
48
// end gsl_util
49
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;
56
57
template <class ElementType, size_t Extent = dynamic_extent>
58
class Span;
59
60
// implementation details
61
namespace span_details {
62
63
inline size_t strlen16(const char16_t* aZeroTerminated) {
64
size_t len = 0;
65
while (*(aZeroTerminated++)) {
66
len++;
67
}
68
return len;
69
}
70
71
// C++14 types that we don't have because we build as C++11.
72
template <class T>
73
using remove_cv_t = typename mozilla::RemoveCV<T>::Type;
74
template <class T>
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;
78
template <class T>
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;
82
83
template <class T>
84
struct is_span_oracle : mozilla::FalseType {};
85
86
template <class ElementType, size_t Extent>
87
struct is_span_oracle<mozilla::Span<ElementType, Extent>> : mozilla::TrueType {
88
};
89
90
template <class T>
91
struct is_span : public is_span_oracle<remove_cv_t<T>> {};
92
93
template <class T>
94
struct is_std_array_oracle : mozilla::FalseType {};
95
96
template <class ElementType, size_t Extent>
97
struct is_std_array_oracle<std::array<ElementType, Extent>>
98
: mozilla::TrueType {};
99
100
template <class T>
101
struct is_std_array : public is_std_array_oracle<remove_cv_t<T>> {};
102
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> {};
108
109
template <class From, class To>
110
struct is_allowed_element_type_conversion
111
: public mozilla::IntegralConstant<
112
bool, mozilla::IsConvertible<From (*)[], To (*)[]>::value> {};
113
114
template <class Span, bool IsConst>
115
class span_iterator {
116
using element_type_ = typename Span::element_type;
117
118
public:
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;
122
123
using reference = conditional_t<IsConst, const element_type_, element_type_>&;
124
using pointer = add_pointer_t<reference>;
125
126
constexpr span_iterator() : span_iterator(nullptr, 0) {}
127
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()));
132
}
133
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_) {}
137
138
constexpr span_iterator<Span, IsConst>& operator=(
139
const span_iterator<Span, IsConst>&) = default;
140
141
constexpr reference operator*() const {
142
MOZ_RELEASE_ASSERT(span_);
143
return (*span_)[index_];
144
}
145
146
constexpr pointer operator->() const {
147
MOZ_RELEASE_ASSERT(span_);
148
return &((*span_)[index_]);
149
}
150
151
constexpr span_iterator& operator++() {
152
MOZ_RELEASE_ASSERT(span_ && index_ >= 0 && index_ < span_->Length());
153
++index_;
154
return *this;
155
}
156
157
constexpr span_iterator operator++(int) {
158
auto ret = *this;
159
++(*this);
160
return ret;
161
}
162
163
constexpr span_iterator& operator--() {
164
MOZ_RELEASE_ASSERT(span_ && index_ > 0 && index_ <= span_->Length());
165
--index_;
166
return *this;
167
}
168
169
constexpr span_iterator operator--(int) {
170
auto ret = *this;
171
--(*this);
172
return ret;
173
}
174
175
constexpr span_iterator operator+(difference_type n) const {
176
auto ret = *this;
177
return ret += n;
178
}
179
180
constexpr span_iterator& operator+=(difference_type n) {
181
MOZ_RELEASE_ASSERT(span_ && (index_ + n) >= 0 &&
182
(index_ + n) <= span_->Length());
183
index_ += n;
184
return *this;
185
}
186
187
constexpr span_iterator operator-(difference_type n) const {
188
auto ret = *this;
189
return ret -= n;
190
}
191
192
constexpr span_iterator& operator-=(difference_type n)
193
194
{
195
return *this += -n;
196
}
197
198
constexpr difference_type operator-(const span_iterator& rhs) const {
199
MOZ_RELEASE_ASSERT(span_ == rhs.span_);
200
return index_ - rhs.index_;
201
}
202
203
constexpr reference operator[](difference_type n) const {
204
return *(*this + n);
205
}
206
207
constexpr friend bool operator==(const span_iterator& lhs,
208
const span_iterator& rhs) {
209
return lhs.span_ == rhs.span_ && lhs.index_ == rhs.index_;
210
}
211
212
constexpr friend bool operator!=(const span_iterator& lhs,
213
const span_iterator& rhs) {
214
return !(lhs == rhs);
215
}
216
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_;
221
}
222
223
constexpr friend bool operator<=(const span_iterator& lhs,
224
const span_iterator& rhs) {
225
return !(rhs < lhs);
226
}
227
228
constexpr friend bool operator>(const span_iterator& lhs,
229
const span_iterator& rhs) {
230
return rhs < lhs;
231
}
232
233
constexpr friend bool operator>=(const span_iterator& lhs,
234
const span_iterator& rhs) {
235
return !(rhs > lhs);
236
}
237
238
void swap(span_iterator& rhs) {
239
std::swap(index_, rhs.index_);
240
std::swap(span_, rhs.span_);
241
}
242
243
protected:
244
const Span* span_;
245
size_t index_;
246
};
247
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) {
252
return rhs + n;
253
}
254
255
template <size_t Ext>
256
class extent_type {
257
public:
258
using index_type = size_t;
259
260
static_assert(Ext >= 0, "A fixed-size Span must be >= 0 in size.");
261
262
constexpr extent_type() {}
263
264
template <index_type Other>
265
constexpr MOZ_IMPLICIT extent_type(extent_type<Other> ext) {
266
static_assert(
267
Other == Ext || Other == dynamic_extent,
268
"Mismatch between fixed-size extent and size of initializing data.");
269
MOZ_RELEASE_ASSERT(ext.size() == Ext);
270
}
271
272
constexpr MOZ_IMPLICIT extent_type(index_type length) {
273
MOZ_RELEASE_ASSERT(length == Ext);
274
}
275
276
constexpr index_type size() const { return Ext; }
277
};
278
279
template <>
280
class extent_type<dynamic_extent> {
281
public:
282
using index_type = size_t;
283
284
template <index_type Other>
285
explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size()) {}
286
287
explicit constexpr extent_type(index_type length) : size_(length) {}
288
289
constexpr index_type size() const { return size_; }
290
291
private:
292
index_type size_;
293
};
294
} // namespace span_details
295
296
/**
297
* Span - slices for C++
298
*
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.
301
*
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.)
309
*
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.
313
*
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
319
* checks.)
320
*
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.
325
*
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.
332
*
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).
341
*
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.)
351
*
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);
356
*
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
359
* AsWritableBytes().
360
*/
361
template <class ElementType, size_t Extent>
362
class Span {
363
public:
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&;
369
370
using iterator =
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>;
376
377
constexpr static const index_type extent = Extent;
378
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.
385
/**
386
* Constructor with no args.
387
*/
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>()) {}
393
394
/**
395
* Constructor for nullptr.
396
*/
397
constexpr MOZ_IMPLICIT Span(std::nullptr_t) : Span() {}
398
399
/**
400
* Constructor for pointer and length.
401
*/
402
constexpr Span(pointer aPtr, index_type aLength) : storage_(aPtr, aLength) {}
403
404
/**
405
* Constructor for start pointer and pointer past end.
406
*/
407
constexpr Span(pointer aStartPtr, pointer aEndPtr)
408
: storage_(aStartPtr, std::distance(aStartPtr, aEndPtr)) {}
409
410
/**
411
* Constructor for C array.
412
*/
413
template <size_t N>
414
constexpr MOZ_IMPLICIT Span(element_type (&aArr)[N])
415
: storage_(&aArr[0], span_details::extent_type<N>()) {}
416
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;
426
427
/**
428
* Constructor for std::array.
429
*/
430
template <size_t N,
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>()) {}
434
435
/**
436
* Constructor for const std::array.
437
*/
438
template <size_t N>
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>()) {}
442
443
/**
444
* Constructor for mozilla::Array.
445
*/
446
template <size_t N,
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>()) {}
450
451
/**
452
* Constructor for const mozilla::Array.
453
*/
454
template <size_t N>
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>()) {}
458
459
/**
460
* Constructor for mozilla::UniquePtr holding an array and length.
461
*/
462
template <class ArrayElementType = std::add_pointer<element_type>>
463
constexpr Span(const mozilla::UniquePtr<ArrayElementType>& aPtr,
464
index_type aLength)
465
: storage_(aPtr.get(), aLength) {}
466
467
// NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the
468
// requirement on Container to be a contiguous sequence container.
469
/**
470
* Constructor for standard-library containers.
471
*/
472
template <
473
class Container,
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())) {}
483
484
/**
485
* Constructor for standard-library containers (const version).
486
*/
487
template <
488
class Container,
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())) {}
498
499
/**
500
* Constructor from other Span.
501
*/
502
constexpr Span(const Span& other) = default;
503
504
/**
505
* Constructor from other Span.
506
*/
507
constexpr Span(Span&& other) = default;
508
509
/**
510
* Constructor from other Span with conversion of element type.
511
*/
512
template <class OtherElementType, size_t OtherExtent,
513
class = span_details::enable_if_t<
514
span_details::is_allowed_extent_conversion<OtherExtent,
515
Extent>::value &&
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())) {}
521
522
/**
523
* Constructor from other Span with conversion of element type.
524
*/
525
template <class OtherElementType, size_t OtherExtent,
526
class = span_details::enable_if_t<
527
span_details::is_allowed_extent_conversion<OtherExtent,
528
Extent>::value &&
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())) {}
534
535
~Span() = default;
536
constexpr Span& operator=(const Span& other) = default;
537
538
constexpr Span& operator=(Span&& other) = default;
539
540
// [Span.sub], Span subviews
541
/**
542
* Subspan with first N elements with compile-time N.
543
*/
544
template <size_t Count>
545
constexpr Span<element_type, Count> First() const {
546
MOZ_RELEASE_ASSERT(Count <= size());
547
return {data(), Count};
548
}
549
550
/**
551
* Subspan with last N elements with compile-time N.
552
*/
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};
558
}
559
560
/**
561
* Subspan with compile-time start index and length.
562
*/
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};
569
}
570
571
/**
572
* Subspan with first N elements with run-time N.
573
*/
574
constexpr Span<element_type, dynamic_extent> First(index_type aCount) const {
575
MOZ_RELEASE_ASSERT(aCount <= size());
576
return {data(), aCount};
577
}
578
579
/**
580
* Subspan with last N elements with run-time N.
581
*/
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};
586
}
587
588
/**
589
* Subspan with run-time start index and length.
590
*/
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};
598
}
599
600
/**
601
* Subspan with run-time start index. (Rust's &foo[start..])
602
*/
603
constexpr Span<element_type, dynamic_extent> From(index_type aStart) const {
604
return Subspan(aStart);
605
}
606
607
/**
608
* Subspan with run-time exclusive end index. (Rust's &foo[..end])
609
*/
610
constexpr Span<element_type, dynamic_extent> To(index_type aEnd) const {
611
return Subspan(0, aEnd);
612
}
613
614
/**
615
* Subspan with run-time start index and exclusive end index.
616
* (Rust's &foo[start..end])
617
*/
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);
622
}
623
624
// [Span.obs], Span observers
625
/**
626
* Number of elements in the span.
627
*/
628
constexpr index_type Length() const { return size(); }
629
630
/**
631
* Number of elements in the span (standard-libray duck typing version).
632
*/
633
constexpr index_type size() const { return storage_.size(); }
634
635
/**
636
* Size of the span in bytes.
637
*/
638
constexpr index_type LengthBytes() const { return size_bytes(); }
639
640
/**
641
* Size of the span in bytes (standard-library naming style version).
642
*/
643
constexpr index_type size_bytes() const {
644
return size() * narrow_cast<index_type>(sizeof(element_type));
645
}
646
647
/**
648
* Checks if the the length of the span is zero.
649
*/
650
constexpr bool IsEmpty() const { return empty(); }
651
652
/**
653
* Checks if the the length of the span is zero (standard-libray duck
654
* typing version).
655
*/
656
constexpr bool empty() const { return size() == 0; }
657
658
// [Span.elem], Span element access
659
constexpr reference operator[](index_type idx) const {
660
MOZ_RELEASE_ASSERT(idx < storage_.size());
661
return data()[idx];
662
}
663
664
/**
665
* Access element of span by index (standard-library duck typing version).
666
*/
667
constexpr reference at(index_type idx) const { return this->operator[](idx); }
668
669
constexpr reference operator()(index_type idx) const {
670
return this->operator[](idx);
671
}
672
673
/**
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.
677
*/
678
constexpr pointer Elements() const { return data(); }
679
680
/**
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.
684
*/
685
constexpr pointer data() const { return storage_.data(); }
686
687
// [Span.iter], Span iterator support
688
iterator begin() const { return {this, 0}; }
689
iterator end() const { return {this, Length()}; }
690
691
const_iterator cbegin() const { return {this, 0}; }
692
const_iterator cend() const { return {this, Length()}; }
693
694
reverse_iterator rbegin() const { return reverse_iterator{end()}; }
695
reverse_iterator rend() const { return reverse_iterator{begin()}; }
696
697
const_reverse_iterator crbegin() const {
698
return const_reverse_iterator{cend()};
699
}
700
const_reverse_iterator crend() const {
701
return const_reverse_iterator{cbegin()};
702
}
703
704
private:
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 {
710
public:
711
template <class OtherExtentType>
712
constexpr storage_type(pointer elements, OtherExtentType ext)
713
: ExtentType(ext)
714
// Replace nullptr with aligned bogus pointer for Rust slice
715
// compatibility. See
717
,
718
data_(elements ? elements
719
: reinterpret_cast<pointer>(alignof(element_type))) {
720
const size_t extentSize = ExtentType::size();
721
MOZ_RELEASE_ASSERT(
722
(!elements && extentSize == 0) ||
723
(elements && extentSize != mozilla::MaxValue<size_t>::value));
724
}
725
726
constexpr pointer data() const { return data_; }
727
728
private:
729
pointer data_;
730
};
731
732
storage_type<span_details::extent_type<Extent>> storage_;
733
};
734
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()) && std::equal(l.begin(), l.end(), r.begin());
740
}
741
742
template <class ElementType, size_t Extent>
743
inline constexpr bool operator!=(const Span<ElementType, Extent>& l,
744
const Span<ElementType, Extent>& r) {
745
return !(l == r);
746
}
747
748
template <class ElementType, size_t Extent>
749
inline constexpr bool operator<(const Span<ElementType, Extent>& l,
750
const Span<ElementType, Extent>& r) {
751
return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
752
}
753
754
template <class ElementType, size_t Extent>
755
inline constexpr bool operator<=(const Span<ElementType, Extent>& l,
756
const Span<ElementType, Extent>& r) {
757
return !(l > r);
758
}
759
760
template <class ElementType, size_t Extent>
761
inline constexpr bool operator>(const Span<ElementType, Extent>& l,
762
const Span<ElementType, Extent>& r) {
763
return r < l;
764
}
765
766
template <class ElementType, size_t Extent>
767
inline constexpr bool operator>=(const Span<ElementType, Extent>& l,
768
const Span<ElementType, Extent>& r) {
769
return !(l < r);
770
}
771
772
namespace span_details {
773
// if we only supported compilers with good constexpr support then
774
// this pair of classes could collapse down to a constexpr function
775
776
// we should use a narrow_cast<> to go to size_t, but older compilers may not
777
// see it as constexpr and so will fail compilation of the template
778
template <class ElementType, size_t Extent>
779
struct calculate_byte_size
780
: mozilla::IntegralConstant<size_t, static_cast<size_t>(
781
sizeof(ElementType) *
782
static_cast<size_t>(Extent))> {};
783
784
template <class ElementType>
785
struct calculate_byte_size<ElementType, dynamic_extent>
786
: mozilla::IntegralConstant<size_t, dynamic_extent> {};
787
} // namespace span_details
788
789
// [Span.objectrep], views of object representation
790
/**
791
* View span as Span<const uint8_t>.
792
*/
793
template <class ElementType, size_t Extent>
794
Span<const uint8_t,
795
span_details::calculate_byte_size<ElementType, Extent>::value>
796
AsBytes(Span<ElementType, Extent> s) {
797
return {reinterpret_cast<const uint8_t*>(s.data()), s.size_bytes()};
798
}
799
800
/**
801
* View span as Span<uint8_t>.
802
*/
803
template <
804
class ElementType, size_t Extent,
805
class = span_details::enable_if_t<!mozilla::IsConst<ElementType>::value>>
806
Span<uint8_t, span_details::calculate_byte_size<ElementType, Extent>::value>
807
AsWritableBytes(Span<ElementType, Extent> s) {
808
return {reinterpret_cast<uint8_t*>(s.data()), s.size_bytes()};
809
}
810
811
//
812
// MakeSpan() - Utility functions for creating Spans
813
//
814
/**
815
* Create span from pointer and length.
816
*/
817
template <class ElementType>
818
Span<ElementType> MakeSpan(ElementType* aPtr,
819
typename Span<ElementType>::index_type aLength) {
820
return Span<ElementType>(aPtr, aLength);
821
}
822
823
/**
824
* Create span from start pointer and pointer past end.
825
*/
826
template <class ElementType>
827
Span<ElementType> MakeSpan(ElementType* aStartPtr, ElementType* aEndPtr) {
828
return Span<ElementType>(aStartPtr, aEndPtr);
829
}
830
831
/**
832
* Create span from C array.
833
* MakeSpan() does not permit creating Span objects from string literals (const
834
* char or char16_t arrays) because the Span length would include the zero
835
* terminator, which may surprise callers. Use MakeStringSpan() to create a
836
* Span whose length that excludes the string literal's zero terminator or use
837
* the MakeSpan() overload that accepts a pointer and length and specify the
838
* string literal's full length.
839
*/
840
template <class ElementType, size_t N,
841
class = span_details::enable_if_t<
842
!IsSame<ElementType, const char>::value &&
843
!IsSame<ElementType, const char16_t>::value>>
844
Span<ElementType> MakeSpan(ElementType (&aArr)[N]) {
845
return Span<ElementType>(aArr, N);
846
}
847
848
/**
849
* Create span from mozilla::Array.
850
*/
851
template <class ElementType, size_t N>
852
Span<ElementType> MakeSpan(mozilla::Array<ElementType, N>& aArr) {
853
return aArr;
854
}
855
856
/**
857
* Create span from const mozilla::Array.
858
*/
859
template <class ElementType, size_t N>
860
Span<const ElementType> MakeSpan(const mozilla::Array<ElementType, N>& arr) {
861
return arr;
862
}
863
864
/**
865
* Create span from standard-library container.
866
*/
867
template <class Container>
868
Span<typename Container::value_type> MakeSpan(Container& cont) {
869
return Span<typename Container::value_type>(cont);
870
}
871
872
/**
873
* Create span from standard-library container (const version).
874
*/
875
template <class Container>
876
Span<const typename Container::value_type> MakeSpan(const Container& cont) {
877
return Span<const typename Container::value_type>(cont);
878
}
879
880
/**
881
* Create span from smart pointer and length.
882
*/
883
template <class Ptr>
884
Span<typename Ptr::element_type> MakeSpan(Ptr& aPtr, size_t aLength) {
885
return Span<typename Ptr::element_type>(aPtr, aLength);
886
}
887
888
/**
889
* Create span from a zero-terminated C string. nullptr is
890
* treated as the empty string.
891
*/
892
inline Span<const char> MakeStringSpan(const char* aZeroTerminated) {
893
if (!aZeroTerminated) {
894
return Span<const char>();
895
}
896
return Span<const char>(aZeroTerminated, std::strlen(aZeroTerminated));
897
}
898
899
/**
900
* Create span from a zero-terminated UTF-16 C string. nullptr is
901
* treated as the empty string.
902
*/
903
inline Span<const char16_t> MakeStringSpan(const char16_t* aZeroTerminated) {
904
if (!aZeroTerminated) {
905
return Span<const char16_t>();
906
}
907
return Span<const char16_t>(aZeroTerminated,
908
span_details::strlen16(aZeroTerminated));
909
}
910
911
} // namespace mozilla
912
913
#endif // mozilla_Span_h