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
# ifdef _MSC_VER
39
# pragma warning(push)
40
41
// turn off some warnings that are noisy about our MOZ_RELEASE_ASSERT statements
42
# pragma warning(disable : 4127) // conditional expression is constant
43
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)
49
50
# if _MSC_VER < 1910
51
# pragma push_macro("constexpr")
52
# define constexpr /*constexpr*/
53
54
# endif // _MSC_VER < 1910
55
# endif // _MSC_VER
56
57
namespace mozilla {
58
59
// Stuff from gsl_util
60
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));
65
}
66
67
// end gsl_util
68
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;
75
76
template <class ElementType, size_t Extent = dynamic_extent>
77
class Span;
78
79
// implementation details
80
namespace span_details {
81
82
inline size_t strlen16(const char16_t* aZeroTerminated) {
83
size_t len = 0;
84
while (*(aZeroTerminated++)) {
85
len++;
86
}
87
return len;
88
}
89
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;
101
102
template <class T>
103
struct is_span_oracle : mozilla::FalseType {};
104
105
template <class ElementType, size_t Extent>
106
struct is_span_oracle<mozilla::Span<ElementType, Extent>> : mozilla::TrueType {
107
};
108
109
template <class T>
110
struct is_span : public is_span_oracle<remove_cv_t<T>> {};
111
112
template <class T>
113
struct is_std_array_oracle : mozilla::FalseType {};
114
115
template <class ElementType, size_t Extent>
116
struct is_std_array_oracle<std::array<ElementType, Extent>>
117
: mozilla::TrueType {};
118
119
template <class T>
120
struct is_std_array : public is_std_array_oracle<remove_cv_t<T>> {};
121
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> {};
127
128
template <class From, class To>
129
struct is_allowed_element_type_conversion
130
: public mozilla::IntegralConstant<
131
bool, mozilla::IsConvertible<From (*)[], To (*)[]>::value> {};
132
133
template <class Span, bool IsConst>
134
class span_iterator {
135
using element_type_ = typename Span::element_type;
136
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;
141
142
using reference = conditional_t<IsConst, const element_type_, element_type_>&;
143
using pointer = add_pointer_t<reference>;
144
145
constexpr span_iterator() : span_iterator(nullptr, 0) {}
146
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()));
151
}
152
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_) {}
156
157
constexpr span_iterator<Span, IsConst>& operator=(
158
const span_iterator<Span, IsConst>&) = default;
159
160
constexpr reference operator*() const {
161
MOZ_RELEASE_ASSERT(span_);
162
return (*span_)[index_];
163
}
164
165
constexpr pointer operator->() const {
166
MOZ_RELEASE_ASSERT(span_);
167
return &((*span_)[index_]);
168
}
169
170
constexpr span_iterator& operator++() {
171
MOZ_RELEASE_ASSERT(span_ && index_ >= 0 && index_ < span_->Length());
172
++index_;
173
return *this;
174
}
175
176
constexpr span_iterator operator++(int) {
177
auto ret = *this;
178
++(*this);
179
return ret;
180
}
181
182
constexpr span_iterator& operator--() {
183
MOZ_RELEASE_ASSERT(span_ && index_ > 0 && index_ <= span_->Length());
184
--index_;
185
return *this;
186
}
187
188
constexpr span_iterator operator--(int) {
189
auto ret = *this;
190
--(*this);
191
return ret;
192
}
193
194
constexpr span_iterator operator+(difference_type n) const {
195
auto ret = *this;
196
return ret += n;
197
}
198
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;
204
}
205
206
constexpr span_iterator operator-(difference_type n) const {
207
auto ret = *this;
208
return ret -= n;
209
}
210
211
constexpr span_iterator& operator-=(difference_type n)
212
213
{
214
return *this += -n;
215
}
216
217
constexpr difference_type operator-(const span_iterator& rhs) const {
218
MOZ_RELEASE_ASSERT(span_ == rhs.span_);
219
return index_ - rhs.index_;
220
}
221
222
constexpr reference operator[](difference_type n) const {
223
return *(*this + n);
224
}
225
226
constexpr friend bool operator==(const span_iterator& lhs,
227
const span_iterator& rhs) {
228
return lhs.span_ == rhs.span_ && lhs.index_ == rhs.index_;
229
}
230
231
constexpr friend bool operator!=(const span_iterator& lhs,
232
const span_iterator& rhs) {
233
return !(lhs == rhs);
234
}
235
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_;
240
}
241
242
constexpr friend bool operator<=(const span_iterator& lhs,
243
const span_iterator& rhs) {
244
return !(rhs < lhs);
245
}
246
247
constexpr friend bool operator>(const span_iterator& lhs,
248
const span_iterator& rhs) {
249
return rhs < lhs;
250
}
251
252
constexpr friend bool operator>=(const span_iterator& lhs,
253
const span_iterator& rhs) {
254
return !(rhs > lhs);
255
}
256
257
void swap(span_iterator& rhs) {
258
std::swap(index_, rhs.index_);
259
std::swap(span_, rhs.span_);
260
}
261
262
protected:
263
const Span* span_;
264
size_t index_;
265
};
266
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;
272
}
273
274
template <size_t Ext>
275
class extent_type {
276
public:
277
using index_type = size_t;
278
279
static_assert(Ext >= 0, "A fixed-size Span must be >= 0 in size.");
280
281
constexpr extent_type() {}
282
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);
289
}
290
291
constexpr MOZ_IMPLICIT extent_type(index_type length) {
292
MOZ_RELEASE_ASSERT(length == Ext);
293
}
294
295
constexpr index_type size() const { return Ext; }
296
};
297
298
template <>
299
class extent_type<dynamic_extent> {
300
public:
301
using index_type = size_t;
302
303
template <index_type Other>
304
explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size()) {}
305
306
explicit constexpr extent_type(index_type length) : size_(length) {}
307
308
constexpr index_type size() const { return size_; }
309
310
private:
311
index_type size_;
312
};
313
} // namespace span_details
314
315
/**
316
* Span - slices for C++
317
*
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.
320
*
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.)
328
*
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.
332
*
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.)
339
*
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.
344
*
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.
351
*
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).
360
*
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.)
370
*
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);
375
*
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().
379
*/
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&;
388
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>;
395
396
constexpr static const index_type extent = Extent;
397
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.
404
/**
405
* Constructor with no args.
406
*/
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>()) {}
412
413
/**
414
* Constructor for nullptr.
415
*/
416
constexpr MOZ_IMPLICIT Span(std::nullptr_t) : Span() {}
417
418
/**
419
* Constructor for pointer and length.
420
*/
421
constexpr Span(pointer aPtr, index_type aLength) : storage_(aPtr, aLength) {}
422
423
/**
424
* Constructor for start pointer and pointer past end.
425
*/
426
constexpr Span(pointer aStartPtr, pointer aEndPtr)
427
: storage_(aStartPtr, std::distance(aStartPtr, aEndPtr)) {}
428
429
/**
430
* Constructor for C array.
431
*/
432
template <size_t N>
433
constexpr MOZ_IMPLICIT Span(element_type (&aArr)[N])
434
: storage_(&aArr[0], span_details::extent_type<N>()) {}
435
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;
445
446
/**
447
* Constructor for std::array.
448
*/
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>()) {}
453
454
/**
455
* Constructor for const std::array.
456
*/
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>()) {}
461
462
/**
463
* Constructor for mozilla::Array.
464
*/
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>()) {}
469
470
/**
471
* Constructor for const mozilla::Array.
472
*/
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>()) {}
477
478
/**
479
* Constructor for mozilla::UniquePtr holding an array and length.
480
*/
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) {}
485
486
// NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the
487
// requirement on Container to be a contiguous sequence container.
488
/**
489
* Constructor for standard-library containers.
490
*/
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())) {}
502
503
/**
504
* Constructor for standard-library containers (const version).
505
*/
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())) {}
517
518
/**
519
* Constructor from other Span.
520
*/
521
constexpr Span(const Span& other) = default;
522
523
/**
524
* Constructor from other Span.
525
*/
526
constexpr Span(Span&& other) = default;
527
528
/**
529
* Constructor from other Span with conversion of element type.
530
*/
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())) {}
540
541
/**
542
* Constructor from other Span with conversion of element type.
543
*/
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())) {}
553
554
~Span() = default;
555
constexpr Span& operator=(const Span& other) = default;
556
557
constexpr Span& operator=(Span&& other) = default;
558
559
// [Span.sub], Span subviews
560
/**
561
* Subspan with first N elements with compile-time N.
562
*/
563
template <size_t Count>
564
constexpr Span<element_type, Count> First() const {
565
MOZ_RELEASE_ASSERT(Count <= size());
566
return {data(), Count};
567
}
568
569
/**
570
* Subspan with last N elements with compile-time N.
571
*/
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};
577
}
578
579
/**
580
* Subspan with compile-time start index and length.
581
*/
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};
588
}
589
590
/**
591
* Subspan with first N elements with run-time N.
592
*/
593
constexpr Span<element_type, dynamic_extent> First(index_type aCount) const {
594
MOZ_RELEASE_ASSERT(aCount <= size());
595
return {data(), aCount};
596
}
597
598
/**
599
* Subspan with last N elements with run-time N.
600
*/
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};
605
}
606
607
/**
608
* Subspan with run-time start index and length.
609
*/
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};
617
}
618
619
/**
620
* Subspan with run-time start index. (Rust's &foo[start..])
621
*/
622
constexpr Span<element_type, dynamic_extent> From(index_type aStart) const {
623
return Subspan(aStart);
624
}
625
626
/**
627
* Subspan with run-time exclusive end index. (Rust's &foo[..end])
628
*/
629
constexpr Span<element_type, dynamic_extent> To(index_type aEnd) const {
630
return Subspan(0, aEnd);
631
}
632
633
/**
634
* Subspan with run-time start index and exclusive end index.
635
* (Rust's &foo[start..end])
636
*/
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);
641
}
642
643
// [Span.obs], Span observers
644
/**
645
* Number of elements in the span.
646
*/
647
constexpr index_type Length() const { return size(); }
648
649
/**
650
* Number of elements in the span (standard-libray duck typing version).
651
*/
652
constexpr index_type size() const { return storage_.size(); }
653
654
/**
655
* Size of the span in bytes.
656
*/
657
constexpr index_type LengthBytes() const { return size_bytes(); }
658
659
/**
660
* Size of the span in bytes (standard-library naming style version).
661
*/
662
constexpr index_type size_bytes() const {
663
return size() * narrow_cast<index_type>(sizeof(element_type));
664
}
665
666
/**
667
* Checks if the the length of the span is zero.
668
*/
669
constexpr bool IsEmpty() const { return empty(); }
670
671
/**
672
* Checks if the the length of the span is zero (standard-libray duck
673
* typing version).
674
*/
675
constexpr bool empty() const { return size() == 0; }
676
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];
681
}
682
683
/**
684
* Access element of span by index (standard-library duck typing version).
685
*/
686
constexpr reference at(index_type idx) const { return this->operator[](idx); }
687
688
constexpr reference operator()(index_type idx) const {
689
return this->operator[](idx);
690
}
691
692
/**
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.
696
*/
697
constexpr pointer Elements() const { return data(); }
698
699
/**
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.
703
*/
704
constexpr pointer data() const { return storage_.data(); }
705
706
// [Span.iter], Span iterator support
707
iterator begin() const { return {this, 0}; }
708
iterator end() const { return {this, Length()}; }
709
710
const_iterator cbegin() const { return {this, 0}; }
711
const_iterator cend() const { return {this, Length()}; }
712
713
reverse_iterator rbegin() const { return reverse_iterator{end()}; }
714
reverse_iterator rend() const { return reverse_iterator{begin()}; }
715
716
const_reverse_iterator crbegin() const {
717
return const_reverse_iterator{cend()};
718
}
719
const_reverse_iterator crend() const {
720
return const_reverse_iterator{cbegin()};
721
}
722
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
736
,
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));
743
}
744
745
constexpr pointer data() const { return data_; }
746
747
private:
748
pointer data_;
749
};
750
751
storage_type<span_details::extent_type<Extent>> storage_;
752
};
753
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());
759
}
760
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);
765
}
766
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());
771
}
772
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);
777
}
778
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;
783
}
784
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);
789
}
790
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
794
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))> {};
802
803
template <class ElementType>
804
struct calculate_byte_size<ElementType, dynamic_extent>
805
: mozilla::IntegralConstant<size_t, dynamic_extent> {};
806
} // namespace span_details
807
808
// [Span.objectrep], views of object representation
809
/**
810
* View span as Span<const uint8_t>.
811
*/
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()};
817
}
818
819
/**
820
* View span as Span<uint8_t>.
821
*/
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()};
828
}
829
830
//
831
// MakeSpan() - Utility functions for creating Spans
832
//
833
/**
834
* Create span from pointer and length.
835
*/
836
template <class ElementType>
837
Span<ElementType> MakeSpan(ElementType* aPtr,
838
typename Span<ElementType>::index_type aLength) {
839
return Span<ElementType>(aPtr, aLength);
840
}
841
842
/**
843
* Create span from start pointer and pointer past end.
844
*/
845
template <class ElementType>
846
Span<ElementType> MakeSpan(ElementType* aStartPtr, ElementType* aEndPtr) {
847
return Span<ElementType>(aStartPtr, aEndPtr);
848
}
849
850
/**
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.
858
*/
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);
865
}
866
867
/**
868
* Create span from mozilla::Array.
869
*/
870
template <class ElementType, size_t N>
871
Span<ElementType> MakeSpan(mozilla::Array<ElementType, N>& aArr) {
872
return aArr;
873
}
874
875
/**
876
* Create span from const mozilla::Array.
877
*/
878
template <class ElementType, size_t N>
879
Span<const ElementType> MakeSpan(const mozilla::Array<ElementType, N>& arr) {
880
return arr;
881
}
882
883
/**
884
* Create span from standard-library container.
885
*/
886
template <class Container>
887
Span<typename Container::value_type> MakeSpan(Container& cont) {
888
return Span<typename Container::value_type>(cont);
889
}
890
891
/**
892
* Create span from standard-library container (const version).
893
*/
894
template <class Container>
895
Span<const typename Container::value_type> MakeSpan(const Container& cont) {
896
return Span<const typename Container::value_type>(cont);
897
}
898
899
/**
900
* Create span from smart pointer and length.
901
*/
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);
905
}
906
907
/**
908
* Create span from a zero-terminated C string. nullptr is
909
* treated as the empty string.
910
*/
911
inline Span<const char> MakeStringSpan(const char* aZeroTerminated) {
912
if (!aZeroTerminated) {
913
return Span<const char>();
914
}
915
return Span<const char>(aZeroTerminated, std::strlen(aZeroTerminated));
916
}
917
918
/**
919
* Create span from a zero-terminated UTF-16 C string. nullptr is
920
* treated as the empty string.
921
*/
922
inline Span<const char16_t> MakeStringSpan(const char16_t* aZeroTerminated) {
923
if (!aZeroTerminated) {
924
return Span<const char16_t>();
925
}
926
return Span<const char16_t>(aZeroTerminated,
927
span_details::strlen16(aZeroTerminated));
928
}
929
930
} // namespace mozilla
931
932
# ifdef _MSC_VER
933
# if _MSC_VER < 1910
934
# undef constexpr
935
# pragma pop_macro("constexpr")
936
937
# endif // _MSC_VER < 1910
938
939
# pragma warning(pop)
940
# endif // _MSC_VER
941
942
#endif // mozilla_Span_h