diff --git a/AK/AKString.h b/AK/AKString.h index 20ee499de28..3c2fdd7f876 100644 --- a/AK/AKString.h +++ b/AK/AKString.h @@ -1,11 +1,12 @@ #pragma once -#include "ByteBuffer.h" -#include "RetainPtr.h" -#include "StringImpl.h" -#include "Traits.h" -#include "Vector.h" -#include "kstdio.h" +#include +#include +#include +#include +#include +#include +#include namespace AK { @@ -14,6 +15,12 @@ public: ~String() { } String() { } + + String(StringView view) + : m_impl(StringImpl::create(view.characters(), view.length())) + { + } + String(const String& other) : m_impl(const_cast(other).m_impl.copy_ref()) { @@ -111,6 +118,8 @@ public: static String format(const char*, ...); + StringView view() const { return { characters(), length() }; } + private: RetainPtr m_impl; }; diff --git a/AK/StringView.h b/AK/StringView.h new file mode 100644 index 00000000000..6dd8cef17a1 --- /dev/null +++ b/AK/StringView.h @@ -0,0 +1,25 @@ +#pragma once + +class StringView { +public: + StringView() { } + StringView(const char* characters, int length) : m_characters(characters), m_length(length) { } + StringView(const unsigned char* characters, int length) : m_characters((const char*)characters), m_length(length) { } + StringView(const char* cstring) + : m_characters(cstring) + { + if (cstring) { + while (*(cstring++)) + ++m_length; + } + } + + bool is_empty() const { return m_length == 0; } + const char* characters() const { return m_characters; } + int length() const { return m_length; } + char operator[](int index) const { return m_characters[index]; } + +private: + const char* m_characters { nullptr }; + int m_length { 0 }; +};