2021-05-28 07:25:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Format.h>
|
|
|
|
#include <AK/OwnPtr.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
class KString {
|
2021-06-02 10:00:09 +00:00
|
|
|
AK_MAKE_NONCOPYABLE(KString);
|
|
|
|
AK_MAKE_NONMOVABLE(KString);
|
|
|
|
|
2021-05-28 07:25:02 +00:00
|
|
|
public:
|
|
|
|
static OwnPtr<KString> try_create_uninitialized(size_t, char*&);
|
2021-05-31 08:51:09 +00:00
|
|
|
static NonnullOwnPtr<KString> must_create_uninitialized(size_t, char*&);
|
2021-05-28 07:25:02 +00:00
|
|
|
static OwnPtr<KString> try_create(StringView const&);
|
2021-05-31 08:51:09 +00:00
|
|
|
static NonnullOwnPtr<KString> must_create(StringView const&);
|
2021-05-28 07:25:02 +00:00
|
|
|
|
2021-06-02 20:25:39 +00:00
|
|
|
void operator delete(void*);
|
|
|
|
|
2021-05-28 07:25:02 +00:00
|
|
|
OwnPtr<KString> try_clone() const;
|
|
|
|
|
|
|
|
bool is_empty() const { return m_length == 0; }
|
|
|
|
size_t length() const { return m_length; }
|
|
|
|
char const* characters() const { return m_characters; }
|
|
|
|
StringView view() const { return { characters(), length() }; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
explicit KString(size_t length)
|
|
|
|
: m_length(length)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t m_length { 0 };
|
|
|
|
char m_characters[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct Formatter<Kernel::KString> : Formatter<StringView> {
|
|
|
|
void format(FormatBuilder& builder, Kernel::KString const& value)
|
|
|
|
{
|
2021-07-06 10:55:08 +00:00
|
|
|
Formatter<StringView>::format(builder, value.view());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct Formatter<OwnPtr<Kernel::KString>> : Formatter<StringView> {
|
|
|
|
void format(FormatBuilder& builder, OwnPtr<Kernel::KString> const& value)
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
Formatter<StringView>::format(builder, value->view());
|
|
|
|
else
|
|
|
|
Formatter<StringView>::format(builder, "[out of memory]"sv);
|
2021-05-28 07:25:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|