Coverage Report

Created: 2026-09-26 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/AK/StringData.h
Line
Count
Source
1
/*
2
 * Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#pragma once
8
9
#include <AK/RefCounted.h>
10
#include <AK/kmalloc.h>
11
12
namespace AK::Detail {
13
14
class StringData final : public RefCounted<StringData> {
15
public:
16
    static ErrorOr<NonnullRefPtr<StringData>> create_uninitialized(size_t byte_count, u8*& buffer)
17
9.33M
    {
18
9.33M
        VERIFY(byte_count);
19
9.33M
        void* slot = malloc(allocation_size_for_string_data(byte_count));
20
9.33M
        if (!slot) {
21
0
            return Error::from_errno(ENOMEM);
22
0
        }
23
9.33M
        auto new_string_data = adopt_ref(*new (slot) StringData(byte_count));
24
9.33M
        buffer = const_cast<u8*>(new_string_data->bytes().data());
25
9.33M
        return new_string_data;
26
9.33M
    }
27
28
    static ErrorOr<NonnullRefPtr<StringData>> create_substring(StringData const& superstring, size_t start, size_t byte_count)
29
4.06k
    {
30
        // Strings of MAX_SHORT_STRING_BYTE_COUNT bytes or less should be handled by the String short string optimization.
31
4.06k
        VERIFY(byte_count > MAX_SHORT_STRING_BYTE_COUNT);
32
33
4.06k
        void* slot = malloc(sizeof(StringData) + sizeof(StringData::SubstringData));
34
4.06k
        if (!slot) {
35
0
            return Error::from_errno(ENOMEM);
36
0
        }
37
4.06k
        return adopt_ref(*new (slot) StringData(superstring, start, byte_count));
38
4.06k
    }
39
40
    struct SubstringData {
41
        StringData const* superstring { nullptr };
42
        u32 start_offset { 0 };
43
    };
44
45
    void operator delete(void* ptr)
46
9.34M
    {
47
9.34M
        kfree_sized(ptr, allocation_size_for_string_data(static_cast<StringData const*>(ptr)->m_byte_count));
48
9.34M
    }
49
50
    ~StringData()
51
9.34M
    {
52
9.34M
        if (m_substring)
53
4.06k
            substring_data().superstring->unref();
54
9.34M
        if (m_is_fly_string)
55
822k
            FlyString::did_destroy_fly_string_data({}, *this);
56
9.34M
    }
57
58
    SubstringData const& substring_data() const
59
9.89k
    {
60
9.89k
        return *reinterpret_cast<SubstringData const*>(m_bytes_or_substring_data);
61
9.89k
    }
62
63
    // NOTE: There is no guarantee about null-termination.
64
    ReadonlyBytes bytes() const
65
29.5M
    {
66
29.5M
        if (m_substring) {
67
1.76k
            auto const& data = substring_data();
68
1.76k
            return data.superstring->bytes().slice(data.start_offset, m_byte_count);
69
1.76k
        }
70
29.5M
        return { &m_bytes_or_substring_data[0], m_byte_count };
71
29.5M
    }
72
73
1.72M
    StringView bytes_as_string_view() const { return { bytes() }; }
74
75
    bool operator==(StringData const& other) const
76
861k
    {
77
861k
        return bytes_as_string_view() == other.bytes_as_string_view();
78
861k
    }
79
80
    unsigned hash() const
81
1.69M
    {
82
1.69M
        if (!m_has_hash)
83
850k
            compute_hash();
84
1.69M
        return m_hash;
85
1.69M
    }
86
87
854k
    bool is_fly_string() const { return m_is_fly_string; }
88
822k
    void set_fly_string(bool is_fly_string) const { m_is_fly_string = is_fly_string; }
89
90
9.85k
    size_t byte_count() const { return m_byte_count; }
91
92
private:
93
    static constexpr size_t allocation_size_for_string_data(size_t length)
94
18.6M
    {
95
18.6M
        return sizeof(StringData) + (sizeof(char) * length);
96
18.6M
    }
97
98
    explicit StringData(size_t byte_count)
99
9.33M
        : m_byte_count(byte_count)
100
9.33M
    {
101
9.33M
    }
102
103
    StringData(StringData const& superstring, size_t start, size_t byte_count)
104
4.06k
        : m_byte_count(byte_count)
105
4.06k
        , m_substring(true)
106
4.06k
    {
107
4.06k
        auto& data = const_cast<SubstringData&>(substring_data());
108
4.06k
        data.start_offset = start;
109
4.06k
        data.superstring = &superstring;
110
4.06k
        superstring.ref();
111
4.06k
    }
112
113
    void compute_hash() const
114
850k
    {
115
850k
        auto bytes = this->bytes();
116
850k
        if (bytes.size() == 0)
117
0
            m_hash = 0;
118
850k
        else
119
850k
            m_hash = string_hash(reinterpret_cast<char const*>(bytes.data()), bytes.size());
120
850k
        m_has_hash = true;
121
850k
    }
122
123
    u32 m_byte_count { 0 };
124
    mutable unsigned m_hash { 0 };
125
    mutable bool m_has_hash { false };
126
    bool m_substring { false };
127
    mutable bool m_is_fly_string { false };
128
129
    alignas(SubstringData) u8 m_bytes_or_substring_data[0];
130
};
131
132
}