/* * Copyright (c) 2021, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include namespace Kernel { class KString { AK_MAKE_NONCOPYABLE(KString); AK_MAKE_NONMOVABLE(KString); public: [[nodiscard]] static KResultOr> try_create_uninitialized(size_t, char*&); [[nodiscard]] static NonnullOwnPtr must_create_uninitialized(size_t, char*&); [[nodiscard]] static KResultOr> try_create(StringView); [[nodiscard]] static NonnullOwnPtr must_create(StringView); void operator delete(void*); KResultOr> try_clone() const; [[nodiscard]] bool is_empty() const { return m_length == 0; } [[nodiscard]] size_t length() const { return m_length; } [[nodiscard]] char const* characters() const { return m_characters; } [[nodiscard]] 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 : Formatter { void format(FormatBuilder& builder, Kernel::KString const& value) { Formatter::format(builder, value.view()); } }; template<> struct Formatter> : Formatter { void format(FormatBuilder& builder, OwnPtr const& value) { if (value) Formatter::format(builder, value->view()); else Formatter::format(builder, "[out of memory]"sv); } }; template<> struct Traits> : public GenericTraits> { using PeekType = Kernel::KString*; using ConstPeekType = Kernel::KString const*; static unsigned hash(NonnullOwnPtr const& p) { return string_hash(p->characters(), p->length()); } static bool equals(NonnullOwnPtr const& a, NonnullOwnPtr const& b) { return a->view() == b->view(); } }; template<> struct Traits> : public GenericTraits> { using PeekType = Kernel::KString*; using ConstPeekType = Kernel::KString const*; static unsigned hash(OwnPtr const& p) { if (!p) return ptr_hash(nullptr); return string_hash(p->characters(), p->length()); } static bool equals(OwnPtr const& a, OwnPtr const& b) { if (!a || !b) return a.ptr() == b.ptr(); if (a == b) return true; return a->view() == b->view(); } }; }