mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 14:38:11 +00:00
Kernel: Add KString, a single-owner string with OOM failure exposion
This is a simple string class for use in the kernel. It encapsulates a length + character array in a single-allocation object. Main differences from AK::String: - Single-owner (no reference counting.) - Allocation failures are exposed, not hidden. The basic idea is to allow better and more precise string management in the kernel.
This commit is contained in:
parent
377b06c8ac
commit
279383a8f3
3 changed files with 89 additions and 0 deletions
48
Kernel/KString.h
Normal file
48
Kernel/KString.h
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* 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 {
|
||||
public:
|
||||
static OwnPtr<KString> try_create_uninitialized(size_t, char*&);
|
||||
static OwnPtr<KString> try_create(StringView const&);
|
||||
|
||||
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)
|
||||
{
|
||||
Formatter<StringView>::format(builder, value.characters());
|
||||
}
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue