mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 17:17:35 +00:00
AK: Add span() / bytes() methods to container types.
This commit is contained in:
parent
c42450786c
commit
a922abd9d7
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; }
|
||||||
|
|
||||||
|
@ -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…
Add table
Add a link
Reference in a new issue