SkSpan cleanup

* Check index in operator[]
* Use begin/end in const versions
* Remove returning SkSpan{} when empty for first, last, and subspan.

Change-Id: I0348ff8effb01e9175c5afaa52ddb0724f86218d
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/303259
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Herb Derby <herb@google.com>
This commit is contained in:
Herb Derby 2020-07-16 09:58:19 -04:00 committed by Skia Commit-Bot
parent c47778631e
commit fee89ccefd

View File

@ -24,13 +24,16 @@ public:
fSize = that.fSize;
return *this;
}
constexpr T& operator [] (size_t i) const { return fPtr[i]; }
constexpr T& operator [] (size_t i) const {
SkASSERT(i < this->size());
return fPtr[i];
}
constexpr T& front() const { return fPtr[0]; }
constexpr T& back() const { return fPtr[fSize - 1]; }
constexpr T* begin() const { return fPtr; }
constexpr T* end() const { return fPtr + fSize; }
constexpr const T* cbegin() const { return fPtr; }
constexpr const T* cend() const { return fPtr + fSize; }
constexpr const T* cbegin() const { return this->begin(); }
constexpr const T* cend() const { return this->end(); }
constexpr auto rbegin() const { return std::make_reverse_iterator(this->end()); }
constexpr auto rend() const { return std::make_reverse_iterator(this->begin()); }
constexpr auto crbegin() const { return std::make_reverse_iterator(this->cend()); }
@ -42,18 +45,15 @@ public:
constexpr size_t size_bytes() const { return fSize * sizeof(T); }
constexpr SkSpan<T> first(size_t prefixLen) const {
SkASSERT(prefixLen <= this->size());
if (prefixLen == 0) { return SkSpan{}; }
return SkSpan{fPtr, prefixLen};
}
constexpr SkSpan<T> last(size_t postfixLen) const {
SkASSERT(postfixLen <= this->size());
if (postfixLen == 0) { return SkSpan{}; }
return SkSpan{fPtr + (this->size() - postfixLen), postfixLen};
}
constexpr SkSpan<T> subspan(size_t offset, size_t count) const {
SkASSERT(offset <= this->size());
SkASSERT(count <= this->size() - offset);
if (count == 0) { return SkSpan{}; }
return SkSpan{fPtr + offset, count};
}