KString.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Format.h>
  8. #include <AK/OwnPtr.h>
  9. namespace Kernel {
  10. class KString {
  11. public:
  12. static OwnPtr<KString> try_create_uninitialized(size_t, char*&);
  13. static NonnullOwnPtr<KString> must_create_uninitialized(size_t, char*&);
  14. static OwnPtr<KString> try_create(StringView const&);
  15. static NonnullOwnPtr<KString> must_create(StringView const&);
  16. OwnPtr<KString> try_clone() const;
  17. bool is_empty() const { return m_length == 0; }
  18. size_t length() const { return m_length; }
  19. char const* characters() const { return m_characters; }
  20. StringView view() const { return { characters(), length() }; }
  21. private:
  22. explicit KString(size_t length)
  23. : m_length(length)
  24. {
  25. }
  26. size_t m_length { 0 };
  27. char m_characters[0];
  28. };
  29. }
  30. namespace AK {
  31. template<>
  32. struct Formatter<Kernel::KString> : Formatter<StringView> {
  33. void format(FormatBuilder& builder, Kernel::KString const& value)
  34. {
  35. Formatter<StringView>::format(builder, value.characters());
  36. }
  37. };
  38. }