2017-02-16 12:43:50 +03:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
|
|
|
//
|
|
|
|
// This code is licensed under the MIT License (MIT).
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Adapted from
|
|
|
|
// https://github.com/Microsoft/GSL/blob/3819df6e378ffccf0e29465afe99c3b324c2aa70/include/gsl/span
|
|
|
|
// and
|
|
|
|
// https://github.com/Microsoft/GSL/blob/3819df6e378ffccf0e29465afe99c3b324c2aa70/include/gsl/gsl_util
|
|
|
|
|
|
|
|
#ifndef mozilla_Span_h
|
|
|
|
#define mozilla_Span_h
|
|
|
|
|
Bug 1609996 - Reorder some includes affected by the previous patches. r=froydnj
This was done by:
This was done by applying:
```
diff --git a/python/mozbuild/mozbuild/code-analysis/mach_commands.py b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
index 789affde7bbf..fe33c4c7d4d1 100644
--- a/python/mozbuild/mozbuild/code-analysis/mach_commands.py
+++ b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
@@ -2007,7 +2007,7 @@ class StaticAnalysis(MachCommandBase):
from subprocess import Popen, PIPE, check_output, CalledProcessError
diff_process = Popen(self._get_clang_format_diff_command(commit), stdout=PIPE)
- args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format]
+ args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format, '-sort-includes']
if not output_file:
args.append("-i")
```
Then running `./mach clang-format -c <commit-hash>`
Then undoing that patch.
Then running check_spidermonkey_style.py --fixup
Then running `./mach clang-format`
I had to fix four things:
* I needed to move <utility> back down in GuardObjects.h because I was hitting
obscure problems with our system include wrappers like this:
0:03.94 /usr/include/stdlib.h:550:14: error: exception specification in declaration does not match previous declaration
0:03.94 extern void *realloc (void *__ptr, size_t __size)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/malloc_decls.h:53:1: note: previous declaration is here
0:03.94 MALLOC_DECL(realloc, void*, void*, size_t)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozilla/mozalloc.h:22:32: note: expanded from macro 'MALLOC_DECL'
0:03.94 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
0:03.94 ^
0:03.94 <scratch space>:178:1: note: expanded from here
0:03.94 realloc_impl
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozmemory_wrap.h:142:41: note: expanded from macro 'realloc_impl'
0:03.94 #define realloc_impl mozmem_malloc_impl(realloc)
Which I really didn't feel like digging into.
* I had to restore the order of TrustOverrideUtils.h and related files in nss
because the .inc files depend on TrustOverrideUtils.h being included earlier.
* I had to add a missing include to RollingNumber.h
* Also had to partially restore include order in JsepSessionImpl.cpp to avoid
some -WError issues due to some static inline functions being defined in a
header but not used in the rest of the compilation unit.
Differential Revision: https://phabricator.services.mozilla.com/D60327
--HG--
extra : moz-landing-system : lando
2020-01-20 19:19:48 +03:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
|
|
|
#include <cstring>
|
|
|
|
#include <iterator>
|
|
|
|
#include <limits>
|
2020-03-28 16:57:16 +03:00
|
|
|
#include <type_traits>
|
Bug 1609996 - Reorder some includes affected by the previous patches. r=froydnj
This was done by:
This was done by applying:
```
diff --git a/python/mozbuild/mozbuild/code-analysis/mach_commands.py b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
index 789affde7bbf..fe33c4c7d4d1 100644
--- a/python/mozbuild/mozbuild/code-analysis/mach_commands.py
+++ b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
@@ -2007,7 +2007,7 @@ class StaticAnalysis(MachCommandBase):
from subprocess import Popen, PIPE, check_output, CalledProcessError
diff_process = Popen(self._get_clang_format_diff_command(commit), stdout=PIPE)
- args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format]
+ args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format, '-sort-includes']
if not output_file:
args.append("-i")
```
Then running `./mach clang-format -c <commit-hash>`
Then undoing that patch.
Then running check_spidermonkey_style.py --fixup
Then running `./mach clang-format`
I had to fix four things:
* I needed to move <utility> back down in GuardObjects.h because I was hitting
obscure problems with our system include wrappers like this:
0:03.94 /usr/include/stdlib.h:550:14: error: exception specification in declaration does not match previous declaration
0:03.94 extern void *realloc (void *__ptr, size_t __size)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/malloc_decls.h:53:1: note: previous declaration is here
0:03.94 MALLOC_DECL(realloc, void*, void*, size_t)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozilla/mozalloc.h:22:32: note: expanded from macro 'MALLOC_DECL'
0:03.94 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
0:03.94 ^
0:03.94 <scratch space>:178:1: note: expanded from here
0:03.94 realloc_impl
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozmemory_wrap.h:142:41: note: expanded from macro 'realloc_impl'
0:03.94 #define realloc_impl mozmem_malloc_impl(realloc)
Which I really didn't feel like digging into.
* I had to restore the order of TrustOverrideUtils.h and related files in nss
because the .inc files depend on TrustOverrideUtils.h being included earlier.
* I had to add a missing include to RollingNumber.h
* Also had to partially restore include order in JsepSessionImpl.cpp to avoid
some -WError issues due to some static inline functions being defined in a
header but not used in the rest of the compilation unit.
Differential Revision: https://phabricator.services.mozilla.com/D60327
--HG--
extra : moz-landing-system : lando
2020-01-20 19:19:48 +03:00
|
|
|
#include <utility>
|
|
|
|
|
2017-02-16 12:43:50 +03:00
|
|
|
#include "mozilla/Array.h"
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/Casting.h"
|
|
|
|
#include "mozilla/IntegerTypeTraits.h"
|
|
|
|
#include "mozilla/UniquePtr.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
// Stuff from gsl_util
|
|
|
|
|
|
|
|
// narrow_cast(): a searchable way to do narrowing casts of values
|
|
|
|
template <class T, class U>
|
|
|
|
inline constexpr T narrow_cast(U&& u) {
|
2018-06-01 19:30:30 +03:00
|
|
|
return static_cast<T>(std::forward<U>(u));
|
2017-02-16 12:43:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// end gsl_util
|
|
|
|
|
|
|
|
// [views.constants], constants
|
|
|
|
// This was -1 in gsl::span, but using size_t for sizes instead of ptrdiff_t
|
|
|
|
// and reserving a magic value that realistically doesn't occur in
|
|
|
|
// compile-time-constant Span sizes makes things a lot less messy in terms of
|
|
|
|
// comparison between signed and unsigned.
|
2020-01-08 19:30:40 +03:00
|
|
|
constexpr const size_t dynamic_extent = std::numeric_limits<size_t>::max();
|
2017-02-16 12:43:50 +03:00
|
|
|
|
|
|
|
template <class ElementType, size_t Extent = dynamic_extent>
|
|
|
|
class Span;
|
|
|
|
|
|
|
|
// implementation details
|
|
|
|
namespace span_details {
|
|
|
|
|
2017-04-04 14:04:14 +03:00
|
|
|
inline size_t strlen16(const char16_t* aZeroTerminated) {
|
|
|
|
size_t len = 0;
|
|
|
|
while (*(aZeroTerminated++)) {
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2017-02-16 12:43:50 +03:00
|
|
|
template <class T>
|
2020-03-28 16:57:20 +03:00
|
|
|
struct is_span_oracle : std::false_type {};
|
2017-02-16 12:43:50 +03:00
|
|
|
|
|
|
|
template <class ElementType, size_t Extent>
|
2020-03-28 16:57:20 +03:00
|
|
|
struct is_span_oracle<mozilla::Span<ElementType, Extent>> : std::true_type {};
|
2017-02-16 12:43:50 +03:00
|
|
|
|
|
|
|
template <class T>
|
2020-03-28 16:57:18 +03:00
|
|
|
struct is_span : public is_span_oracle<std::remove_cv_t<T>> {};
|
2017-02-16 12:43:50 +03:00
|
|
|
|
|
|
|
template <class T>
|
2020-03-28 16:57:20 +03:00
|
|
|
struct is_std_array_oracle : std::false_type {};
|
2017-02-16 12:43:50 +03:00
|
|
|
|
|
|
|
template <class ElementType, size_t Extent>
|
2020-03-28 16:57:20 +03:00
|
|
|
struct is_std_array_oracle<std::array<ElementType, Extent>> : std::true_type {};
|
2017-02-16 12:43:50 +03:00
|
|
|
|
|
|
|
template <class T>
|
2020-03-28 16:57:18 +03:00
|
|
|
struct is_std_array : public is_std_array_oracle<std::remove_cv_t<T>> {};
|
2017-02-16 12:43:50 +03:00
|
|
|
|
|
|
|
template <size_t From, size_t To>
|
|
|
|
struct is_allowed_extent_conversion
|
2020-03-28 16:57:19 +03:00
|
|
|
: public std::integral_constant<bool, From == To ||
|
|
|
|
From == mozilla::dynamic_extent ||
|
|
|
|
To == mozilla::dynamic_extent> {};
|
2017-02-16 12:43:50 +03:00
|
|
|
|
|
|
|
template <class From, class To>
|
|
|
|
struct is_allowed_element_type_conversion
|
2020-03-28 16:57:19 +03:00
|
|
|
: public std::integral_constant<
|
2020-03-28 16:57:17 +03:00
|
|
|
bool, std::is_convertible_v<From (*)[], To (*)[]>> {};
|
2017-02-16 12:43:50 +03:00
|
|
|
|
2020-05-28 13:05:38 +03:00
|
|
|
struct SpanKnownBounds {};
|
|
|
|
|
2020-05-28 13:03:06 +03:00
|
|
|
template <class SpanT, bool IsConst>
|
2017-02-16 12:43:50 +03:00
|
|
|
class span_iterator {
|
2020-05-28 13:03:06 +03:00
|
|
|
using element_type_ = typename SpanT::element_type;
|
2017-02-16 12:43:50 +03:00
|
|
|
|
2020-05-30 12:55:27 +03:00
|
|
|
template <class ElementType, size_t Extent>
|
|
|
|
friend class ::mozilla::Span;
|
2020-05-28 13:05:38 +03:00
|
|
|
|
2017-02-16 12:43:50 +03:00
|
|
|
public:
|
|
|
|
using iterator_category = std::random_access_iterator_tag;
|
2020-03-28 16:57:19 +03:00
|
|
|
using value_type = std::remove_const_t<element_type_>;
|
2020-05-28 13:03:06 +03:00
|
|
|
using difference_type = typename SpanT::index_type;
|
2017-02-16 12:43:50 +03:00
|
|
|
|
2020-03-28 16:57:18 +03:00
|
|
|
using reference =
|
|
|
|
std::conditional_t<IsConst, const element_type_, element_type_>&;
|
2020-03-28 16:57:16 +03:00
|
|
|
using pointer = std::add_pointer_t<reference>;
|
2017-02-16 12:43:50 +03:00
|
|
|
|
2020-05-28 14:17:42 +03:00
|
|
|
constexpr span_iterator() : span_iterator(nullptr, 0, SpanKnownBounds{}) {}
|
2017-02-16 12:43:50 +03:00
|
|
|
|
2020-05-28 13:03:06 +03:00
|
|
|
constexpr span_iterator(const SpanT* span, typename SpanT::index_type index)
|
2017-02-16 12:43:50 +03:00
|
|
|
: span_(span), index_(index) {
|
|
|
|
MOZ_RELEASE_ASSERT(span == nullptr ||
|
|
|
|
(index_ >= 0 && index <= span_->Length()));
|
|
|
|
}
|
|
|
|
|
2020-05-28 13:05:38 +03:00
|
|
|
private:
|
|
|
|
// For whatever reason, the compiler doesn't like optimizing away the above
|
2020-05-30 12:55:27 +03:00
|
|
|
// MOZ_RELEASE_ASSERT when `span_iterator` is constructed for
|
|
|
|
// obviously-correct cases like `span.begin()` or `span.end()`. We provide
|
|
|
|
// this private constructor for such cases.
|
2020-05-28 13:05:38 +03:00
|
|
|
constexpr span_iterator(const SpanT* span, typename SpanT::index_type index,
|
2020-05-30 12:55:27 +03:00
|
|
|
SpanKnownBounds)
|
2020-05-28 13:05:38 +03:00
|
|
|
: span_(span), index_(index) {}
|
|
|
|
|
|
|
|
public:
|
2020-05-28 12:54:37 +03:00
|
|
|
// `other` is already correct by construction; we do not need to go through
|
|
|
|
// the release assert above. Put differently, this constructor is effectively
|
|
|
|
// a copy constructor and therefore needs no assertions.
|
2020-05-28 13:03:06 +03:00
|
|
|
friend class span_iterator<SpanT, true>;
|
|
|
|
constexpr MOZ_IMPLICIT span_iterator(const span_iterator<SpanT, false>& other)
|
2020-05-28 12:54:37 +03:00
|
|
|
: span_(other.span_), index_(other.index_) {}
|
2017-02-16 12:43:50 +03:00
|
|
|
|
2020-05-28 13:03:06 +03:00
|
|
|
constexpr span_iterator<SpanT, IsConst>& operator=(
|
|
|
|
const span_iterator<SpanT, IsConst>&) = default;
|
2017-02-16 12:43:50 +03:00
|
|
|
|
2018-04-05 15:55:00 +03:00
|
|
|
constexpr reference operator*() const {
|
2017-02-16 12:43:50 +03:00
|
|
|
MOZ_RELEASE_ASSERT(span_);
|
|
|
|
return (*span_)[index_];
|
|
|
|
}
|
|
|
|
|
2017-12-08 17:20:56 +03:00
|
|
|
constexpr pointer operator->() const {
|
2017-02-16 12:43:50 +03:00
|
|
|
MOZ_RELEASE_ASSERT(span_);
|
|
|
|
return &((*span_)[index_]);
|
|
|
|
}
|
|
|
|
|
2018-04-05 15:55:00 +03:00
|
|
|
constexpr span_iterator& operator++() {
|
2017-02-16 12:43:50 +03:00
|
|
|
++index_;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-12-08 17:20:56 +03:00
|
|
|
constexpr span_iterator operator++(int) {
|
2017-02-16 12:43:50 +03:00
|
|
|
auto ret = *this;
|
|
|
|
++(*this);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-04-05 15:55:00 +03:00
|
|
|
constexpr span_iterator& operator--() {
|
2017-02-16 12:43:50 +03:00
|
|
|
--index_;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-12-08 17:20:56 +03:00
|
|
|
constexpr span_iterator operator--(int) {
|
2017-02-16 12:43:50 +03:00
|
|
|
auto ret = *this;
|
|
|
|
--(*this);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr span_iterator operator+(difference_type n) const {
|
|
|
|
auto ret = *this;
|
|
|
|
return ret += n;
|
|
|
|
}
|
|
|
|
|
2018-04-05 15:55:00 +03:00
|
|
|
constexpr span_iterator& operator+=(difference_type n) {
|
2017-02-16 12:43:50 +03:00
|
|
|
MOZ_RELEASE_ASSERT(span_ && (index_ + n) >= 0 &&
|
|
|
|
(index_ + n) <= span_->Length());
|
|
|
|
index_ += n;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr span_iterator operator-(difference_type n) const {
|
|
|
|
auto ret = *this;
|
|
|
|
return ret -= n;
|
|
|
|
}
|
|
|
|
|
2020-04-29 19:04:56 +03:00
|
|
|
constexpr span_iterator& operator-=(difference_type n) { return *this += -n; }
|
2017-02-16 12:43:50 +03:00
|
|
|
|
|
|
|
constexpr difference_type operator-(const span_iterator& rhs) const {
|
|
|
|
MOZ_RELEASE_ASSERT(span_ == rhs.span_);
|
|
|
|
return index_ - rhs.index_;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr reference operator[](difference_type n) const {
|
|
|
|
return *(*this + n);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr friend bool operator==(const span_iterator& lhs,
|
|
|
|
const span_iterator& rhs) {
|
2020-07-02 16:46:01 +03:00
|
|
|
// Iterators from different spans are uncomparable. A diagnostic assertion
|
|
|
|
// should be enough to check this, though. To ensure that no iterators from
|
|
|
|
// different spans are ever considered equal, still compare them in release
|
|
|
|
// builds.
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(lhs.span_ == rhs.span_);
|
|
|
|
return lhs.index_ == rhs.index_ && lhs.span_ == rhs.span_;
|
2017-02-16 12:43:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr friend bool operator!=(const span_iterator& lhs,
|
|
|
|
const span_iterator& rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
2018-04-05 15:55:00 +03:00
|
|
|
constexpr friend bool operator<(const span_iterator& lhs,
|
2017-02-16 12:43:50 +03:00
|
|
|
const span_iterator& rhs) {
|
2020-07-02 16:46:01 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(lhs.span_ == rhs.span_);
|
2017-02-16 12:43:50 +03:00
|
|
|
return lhs.index_ < rhs.index_;
|
|
|
|
}
|
|
|
|
|
2017-12-08 17:20:56 +03:00
|
|
|
constexpr friend bool operator<=(const span_iterator& lhs,
|
2017-04-20 21:58:38 +03:00
|
|
|
const span_iterator& rhs) {
|
2017-02-16 12:43:50 +03:00
|
|
|
return !(rhs < lhs);
|
|
|
|
}
|
|
|
|
|
2017-12-08 17:20:56 +03:00
|
|
|
constexpr friend bool operator>(const span_iterator& lhs,
|
2017-04-20 21:58:38 +03:00
|
|
|
const span_iterator& rhs) {
|
2017-02-16 12:43:50 +03:00
|
|
|
return rhs < lhs;
|
|
|
|
}
|
|
|
|
|
2017-12-08 17:20:56 +03:00
|
|
|
constexpr friend bool operator>=(const span_iterator& lhs,
|
2017-04-20 21:58:38 +03:00
|
|
|
const span_iterator& rhs) {
|
2017-02-16 12:43:50 +03:00
|
|
|
return !(rhs > lhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void swap(span_iterator& rhs) {
|
|
|
|
std::swap(index_, rhs.index_);
|
|
|
|
std::swap(span_, rhs.span_);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2020-05-28 13:03:06 +03:00
|
|
|
const SpanT* span_;
|
2017-02-16 12:43:50 +03:00
|
|
|
size_t index_;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Span, bool IsConst>
|
|
|
|
inline constexpr span_iterator<Span, IsConst> operator+(
|
|
|
|
typename span_iterator<Span, IsConst>::difference_type n,
|
|
|
|
const span_iterator<Span, IsConst>& rhs) {
|
|
|
|
return rhs + n;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t Ext>
|
|
|
|
class extent_type {
|
|
|
|
public:
|
|
|
|
using index_type = size_t;
|
|
|
|
|
|
|
|
static_assert(Ext >= 0, "A fixed-size Span must be >= 0 in size.");
|
|
|
|
|
2020-02-13 14:20:27 +03:00
|
|
|
constexpr extent_type() = default;
|
2017-02-16 12:43:50 +03:00
|
|
|
|
|
|
|
template <index_type Other>
|
2018-04-05 15:55:00 +03:00
|
|
|
constexpr MOZ_IMPLICIT extent_type(extent_type<Other> ext) {
|
2017-02-16 12:43:50 +03:00
|
|
|
static_assert(
|
|
|
|
Other == Ext || Other == dynamic_extent,
|
|
|
|
"Mismatch between fixed-size extent and size of initializing data.");
|
|
|
|
MOZ_RELEASE_ASSERT(ext.size() == Ext);
|
|
|
|
}
|
|
|
|
|
2018-04-05 15:55:00 +03:00
|
|
|
constexpr MOZ_IMPLICIT extent_type(index_type length) {
|
2017-02-16 12:43:50 +03:00
|
|
|
MOZ_RELEASE_ASSERT(length == Ext);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr index_type size() const { return Ext; }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
class extent_type<dynamic_extent> {
|
|
|
|
public:
|
|
|
|
using index_type = size_t;
|
|
|
|
|
|
|
|
template <index_type Other>
|
|
|
|
explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size()) {}
|
|
|
|
|
|
|
|
explicit constexpr extent_type(index_type length) : size_(length) {}
|
|
|
|
|
|
|
|
constexpr index_type size() const { return size_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
index_type size_;
|
|
|
|
};
|
|
|
|
} // namespace span_details
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Span - slices for C++
|
|
|
|
*
|
|
|
|
* Span implements Rust's slice concept for C++. It's called "Span" instead of
|
|
|
|
* "Slice" to follow the naming used in C++ Core Guidelines.
|
|
|
|
*
|
|
|
|
* A Span wraps a pointer and a length that identify a non-owning view to a
|
|
|
|
* contiguous block of memory of objects of the same type. Various types,
|
|
|
|
* including (pre-decay) C arrays, XPCOM strings, nsTArray, mozilla::Array,
|
|
|
|
* mozilla::Range and contiguous standard-library containers, auto-convert
|
|
|
|
* into Spans when attempting to pass them as arguments to methods that take
|
2020-08-07 10:50:04 +03:00
|
|
|
* Spans. (Span itself autoconverts into mozilla::Range.)
|
2017-02-16 12:43:50 +03:00
|
|
|
*
|
|
|
|
* Like Rust's slices, Span provides safety against out-of-bounds access by
|
|
|
|
* performing run-time bound checks. However, unlike Rust's slices, Span
|
|
|
|
* cannot provide safety against use-after-free.
|
|
|
|
*
|
|
|
|
* (Note: Span is like Rust's slice only conceptually. Due to the lack of
|
|
|
|
* ABI guarantees, you should still decompose spans/slices to raw pointer
|
2017-06-12 12:09:46 +03:00
|
|
|
* and length parts when crossing the FFI. The Elements() and data() methods
|
|
|
|
* are guaranteed to return a non-null pointer even for zero-length spans,
|
|
|
|
* so the pointer can be used as a raw part of a Rust slice without further
|
|
|
|
* checks.)
|
2017-02-16 12:43:50 +03:00
|
|
|
*
|
2020-08-07 10:50:04 +03:00
|
|
|
* In addition to having constructors (with the support of deduction guides)
|
|
|
|
* that take various well-known types, a Span for an arbitrary type can be
|
|
|
|
* constructed from a pointer and a length or a pointer and another pointer
|
|
|
|
* pointing just past the last element.
|
2017-02-16 12:43:50 +03:00
|
|
|
*
|
2017-04-04 14:04:14 +03:00
|
|
|
* A Span<const char> or Span<const char16_t> can be obtained for const char*
|
|
|
|
* or const char16_t pointing to a zero-terminated string using the
|
2018-03-24 22:36:00 +03:00
|
|
|
* MakeStringSpan() function (which treats a nullptr argument equivalently
|
|
|
|
* to the empty string). Corresponding implicit constructor does not exist
|
2017-04-04 14:04:14 +03:00
|
|
|
* in order to avoid accidental construction in cases where const char* or
|
|
|
|
* const char16_t* do not point to a zero-terminated string.
|
2017-02-16 12:43:50 +03:00
|
|
|
*
|
|
|
|
* Span has methods that follow the Mozilla naming style and methods that
|
|
|
|
* don't. The methods that follow the Mozilla naming style are meant to be
|
|
|
|
* used directly from Mozilla code. The methods that don't are meant for
|
|
|
|
* integration with C++11 range-based loops and with meta-programming that
|
|
|
|
* expects the same methods that are found on the standard-library
|
|
|
|
* containers. For example, to decompose a Span into its parts in Mozilla
|
|
|
|
* code, use Elements() and Length() (as with nsTArray) instead of data()
|
|
|
|
* and size() (as with std::vector).
|
|
|
|
*
|
|
|
|
* The pointer and length wrapped by a Span cannot be changed after a Span has
|
|
|
|
* been created. When new values are required, simply create a new Span. Span
|
|
|
|
* has a method called Subspan() that works analogously to the Substring()
|
|
|
|
* method of XPCOM strings taking a start index and an optional length. As a
|
|
|
|
* Mozilla extension (relative to Microsoft's gsl::span that mozilla::Span is
|
|
|
|
* based on), Span has methods From(start), To(end) and FromTo(start, end)
|
|
|
|
* that correspond to Rust's &slice[start..], &slice[..end] and
|
|
|
|
* &slice[start..end], respectively. (That is, the end index is the index of
|
|
|
|
* the first element not to be included in the new subspan.)
|
|
|
|
*
|
|
|
|
* When indicating a Span that's only read from, const goes inside the type
|
|
|
|
* parameter. Don't put const in front of Span. That is:
|
|
|
|
* size_t ReadsFromOneSpanAndWritesToAnother(Span<const uint8_t> aReadFrom,
|
|
|
|
* Span<uint8_t> aWrittenTo);
|
|
|
|
*
|
|
|
|
* Any Span<const T> can be viewed as Span<const uint8_t> using the function
|
|
|
|
* AsBytes(). Any Span<T> can be viewed as Span<uint8_t> using the function
|
|
|
|
* AsWritableBytes().
|
2020-07-02 16:46:01 +03:00
|
|
|
*
|
|
|
|
* Note that iterators from different Span instances are uncomparable, even if
|
|
|
|
* they refer to the same memory. This also applies to any spans derived via
|
|
|
|
* Subspan etc.
|
2017-02-16 12:43:50 +03:00
|
|
|
*/
|
2019-12-07 00:02:28 +03:00
|
|
|
template <class ElementType, size_t Extent /* = dynamic_extent */>
|
2017-02-16 12:43:50 +03:00
|
|
|
class Span {
|
|
|
|
public:
|
|
|
|
// constants and types
|
|
|
|
using element_type = ElementType;
|
|
|
|
using index_type = size_t;
|
|
|
|
using pointer = element_type*;
|
|
|
|
using reference = element_type&;
|
|
|
|
|
|
|
|
using iterator =
|
|
|
|
span_details::span_iterator<Span<ElementType, Extent>, false>;
|
|
|
|
using const_iterator =
|
|
|
|
span_details::span_iterator<Span<ElementType, Extent>, true>;
|
|
|
|
using reverse_iterator = std::reverse_iterator<iterator>;
|
|
|
|
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
|
|
|
|
|
|
|
constexpr static const index_type extent = Extent;
|
|
|
|
|
|
|
|
// [Span.cons], Span constructors, copy, assignment, and destructor
|
2020-03-28 16:35:31 +03:00
|
|
|
// "Dependent" is needed to make "std::enable_if_t<(Dependent ||
|
2020-01-08 19:30:40 +03:00
|
|
|
// Extent == 0 || Extent == dynamic_extent)>" SFINAE,
|
|
|
|
// since
|
2020-03-28 16:35:31 +03:00
|
|
|
// "std::enable_if_t<(Extent == 0 || Extent == dynamic_extent)>" is
|
2020-01-08 19:30:40 +03:00
|
|
|
// ill-formed when Extent is neither of the extreme values.
|
2017-02-16 12:43:50 +03:00
|
|
|
/**
|
|
|
|
* Constructor with no args.
|
|
|
|
*/
|
|
|
|
template <bool Dependent = false,
|
2020-03-28 16:35:31 +03:00
|
|
|
class = std::enable_if_t<(Dependent || Extent == 0 ||
|
|
|
|
Extent == dynamic_extent)>>
|
2017-02-16 12:43:50 +03:00
|
|
|
constexpr Span() : storage_(nullptr, span_details::extent_type<0>()) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for nullptr.
|
|
|
|
*/
|
|
|
|
constexpr MOZ_IMPLICIT Span(std::nullptr_t) : Span() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for pointer and length.
|
|
|
|
*/
|
|
|
|
constexpr Span(pointer aPtr, index_type aLength) : storage_(aPtr, aLength) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for start pointer and pointer past end.
|
|
|
|
*/
|
|
|
|
constexpr Span(pointer aStartPtr, pointer aEndPtr)
|
|
|
|
: storage_(aStartPtr, std::distance(aStartPtr, aEndPtr)) {}
|
|
|
|
|
2020-04-29 18:20:50 +03:00
|
|
|
/**
|
|
|
|
* Constructor for pair of Span iterators.
|
|
|
|
*/
|
|
|
|
template <typename OtherElementType, size_t OtherExtent, bool IsConst>
|
|
|
|
constexpr Span(
|
|
|
|
span_details::span_iterator<Span<OtherElementType, OtherExtent>, IsConst>
|
|
|
|
aBegin,
|
|
|
|
span_details::span_iterator<Span<OtherElementType, OtherExtent>, IsConst>
|
|
|
|
aEnd)
|
|
|
|
: storage_(aBegin == aEnd ? nullptr : &*aBegin, aEnd - aBegin) {}
|
|
|
|
|
2017-02-16 12:43:50 +03:00
|
|
|
/**
|
|
|
|
* Constructor for C array.
|
|
|
|
*/
|
|
|
|
template <size_t N>
|
|
|
|
constexpr MOZ_IMPLICIT Span(element_type (&aArr)[N])
|
|
|
|
: storage_(&aArr[0], span_details::extent_type<N>()) {}
|
|
|
|
|
2017-09-20 10:38:07 +03:00
|
|
|
// Implicit constructors for char* and char16_t* pointers are deleted in order
|
|
|
|
// to avoid accidental construction in cases where a pointer does not point to
|
|
|
|
// a zero-terminated string. A Span<const char> or Span<const char16_t> can be
|
|
|
|
// obtained for const char* or const char16_t pointing to a zero-terminated
|
|
|
|
// string using the MakeStringSpan() function.
|
2020-08-07 10:42:52 +03:00
|
|
|
// (This must be a template because otherwise it will prevent the previous
|
|
|
|
// array constructor to match because an array decays to a pointer. This only
|
|
|
|
// exists to point to the above explanation, since there's no other
|
|
|
|
// constructor that would match.)
|
|
|
|
template <
|
|
|
|
typename T,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
std::is_pointer_v<T> &&
|
|
|
|
(std::is_same_v<std::remove_const_t<std::decay_t<T>>, char> ||
|
|
|
|
std::is_same_v<std::remove_const_t<std::decay_t<T>>, char16_t>)>>
|
|
|
|
Span(T& aStr) = delete;
|
2017-09-20 10:38:07 +03:00
|
|
|
|
2017-02-16 12:43:50 +03:00
|
|
|
/**
|
|
|
|
* Constructor for std::array.
|
|
|
|
*/
|
|
|
|
template <size_t N,
|
2020-03-28 16:57:19 +03:00
|
|
|
class ArrayElementType = std::remove_const_t<element_type>>
|
2017-02-16 12:43:50 +03:00
|
|
|
constexpr MOZ_IMPLICIT Span(std::array<ArrayElementType, N>& aArr)
|
|
|
|
: storage_(&aArr[0], span_details::extent_type<N>()) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for const std::array.
|
|
|
|
*/
|
|
|
|
template <size_t N>
|
|
|
|
constexpr MOZ_IMPLICIT Span(
|
2020-03-28 16:57:19 +03:00
|
|
|
const std::array<std::remove_const_t<element_type>, N>& aArr)
|
2017-02-16 12:43:50 +03:00
|
|
|
: storage_(&aArr[0], span_details::extent_type<N>()) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for mozilla::Array.
|
|
|
|
*/
|
|
|
|
template <size_t N,
|
2020-03-28 16:57:19 +03:00
|
|
|
class ArrayElementType = std::remove_const_t<element_type>>
|
2017-02-16 12:43:50 +03:00
|
|
|
constexpr MOZ_IMPLICIT Span(mozilla::Array<ArrayElementType, N>& aArr)
|
|
|
|
: storage_(&aArr[0], span_details::extent_type<N>()) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for const mozilla::Array.
|
|
|
|
*/
|
|
|
|
template <size_t N>
|
|
|
|
constexpr MOZ_IMPLICIT Span(
|
2020-03-28 16:57:19 +03:00
|
|
|
const mozilla::Array<std::remove_const_t<element_type>, N>& aArr)
|
2017-02-16 12:43:50 +03:00
|
|
|
: storage_(&aArr[0], span_details::extent_type<N>()) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for mozilla::UniquePtr holding an array and length.
|
|
|
|
*/
|
|
|
|
template <class ArrayElementType = std::add_pointer<element_type>>
|
|
|
|
constexpr Span(const mozilla::UniquePtr<ArrayElementType>& aPtr,
|
|
|
|
index_type aLength)
|
|
|
|
: storage_(aPtr.get(), aLength) {}
|
|
|
|
|
|
|
|
// NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the
|
|
|
|
// requirement on Container to be a contiguous sequence container.
|
|
|
|
/**
|
|
|
|
* Constructor for standard-library containers.
|
|
|
|
*/
|
2020-03-28 16:57:18 +03:00
|
|
|
template <
|
|
|
|
class Container,
|
2020-08-07 10:42:52 +03:00
|
|
|
class Dummy = std::enable_if_t<
|
|
|
|
!std::is_const_v<Container> &&
|
|
|
|
!span_details::is_span<Container>::value &&
|
|
|
|
!span_details::is_std_array<Container>::value &&
|
|
|
|
std::is_convertible_v<typename Container::pointer, pointer> &&
|
|
|
|
std::is_convertible_v<typename Container::pointer,
|
|
|
|
decltype(std::declval<Container>().data())>,
|
|
|
|
Container>>
|
|
|
|
constexpr MOZ_IMPLICIT Span(Container& cont, Dummy* = nullptr)
|
2017-02-16 12:43:50 +03:00
|
|
|
: Span(cont.data(), ReleaseAssertedCast<index_type>(cont.size())) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for standard-library containers (const version).
|
|
|
|
*/
|
2020-03-28 16:57:18 +03:00
|
|
|
template <
|
|
|
|
class Container,
|
|
|
|
class = std::enable_if_t<
|
|
|
|
std::is_const_v<element_type> &&
|
|
|
|
!span_details::is_span<Container>::value &&
|
|
|
|
std::is_convertible_v<typename Container::pointer, pointer> &&
|
|
|
|
std::is_convertible_v<typename Container::pointer,
|
|
|
|
decltype(std::declval<Container>().data())>>>
|
2017-02-16 12:43:50 +03:00
|
|
|
constexpr MOZ_IMPLICIT Span(const Container& cont)
|
|
|
|
: Span(cont.data(), ReleaseAssertedCast<index_type>(cont.size())) {}
|
|
|
|
|
2020-08-07 10:42:52 +03:00
|
|
|
// NB: the SFINAE here uses .Elements() as a incomplete/imperfect proxy for
|
|
|
|
// the requirement on Container to be a contiguous sequence container.
|
|
|
|
/**
|
|
|
|
* Constructor for contiguous Mozilla containers.
|
|
|
|
*/
|
|
|
|
template <
|
|
|
|
class Container,
|
|
|
|
class = std::enable_if_t<
|
|
|
|
!std::is_const_v<Container> &&
|
|
|
|
!span_details::is_span<Container>::value &&
|
|
|
|
!span_details::is_std_array<Container>::value &&
|
|
|
|
std::is_convertible_v<typename Container::elem_type*, pointer> &&
|
|
|
|
std::is_convertible_v<
|
|
|
|
typename Container::elem_type*,
|
|
|
|
decltype(std::declval<Container>().Elements())>>>
|
|
|
|
constexpr MOZ_IMPLICIT Span(Container& cont, void* = nullptr)
|
|
|
|
: Span(cont.Elements(), ReleaseAssertedCast<index_type>(cont.Length())) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for contiguous Mozilla containers (const version).
|
|
|
|
*/
|
|
|
|
template <
|
|
|
|
class Container,
|
|
|
|
class = std::enable_if_t<
|
|
|
|
std::is_const_v<element_type> &&
|
|
|
|
!span_details::is_span<Container>::value &&
|
|
|
|
std::is_convertible_v<typename Container::elem_type*, pointer> &&
|
|
|
|
std::is_convertible_v<
|
|
|
|
typename Container::elem_type*,
|
|
|
|
decltype(std::declval<Container>().Elements())>>>
|
|
|
|
constexpr MOZ_IMPLICIT Span(const Container& cont, void* = nullptr)
|
|
|
|
: Span(cont.Elements(), ReleaseAssertedCast<index_type>(cont.Length())) {}
|
|
|
|
|
2017-02-16 12:43:50 +03:00
|
|
|
/**
|
|
|
|
* Constructor from other Span.
|
|
|
|
*/
|
|
|
|
constexpr Span(const Span& other) = default;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor from other Span.
|
|
|
|
*/
|
|
|
|
constexpr Span(Span&& other) = default;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor from other Span with conversion of element type.
|
|
|
|
*/
|
2020-03-28 16:35:31 +03:00
|
|
|
template <
|
|
|
|
class OtherElementType, size_t OtherExtent,
|
|
|
|
class = std::enable_if_t<span_details::is_allowed_extent_conversion<
|
|
|
|
OtherExtent, Extent>::value &&
|
|
|
|
span_details::is_allowed_element_type_conversion<
|
|
|
|
OtherElementType, element_type>::value>>
|
2017-02-16 12:43:50 +03:00
|
|
|
constexpr MOZ_IMPLICIT Span(const Span<OtherElementType, OtherExtent>& other)
|
|
|
|
: storage_(other.data(),
|
|
|
|
span_details::extent_type<OtherExtent>(other.size())) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor from other Span with conversion of element type.
|
|
|
|
*/
|
2020-03-28 16:35:31 +03:00
|
|
|
template <
|
|
|
|
class OtherElementType, size_t OtherExtent,
|
|
|
|
class = std::enable_if_t<span_details::is_allowed_extent_conversion<
|
|
|
|
OtherExtent, Extent>::value &&
|
|
|
|
span_details::is_allowed_element_type_conversion<
|
|
|
|
OtherElementType, element_type>::value>>
|
2017-02-16 12:43:50 +03:00
|
|
|
constexpr MOZ_IMPLICIT Span(Span<OtherElementType, OtherExtent>&& other)
|
|
|
|
: storage_(other.data(),
|
|
|
|
span_details::extent_type<OtherExtent>(other.size())) {}
|
|
|
|
|
|
|
|
~Span() = default;
|
2018-04-05 15:55:00 +03:00
|
|
|
constexpr Span& operator=(const Span& other) = default;
|
2017-02-16 12:43:50 +03:00
|
|
|
|
2018-04-05 15:55:00 +03:00
|
|
|
constexpr Span& operator=(Span&& other) = default;
|
2017-02-16 12:43:50 +03:00
|
|
|
|
|
|
|
// [Span.sub], Span subviews
|
|
|
|
/**
|
|
|
|
* Subspan with first N elements with compile-time N.
|
|
|
|
*/
|
|
|
|
template <size_t Count>
|
2017-12-08 17:20:56 +03:00
|
|
|
constexpr Span<element_type, Count> First() const {
|
2017-02-16 12:43:50 +03:00
|
|
|
MOZ_RELEASE_ASSERT(Count <= size());
|
|
|
|
return {data(), Count};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subspan with last N elements with compile-time N.
|
|
|
|
*/
|
|
|
|
template <size_t Count>
|
2017-12-08 17:20:56 +03:00
|
|
|
constexpr Span<element_type, Count> Last() const {
|
2017-09-19 23:18:38 +03:00
|
|
|
const size_t len = size();
|
|
|
|
MOZ_RELEASE_ASSERT(Count <= len);
|
|
|
|
return {data() + (len - Count), Count};
|
2017-02-16 12:43:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subspan with compile-time start index and length.
|
|
|
|
*/
|
|
|
|
template <size_t Offset, size_t Count = dynamic_extent>
|
2017-12-08 17:20:56 +03:00
|
|
|
constexpr Span<element_type, Count> Subspan() const {
|
2017-09-19 23:18:38 +03:00
|
|
|
const size_t len = size();
|
|
|
|
MOZ_RELEASE_ASSERT(Offset <= len &&
|
|
|
|
(Count == dynamic_extent || (Offset + Count <= len)));
|
|
|
|
return {data() + Offset, Count == dynamic_extent ? len - Offset : Count};
|
2017-02-16 12:43:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subspan with first N elements with run-time N.
|
|
|
|
*/
|
2017-12-08 17:20:56 +03:00
|
|
|
constexpr Span<element_type, dynamic_extent> First(index_type aCount) const {
|
2017-02-16 12:43:50 +03:00
|
|
|
MOZ_RELEASE_ASSERT(aCount <= size());
|
|
|
|
return {data(), aCount};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subspan with last N elements with run-time N.
|
|
|
|
*/
|
2017-12-08 17:20:56 +03:00
|
|
|
constexpr Span<element_type, dynamic_extent> Last(index_type aCount) const {
|
2017-09-19 23:18:38 +03:00
|
|
|
const size_t len = size();
|
|
|
|
MOZ_RELEASE_ASSERT(aCount <= len);
|
|
|
|
return {data() + (len - aCount), aCount};
|
2017-02-16 12:43:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subspan with run-time start index and length.
|
|
|
|
*/
|
2017-12-08 17:20:56 +03:00
|
|
|
constexpr Span<element_type, dynamic_extent> Subspan(
|
2017-02-16 12:43:50 +03:00
|
|
|
index_type aStart, index_type aLength = dynamic_extent) const {
|
2017-09-19 23:18:38 +03:00
|
|
|
const size_t len = size();
|
|
|
|
MOZ_RELEASE_ASSERT(aStart <= len && (aLength == dynamic_extent ||
|
|
|
|
(aStart + aLength <= len)));
|
2017-02-16 12:43:50 +03:00
|
|
|
return {data() + aStart,
|
2017-09-19 23:18:38 +03:00
|
|
|
aLength == dynamic_extent ? len - aStart : aLength};
|
2017-02-16 12:43:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subspan with run-time start index. (Rust's &foo[start..])
|
|
|
|
*/
|
2017-12-08 17:20:56 +03:00
|
|
|
constexpr Span<element_type, dynamic_extent> From(index_type aStart) const {
|
2017-02-16 12:43:50 +03:00
|
|
|
return Subspan(aStart);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subspan with run-time exclusive end index. (Rust's &foo[..end])
|
|
|
|
*/
|
2017-12-08 17:20:56 +03:00
|
|
|
constexpr Span<element_type, dynamic_extent> To(index_type aEnd) const {
|
2017-02-16 12:43:50 +03:00
|
|
|
return Subspan(0, aEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subspan with run-time start index and exclusive end index.
|
|
|
|
* (Rust's &foo[start..end])
|
|
|
|
*/
|
2017-12-08 17:20:56 +03:00
|
|
|
constexpr Span<element_type, dynamic_extent> FromTo(index_type aStart,
|
2017-02-16 12:43:50 +03:00
|
|
|
index_type aEnd) const {
|
|
|
|
MOZ_RELEASE_ASSERT(aStart <= aEnd);
|
|
|
|
return Subspan(aStart, aEnd - aStart);
|
|
|
|
}
|
|
|
|
|
|
|
|
// [Span.obs], Span observers
|
|
|
|
/**
|
|
|
|
* Number of elements in the span.
|
|
|
|
*/
|
|
|
|
constexpr index_type Length() const { return size(); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of elements in the span (standard-libray duck typing version).
|
|
|
|
*/
|
|
|
|
constexpr index_type size() const { return storage_.size(); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Size of the span in bytes.
|
|
|
|
*/
|
|
|
|
constexpr index_type LengthBytes() const { return size_bytes(); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Size of the span in bytes (standard-library naming style version).
|
|
|
|
*/
|
|
|
|
constexpr index_type size_bytes() const {
|
|
|
|
return size() * narrow_cast<index_type>(sizeof(element_type));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the the length of the span is zero.
|
|
|
|
*/
|
|
|
|
constexpr bool IsEmpty() const { return empty(); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the the length of the span is zero (standard-libray duck
|
|
|
|
* typing version).
|
|
|
|
*/
|
|
|
|
constexpr bool empty() const { return size() == 0; }
|
|
|
|
|
|
|
|
// [Span.elem], Span element access
|
2017-12-08 17:20:56 +03:00
|
|
|
constexpr reference operator[](index_type idx) const {
|
2017-02-16 12:43:50 +03:00
|
|
|
MOZ_RELEASE_ASSERT(idx < storage_.size());
|
|
|
|
return data()[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Access element of span by index (standard-library duck typing version).
|
|
|
|
*/
|
|
|
|
constexpr reference at(index_type idx) const { return this->operator[](idx); }
|
|
|
|
|
|
|
|
constexpr reference operator()(index_type idx) const {
|
|
|
|
return this->operator[](idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-12 12:09:46 +03:00
|
|
|
* Pointer to the first element of the span. The return value is never
|
|
|
|
* nullptr, not ever for zero-length spans, so it can be passed as-is
|
|
|
|
* to std::slice::from_raw_parts() in Rust.
|
2017-02-16 12:43:50 +03:00
|
|
|
*/
|
|
|
|
constexpr pointer Elements() const { return data(); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pointer to the first element of the span (standard-libray duck typing
|
2017-06-12 12:09:46 +03:00
|
|
|
* version). The return value is never nullptr, not ever for zero-length
|
|
|
|
* spans, so it can be passed as-is to std::slice::from_raw_parts() in Rust.
|
2017-02-16 12:43:50 +03:00
|
|
|
*/
|
|
|
|
constexpr pointer data() const { return storage_.data(); }
|
|
|
|
|
|
|
|
// [Span.iter], Span iterator support
|
2020-05-28 13:05:38 +03:00
|
|
|
iterator begin() const { return {this, 0, span_details::SpanKnownBounds{}}; }
|
2020-05-30 12:55:27 +03:00
|
|
|
iterator end() const {
|
|
|
|
return {this, Length(), span_details::SpanKnownBounds{}};
|
|
|
|
}
|
2017-02-16 12:43:50 +03:00
|
|
|
|
2020-05-30 12:55:27 +03:00
|
|
|
const_iterator cbegin() const {
|
|
|
|
return {this, 0, span_details::SpanKnownBounds{}};
|
|
|
|
}
|
|
|
|
const_iterator cend() const {
|
|
|
|
return {this, Length(), span_details::SpanKnownBounds{}};
|
|
|
|
}
|
2017-02-16 12:43:50 +03:00
|
|
|
|
|
|
|
reverse_iterator rbegin() const { return reverse_iterator{end()}; }
|
|
|
|
reverse_iterator rend() const { return reverse_iterator{begin()}; }
|
|
|
|
|
|
|
|
const_reverse_iterator crbegin() const {
|
|
|
|
return const_reverse_iterator{cend()};
|
|
|
|
}
|
|
|
|
const_reverse_iterator crend() const {
|
|
|
|
return const_reverse_iterator{cbegin()};
|
|
|
|
}
|
|
|
|
|
2020-04-27 14:16:30 +03:00
|
|
|
template <size_t SplitPoint>
|
|
|
|
constexpr std::pair<Span<ElementType, SplitPoint>,
|
|
|
|
Span<ElementType, Extent - SplitPoint>>
|
|
|
|
SplitAt() const {
|
|
|
|
static_assert(Extent != dynamic_extent);
|
|
|
|
static_assert(SplitPoint <= Extent);
|
|
|
|
return {First<SplitPoint>(), Last<Extent - SplitPoint>()};
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr std::pair<Span<ElementType, dynamic_extent>,
|
|
|
|
Span<ElementType, dynamic_extent>>
|
|
|
|
SplitAt(const index_type aSplitPoint) const {
|
|
|
|
MOZ_RELEASE_ASSERT(aSplitPoint <= Length());
|
|
|
|
return {First(aSplitPoint), Last(Length() - aSplitPoint)};
|
|
|
|
}
|
|
|
|
|
2020-05-11 12:27:40 +03:00
|
|
|
constexpr Span<std::add_const_t<ElementType>, Extent> AsConst() const {
|
|
|
|
return {Elements(), Length()};
|
|
|
|
}
|
|
|
|
|
2017-02-16 12:43:50 +03:00
|
|
|
private:
|
|
|
|
// this implementation detail class lets us take advantage of the
|
|
|
|
// empty base class optimization to pay for only storage of a single
|
|
|
|
// pointer in the case of fixed-size Spans
|
|
|
|
template <class ExtentType>
|
|
|
|
class storage_type : public ExtentType {
|
|
|
|
public:
|
|
|
|
template <class OtherExtentType>
|
2018-04-05 15:55:00 +03:00
|
|
|
constexpr storage_type(pointer elements, OtherExtentType ext)
|
2017-02-16 12:43:50 +03:00
|
|
|
: ExtentType(ext)
|
2018-06-04 12:59:46 +03:00
|
|
|
// Replace nullptr with aligned bogus pointer for Rust slice
|
|
|
|
// compatibility. See
|
2017-06-12 12:09:46 +03:00
|
|
|
// https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html
|
2018-06-04 12:59:46 +03:00
|
|
|
,
|
|
|
|
data_(elements ? elements
|
|
|
|
: reinterpret_cast<pointer>(alignof(element_type))) {
|
2017-09-19 23:18:38 +03:00
|
|
|
const size_t extentSize = ExtentType::size();
|
2020-01-08 19:30:40 +03:00
|
|
|
MOZ_RELEASE_ASSERT((!elements && extentSize == 0) ||
|
|
|
|
(elements && extentSize != dynamic_extent));
|
2017-02-16 12:43:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr pointer data() const { return data_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
pointer data_;
|
|
|
|
};
|
|
|
|
|
|
|
|
storage_type<span_details::extent_type<Extent>> storage_;
|
|
|
|
};
|
|
|
|
|
2020-04-29 18:20:50 +03:00
|
|
|
template <typename T, size_t OtherExtent, bool IsConst>
|
|
|
|
Span(span_details::span_iterator<Span<T, OtherExtent>, IsConst> aBegin,
|
|
|
|
span_details::span_iterator<Span<T, OtherExtent>, IsConst> aEnd)
|
|
|
|
-> Span<std::conditional_t<IsConst, std::add_const_t<T>, T>>;
|
|
|
|
|
2020-04-27 19:46:57 +03:00
|
|
|
template <typename T, size_t Extent>
|
2020-08-07 10:42:52 +03:00
|
|
|
Span(T (&)[Extent]) -> Span<T, Extent>;
|
2020-04-27 19:46:57 +03:00
|
|
|
|
2020-04-29 19:04:56 +03:00
|
|
|
template <class Container>
|
|
|
|
Span(Container&) -> Span<typename Container::value_type>;
|
|
|
|
|
|
|
|
template <class Container>
|
|
|
|
Span(const Container&) -> Span<const typename Container::value_type>;
|
|
|
|
|
2020-08-07 10:42:52 +03:00
|
|
|
template <typename T, size_t Extent>
|
|
|
|
Span(mozilla::Array<T, Extent>&) -> Span<T, Extent>;
|
|
|
|
|
|
|
|
template <typename T, size_t Extent>
|
|
|
|
Span(const mozilla::Array<T, Extent>&) -> Span<const T, Extent>;
|
|
|
|
|
2017-02-16 12:43:50 +03:00
|
|
|
// [Span.comparison], Span comparison operators
|
|
|
|
template <class ElementType, size_t FirstExtent, size_t SecondExtent>
|
|
|
|
inline constexpr bool operator==(const Span<ElementType, FirstExtent>& l,
|
|
|
|
const Span<ElementType, SecondExtent>& r) {
|
2019-10-04 05:08:11 +03:00
|
|
|
return (l.size() == r.size()) &&
|
|
|
|
std::equal(l.data(), l.data() + l.size(), r.data());
|
2017-02-16 12:43:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ElementType, size_t Extent>
|
|
|
|
inline constexpr bool operator!=(const Span<ElementType, Extent>& l,
|
|
|
|
const Span<ElementType, Extent>& r) {
|
|
|
|
return !(l == r);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ElementType, size_t Extent>
|
|
|
|
inline constexpr bool operator<(const Span<ElementType, Extent>& l,
|
|
|
|
const Span<ElementType, Extent>& r) {
|
2019-10-04 05:08:11 +03:00
|
|
|
return std::lexicographical_compare(l.data(), l.data() + l.size(), r.data(),
|
|
|
|
r.data() + r.size());
|
2017-02-16 12:43:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ElementType, size_t Extent>
|
|
|
|
inline constexpr bool operator<=(const Span<ElementType, Extent>& l,
|
|
|
|
const Span<ElementType, Extent>& r) {
|
|
|
|
return !(l > r);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ElementType, size_t Extent>
|
|
|
|
inline constexpr bool operator>(const Span<ElementType, Extent>& l,
|
|
|
|
const Span<ElementType, Extent>& r) {
|
|
|
|
return r < l;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ElementType, size_t Extent>
|
|
|
|
inline constexpr bool operator>=(const Span<ElementType, Extent>& l,
|
|
|
|
const Span<ElementType, Extent>& r) {
|
|
|
|
return !(l < r);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace span_details {
|
|
|
|
// if we only supported compilers with good constexpr support then
|
|
|
|
// this pair of classes could collapse down to a constexpr function
|
|
|
|
|
|
|
|
// we should use a narrow_cast<> to go to size_t, but older compilers may not
|
|
|
|
// see it as constexpr and so will fail compilation of the template
|
|
|
|
template <class ElementType, size_t Extent>
|
|
|
|
struct calculate_byte_size
|
2020-03-28 16:57:19 +03:00
|
|
|
: std::integral_constant<size_t,
|
|
|
|
static_cast<size_t>(sizeof(ElementType) *
|
|
|
|
static_cast<size_t>(Extent))> {
|
|
|
|
};
|
2017-02-16 12:43:50 +03:00
|
|
|
|
|
|
|
template <class ElementType>
|
|
|
|
struct calculate_byte_size<ElementType, dynamic_extent>
|
2020-03-28 16:57:19 +03:00
|
|
|
: std::integral_constant<size_t, dynamic_extent> {};
|
2017-02-16 12:43:50 +03:00
|
|
|
} // namespace span_details
|
|
|
|
|
|
|
|
// [Span.objectrep], views of object representation
|
|
|
|
/**
|
|
|
|
* View span as Span<const uint8_t>.
|
|
|
|
*/
|
|
|
|
template <class ElementType, size_t Extent>
|
|
|
|
Span<const uint8_t,
|
|
|
|
span_details::calculate_byte_size<ElementType, Extent>::value>
|
|
|
|
AsBytes(Span<ElementType, Extent> s) {
|
|
|
|
return {reinterpret_cast<const uint8_t*>(s.data()), s.size_bytes()};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* View span as Span<uint8_t>.
|
|
|
|
*/
|
2020-03-28 16:57:14 +03:00
|
|
|
template <class ElementType, size_t Extent,
|
2020-03-28 16:35:31 +03:00
|
|
|
class = std::enable_if_t<!std::is_const_v<ElementType>>>
|
2017-02-16 12:43:50 +03:00
|
|
|
Span<uint8_t, span_details::calculate_byte_size<ElementType, Extent>::value>
|
|
|
|
AsWritableBytes(Span<ElementType, Extent> s) {
|
|
|
|
return {reinterpret_cast<uint8_t*>(s.data()), s.size_bytes()};
|
|
|
|
}
|
|
|
|
|
2019-08-30 16:16:19 +03:00
|
|
|
/**
|
|
|
|
* View a span of uint8_t as a span of char.
|
|
|
|
*/
|
|
|
|
inline Span<const char> AsChars(Span<const uint8_t> s) {
|
|
|
|
return {reinterpret_cast<const char*>(s.data()), s.size()};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* View a writable span of uint8_t as a span of char.
|
|
|
|
*/
|
|
|
|
inline Span<char> AsWritableChars(Span<uint8_t> s) {
|
|
|
|
return {reinterpret_cast<char*>(s.data()), s.size()};
|
|
|
|
}
|
|
|
|
|
2017-02-16 12:43:50 +03:00
|
|
|
/**
|
2018-03-24 22:36:00 +03:00
|
|
|
* Create span from a zero-terminated C string. nullptr is
|
|
|
|
* treated as the empty string.
|
2017-02-16 12:43:50 +03:00
|
|
|
*/
|
2017-04-04 14:04:14 +03:00
|
|
|
inline Span<const char> MakeStringSpan(const char* aZeroTerminated) {
|
2018-03-24 22:36:00 +03:00
|
|
|
if (!aZeroTerminated) {
|
|
|
|
return Span<const char>();
|
|
|
|
}
|
2017-04-04 14:04:14 +03:00
|
|
|
return Span<const char>(aZeroTerminated, std::strlen(aZeroTerminated));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-24 22:36:00 +03:00
|
|
|
* Create span from a zero-terminated UTF-16 C string. nullptr is
|
|
|
|
* treated as the empty string.
|
2017-04-04 14:04:14 +03:00
|
|
|
*/
|
|
|
|
inline Span<const char16_t> MakeStringSpan(const char16_t* aZeroTerminated) {
|
2018-03-24 22:36:00 +03:00
|
|
|
if (!aZeroTerminated) {
|
|
|
|
return Span<const char16_t>();
|
|
|
|
}
|
2017-04-04 14:04:14 +03:00
|
|
|
return Span<const char16_t>(aZeroTerminated,
|
|
|
|
span_details::strlen16(aZeroTerminated));
|
2017-02-16 12:43:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_Span_h
|