summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorasynts <asynts@gmail.com>2020-08-05 10:37:34 +0200
committerAndreas Kling <kling@serenityos.org>2020-08-06 10:33:16 +0200
commit75cde94c6a72be4f83a739a0a14258ca5f9a17dd (patch)
tree933217b898735453735a417112585bc7f2f5b084 /AK
parent42b488065367b070b71a57cea1073453318fbf23 (diff)
downloadserenity-75cde94c6a72be4f83a739a0a14258ca5f9a17dd.zip
AK: Add String constructor from ReadonlyBytes.
Diffstat (limited to 'AK')
-rw-r--r--AK/String.h17
-rw-r--r--AK/StringImpl.cpp5
-rw-r--r--AK/StringImpl.h1
3 files changed, 23 insertions, 0 deletions
diff --git a/AK/String.h b/AK/String.h
index 3c175562d3..e1c5effef7 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<StringImpl&>(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 88b5b8f24e..f90d0e632a 100644
--- a/AK/StringImpl.cpp
+++ b/AK/StringImpl.cpp
@@ -133,6 +133,11 @@ RefPtr<StringImpl> StringImpl::create(const char* cstring, ShouldChomp shouldCho
return create(cstring, strlen(cstring), shouldChomp);
}
+RefPtr<StringImpl> StringImpl::create(ReadonlyBytes bytes, ShouldChomp shouldChomp)
+{
+ return StringImpl::create(reinterpret_cast<const char*>(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 721144e68b..47ae2c6905 100644
--- a/AK/StringImpl.h
+++ b/AK/StringImpl.h
@@ -45,6 +45,7 @@ public:
static NonnullRefPtr<StringImpl> create_uninitialized(size_t length, char*& buffer);
static RefPtr<StringImpl> create(const char* cstring, ShouldChomp = NoChomp);
static RefPtr<StringImpl> create(const char* cstring, size_t length, ShouldChomp = NoChomp);
+ static RefPtr<StringImpl> create(ReadonlyBytes, ShouldChomp = NoChomp);
NonnullRefPtr<StringImpl> to_lowercase() const;
NonnullRefPtr<StringImpl> to_uppercase() const;