| 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 |
| 18 |
// https://github.com/Microsoft/GSL/blob/3819df6e378ffccf0e29465afe99c3b324c2aa70/include/gsl/span |
| 19 |
// and |
| 20 |
// https://github.com/Microsoft/GSL/blob/3819df6e378ffccf0e29465afe99c3b324c2aa70/include/gsl/gsl_util |
| 21 |
|
| 22 |
#ifndef mozilla_Span_h |
| 23 |
#define mozilla_Span_h |
| 24 |
|
| 25 |
#include <array> |
| 26 |
#include <cstddef> |
| 27 |
#include <cstdint> |
| 28 |
#include <iterator> |
| 29 |
#include <limits> |
| 30 |
#include <string> |
| 31 |
#include <type_traits> |
| 32 |
#include <utility> |
| 33 |
|
| 34 |
#include "mozilla/Assertions.h" |
| 35 |
#include "mozilla/Attributes.h" |
| 36 |
#include "mozilla/Casting.h" |
| 37 |
#include "mozilla/UniquePtr.h" |
| 38 |
|
| 39 |
namespace mozilla { |
| 40 |
|
| 41 |
template <typename T, size_t Length> |
| 42 |
class Array; |
| 43 |
|
| 44 |
template <typename Enum, typename T, size_t Length> |
| 45 |
class EnumeratedArray; |
| 46 |
|
| 47 |
// Stuff from gsl_util |
| 48 |
|
| 49 |
// narrow_cast(): a searchable way to do narrowing casts of values |
| 50 |
template <class T, class U> |
| 51 |
inline constexpr T narrow_cast(U&& u) { |
| 52 |
return static_cast<T>(std::forward<U>(u)); |
| 53 |
} |
| 54 |
|
| 55 |
// end gsl_util |
| 56 |
|
| 57 |
// [views.constants], constants |
| 58 |
// This was -1 in gsl::span, but using size_t for sizes instead of ptrdiff_t |
| 59 |
// and reserving a magic value that realistically doesn't occur in |
| 60 |
// compile-time-constant Span sizes makes things a lot less messy in terms of |
| 61 |
// comparison between signed and unsigned. |
| 62 |
constexpr const size_t dynamic_extent = std::numeric_limits<size_t>::max(); |
| 63 |
|
| 64 |
template <class ElementType, size_t Extent = dynamic_extent> |
| 65 |
class Span; |
| 66 |
|
| 67 |
// implementation details |
| 68 |
namespace span_details { |
| 69 |
|
| 70 |
template <class T> |
| 71 |
struct is_span_oracle : std::false_type {}; |
| 72 |
|
| 73 |
template <class ElementType, size_t Extent> |
| 74 |
struct is_span_oracle<mozilla::Span<ElementType, Extent>> : std::true_type {}; |
| 75 |
|
| 76 |
template <class T> |
| 77 |
struct is_span : public is_span_oracle<std::remove_cv_t<T>> {}; |
| 78 |
|
| 79 |
template <class T> |
| 80 |
struct is_std_array_oracle : std::false_type {}; |
| 81 |
|
| 82 |
template <class ElementType, size_t Extent> |
| 83 |
struct is_std_array_oracle<std::array<ElementType, Extent>> : std::true_type {}; |
| 84 |
|
| 85 |
template <class T> |
| 86 |
struct is_std_array : public is_std_array_oracle<std::remove_cv_t<T>> {}; |
| 87 |
|
| 88 |
template <size_t From, size_t To> |
| 89 |
struct is_allowed_extent_conversion |
| 90 |
: public std::integral_constant<bool, From == To || |
| 91 |
From == mozilla::dynamic_extent || |
| 92 |
To == mozilla::dynamic_extent> {}; |
| 93 |
|
| 94 |
template <class From, class To> |
| 95 |
struct is_allowed_element_type_conversion |
| 96 |
: public std::integral_constant< |
| 97 |
bool, std::is_convertible_v<From (*)[], To (*)[]>> {}; |
| 98 |
|
| 99 |
struct SpanKnownBounds {}; |
| 100 |
|
| 101 |
template <class SpanT, bool IsConst> |
| 102 |
class span_iterator { |
| 103 |
using element_type_ = typename SpanT::element_type; |
| 104 |
|
| 105 |
template <class ElementType, size_t Extent> |
| 106 |
friend class ::mozilla::Span; |
| 107 |
|
| 108 |
public: |
| 109 |
using iterator_category = std::random_access_iterator_tag; |
| 110 |
using value_type = std::remove_const_t<element_type_>; |
| 111 |
using difference_type = ptrdiff_t; |
| 112 |
|
| 113 |
using reference = |
| 114 |
std::conditional_t<IsConst, const element_type_, element_type_>&; |
| 115 |
using pointer = std::add_pointer_t<reference>; |
| 116 |
|
| 117 |
constexpr span_iterator() : span_iterator(nullptr, 0, SpanKnownBounds{}) {} |
| 118 |
|
| 119 |
constexpr span_iterator(const SpanT* span, typename SpanT::index_type index) |
| 120 |
: span_(span), index_(index) { |
| 121 |
MOZ_RELEASE_ASSERT(span == nullptr || |
| 122 |
(index_ >= 0 && index <= span_->Length())); |
| 123 |
} |
| 124 |
|
| 125 |
private: |
| 126 |
// For whatever reason, the compiler doesn't like optimizing away the above |
| 127 |
// MOZ_RELEASE_ASSERT when `span_iterator` is constructed for |
| 128 |
// obviously-correct cases like `span.begin()` or `span.end()`. We provide |
| 129 |
// this private constructor for such cases. |
| 130 |
constexpr span_iterator(const SpanT* span, typename SpanT::index_type index, |
| 131 |
SpanKnownBounds) |
| 132 |
: span_(span), index_(index) {} |
| 133 |
|
| 134 |
public: |
| 135 |
// `other` is already correct by construction; we do not need to go through |
| 136 |
// the release assert above. Put differently, this constructor is effectively |
| 137 |
// a copy constructor and therefore needs no assertions. |
| 138 |
friend class span_iterator<SpanT, true>; |
| 139 |
constexpr MOZ_IMPLICIT span_iterator(const span_iterator<SpanT, false>& other) |
| 140 |
: span_(other.span_), index_(other.index_) {} |
| 141 |
|
| 142 |
constexpr span_iterator<SpanT, IsConst>& operator=( |
| 143 |
const span_iterator<SpanT, IsConst>&) = default; |
| 144 |
|
| 145 |
constexpr reference operator*() const { |
| 146 |
MOZ_RELEASE_ASSERT(span_); |
| 147 |
return (*span_)[index_]; |
| 148 |
} |
| 149 |
|
| 150 |
constexpr pointer operator->() const { |
| 151 |
MOZ_RELEASE_ASSERT(span_); |
| 152 |
return &((*span_)[index_]); |
| 153 |
} |
| 154 |
|
| 155 |
constexpr span_iterator& operator++() { |
| 156 |
++index_; |
| 157 |
return *this; |
| 158 |
} |
| 159 |
|
| 160 |
constexpr span_iterator operator++(int) { |
| 161 |
auto ret = *this; |
| 162 |
++(*this); |
| 163 |
return ret; |
| 164 |
} |
| 165 |
|
| 166 |
constexpr span_iterator& operator--() { |
| 167 |
--index_; |
| 168 |
return *this; |
| 169 |
} |
| 170 |
|
| 171 |
constexpr span_iterator operator--(int) { |
| 172 |
auto ret = *this; |
| 173 |
--(*this); |
| 174 |
return ret; |
| 175 |
} |
| 176 |
|
| 177 |
constexpr span_iterator operator+(difference_type n) const { |
| 178 |
auto ret = *this; |
| 179 |
return ret += n; |
| 180 |
} |
| 181 |
|
| 182 |
constexpr span_iterator& operator+=(difference_type n) { |
| 183 |
MOZ_RELEASE_ASSERT(span_ && (index_ + n) >= 0 && |
| 184 |
(index_ + n) <= span_->Length()); |
| 185 |
index_ += n; |
| 186 |
return *this; |
| 187 |
} |
| 188 |
|
| 189 |
constexpr span_iterator operator-(difference_type n) const { |
| 190 |
auto ret = *this; |
| 191 |
return ret -= n; |
| 192 |
} |
| 193 |
|
| 194 |
constexpr span_iterator& operator-=(difference_type n) { return *this += -n; } |
| 195 |
|
| 196 |
constexpr difference_type operator-(const span_iterator& rhs) const { |
| 197 |
MOZ_RELEASE_ASSERT(span_ == rhs.span_); |
| 198 |
return index_ - rhs.index_; |
| 199 |
} |
| 200 |
|
| 201 |
constexpr reference operator[](difference_type n) const { |
| 202 |
return *(*this + n); |
| 203 |
} |
| 204 |
|
| 205 |
constexpr friend bool operator==(const span_iterator& lhs, |
| 206 |
const span_iterator& rhs) { |
| 207 |
// Iterators from different spans are uncomparable. A diagnostic assertion |
| 208 |
// should be enough to check this, though. To ensure that no iterators from |
| 209 |
// different spans are ever considered equal, still compare them in release |
| 210 |
// builds. |
| 211 |
MOZ_DIAGNOSTIC_ASSERT(lhs.span_ == rhs.span_); |
| 212 |
return lhs.index_ == rhs.index_ && lhs.span_ == rhs.span_; |
| 213 |
} |
| 214 |
|
| 215 |
constexpr friend bool operator!=(const span_iterator& lhs, |
| 216 |
const span_iterator& rhs) { |
| 217 |
return !(lhs == rhs); |
| 218 |
} |
| 219 |
|
| 220 |
constexpr friend bool operator<(const span_iterator& lhs, |
| 221 |
const span_iterator& rhs) { |
| 222 |
MOZ_DIAGNOSTIC_ASSERT(lhs.span_ == rhs.span_); |
| 223 |
return lhs.index_ < rhs.index_; |
| 224 |
} |
| 225 |
|
| 226 |
constexpr friend bool operator<=(const span_iterator& lhs, |
| 227 |
const span_iterator& rhs) { |
| 228 |
return !(rhs < lhs); |
| 229 |
} |
| 230 |
|
| 231 |
constexpr friend bool operator>(const span_iterator& lhs, |
| 232 |
const span_iterator& rhs) { |
| 233 |
return rhs < lhs; |
| 234 |
} |
| 235 |
|
| 236 |
constexpr friend bool operator>=(const span_iterator& lhs, |
| 237 |
const span_iterator& rhs) { |
| 238 |
return !(rhs > lhs); |
| 239 |
} |
| 240 |
|
| 241 |
void swap(span_iterator& rhs) { |
| 242 |
std::swap(index_, rhs.index_); |
| 243 |
std::swap(span_, rhs.span_); |
| 244 |
} |
| 245 |
|
| 246 |
protected: |
| 247 |
const SpanT* span_; |
| 248 |
size_t index_; |
| 249 |
}; |
| 250 |
|
| 251 |
template <class Span, bool IsConst> |
| 252 |
inline constexpr span_iterator<Span, IsConst> operator+( |
| 253 |
typename span_iterator<Span, IsConst>::difference_type n, |
| 254 |
const span_iterator<Span, IsConst>& rhs) { |
| 255 |
return rhs + n; |
| 256 |
} |
| 257 |
|
| 258 |
template <size_t Ext> |
| 259 |
class extent_type { |
| 260 |
public: |
| 261 |
using index_type = size_t; |
| 262 |
|
| 263 |
static_assert(Ext >= 0, "A fixed-size Span must be >= 0 in size."); |
| 264 |
|
| 265 |
constexpr extent_type() = default; |
| 266 |
|
| 267 |
template <index_type Other> |
| 268 |
constexpr MOZ_IMPLICIT extent_type(extent_type<Other> ext) { |
| 269 |
static_assert( |
| 270 |
Other == Ext || Other == dynamic_extent, |
| 271 |
"Mismatch between fixed-size extent and size of initializing data."); |
| 272 |
MOZ_RELEASE_ASSERT(ext.size() == Ext); |
| 273 |
} |
| 274 |
|
| 275 |
constexpr MOZ_IMPLICIT extent_type(index_type length) { |
| 276 |
MOZ_RELEASE_ASSERT(length == Ext); |
| 277 |
} |
| 278 |
|
| 279 |
constexpr index_type size() const { return Ext; } |
| 280 |
}; |
| 281 |
|
| 282 |
template <> |
| 283 |
class extent_type<dynamic_extent> { |
| 284 |
public: |
| 285 |
using index_type = size_t; |
| 286 |
|
| 287 |
template <index_type Other> |
| 288 |
explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size()) {} |
| 289 |
|
| 290 |
explicit constexpr extent_type(index_type length) : size_(length) {} |
| 291 |
|
| 292 |
constexpr index_type size() const { return size_; } |
| 293 |
|
| 294 |
private: |
| 295 |
index_type size_; |
| 296 |
}; |
| 297 |
} // namespace span_details |
| 298 |
|
| 299 |
/** |
| 300 |
* Span - slices for C++ |
| 301 |
* |
| 302 |
* Span implements Rust's slice concept for C++. It's called "Span" instead of |
| 303 |
* "Slice" to follow the naming used in C++ Core Guidelines. |
| 304 |
* |
| 305 |
* A Span wraps a pointer and a length that identify a non-owning view to a |
| 306 |
* contiguous block of memory of objects of the same type. Various types, |
| 307 |
* including (pre-decay) C arrays, XPCOM strings, nsTArray, mozilla::Array, |
| 308 |
* mozilla::Range and contiguous standard-library containers, auto-convert |
| 309 |
* into Spans when attempting to pass them as arguments to methods that take |
| 310 |
* Spans. (Span itself autoconverts into mozilla::Range.) |
| 311 |
* |
| 312 |
* Like Rust's slices, Span provides safety against out-of-bounds access by |
| 313 |
* performing run-time bound checks. However, unlike Rust's slices, Span |
| 314 |
* cannot provide safety against use-after-free. |
| 315 |
* |
| 316 |
* (Note: Span is like Rust's slice only conceptually. Due to the lack of |
| 317 |
* ABI guarantees, you should still decompose spans/slices to raw pointer |
| 318 |
* and length parts when crossing the FFI. The Elements() and data() methods |
| 319 |
* are guaranteed to return a non-null pointer even for zero-length spans, |
| 320 |
* so the pointer can be used as a raw part of a Rust slice without further |
| 321 |
* checks.) |
| 322 |
* |
| 323 |
* In addition to having constructors (with the support of deduction guides) |
| 324 |
* that take various well-known types, a Span for an arbitrary type can be |
| 325 |
* constructed from a pointer and a length or a pointer and another pointer |
| 326 |
* pointing just past the last element. |
| 327 |
* |
| 328 |
* A Span<const char> or Span<const char16_t> can be obtained for const char* |
| 329 |
* or const char16_t pointing to a zero-terminated string using the |
| 330 |
* MakeStringSpan() function (which treats a nullptr argument equivalently |
| 331 |
* to the empty string). Corresponding implicit constructor does not exist |
| 332 |
* in order to avoid accidental construction in cases where const char* or |
| 333 |
* const char16_t* do not point to a zero-terminated string. |
| 334 |
* |
| 335 |
* Span has methods that follow the Mozilla naming style and methods that |
| 336 |
* don't. The methods that follow the Mozilla naming style are meant to be |
| 337 |
* used directly from Mozilla code. The methods that don't are meant for |
| 338 |
* integration with C++11 range-based loops and with meta-programming that |
| 339 |
* expects the same methods that are found on the standard-library |
| 340 |
* containers. For example, to decompose a Span into its parts in Mozilla |
| 341 |
* code, use Elements() and Length() (as with nsTArray) instead of data() |
| 342 |
* and size() (as with std::vector). |
| 343 |
* |
| 344 |
* The pointer and length wrapped by a Span cannot be changed after a Span has |
| 345 |
* been created. When new values are required, simply create a new Span. Span |
| 346 |
* has a method called Subspan() that works analogously to the Substring() |
| 347 |
* method of XPCOM strings taking a start index and an optional length. As a |
| 348 |
* Mozilla extension (relative to Microsoft's gsl::span that mozilla::Span is |
| 349 |
* based on), Span has methods From(start), To(end) and FromTo(start, end) |
| 350 |
* that correspond to Rust's &slice[start..], &slice[..end] and |
| 351 |
* &slice[start..end], respectively. (That is, the end index is the index of |
| 352 |
* the first element not to be included in the new subspan.) |
| 353 |
* |
| 354 |
* When indicating a Span that's only read from, const goes inside the type |
| 355 |
* parameter. Don't put const in front of Span. That is: |
| 356 |
* size_t ReadsFromOneSpanAndWritesToAnother(Span<const uint8_t> aReadFrom, |
| 357 |
* Span<uint8_t> aWrittenTo); |
| 358 |
* |
| 359 |
* Any Span<const T> can be viewed as Span<const uint8_t> using the function |
| 360 |
* AsBytes(). Any Span<T> can be viewed as Span<uint8_t> using the function |
| 361 |
* AsWritableBytes(). |
| 362 |
* |
| 363 |
* Note that iterators from different Span instances are uncomparable, even if |
| 364 |
* they refer to the same memory. This also applies to any spans derived via |
| 365 |
* Subspan etc. |
| 366 |
*/ |
| 367 |
template <class ElementType, size_t Extent /* = dynamic_extent */> |
| 368 |
class MOZ_GSL_POINTER Span { |
| 369 |
public: |
| 370 |
// constants and types |
| 371 |
using element_type = ElementType; |
| 372 |
using value_type = std::remove_cv_t<element_type>; |
| 373 |
using index_type = size_t; |
| 374 |
using pointer = element_type*; |
| 375 |
using reference = element_type&; |
| 376 |
|
| 377 |
using iterator = |
| 378 |
span_details::span_iterator<Span<ElementType, Extent>, false>; |
| 379 |
using const_iterator = |
| 380 |
span_details::span_iterator<Span<ElementType, Extent>, true>; |
| 381 |
using reverse_iterator = std::reverse_iterator<iterator>; |
| 382 |
using const_reverse_iterator = std::reverse_iterator<const_iterator>; |
| 383 |
|
| 384 |
constexpr static const index_type extent = Extent; |
| 385 |
|
| 386 |
// [Span.cons], Span constructors, copy, assignment, and destructor |
| 387 |
// "Dependent" is needed to make "std::enable_if_t<(Dependent || |
| 388 |
// Extent == 0 || Extent == dynamic_extent)>" SFINAE, |
| 389 |
// since |
| 390 |
// "std::enable_if_t<(Extent == 0 || Extent == dynamic_extent)>" is |
| 391 |
// ill-formed when Extent is neither of the extreme values. |
| 392 |
/** |
| 393 |
* Constructor with no args. |
| 394 |
*/ |
| 395 |
template <bool Dependent = false, |
| 396 |
class = std::enable_if_t<(Dependent || Extent == 0 || |
| 397 |
Extent == dynamic_extent)>> |
| 398 |
constexpr Span() : storage_(nullptr, span_details::extent_type<0>()) {} |
| 399 |
|
| 400 |
/** |
| 401 |
* Constructor for nullptr. |
| 402 |
*/ |
| 403 |
constexpr MOZ_IMPLICIT Span(std::nullptr_t) : Span() {} |
| 404 |
|
| 405 |
/** |
| 406 |
* Constructor for pointer and length. |
| 407 |
*/ |
| 408 |
constexpr Span(pointer aPtr MOZ_LIFETIME_BOUND, index_type aLength) |
| 409 |
: storage_(aPtr, aLength) {} |
| 410 |
|
| 411 |
/** |
| 412 |
* Constructor for start pointer and pointer past end. |
| 413 |
*/ |
| 414 |
constexpr Span(pointer aStartPtr MOZ_LIFETIME_BOUND, |
| 415 |
pointer aEndPtr MOZ_LIFETIME_BOUND) |
| 416 |
: storage_(aStartPtr, std::distance(aStartPtr, aEndPtr)) {} |
| 417 |
|
| 418 |
/** |
| 419 |
* Constructor for pair of Span iterators. |
| 420 |
*/ |
| 421 |
template <typename OtherElementType, size_t OtherExtent, bool IsConst> |
| 422 |
constexpr Span( |
| 423 |
span_details::span_iterator<Span<OtherElementType, OtherExtent>, IsConst> |
| 424 |
aBegin, |
| 425 |
span_details::span_iterator<Span<OtherElementType, OtherExtent>, IsConst> |
| 426 |
aEnd) |
| 427 |
: storage_(aBegin == aEnd ? nullptr : &*aBegin, aEnd - aBegin) {} |
| 428 |
|
| 429 |
/** |
| 430 |
* Constructor for {iterator,size_t} |
| 431 |
*/ |
| 432 |
template <typename OtherElementType, size_t OtherExtent, bool IsConst> |
| 433 |
constexpr Span( |
| 434 |
span_details::span_iterator<Span<OtherElementType, OtherExtent>, IsConst> |
| 435 |
aBegin, |
| 436 |
index_type aLength) |
| 437 |
: storage_(!aLength ? nullptr : &*aBegin, aLength) {} |
| 438 |
|
| 439 |
/** |
| 440 |
* Constructor for C array. |
| 441 |
*/ |
| 442 |
template <size_t N> |
| 443 |
constexpr MOZ_IMPLICIT Span(element_type (&aArr MOZ_LIFETIME_BOUND)[N]) |
| 444 |
: storage_(&aArr[0], span_details::extent_type<N>()) {} |
| 445 |
|
| 446 |
// Implicit constructors for char* and char16_t* pointers are deleted in order |
| 447 |
// to avoid accidental construction in cases where a pointer does not point to |
| 448 |
// a zero-terminated string. A Span<const char> or Span<const char16_t> can be |
| 449 |
// obtained for const char* or const char16_t pointing to a zero-terminated |
| 450 |
// string using the MakeStringSpan() function. |
| 451 |
// (This must be a template because otherwise it will prevent the previous |
| 452 |
// array constructor to match because an array decays to a pointer. This only |
| 453 |
// exists to point to the above explanation, since there's no other |
| 454 |
// constructor that would match.) |
| 455 |
template < |
| 456 |
typename T, |
| 457 |
typename = std::enable_if_t< |
| 458 |
std::is_pointer_v<T> && |
| 459 |
(std::is_same_v<std::remove_const_t<std::decay_t<T>>, char> || |
| 460 |
std::is_same_v<std::remove_const_t<std::decay_t<T>>, char16_t>)>> |
| 461 |
Span(T& aStr) = delete; |
| 462 |
|
| 463 |
/** |
| 464 |
* Constructor for std::array. |
| 465 |
*/ |
| 466 |
template <size_t N, |
| 467 |
class ArrayElementType = std::remove_const_t<element_type>> |
| 468 |
constexpr MOZ_IMPLICIT Span( |
| 469 |
std::array<ArrayElementType, N>& aArr MOZ_LIFETIME_BOUND) |
| 470 |
: storage_(&aArr[0], span_details::extent_type<N>()) {} |
| 471 |
|
| 472 |
/** |
| 473 |
* Constructor for const std::array. |
| 474 |
*/ |
| 475 |
template <size_t N> |
| 476 |
constexpr MOZ_IMPLICIT Span( |
| 477 |
const std::array<std::remove_const_t<element_type>, N>& aArr |
| 478 |
MOZ_LIFETIME_BOUND) |
| 479 |
: storage_(&aArr[0], span_details::extent_type<N>()) {} |
| 480 |
|
| 481 |
/** |
| 482 |
* Constructor for mozilla::Array. |
| 483 |
*/ |
| 484 |
template <size_t N, |
| 485 |
class ArrayElementType = std::remove_const_t<element_type>> |
| 486 |
constexpr MOZ_IMPLICIT Span( |
| 487 |
mozilla::Array<ArrayElementType, N>& aArr MOZ_LIFETIME_BOUND) |
| 488 |
: storage_(&aArr[0], span_details::extent_type<N>()) {} |
| 489 |
|
| 490 |
/** |
| 491 |
* Constructor for const mozilla::Array. |
| 492 |
*/ |
| 493 |
template <size_t N> |
| 494 |
constexpr MOZ_IMPLICIT Span( |
| 495 |
const mozilla::Array<std::remove_const_t<element_type>, N>& aArr |
| 496 |
MOZ_LIFETIME_BOUND) |
| 497 |
: storage_(&aArr[0], span_details::extent_type<N>()) {} |
| 498 |
|
| 499 |
/** |
| 500 |
* Constructor for mozilla::EnumeratedArray. |
| 501 |
*/ |
| 502 |
template <size_t N, class Enum, |
| 503 |
class ArrayElementType = std::remove_const_t<element_type>> |
| 504 |
constexpr MOZ_IMPLICIT Span( |
| 505 |
mozilla::EnumeratedArray<Enum, ArrayElementType, N>& aArr |
| 506 |
MOZ_LIFETIME_BOUND) |
| 507 |
: storage_(&aArr[Enum(0)], span_details::extent_type<N>()) {} |
| 508 |
|
| 509 |
/** |
| 510 |
* Constructor for const mozilla::EnumeratedArray. |
| 511 |
*/ |
| 512 |
template <size_t N, class Enum> |
| 513 |
constexpr MOZ_IMPLICIT Span( |
| 514 |
const mozilla::EnumeratedArray<Enum, std::remove_const_t<element_type>, |
| 515 |
N>& aArr MOZ_LIFETIME_BOUND) |
| 516 |
: storage_(&aArr[Enum(0)], span_details::extent_type<N>()) {} |
| 517 |
|
| 518 |
/** |
| 519 |
* Constructor for mozilla::UniquePtr holding an array and length. |
| 520 |
*/ |
| 521 |
template <class ArrayElementType = std::add_pointer<element_type>, |
| 522 |
class DeleterType> |
| 523 |
constexpr Span(const mozilla::UniquePtr<ArrayElementType, DeleterType>& aPtr |
| 524 |
MOZ_LIFETIME_BOUND, |
| 525 |
index_type aLength) |
| 526 |
: storage_(aPtr.get(), aLength) {} |
| 527 |
|
| 528 |
// NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the |
| 529 |
// requirement on Container to be a contiguous sequence container. |
| 530 |
/** |
| 531 |
* Constructor for standard-library containers. |
| 532 |
*/ |
| 533 |
template < |
| 534 |
class Container, |
| 535 |
class Dummy = std::enable_if_t< |
| 536 |
!std::is_const_v<Container> && |
| 537 |
!span_details::is_span<Container>::value && |
| 538 |
!span_details::is_std_array<Container>::value && |
| 539 |
std::is_convertible_v<typename Container::pointer, pointer> && |
| 540 |
std::is_convertible_v<typename Container::pointer, |
| 541 |
decltype(std::declval<Container>().data())>, |
| 542 |
Container>> |
| 543 |
constexpr MOZ_IMPLICIT Span(Container& cont, Dummy* = nullptr) |
| 544 |
: Span(cont.data(), ReleaseAssertedCast<index_type>(cont.size())) {} |
| 545 |
|
| 546 |
/** |
| 547 |
* Constructor for standard-library containers (const version). |
| 548 |
*/ |
| 549 |
template < |
| 550 |
class Container, |
| 551 |
class = std::enable_if_t< |
| 552 |
std::is_const_v<element_type> && |
| 553 |
!span_details::is_span<Container>::value && |
| 554 |
std::is_convertible_v<typename Container::pointer, pointer> && |
| 555 |
std::is_convertible_v<typename Container::pointer, |
| 556 |
decltype(std::declval<Container>().data())>>> |
| 557 |
constexpr MOZ_IMPLICIT Span(const Container& cont) |
| 558 |
: Span(cont.data(), ReleaseAssertedCast<index_type>(cont.size())) {} |
| 559 |
|
| 560 |
// NB: the SFINAE here uses .Elements() as a incomplete/imperfect proxy for |
| 561 |
// the requirement on Container to be a contiguous sequence container. |
| 562 |
/** |
| 563 |
* Constructor for contiguous Mozilla containers. |
| 564 |
*/ |
| 565 |
template < |
| 566 |
class Container, |
| 567 |
class = std::enable_if_t< |
| 568 |
!std::is_const_v<Container> && |
| 569 |
!span_details::is_span<Container>::value && |
| 570 |
!span_details::is_std_array<Container>::value && |
| 571 |
std::is_convertible_v<typename Container::value_type*, pointer> && |
| 572 |
std::is_convertible_v< |
| 573 |
typename Container::value_type*, |
| 574 |
decltype(std::declval<Container>().Elements())>>> |
| 575 |
constexpr MOZ_IMPLICIT Span(Container& cont, void* = nullptr) |
| 576 |
: Span(cont.Elements(), ReleaseAssertedCast<index_type>(cont.Length())) {} |
| 577 |
|
| 578 |
/** |
| 579 |
* Constructor for contiguous Mozilla containers (const version). |
| 580 |
*/ |
| 581 |
template < |
| 582 |
class Container, |
| 583 |
class = std::enable_if_t< |
| 584 |
std::is_const_v<element_type> && |
| 585 |
!span_details::is_span<Container>::value && |
| 586 |
std::is_convertible_v<typename Container::value_type*, pointer> && |
| 587 |
std::is_convertible_v< |
| 588 |
typename Container::value_type*, |
| 589 |
decltype(std::declval<Container>().Elements())>>> |
| 590 |
constexpr MOZ_IMPLICIT Span(const Container& cont, void* = nullptr) |
| 591 |
: Span(cont.Elements(), ReleaseAssertedCast<index_type>(cont.Length())) {} |
| 592 |
|
| 593 |
/** |
| 594 |
* Constructor from other Span. |
| 595 |
*/ |
| 596 |
constexpr Span(const Span& other) = default; |
| 597 |
|
| 598 |
/** |
| 599 |
* Constructor from other Span. |
| 600 |
*/ |
| 601 |
constexpr Span(Span&& other) = default; |
| 602 |
|
| 603 |
/** |
| 604 |
* Constructor from other Span with conversion of element type. |
| 605 |
*/ |
| 606 |
template < |
| 607 |
class OtherElementType, size_t OtherExtent, |
| 608 |
class = std::enable_if_t<span_details::is_allowed_extent_conversion< |
| 609 |
OtherExtent, Extent>::value && |
| 610 |
span_details::is_allowed_element_type_conversion< |
| 611 |
OtherElementType, element_type>::value>> |
| 612 |
constexpr MOZ_IMPLICIT Span(const Span<OtherElementType, OtherExtent>& other) |
| 613 |
: storage_(other.data(), |
| 614 |
span_details::extent_type<OtherExtent>(other.size())) {} |
| 615 |
|
| 616 |
/** |
| 617 |
* Constructor from other Span with conversion of element type. |
| 618 |
*/ |
| 619 |
template < |
| 620 |
class OtherElementType, size_t OtherExtent, |
| 621 |
class = std::enable_if_t<span_details::is_allowed_extent_conversion< |
| 622 |
OtherExtent, Extent>::value && |
| 623 |
span_details::is_allowed_element_type_conversion< |
| 624 |
OtherElementType, element_type>::value>> |
| 625 |
constexpr MOZ_IMPLICIT Span(Span<OtherElementType, OtherExtent>&& other) |
| 626 |
: storage_(other.data(), |
| 627 |
span_details::extent_type<OtherExtent>(other.size())) {} |
| 628 |
|
| 629 |
~Span() = default; |
| 630 |
constexpr Span& operator=(const Span& other) = default; |
| 631 |
|
| 632 |
constexpr Span& operator=(Span&& other) = default; |
| 633 |
|
| 634 |
// [Span.sub], Span subviews |
| 635 |
/** |
| 636 |
* Subspan with first N elements with compile-time N. |
| 637 |
*/ |
| 638 |
template <size_t Count> |
| 639 |
constexpr Span<element_type, Count> First() const { |
| 640 |
MOZ_RELEASE_ASSERT(Count <= size()); |
| 641 |
return {data(), Count}; |
| 642 |
} |
| 643 |
|
| 644 |
/** |
| 645 |
* Subspan with last N elements with compile-time N. |
| 646 |
*/ |
| 647 |
template <size_t Count> |
| 648 |
constexpr Span<element_type, Count> Last() const { |
| 649 |
const size_t len = size(); |
| 650 |
MOZ_RELEASE_ASSERT(Count <= len); |
| 651 |
return {data() + (len - Count), Count}; |
| 652 |
} |
| 653 |
|
| 654 |
/** |
| 655 |
* Subspan with compile-time start index and length. |
| 656 |
*/ |
| 657 |
template <size_t Offset, size_t Count = dynamic_extent> |
| 658 |
constexpr Span<element_type, Count> Subspan() const { |
| 659 |
const size_t len = size(); |
| 660 |
MOZ_RELEASE_ASSERT(Offset <= len && |
| 661 |
(Count == dynamic_extent || (Count <= len - Offset))); |
| 662 |
return {data() + Offset, Count == dynamic_extent ? len - Offset : Count}; |
| 663 |
} |
| 664 |
|
| 665 |
/** |
| 666 |
* Subspan with first N elements with run-time N. |
| 667 |
*/ |
| 668 |
constexpr Span<element_type, dynamic_extent> First(index_type aCount) const { |
| 669 |
MOZ_RELEASE_ASSERT(aCount <= size()); |
| 670 |
return {data(), aCount}; |
| 671 |
} |
| 672 |
|
| 673 |
/** |
| 674 |
* Subspan with last N elements with run-time N. |
| 675 |
*/ |
| 676 |
constexpr Span<element_type, dynamic_extent> Last(index_type aCount) const { |
| 677 |
const size_t len = size(); |
| 678 |
MOZ_RELEASE_ASSERT(aCount <= len); |
| 679 |
return {data() + (len - aCount), aCount}; |
| 680 |
} |
| 681 |
|
| 682 |
/** |
| 683 |
* Subspan with run-time start index and length. |
| 684 |
*/ |
| 685 |
constexpr Span<element_type, dynamic_extent> Subspan( |
| 686 |
index_type aStart, index_type aLength = dynamic_extent) const { |
| 687 |
const size_t len = size(); |
| 688 |
MOZ_RELEASE_ASSERT(aStart <= len && (aLength == dynamic_extent || |
| 689 |
(aLength <= len - aStart))); |
| 690 |
return {data() + aStart, |
| 691 |
aLength == dynamic_extent ? len - aStart : aLength}; |
| 692 |
} |
| 693 |
|
| 694 |
/** |
| 695 |
* Subspan with run-time start index. (Rust's &foo[start..]) |
| 696 |
*/ |
| 697 |
constexpr Span<element_type, dynamic_extent> From(index_type aStart) const { |
| 698 |
return Subspan(aStart); |
| 699 |
} |
| 700 |
|
| 701 |
/** |
| 702 |
* Subspan with run-time exclusive end index. (Rust's &foo[..end]) |
| 703 |
*/ |
| 704 |
constexpr Span<element_type, dynamic_extent> To(index_type aEnd) const { |
| 705 |
return Subspan(0, aEnd); |
| 706 |
} |
| 707 |
|
| 708 |
/// std::span-compatible method name |
| 709 |
constexpr auto subspan(index_type aStart, |
| 710 |
index_type aLength = dynamic_extent) const { |
| 711 |
return Subspan(aStart, aLength); |
| 712 |
} |
| 713 |
/// std::span-compatible method name |
| 714 |
constexpr auto from(index_type aStart) const { return From(aStart); } |
| 715 |
/// std::span-compatible method name |
| 716 |
constexpr auto to(index_type aEnd) const { return To(aEnd); } |
| 717 |
|
| 718 |
/** |
| 719 |
* Subspan with run-time start index and exclusive end index. |
| 720 |
* (Rust's &foo[start..end]) |
| 721 |
*/ |
| 722 |
constexpr Span<element_type, dynamic_extent> FromTo(index_type aStart, |
| 723 |
index_type aEnd) const { |
| 724 |
MOZ_RELEASE_ASSERT(aStart <= aEnd); |
| 725 |
return Subspan(aStart, aEnd - aStart); |
| 726 |
} |
| 727 |
|
| 728 |
// [Span.obs], Span observers |
| 729 |
/** |
| 730 |
* Number of elements in the span. |
| 731 |
*/ |
| 732 |
constexpr index_type Length() const { return size(); } |
| 733 |
|
| 734 |
/** |
| 735 |
* Number of elements in the span (standard-libray duck typing version). |
| 736 |
*/ |
| 737 |
constexpr index_type size() const { return storage_.size(); } |
| 738 |
|
| 739 |
/** |
| 740 |
* Size of the span in bytes. |
| 741 |
*/ |
| 742 |
constexpr index_type LengthBytes() const { return size_bytes(); } |
| 743 |
|
| 744 |
/** |
| 745 |
* Size of the span in bytes (standard-library naming style version). |
| 746 |
*/ |
| 747 |
constexpr index_type size_bytes() const { |
| 748 |
return size() * narrow_cast<index_type>(sizeof(element_type)); |
| 749 |
} |
| 750 |
|
| 751 |
/** |
| 752 |
* Checks if the the length of the span is zero. |
| 753 |
*/ |
| 754 |
constexpr bool IsEmpty() const { return empty(); } |
| 755 |
|
| 756 |
/** |
| 757 |
* Checks if the the length of the span is zero (standard-libray duck |
| 758 |
* typing version). |
| 759 |
*/ |
| 760 |
constexpr bool empty() const { return size() == 0; } |
| 761 |
|
| 762 |
// [Span.elem], Span element access |
| 763 |
constexpr reference operator[](index_type idx) const { |
| 764 |
MOZ_RELEASE_ASSERT(idx < storage_.size()); |
| 765 |
return data()[idx]; |
| 766 |
} |
| 767 |
|
| 768 |
/** |
| 769 |
* Access element of span by index (standard-library duck typing version). |
| 770 |
*/ |
| 771 |
constexpr reference at(index_type idx) const { return this->operator[](idx); } |
| 772 |
|
| 773 |
constexpr reference operator()(index_type idx) const { |
| 774 |
return this->operator[](idx); |
| 775 |
} |
| 776 |
|
| 777 |
/** |
| 778 |
* Pointer to the first element of the span. The return value is never |
| 779 |
* nullptr, not ever for zero-length spans, so it can be passed as-is |
| 780 |
* to std::slice::from_raw_parts() in Rust. |
| 781 |
*/ |
| 782 |
constexpr pointer Elements() const { return data(); } |
| 783 |
|
| 784 |
/** |
| 785 |
* Pointer to the first element of the span (standard-libray duck typing |
| 786 |
* version). The return value is never nullptr, not ever for zero-length |
| 787 |
* spans, so it can be passed as-is to std::slice::from_raw_parts() in Rust. |
| 788 |
*/ |
| 789 |
constexpr pointer data() const { return storage_.data(); } |
| 790 |
|
| 791 |
// [Span.iter], Span iterator support |
| 792 |
iterator begin() const { return {this, 0, span_details::SpanKnownBounds{}}; } |
| 793 |
iterator end() const { |
| 794 |
return {this, Length(), span_details::SpanKnownBounds{}}; |
| 795 |
} |
| 796 |
|
| 797 |
const_iterator cbegin() const { |
| 798 |
return {this, 0, span_details::SpanKnownBounds{}}; |
| 799 |
} |
| 800 |
const_iterator cend() const { |
| 801 |
return {this, Length(), span_details::SpanKnownBounds{}}; |
| 802 |
} |
| 803 |
|
| 804 |
reverse_iterator rbegin() const { return reverse_iterator{end()}; } |
| 805 |
reverse_iterator rend() const { return reverse_iterator{begin()}; } |
| 806 |
|
| 807 |
const_reverse_iterator crbegin() const { |
| 808 |
return const_reverse_iterator{cend()}; |
| 809 |
} |
| 810 |
const_reverse_iterator crend() const { |
| 811 |
return const_reverse_iterator{cbegin()}; |
| 812 |
} |
| 813 |
|
| 814 |
template <size_t SplitPoint> |
| 815 |
constexpr std::pair<Span<ElementType, SplitPoint>, |
| 816 |
Span<ElementType, Extent - SplitPoint>> |
| 817 |
SplitAt() const { |
| 818 |
static_assert(Extent != dynamic_extent); |
| 819 |
static_assert(SplitPoint <= Extent); |
| 820 |
return {First<SplitPoint>(), Last<Extent - SplitPoint>()}; |
| 821 |
} |
| 822 |
|
| 823 |
constexpr std::pair<Span<ElementType, dynamic_extent>, |
| 824 |
Span<ElementType, dynamic_extent>> |
| 825 |
SplitAt(const index_type aSplitPoint) const { |
| 826 |
MOZ_RELEASE_ASSERT(aSplitPoint <= Length()); |
| 827 |
return {First(aSplitPoint), Last(Length() - aSplitPoint)}; |
| 828 |
} |
| 829 |
|
| 830 |
constexpr Span<std::add_const_t<ElementType>, Extent> AsConst() const { |
| 831 |
return {Elements(), Length()}; |
| 832 |
} |
| 833 |
|
| 834 |
private: |
| 835 |
// this implementation detail class lets us take advantage of the |
| 836 |
// empty base class optimization to pay for only storage of a single |
| 837 |
// pointer in the case of fixed-size Spans |
| 838 |
template <class ExtentType> |
| 839 |
class storage_type : public ExtentType { |
| 840 |
public: |
| 841 |
template <class OtherExtentType> |
| 842 |
constexpr storage_type(pointer elements, OtherExtentType ext) |
| 843 |
: ExtentType(ext) |
| 844 |
// Replace nullptr with aligned bogus pointer for Rust slice |
| 845 |
// compatibility. See |
| 846 |
// https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html |
| 847 |
, |
| 848 |
data_(elements ? elements |
| 849 |
: reinterpret_cast<pointer>(alignof(element_type))) { |
| 850 |
const size_t extentSize = ExtentType::size(); |
| 851 |
MOZ_RELEASE_ASSERT((!elements && extentSize == 0) || |
| 852 |
(elements && extentSize != dynamic_extent)); |
| 853 |
} |
| 854 |
|
| 855 |
constexpr pointer data() const { return data_; } |
| 856 |
|
| 857 |
private: |
| 858 |
pointer data_; |
| 859 |
}; |
| 860 |
|
| 861 |
storage_type<span_details::extent_type<Extent>> storage_; |
| 862 |
}; |
| 863 |
|
| 864 |
template <typename T, size_t OtherExtent, bool IsConst> |
| 865 |
Span(span_details::span_iterator<Span<T, OtherExtent>, IsConst> aBegin, |
| 866 |
span_details::span_iterator<Span<T, OtherExtent>, IsConst> aEnd) |
| 867 |
-> Span<std::conditional_t<IsConst, std::add_const_t<T>, T>>; |
| 868 |
|
| 869 |
template <typename T, size_t Extent> |
| 870 |
Span(T (&)[Extent]) -> Span<T, Extent>; |
| 871 |
|
| 872 |
template <class Container> |
| 873 |
Span(Container&) -> Span<typename Container::value_type>; |
| 874 |
|
| 875 |
template <class Container> |
| 876 |
Span(const Container&) -> Span<const typename Container::value_type>; |
| 877 |
|
| 878 |
template <typename T, size_t Extent> |
| 879 |
Span(mozilla::Array<T, Extent>&) -> Span<T, Extent>; |
| 880 |
|
| 881 |
template <typename T, size_t Extent> |
| 882 |
Span(const mozilla::Array<T, Extent>&) -> Span<const T, Extent>; |
| 883 |
|
| 884 |
template <typename Enum, typename T, size_t Extent> |
| 885 |
Span(mozilla::EnumeratedArray<Enum, T, Extent>&) -> Span<T, Extent>; |
| 886 |
|
| 887 |
template <typename Enum, typename T, size_t Extent> |
| 888 |
Span(const mozilla::EnumeratedArray<Enum, T, Extent>&) -> Span<const T, Extent>; |
| 889 |
|
| 890 |
// [Span.comparison], Span comparison operators |
| 891 |
template <class ElementType, size_t FirstExtent, size_t SecondExtent> |
| 892 |
inline constexpr bool operator==(const Span<ElementType, FirstExtent>& l, |
| 893 |
const Span<ElementType, SecondExtent>& r) { |
| 894 |
return (l.size() == r.size()) && |
| 895 |
std::equal(l.data(), l.data() + l.size(), r.data()); |
| 896 |
} |
| 897 |
|
| 898 |
template <class ElementType, size_t Extent> |
| 899 |
inline constexpr bool operator!=(const Span<ElementType, Extent>& l, |
| 900 |
const Span<ElementType, Extent>& r) { |
| 901 |
return !(l == r); |
| 902 |
} |
| 903 |
|
| 904 |
template <class ElementType, size_t Extent> |
| 905 |
inline constexpr bool operator<(const Span<ElementType, Extent>& l, |
| 906 |
const Span<ElementType, Extent>& r) { |
| 907 |
return std::lexicographical_compare(l.data(), l.data() + l.size(), r.data(), |
| 908 |
r.data() + r.size()); |
| 909 |
} |
| 910 |
|
| 911 |
template <class ElementType, size_t Extent> |
| 912 |
inline constexpr bool operator<=(const Span<ElementType, Extent>& l, |
| 913 |
const Span<ElementType, Extent>& r) { |
| 914 |
return !(l > r); |
| 915 |
} |
| 916 |
|
| 917 |
template <class ElementType, size_t Extent> |
| 918 |
inline constexpr bool operator>(const Span<ElementType, Extent>& l, |
| 919 |
const Span<ElementType, Extent>& r) { |
| 920 |
return r < l; |
| 921 |
} |
| 922 |
|
| 923 |
template <class ElementType, size_t Extent> |
| 924 |
inline constexpr bool operator>=(const Span<ElementType, Extent>& l, |
| 925 |
const Span<ElementType, Extent>& r) { |
| 926 |
return !(l < r); |
| 927 |
} |
| 928 |
|
| 929 |
namespace span_details { |
| 930 |
// if we only supported compilers with good constexpr support then |
| 931 |
// this pair of classes could collapse down to a constexpr function |
| 932 |
|
| 933 |
// we should use a narrow_cast<> to go to size_t, but older compilers may not |
| 934 |
// see it as constexpr and so will fail compilation of the template |
| 935 |
template <class ElementType, size_t Extent> |
| 936 |
struct calculate_byte_size |
| 937 |
: std::integral_constant<size_t, |
| 938 |
static_cast<size_t>(sizeof(ElementType) * |
| 939 |
static_cast<size_t>(Extent))> { |
| 940 |
}; |
| 941 |
|
| 942 |
template <class ElementType> |
| 943 |
struct calculate_byte_size<ElementType, dynamic_extent> |
| 944 |
: std::integral_constant<size_t, dynamic_extent> {}; |
| 945 |
} // namespace span_details |
| 946 |
|
| 947 |
// [Span.objectrep], views of object representation |
| 948 |
/** |
| 949 |
* View span as Span<const uint8_t>. |
| 950 |
*/ |
| 951 |
template <class ElementType, size_t Extent> |
| 952 |
Span<const uint8_t, |
| 953 |
span_details::calculate_byte_size<ElementType, Extent>::value> |
| 954 |
AsBytes(Span<ElementType, Extent> s) { |
| 955 |
return {reinterpret_cast<const uint8_t*>(s.data()), s.size_bytes()}; |
| 956 |
} |
| 957 |
|
| 958 |
/** |
| 959 |
* View span as Span<uint8_t>. |
| 960 |
*/ |
| 961 |
template <class ElementType, size_t Extent, |
| 962 |
class = std::enable_if_t<!std::is_const_v<ElementType>>> |
| 963 |
Span<uint8_t, span_details::calculate_byte_size<ElementType, Extent>::value> |
| 964 |
AsWritableBytes(Span<ElementType, Extent> s) { |
| 965 |
return {reinterpret_cast<uint8_t*>(s.data()), s.size_bytes()}; |
| 966 |
} |
| 967 |
|
| 968 |
/** |
| 969 |
* View a span of uint8_t as a span of char. |
| 970 |
*/ |
| 971 |
inline Span<const char> AsChars(Span<const uint8_t> s) { |
| 972 |
return {reinterpret_cast<const char*>(s.data()), s.size()}; |
| 973 |
} |
| 974 |
|
| 975 |
/** |
| 976 |
* View a writable span of uint8_t as a span of char. |
| 977 |
*/ |
| 978 |
inline Span<char> AsWritableChars(Span<uint8_t> s) { |
| 979 |
return {reinterpret_cast<char*>(s.data()), s.size()}; |
| 980 |
} |
| 981 |
|
| 982 |
/** |
| 983 |
* Create span from a zero-terminated C string. nullptr is |
| 984 |
* treated as the empty string. |
| 985 |
*/ |
| 986 |
constexpr Span<const char> MakeStringSpan(const char* aZeroTerminated) { |
| 987 |
if (!aZeroTerminated) { |
| 988 |
return Span<const char>(); |
| 989 |
} |
| 990 |
return Span<const char>(aZeroTerminated, |
| 991 |
std::char_traits<char>::length(aZeroTerminated)); |
| 992 |
} |
| 993 |
|
| 994 |
/** |
| 995 |
* Create span from a zero-terminated UTF-16 C string. nullptr is |
| 996 |
* treated as the empty string. |
| 997 |
*/ |
| 998 |
constexpr Span<const char16_t> MakeStringSpan(const char16_t* aZeroTerminated) { |
| 999 |
if (!aZeroTerminated) { |
| 1000 |
return Span<const char16_t>(); |
| 1001 |
} |
| 1002 |
return Span<const char16_t>( |
| 1003 |
aZeroTerminated, std::char_traits<char16_t>::length(aZeroTerminated)); |
| 1004 |
} |
| 1005 |
|
| 1006 |
} // namespace mozilla |
| 1007 |
|
| 1008 |
#endif // mozilla_Span_h |