AK: Add String constructor from ReadonlyBytes.

This commit is contained in:
asynts 2020-08-05 10:37:34 +02:00 committed by Andreas Kling
parent 42b4880653
commit 75cde94c6a
Notes: sideshowbarker 2024-07-19 04:14:28 +09:00
3 changed files with 23 additions and 0 deletions

View file

@ -83,6 +83,11 @@ public:
{ {
} }
explicit String(ReadonlyBytes bytes, ShouldChomp shouldChomp = NoChomp)
: m_impl(StringImpl::create(bytes, shouldChomp))
{
}
String(const StringImpl& impl) String(const StringImpl& impl)
: m_impl(const_cast<StringImpl&>(impl)) : m_impl(const_cast<StringImpl&>(impl))
{ {
@ -196,6 +201,18 @@ public:
return *this; 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 u32 hash() const
{ {
if (!m_impl) if (!m_impl)

View file

@ -133,6 +133,11 @@ RefPtr<StringImpl> StringImpl::create(const char* cstring, ShouldChomp shouldCho
return create(cstring, strlen(cstring), shouldChomp); 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) static inline bool is_ascii_lowercase(char c)
{ {
return c >= 'a' && c <= 'z'; return c >= 'a' && c <= 'z';

View file

@ -45,6 +45,7 @@ public:
static NonnullRefPtr<StringImpl> create_uninitialized(size_t length, char*& buffer); 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, ShouldChomp = NoChomp);
static RefPtr<StringImpl> create(const char* cstring, size_t length, 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_lowercase() const;
NonnullRefPtr<StringImpl> to_uppercase() const; NonnullRefPtr<StringImpl> to_uppercase() const;