summaryrefslogtreecommitdiff
path: root/AK/Span.h
diff options
context:
space:
mode:
authorasynts <asynts@gmail.com>2020-08-18 18:02:04 +0200
committerAndreas Kling <kling@serenityos.org>2020-08-20 16:28:31 +0200
commitdf21487794d940bc22e3da9b011a99db43d44693 (patch)
treef2abe8e5479344731562682a2cd04da6e4040694 /AK/Span.h
parent413db2d6d5a49400d9587c9aa37bedd83968a6dd (diff)
downloadserenity-df21487794d940bc22e3da9b011a99db43d44693.zip
AK: Span: Allow slicing with zero length.
Previously, the following would not work: Bytes{}.slice(0); because it was asserted that `start < size()`.
Diffstat (limited to 'AK/Span.h')
-rw-r--r--AK/Span.h16
1 files changed, 10 insertions, 6 deletions
diff --git a/AK/Span.h b/AK/Span.h
index ec62904313..e6b0d74f61 100644
--- a/AK/Span.h
+++ b/AK/Span.h
@@ -142,16 +142,20 @@ public:
ALWAYS_INLINE bool is_empty() const { return this->m_size == 0; }
- ALWAYS_INLINE Span slice(size_t start, size_t size) const
+ ALWAYS_INLINE Span slice(size_t start, size_t length) const
{
- ASSERT(start + size <= this->m_size);
- return { this->m_values + start, size };
+ ASSERT(start + length <= size());
+ return { this->m_values + start, length };
}
-
ALWAYS_INLINE Span slice(size_t start) const
{
- ASSERT(start < this->m_size);
- return { this->m_values + start, this->m_size - start };
+ ASSERT(start <= size());
+ return { this->m_values + start, size() - start };
+ }
+
+ ALWAYS_INLINE Span trim(size_t length) const
+ {
+ return { this->m_values, min(size(), length) };
}
ALWAYS_INLINE T* offset(size_t start) const