Coverage Report

Created: 2026-09-14 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/AK/StringImpl.cpp
Line
Count
Source
1
/*
2
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#include <AK/CharacterTypes.h>
8
#include <AK/DeprecatedFlyString.h>
9
#include <AK/StringHash.h>
10
#include <AK/StringImpl.h>
11
#include <AK/kmalloc.h>
12
13
namespace AK {
14
15
static StringImpl* s_the_empty_stringimpl = nullptr;
16
17
StringImpl& StringImpl::the_empty_stringimpl()
18
3.85G
{
19
3.85G
    if (!s_the_empty_stringimpl) {
20
55
        void* slot = kmalloc(sizeof(StringImpl) + sizeof(char));
21
55
        s_the_empty_stringimpl = new (slot) StringImpl(ConstructTheEmptyStringImpl);
22
55
    }
23
3.85G
    return *s_the_empty_stringimpl;
24
3.85G
}
25
26
StringImpl::StringImpl(ConstructWithInlineBufferTag, size_t length)
27
435M
    : m_length(length)
28
435M
{
29
435M
}
30
31
StringImpl::~StringImpl()
32
435M
{
33
435M
    if (m_fly)
34
37.6k
        DeprecatedFlyString::did_destroy_impl({}, *this);
35
435M
}
36
37
NonnullRefPtr<StringImpl const> StringImpl::create_uninitialized(size_t length, char*& buffer)
38
435M
{
39
435M
    VERIFY(length);
40
435M
    void* slot = kmalloc(allocation_size_for_stringimpl(length));
41
435M
    VERIFY(slot);
42
435M
    auto new_stringimpl = adopt_ref(*new (slot) StringImpl(ConstructWithInlineBuffer, length));
43
435M
    buffer = const_cast<char*>(new_stringimpl->characters());
44
435M
    buffer[length] = '\0';
45
435M
    return new_stringimpl;
46
435M
}
47
48
RefPtr<StringImpl const> StringImpl::create(char const* cstring, size_t length, ShouldChomp should_chomp)
49
675M
{
50
675M
    if (should_chomp) {
51
0
        while (length) {
52
0
            char last_ch = cstring[length - 1];
53
0
            if (!last_ch || last_ch == '\n' || last_ch == '\r')
54
0
                --length;
55
0
            else
56
0
                break;
57
0
        }
58
0
    }
59
60
675M
    if (!length)
61
239M
        return the_empty_stringimpl();
62
63
435M
    char* buffer;
64
435M
    auto new_stringimpl = create_uninitialized(length, buffer);
65
435M
    memcpy(buffer, cstring, length * sizeof(char));
66
67
435M
    return new_stringimpl;
68
675M
}
69
70
RefPtr<StringImpl const> StringImpl::create(char const* cstring, ShouldChomp shouldChomp)
71
44.3M
{
72
44.3M
    if (!cstring || !*cstring)
73
32.1M
        return the_empty_stringimpl();
74
75
12.2M
    return create(cstring, strlen(cstring), shouldChomp);
76
44.3M
}
77
78
RefPtr<StringImpl const> StringImpl::create(ReadonlyBytes bytes, ShouldChomp shouldChomp)
79
2.77k
{
80
2.77k
    return StringImpl::create(reinterpret_cast<char const*>(bytes.data()), bytes.size(), shouldChomp);
81
2.77k
}
82
83
RefPtr<StringImpl const> StringImpl::create_lowercased(char const* cstring, size_t length)
84
1.07k
{
85
1.07k
    if (!length)
86
0
        return the_empty_stringimpl();
87
1.07k
    char* buffer;
88
1.07k
    auto impl = create_uninitialized(length, buffer);
89
19.8M
    for (size_t i = 0; i < length; ++i)
90
19.8M
        buffer[i] = (char)to_ascii_lowercase(cstring[i]);
91
1.07k
    return impl;
92
1.07k
}
93
94
RefPtr<StringImpl const> StringImpl::create_uppercased(char const* cstring, size_t length)
95
0
{
96
0
    if (!length)
97
0
        return the_empty_stringimpl();
98
0
    char* buffer;
99
0
    auto impl = create_uninitialized(length, buffer);
100
0
    for (size_t i = 0; i < length; ++i)
101
0
        buffer[i] = (char)to_ascii_uppercase(cstring[i]);
102
0
    return impl;
103
0
}
104
105
NonnullRefPtr<StringImpl const> StringImpl::to_lowercase() const
106
0
{
107
0
    for (size_t i = 0; i < m_length; ++i) {
108
0
        if (is_ascii_upper_alpha(characters()[i]))
109
0
            return create_lowercased(characters(), m_length).release_nonnull();
110
0
    }
111
0
    return const_cast<StringImpl&>(*this);
112
0
}
113
114
NonnullRefPtr<StringImpl const> StringImpl::to_uppercase() const
115
0
{
116
0
    for (size_t i = 0; i < m_length; ++i) {
117
0
        if (is_ascii_lower_alpha(characters()[i]))
118
0
            return create_uppercased(characters(), m_length).release_nonnull();
119
0
    }
120
0
    return const_cast<StringImpl&>(*this);
121
0
}
122
123
unsigned StringImpl::case_insensitive_hash() const
124
12.8M
{
125
12.8M
    return case_insensitive_string_hash(characters(), length());
126
12.8M
}
127
128
void StringImpl::compute_hash() const
129
15.8M
{
130
15.8M
    if (!length())
131
3
        m_hash = 0;
132
15.8M
    else
133
15.8M
        m_hash = string_hash(characters(), m_length);
134
15.8M
    m_has_hash = true;
135
15.8M
}
136
137
}