File tree 4 files changed +31
-2
lines changed
4 files changed +31
-2
lines changed Original file line number Diff line number Diff line change 15
15
16
16
namespace AK {
17
17
18
+ template <>
19
+ struct Traits <FlyString>;
20
+
18
21
class FlyString {
19
22
AK_MAKE_DEFAULT_MOVABLE (FlyString);
20
23
AK_MAKE_DEFAULT_COPYABLE (FlyString);
@@ -79,6 +82,12 @@ class FlyString {
79
82
return (... || this ->operator ==(forward<Ts>(strings)));
80
83
}
81
84
85
+ FlyString (Badge<Optional<FlyString>>)
86
+ : m_data(nullptr )
87
+ {
88
+ }
89
+ ALWAYS_INLINE constexpr bool is_null (Badge<Traits<FlyString>> badge) { return m_data.is_null (move (badge)); }
90
+
82
91
private:
83
92
explicit FlyString (Detail::StringBase data)
84
93
: m_data(move(data))
@@ -91,6 +100,9 @@ class FlyString {
91
100
template <>
92
101
struct Traits <FlyString> : public DefaultTraits<FlyString> {
93
102
static unsigned hash (FlyString const &);
103
+
104
+ constexpr static auto special_optional_empty_value (Badge<Optional<FlyString>> badge) { return FlyString (move (badge)); }
105
+ constexpr static bool optional_has_value (String const & str) { return !str.is_null (Badge<Traits<FlyString>> {}); }
94
106
};
95
107
96
108
template <>
Original file line number Diff line number Diff line change @@ -202,6 +202,11 @@ class String : public Detail::StringBase {
202
202
requires (IsSame<RemoveCVReference<T>, StringView>)
203
203
static ErrorOr<String> from_byte_string (T&&) = delete;
204
204
205
+ constexpr String (Badge<Optional<String>>)
206
+ : StringBase(nullptr )
207
+ {
208
+ }
209
+
205
210
private:
206
211
friend class ::AK::FlyString;
207
212
@@ -216,6 +221,9 @@ class String : public Detail::StringBase {
216
221
template <>
217
222
struct Traits <String> : public DefaultTraits<String> {
218
223
static unsigned hash (String const &);
224
+
225
+ constexpr static auto special_optional_empty_value (Badge<Optional<String>> badge) { return String (move (badge)); }
226
+ constexpr static bool optional_has_value (String const & str) { return !str.is_null (Badge<Traits<String>> {}); }
219
227
};
220
228
221
229
template <>
Original file line number Diff line number Diff line change @@ -117,7 +117,7 @@ ErrorOr<StringBase> StringBase::substring_from_byte_offset_with_shared_superstri
117
117
118
118
void StringBase::destroy_string ()
119
119
{
120
- if (!is_short_string ())
120
+ if (!is_short_string () && m_raw != 0 )
121
121
m_data->unref ();
122
122
}
123
123
Original file line number Diff line number Diff line change @@ -69,9 +69,17 @@ class StringBase {
69
69
70
70
[[nodiscard]] bool operator ==(StringBase const &) const ;
71
71
72
- [[nodiscard]] ALWAYS_INLINE FlatPtr raw (Badge<FlyString>) const { return bit_cast<FlatPtr>(m_data); }
72
+ [[nodiscard]] ALWAYS_INLINE FlatPtr raw (Badge<FlyString>) const { return m_raw; }
73
+ template <OneOf<String, FlyString> T>
74
+ [[nodiscard]] ALWAYS_INLINE constexpr bool is_null (Badge<Traits<T>>) const { return m_raw; }
73
75
74
76
protected:
77
+ // For Optional
78
+ constexpr explicit StringBase (nullptr_t )
79
+ : m_raw { 0 }
80
+ {
81
+ }
82
+
75
83
template <typename Func>
76
84
ErrorOr<void > replace_with_new_string (size_t byte_count, Func&& callback)
77
85
{
@@ -124,6 +132,7 @@ class StringBase {
124
132
union {
125
133
ShortString m_short_string;
126
134
Detail::StringData const * m_data { nullptr };
135
+ FlatPtr m_raw;
127
136
};
128
137
};
129
138
You can’t perform that action at this time.
0 commit comments