Coverage Report

Created: 2022-05-20 06:19

/src/serenity/AK/StringView.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#include <AK/AnyOf.h>
8
#include <AK/ByteBuffer.h>
9
#include <AK/Find.h>
10
#include <AK/Function.h>
11
#include <AK/Memory.h>
12
#include <AK/StringView.h>
13
#include <AK/Vector.h>
14
15
#ifndef KERNEL
16
#    include <AK/FlyString.h>
17
#    include <AK/String.h>
18
#endif
19
20
namespace AK {
21
22
#ifndef KERNEL
23
StringView::StringView(String const& string)
24
    : m_characters(string.characters())
25
    , m_length(string.length())
26
175k
{
27
175k
}
28
29
StringView::StringView(FlyString const& string)
30
    : m_characters(string.characters())
31
    , m_length(string.length())
32
523k
{
33
523k
}
34
#endif
35
36
StringView::StringView(ByteBuffer const& buffer)
37
    : m_characters((char const*)buffer.data())
38
    , m_length(buffer.size())
39
0
{
40
0
}
41
42
Vector<StringView> StringView::split_view(char const separator, bool keep_empty) const
43
0
{
44
0
    StringView seperator_view { &separator, 1 };
45
0
    return split_view(seperator_view, keep_empty);
46
0
}
47
48
Vector<StringView> StringView::split_view(StringView separator, bool keep_empty) const
49
0
{
50
0
    Vector<StringView> parts;
51
0
    for_each_split_view(separator, keep_empty, [&](StringView view) {
52
0
        parts.append(view);
53
0
    });
54
0
    return parts;
55
0
}
56
57
Vector<StringView> StringView::lines(bool consider_cr) const
58
0
{
59
0
    if (is_empty())
60
0
        return {};
61
62
0
    if (!consider_cr)
63
0
        return split_view('\n', true);
64
65
0
    Vector<StringView> v;
66
0
    size_t substart = 0;
67
0
    bool last_ch_was_cr = false;
68
0
    bool split_view = false;
69
0
    for (size_t i = 0; i < length(); ++i) {
70
0
        char ch = characters_without_null_termination()[i];
71
0
        if (ch == '\n') {
72
0
            split_view = true;
73
0
            if (last_ch_was_cr) {
74
0
                substart = i + 1;
75
0
                split_view = false;
76
0
            }
77
0
        }
78
0
        if (ch == '\r') {
79
0
            split_view = true;
80
0
            last_ch_was_cr = true;
81
0
        } else {
82
0
            last_ch_was_cr = false;
83
0
        }
84
0
        if (split_view) {
85
0
            size_t sublen = i - substart;
86
0
            v.append(substring_view(substart, sublen));
87
0
            substart = i + 1;
88
0
        }
89
0
        split_view = false;
90
0
    }
91
0
    size_t taillen = length() - substart;
92
0
    if (taillen != 0)
93
0
        v.append(substring_view(substart, taillen));
94
0
    return v;
95
0
}
96
97
bool StringView::starts_with(char ch) const
98
0
{
99
0
    if (is_empty())
100
0
        return false;
101
0
    return ch == characters_without_null_termination()[0];
102
0
}
103
104
bool StringView::starts_with(StringView str, CaseSensitivity case_sensitivity) const
105
4.16M
{
106
4.16M
    return StringUtils::starts_with(*this, str, case_sensitivity);
107
4.16M
}
108
109
bool StringView::ends_with(char ch) const
110
0
{
111
0
    if (is_empty())
112
0
        return false;
113
0
    return ch == characters_without_null_termination()[length() - 1];
114
0
}
115
116
bool StringView::ends_with(StringView str, CaseSensitivity case_sensitivity) const
117
0
{
118
0
    return StringUtils::ends_with(*this, str, case_sensitivity);
119
0
}
120
121
bool StringView::matches(StringView mask, Vector<MaskSpan>& mask_spans, CaseSensitivity case_sensitivity) const
122
0
{
123
0
    return StringUtils::matches(*this, mask, case_sensitivity, &mask_spans);
124
0
}
125
126
bool StringView::matches(StringView mask, CaseSensitivity case_sensitivity) const
127
0
{
128
0
    return StringUtils::matches(*this, mask, case_sensitivity);
129
0
}
130
131
bool StringView::contains(char needle) const
132
52.8M
{
133
101M
    for (char current : *this) {
134
101M
        if (current == needle)
135
3.05M
            return true;
136
101M
    }
137
49.8M
    return false;
138
52.8M
}
139
140
bool StringView::contains(StringView needle, CaseSensitivity case_sensitivity) const
141
3.96M
{
142
3.96M
    return StringUtils::contains(*this, needle, case_sensitivity);
143
3.96M
}
144
145
bool StringView::equals_ignoring_case(StringView other) const
146
0
{
147
0
    return StringUtils::equals_ignoring_case(*this, other);
148
0
}
149
150
#ifndef KERNEL
151
String StringView::to_lowercase_string() const
152
0
{
153
0
    return StringImpl::create_lowercased(characters_without_null_termination(), length());
154
0
}
155
156
String StringView::to_uppercase_string() const
157
0
{
158
0
    return StringImpl::create_uppercased(characters_without_null_termination(), length());
159
0
}
160
161
String StringView::to_titlecase_string() const
162
0
{
163
0
    return StringUtils::to_titlecase(*this);
164
0
}
165
#endif
166
167
StringView StringView::substring_view_starting_from_substring(StringView substring) const
168
0
{
169
0
    char const* remaining_characters = substring.characters_without_null_termination();
170
0
    VERIFY(remaining_characters >= m_characters);
171
0
    VERIFY(remaining_characters <= m_characters + m_length);
172
0
    size_t remaining_length = m_length - (remaining_characters - m_characters);
173
0
    return { remaining_characters, remaining_length };
174
0
}
175
176
StringView StringView::substring_view_starting_after_substring(StringView substring) const
177
0
{
178
0
    char const* remaining_characters = substring.characters_without_null_termination() + substring.length();
179
0
    VERIFY(remaining_characters >= m_characters);
180
0
    VERIFY(remaining_characters <= m_characters + m_length);
181
0
    size_t remaining_length = m_length - (remaining_characters - m_characters);
182
0
    return { remaining_characters, remaining_length };
183
0
}
184
185
bool StringView::copy_characters_to_buffer(char* buffer, size_t buffer_size) const
186
0
{
187
    // We must fit at least the NUL-terminator.
188
0
    VERIFY(buffer_size > 0);
189
190
0
    size_t characters_to_copy = min(m_length, buffer_size - 1);
191
0
    __builtin_memcpy(buffer, m_characters, characters_to_copy);
192
0
    buffer[characters_to_copy] = 0;
193
194
0
    return characters_to_copy == m_length;
195
0
}
196
197
template<typename T>
198
Optional<T> StringView::to_int() const
199
0
{
200
0
    return StringUtils::convert_to_int<T>(*this);
201
0
}
Unexecuted instantiation: AK::Optional<signed char> AK::StringView::to_int<signed char>() const
Unexecuted instantiation: AK::Optional<short> AK::StringView::to_int<short>() const
Unexecuted instantiation: AK::Optional<int> AK::StringView::to_int<int>() const
Unexecuted instantiation: AK::Optional<long> AK::StringView::to_int<long>() const
Unexecuted instantiation: AK::Optional<long long> AK::StringView::to_int<long long>() const
202
203
template Optional<i8> StringView::to_int() const;
204
template Optional<i16> StringView::to_int() const;
205
template Optional<i32> StringView::to_int() const;
206
template Optional<long> StringView::to_int() const;
207
template Optional<long long> StringView::to_int() const;
208
209
template<typename T>
210
Optional<T> StringView::to_uint() const
211
602k
{
212
602k
    return StringUtils::convert_to_uint<T>(*this);
213
602k
}
Unexecuted instantiation: AK::Optional<unsigned char> AK::StringView::to_uint<unsigned char>() const
Unexecuted instantiation: AK::Optional<unsigned short> AK::StringView::to_uint<unsigned short>() const
AK::Optional<unsigned int> AK::StringView::to_uint<unsigned int>() const
Line
Count
Source
211
602k
{
212
602k
    return StringUtils::convert_to_uint<T>(*this);
213
602k
}
AK::Optional<unsigned long> AK::StringView::to_uint<unsigned long>() const
Line
Count
Source
211
24
{
212
24
    return StringUtils::convert_to_uint<T>(*this);
213
24
}
Unexecuted instantiation: AK::Optional<unsigned long long> AK::StringView::to_uint<unsigned long long>() const
Unexecuted instantiation: AK::Optional<long> AK::StringView::to_uint<long>() const
Unexecuted instantiation: AK::Optional<long long> AK::StringView::to_uint<long long>() const
214
215
template Optional<u8> StringView::to_uint() const;
216
template Optional<u16> StringView::to_uint() const;
217
template Optional<u32> StringView::to_uint() const;
218
template Optional<unsigned long> StringView::to_uint() const;
219
template Optional<unsigned long long> StringView::to_uint() const;
220
template Optional<long> StringView::to_uint() const;
221
template Optional<long long> StringView::to_uint() const;
222
223
#ifndef KERNEL
224
bool StringView::operator==(String const& string) const
225
2.11M
{
226
2.11M
    return *this == string.view();
227
2.11M
}
228
229
232
String StringView::to_string() const { return String { *this }; }
230
231
String StringView::replace(StringView needle, StringView replacement, bool all_occurrences) const
232
0
{
233
0
    return StringUtils::replace(*this, needle, replacement, all_occurrences);
234
0
}
235
#endif
236
237
Vector<size_t> StringView::find_all(StringView needle) const
238
0
{
239
0
    return StringUtils::find_all(*this, needle);
240
0
}
241
242
Vector<StringView> StringView::split_view_if(Function<bool(char)> const& predicate, bool keep_empty) const
243
0
{
244
0
    if (is_empty())
245
0
        return {};
246
247
0
    Vector<StringView> v;
248
0
    size_t substart = 0;
249
0
    for (size_t i = 0; i < length(); ++i) {
250
0
        char ch = characters_without_null_termination()[i];
251
0
        if (predicate(ch)) {
252
0
            size_t sublen = i - substart;
253
0
            if (sublen != 0 || keep_empty)
254
0
                v.append(substring_view(substart, sublen));
255
0
            substart = i + 1;
256
0
        }
257
0
    }
258
0
    size_t taillen = length() - substart;
259
0
    if (taillen != 0 || keep_empty)
260
0
        v.append(substring_view(substart, taillen));
261
0
    return v;
262
0
}
263
264
}