diff --git a/AK/ByteBuffer.h b/AK/ByteBuffer.h index 9772e2091fb..db528f95c09 100644 --- a/AK/ByteBuffer.h +++ b/AK/ByteBuffer.h @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -71,6 +72,9 @@ public: u8* data() { return m_data; } const u8* data() const { return m_data; } + Bytes span() { return { data(), size() }; } + ReadonlyBytes span() const { return { data(), size() }; } + u8* offset_pointer(int offset) { return m_data + offset; } const u8* offset_pointer(int offset) const { return m_data + offset; } @@ -93,10 +97,10 @@ private: Wrap, Adopt }; - explicit ByteBufferImpl(size_t); // For ConstructionMode=Uninitialized + explicit ByteBufferImpl(size_t); // For ConstructionMode=Uninitialized ByteBufferImpl(const void*, size_t, ConstructionMode); // For ConstructionMode=Copy - ByteBufferImpl(void*, size_t, ConstructionMode); // For ConstructionMode=Wrap/Adopt - ByteBufferImpl() {} + ByteBufferImpl(void*, size_t, ConstructionMode); // For ConstructionMode=Wrap/Adopt + ByteBufferImpl() { } u8* m_data { nullptr }; size_t m_size { 0 }; @@ -105,8 +109,8 @@ private: class ByteBuffer { public: - ByteBuffer() {} - ByteBuffer(std::nullptr_t) {} + ByteBuffer() { } + ByteBuffer(std::nullptr_t) { } ByteBuffer(const ByteBuffer& other) : m_impl(other.m_impl) { @@ -158,6 +162,9 @@ public: u8* data() { return m_impl ? m_impl->data() : nullptr; } const u8* data() const { return m_impl ? m_impl->data() : nullptr; } + Bytes span() { return m_impl ? m_impl->span() : nullptr; } + ReadonlyBytes span() const { return m_impl ? m_impl->span() : nullptr; } + u8* offset_pointer(int offset) { return m_impl ? m_impl->offset_pointer(offset) : nullptr; } const u8* offset_pointer(int offset) const { return m_impl ? m_impl->offset_pointer(offset) : nullptr; } diff --git a/AK/String.h b/AK/String.h index 8362f4fefaa..1d04b9fafa3 100644 --- a/AK/String.h +++ b/AK/String.h @@ -137,6 +137,9 @@ public: ALWAYS_INLINE bool is_empty() const { return length() == 0; } ALWAYS_INLINE size_t length() const { return m_impl ? m_impl->length() : 0; } ALWAYS_INLINE const char* characters() const { return m_impl ? m_impl->characters() : nullptr; } + + ALWAYS_INLINE ReadonlyBytes bytes() const { return m_impl ? m_impl->bytes() : nullptr; } + ALWAYS_INLINE const char& operator[](size_t i) const { return (*m_impl)[i]; diff --git a/AK/StringImpl.h b/AK/StringImpl.h index 5434a7e17c5..721144e68b9 100644 --- a/AK/StringImpl.h +++ b/AK/StringImpl.h @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -58,6 +59,9 @@ public: size_t length() const { return m_length; } const char* characters() const { return &m_inline_buffer[0]; } + + ALWAYS_INLINE ReadonlyBytes bytes() const { return { characters(), length() }; } + const char& operator[](size_t i) const { ASSERT(i < m_length); diff --git a/AK/StringView.h b/AK/StringView.h index 35c99e69f55..c0d65a511bb 100644 --- a/AK/StringView.h +++ b/AK/StringView.h @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -63,8 +64,12 @@ public: bool is_null() const { return !m_characters; } bool is_empty() const { return m_length == 0; } + const char* characters_without_null_termination() const { return m_characters; } size_t length() const { return m_length; } + + ReadonlyBytes bytes() const { return { m_characters, m_length }; } + const char& operator[](size_t index) const { return m_characters[index]; } ConstIterator begin() const { return characters_without_null_termination(); }