From 75cde94c6a72be4f83a739a0a14258ca5f9a17dd Mon Sep 17 00:00:00 2001 From: asynts Date: Wed, 5 Aug 2020 10:37:34 +0200 Subject: [PATCH] AK: Add String constructor from ReadonlyBytes. --- AK/String.h | 17 +++++++++++++++++ AK/StringImpl.cpp | 5 +++++ AK/StringImpl.h | 1 + 3 files changed, 23 insertions(+) diff --git a/AK/String.h b/AK/String.h index 3c175562d32..e1c5effef7c 100644 --- a/AK/String.h +++ b/AK/String.h @@ -83,6 +83,11 @@ public: { } + explicit String(ReadonlyBytes bytes, ShouldChomp shouldChomp = NoChomp) + : m_impl(StringImpl::create(bytes, shouldChomp)) + { + } + String(const StringImpl& impl) : m_impl(const_cast(impl)) { @@ -196,6 +201,18 @@ public: return *this; } + String& operator=(std::nullptr_t) + { + m_impl = nullptr; + return *this; + } + + String& operator=(ReadonlyBytes bytes) + { + m_impl = StringImpl::create(bytes); + return *this; + } + u32 hash() const { if (!m_impl) diff --git a/AK/StringImpl.cpp b/AK/StringImpl.cpp index 88b5b8f24e8..f90d0e632ad 100644 --- a/AK/StringImpl.cpp +++ b/AK/StringImpl.cpp @@ -133,6 +133,11 @@ RefPtr StringImpl::create(const char* cstring, ShouldChomp shouldCho return create(cstring, strlen(cstring), shouldChomp); } +RefPtr StringImpl::create(ReadonlyBytes bytes, ShouldChomp shouldChomp) +{ + return StringImpl::create(reinterpret_cast(bytes.data()), bytes.size(), shouldChomp); +} + static inline bool is_ascii_lowercase(char c) { return c >= 'a' && c <= 'z'; diff --git a/AK/StringImpl.h b/AK/StringImpl.h index 721144e68b9..47ae2c69059 100644 --- a/AK/StringImpl.h +++ b/AK/StringImpl.h @@ -45,6 +45,7 @@ public: static NonnullRefPtr create_uninitialized(size_t length, char*& buffer); static RefPtr create(const char* cstring, ShouldChomp = NoChomp); static RefPtr create(const char* cstring, size_t length, ShouldChomp = NoChomp); + static RefPtr create(ReadonlyBytes, ShouldChomp = NoChomp); NonnullRefPtr to_lowercase() const; NonnullRefPtr to_uppercase() const;