Coverage Report

Created: 2026-11-13 08:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/AK/DeprecatedFlyString.cpp
Line
Count
Source
1
/*
2
 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#include <AK/ByteString.h>
8
#include <AK/DeprecatedFlyString.h>
9
#include <AK/HashTable.h>
10
#include <AK/Optional.h>
11
#include <AK/Singleton.h>
12
#include <AK/StringUtils.h>
13
#include <AK/StringView.h>
14
15
namespace AK {
16
17
struct DeprecatedFlyStringImplTraits : public Traits<StringImpl const*> {
18
119k
    static unsigned hash(StringImpl const* s) { return s->hash(); }
19
    static bool equals(StringImpl const* a, StringImpl const* b)
20
86.3k
    {
21
86.3k
        return *a == *b;
22
86.3k
    }
23
};
24
25
static Singleton<HashTable<StringImpl const*, DeprecatedFlyStringImplTraits>> s_table;
26
27
static HashTable<StringImpl const*, DeprecatedFlyStringImplTraits>& fly_impls()
28
2.02M
{
29
2.02M
    return *s_table;
30
2.02M
}
31
32
void DeprecatedFlyString::did_destroy_impl(Badge<StringImpl>, StringImpl& impl)
33
38.5k
{
34
38.5k
    fly_impls().remove(&impl);
35
38.5k
}
36
37
DeprecatedFlyString::DeprecatedFlyString(ByteString const& string)
38
49.6k
    : m_impl(string.impl())
39
49.6k
{
40
49.6k
    if (string.impl()->is_fly())
41
10.7k
        return;
42
43
38.8k
    auto it = fly_impls().find(string.impl());
44
38.8k
    if (it == fly_impls().end()) {
45
38.2k
        fly_impls().set(string.impl());
46
38.2k
        string.impl()->set_fly({}, true);
47
38.2k
        m_impl = string.impl();
48
38.2k
    } else {
49
575
        VERIFY((*it)->is_fly());
50
575
        m_impl = **it;
51
575
    }
52
38.8k
}
53
54
DeprecatedFlyString::DeprecatedFlyString(StringView string)
55
935k
    : m_impl(StringImpl::the_empty_stringimpl())
56
935k
{
57
935k
    if (string.is_null())
58
0
        return;
59
1.24M
    auto it = fly_impls().find(string.hash(), [&](auto& candidate) {
60
1.24M
        return string == *candidate;
61
1.24M
    });
62
935k
    if (it == fly_impls().end()) {
63
835
        auto new_string = string.to_byte_string();
64
835
        fly_impls().set(new_string.impl());
65
835
        new_string.impl()->set_fly({}, true);
66
835
        m_impl = new_string.impl();
67
934k
    } else {
68
934k
        VERIFY((*it)->is_fly());
69
934k
        m_impl = **it;
70
934k
    }
71
935k
}
72
73
bool DeprecatedFlyString::equals_ignoring_ascii_case(StringView other) const
74
0
{
75
0
    return StringUtils::equals_ignoring_ascii_case(view(), other);
76
0
}
77
78
bool DeprecatedFlyString::starts_with(StringView str, CaseSensitivity case_sensitivity) const
79
0
{
80
0
    return StringUtils::starts_with(view(), str, case_sensitivity);
81
0
}
82
83
bool DeprecatedFlyString::ends_with(StringView str, CaseSensitivity case_sensitivity) const
84
0
{
85
0
    return StringUtils::ends_with(view(), str, case_sensitivity);
86
0
}
87
88
DeprecatedFlyString DeprecatedFlyString::to_lowercase() const
89
0
{
90
0
    return ByteString(*m_impl).to_lowercase();
91
0
}
92
93
bool DeprecatedFlyString::operator==(ByteString const& other) const
94
0
{
95
0
    return m_impl == other.impl() || view() == other.view();
96
0
}
97
98
bool DeprecatedFlyString::operator==(StringView string) const
99
946k
{
100
946k
    return view() == string;
101
946k
}
102
103
bool DeprecatedFlyString::operator==(char const* string) const
104
0
{
105
0
    return view() == string;
106
0
}
107
108
}