mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 09:00:22 +00:00
AK: Add span() / bytes() methods to container types.
This commit is contained in:
parent
c42450786c
commit
a922abd9d7
Notes:
sideshowbarker
2024-07-19 04:32:52 +09:00
Author: https://github.com/asynts Commit: https://github.com/SerenityOS/serenity/commit/a922abd9d7e Pull-request: https://github.com/SerenityOS/serenity/pull/2903
4 changed files with 24 additions and 5 deletions
|
@ -29,6 +29,7 @@
|
||||||
#include <AK/NonnullRefPtr.h>
|
#include <AK/NonnullRefPtr.h>
|
||||||
#include <AK/RefCounted.h>
|
#include <AK/RefCounted.h>
|
||||||
#include <AK/RefPtr.h>
|
#include <AK/RefPtr.h>
|
||||||
|
#include <AK/Span.h>
|
||||||
#include <AK/StdLibExtras.h>
|
#include <AK/StdLibExtras.h>
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
#include <AK/kmalloc.h>
|
#include <AK/kmalloc.h>
|
||||||
|
@ -71,6 +72,9 @@ public:
|
||||||
u8* data() { return m_data; }
|
u8* data() { return m_data; }
|
||||||
const u8* data() const { 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; }
|
u8* offset_pointer(int offset) { return m_data + offset; }
|
||||||
const u8* offset_pointer(int offset) const { return m_data + offset; }
|
const u8* offset_pointer(int offset) const { return m_data + offset; }
|
||||||
|
|
||||||
|
@ -93,10 +97,10 @@ private:
|
||||||
Wrap,
|
Wrap,
|
||||||
Adopt
|
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(const void*, size_t, ConstructionMode); // For ConstructionMode=Copy
|
||||||
ByteBufferImpl(void*, size_t, ConstructionMode); // For ConstructionMode=Wrap/Adopt
|
ByteBufferImpl(void*, size_t, ConstructionMode); // For ConstructionMode=Wrap/Adopt
|
||||||
ByteBufferImpl() {}
|
ByteBufferImpl() { }
|
||||||
|
|
||||||
u8* m_data { nullptr };
|
u8* m_data { nullptr };
|
||||||
size_t m_size { 0 };
|
size_t m_size { 0 };
|
||||||
|
@ -105,8 +109,8 @@ private:
|
||||||
|
|
||||||
class ByteBuffer {
|
class ByteBuffer {
|
||||||
public:
|
public:
|
||||||
ByteBuffer() {}
|
ByteBuffer() { }
|
||||||
ByteBuffer(std::nullptr_t) {}
|
ByteBuffer(std::nullptr_t) { }
|
||||||
ByteBuffer(const ByteBuffer& other)
|
ByteBuffer(const ByteBuffer& other)
|
||||||
: m_impl(other.m_impl)
|
: m_impl(other.m_impl)
|
||||||
{
|
{
|
||||||
|
@ -158,6 +162,9 @@ public:
|
||||||
u8* data() { return m_impl ? m_impl->data() : nullptr; }
|
u8* data() { return m_impl ? m_impl->data() : nullptr; }
|
||||||
const u8* data() const { 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; }
|
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; }
|
const u8* offset_pointer(int offset) const { return m_impl ? m_impl->offset_pointer(offset) : nullptr; }
|
||||||
|
|
||||||
|
|
|
@ -137,6 +137,9 @@ public:
|
||||||
ALWAYS_INLINE bool is_empty() const { return length() == 0; }
|
ALWAYS_INLINE bool is_empty() const { return length() == 0; }
|
||||||
ALWAYS_INLINE size_t length() const { return m_impl ? m_impl->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 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
|
ALWAYS_INLINE const char& operator[](size_t i) const
|
||||||
{
|
{
|
||||||
return (*m_impl)[i];
|
return (*m_impl)[i];
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
#include <AK/Badge.h>
|
#include <AK/Badge.h>
|
||||||
#include <AK/RefCounted.h>
|
#include <AK/RefCounted.h>
|
||||||
#include <AK/RefPtr.h>
|
#include <AK/RefPtr.h>
|
||||||
|
#include <AK/Span.h>
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
#include <AK/kmalloc.h>
|
#include <AK/kmalloc.h>
|
||||||
|
|
||||||
|
@ -58,6 +59,9 @@ public:
|
||||||
|
|
||||||
size_t length() const { return m_length; }
|
size_t length() const { return m_length; }
|
||||||
const char* characters() const { return &m_inline_buffer[0]; }
|
const char* characters() const { return &m_inline_buffer[0]; }
|
||||||
|
|
||||||
|
ALWAYS_INLINE ReadonlyBytes bytes() const { return { characters(), length() }; }
|
||||||
|
|
||||||
const char& operator[](size_t i) const
|
const char& operator[](size_t i) const
|
||||||
{
|
{
|
||||||
ASSERT(i < m_length);
|
ASSERT(i < m_length);
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
#include <AK/Assertions.h>
|
#include <AK/Assertions.h>
|
||||||
#include <AK/Checked.h>
|
#include <AK/Checked.h>
|
||||||
#include <AK/Forward.h>
|
#include <AK/Forward.h>
|
||||||
|
#include <AK/Span.h>
|
||||||
#include <AK/StdLibExtras.h>
|
#include <AK/StdLibExtras.h>
|
||||||
#include <AK/StringUtils.h>
|
#include <AK/StringUtils.h>
|
||||||
|
|
||||||
|
@ -63,8 +64,12 @@ public:
|
||||||
|
|
||||||
bool is_null() const { return !m_characters; }
|
bool is_null() const { return !m_characters; }
|
||||||
bool is_empty() const { return m_length == 0; }
|
bool is_empty() const { return m_length == 0; }
|
||||||
|
|
||||||
const char* characters_without_null_termination() const { return m_characters; }
|
const char* characters_without_null_termination() const { return m_characters; }
|
||||||
size_t length() const { return m_length; }
|
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]; }
|
const char& operator[](size_t index) const { return m_characters[index]; }
|
||||||
|
|
||||||
ConstIterator begin() const { return characters_without_null_termination(); }
|
ConstIterator begin() const { return characters_without_null_termination(); }
|
||||||
|
|
Loading…
Reference in a new issue