Serenity Operating System
at master 86 lines 2.5 kB view raw
1/* 2 * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#pragma once 8 9#include <AK/Error.h> 10#include <AK/Format.h> 11#include <AK/Platform.h> 12#include <AK/String.h> 13#include <AK/Traits.h> 14#include <AK/Types.h> 15 16namespace AK { 17 18class FlyString { 19public: 20 FlyString(); 21 ~FlyString(); 22 23 static ErrorOr<FlyString> from_utf8(StringView); 24 FlyString(String const&); 25 FlyString& operator=(String const&); 26 27 FlyString(FlyString const&); 28 FlyString& operator=(FlyString const&); 29 30 FlyString(FlyString&&); 31 FlyString& operator=(FlyString&&); 32 33 [[nodiscard]] bool is_empty() const; 34 [[nodiscard]] unsigned hash() const; 35 36 explicit operator String() const; 37 String to_string() const; 38 39 [[nodiscard]] Utf8View code_points() const; 40 [[nodiscard]] ReadonlyBytes bytes() const; 41 [[nodiscard]] StringView bytes_as_string_view() const; 42 43 [[nodiscard]] bool operator==(FlyString const& other) const; 44 [[nodiscard]] bool operator==(String const&) const; 45 [[nodiscard]] bool operator==(StringView) const; 46 [[nodiscard]] bool operator==(char const*) const; 47 48 static void did_destroy_fly_string_data(Badge<Detail::StringData>, StringView); 49 [[nodiscard]] uintptr_t data(Badge<String>) const; 50 51 // This is primarily interesting to unit tests. 52 [[nodiscard]] static size_t number_of_fly_strings(); 53 54 // FIXME: Remove these once all code has been ported to FlyString 55 [[nodiscard]] DeprecatedFlyString to_deprecated_fly_string() const; 56 static ErrorOr<FlyString> from_deprecated_fly_string(DeprecatedFlyString const&); 57 58 // Compare this FlyString against another string with ASCII caseless matching. 59 [[nodiscard]] bool equals_ignoring_ascii_case(FlyString const&) const; 60 61private: 62 // This will hold either the pointer to the Detail::StringData it represents or the raw bytes of 63 // an inlined short string. 64 uintptr_t m_data { 0 }; 65}; 66 67template<> 68struct Traits<FlyString> : public GenericTraits<FlyString> { 69 static unsigned hash(FlyString const&); 70}; 71 72template<> 73struct Formatter<FlyString> : Formatter<StringView> { 74 ErrorOr<void> format(FormatBuilder&, FlyString const&); 75}; 76 77} 78 79[[nodiscard]] ALWAYS_INLINE AK::ErrorOr<AK::FlyString> operator""_fly_string(char const* cstring, size_t length) 80{ 81 return AK::FlyString::from_utf8(AK::StringView(cstring, length)); 82} 83 84#if USING_AK_GLOBALLY 85using AK::FlyString; 86#endif