summaryrefslogtreecommitdiff
path: root/AK/FlyString.cpp
blob: f8089be5701ff19f01835cbbf03a4eabdcab630f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/FlyString.h>
#include <AK/HashMap.h>
#include <AK/Singleton.h>
#include <AK/StringView.h>
#include <AK/Utf8View.h>

namespace AK {

static auto& all_fly_strings()
{
    static Singleton<HashMap<StringView, uintptr_t>> table;
    return *table;
}

FlyString::FlyString()
    : m_data(String {}.to_fly_string_data({}))
{
}

FlyString::~FlyString()
{
    String::unref_fly_string_data({}, m_data);
}

ErrorOr<FlyString> FlyString::from_utf8(StringView string)
{
    return FlyString { TRY(String::from_utf8(string)) };
}

FlyString::FlyString(String const& string)
{
    if (string.is_short_string()) {
        m_data = string.to_fly_string_data({});
        return;
    }

    auto it = all_fly_strings().find(string.bytes_as_string_view());
    if (it == all_fly_strings().end()) {
        m_data = string.to_fly_string_data({});

        all_fly_strings().set(string.bytes_as_string_view(), m_data);
        string.did_create_fly_string({});
    } else {
        m_data = it->value;
    }

    String::ref_fly_string_data({}, m_data);
}

FlyString& FlyString::operator=(String const& string)
{
    *this = FlyString { string };
    return *this;
}

FlyString::FlyString(FlyString const& other)
    : m_data(other.m_data)
{
    String::ref_fly_string_data({}, m_data);
}

FlyString& FlyString::operator=(FlyString const& other)
{
    if (this != &other) {
        m_data = other.m_data;
        String::ref_fly_string_data({}, m_data);
    }

    return *this;
}

FlyString::FlyString(FlyString&& other)
    : m_data(other.m_data)
{
    other.m_data = String {}.to_fly_string_data({});
}

FlyString& FlyString::operator=(FlyString&& other)
{
    m_data = other.m_data;
    other.m_data = String {}.to_fly_string_data({});

    return *this;
}

bool FlyString::is_empty() const
{
    return bytes_as_string_view().is_empty();
}

unsigned FlyString::hash() const
{
    return bytes_as_string_view().hash();
}

FlyString::operator String() const
{
    return to_string();
}

String FlyString::to_string() const
{
    return String::fly_string_data_to_string({}, m_data);
}

Utf8View FlyString::code_points() const
{
    return Utf8View { bytes_as_string_view() };
}

ReadonlyBytes FlyString::bytes() const
{
    return bytes_as_string_view().bytes();
}

StringView FlyString::bytes_as_string_view() const
{
    return String::fly_string_data_to_string_view({}, m_data);
}

bool FlyString::operator==(FlyString const& other) const
{
    return m_data == other.m_data;
}

bool FlyString::operator==(String const& other) const
{
    if (m_data == other.to_fly_string_data({}))
        return true;

    return bytes_as_string_view() == other.bytes_as_string_view();
}

bool FlyString::operator==(StringView string) const
{
    return bytes_as_string_view() == string;
}

bool FlyString::operator==(char const* string) const
{
    return bytes_as_string_view() == string;
}

void FlyString::did_destroy_fly_string_data(Badge<Detail::StringData>, StringView string_data)
{
    all_fly_strings().remove(string_data);
}

uintptr_t FlyString::data(Badge<String>) const
{
    return m_data;
}

size_t FlyString::number_of_fly_strings()
{
    return all_fly_strings().size();
}

unsigned Traits<FlyString>::hash(FlyString const& fly_string)
{
    return fly_string.bytes_as_string_view().hash();
}

ErrorOr<void> Formatter<FlyString>::format(FormatBuilder& builder, FlyString const& fly_string)
{
    return Formatter<StringView>::format(builder, fly_string.bytes_as_string_view());
}

}