Skip to content

Commit 3bdaed5

Browse files
MaxWipfliawesomekling
authored andcommitted
AK+Everywhere: Remove StringView::find_{first,last}_of(char) methods
This removes StringView::find_first_of(char) and find_last_of(char) and replaces all its usages with find and find_last respectively. This is because those two methods are functionally equivalent. find_{first,last}_of should only be used if searching for multiple different characters, which is never the case with the char argument. This also adds the [[nodiscard]] to the remaining find_{first,last}_of methods.
1 parent 56253bf commit 3bdaed5

File tree

10 files changed

+39
-66
lines changed

10 files changed

+39
-66
lines changed

AK/LexicalPath.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ LexicalPath::LexicalPath(String path)
2929

3030
m_parts = m_string.split_view('/');
3131

32-
auto last_slash_index = m_string.view().find_last_of('/');
32+
auto last_slash_index = m_string.view().find_last('/');
3333
if (!last_slash_index.has_value()) {
3434
// The path contains a single part and is not absolute. m_dirname = "."sv
3535
m_dirname = { &s_single_dot, 1 };
@@ -47,7 +47,7 @@ LexicalPath::LexicalPath(String path)
4747
m_basename = m_parts.last();
4848
}
4949

50-
auto last_dot_index = m_basename.find_last_of('.');
50+
auto last_dot_index = m_basename.find_last('.');
5151
// NOTE: if the dot index is 0, this means we have ".foo", it's not an extension, as the title would then be "".
5252
if (last_dot_index.has_value() && *last_dot_index != 0) {
5353
m_title = m_basename.substring_view(0, *last_dot_index);

AK/StringView.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,6 @@ bool StringView::operator==(const String& string) const
238238
return !__builtin_memcmp(m_characters, string.characters(), m_length);
239239
}
240240

241-
Optional<size_t> StringView::find_first_of(char c) const
242-
{
243-
if (const auto location = AK::find(begin(), end(), c); location != end()) {
244-
return location.index();
245-
}
246-
return {};
247-
}
248-
249241
Optional<size_t> StringView::find_first_of(const StringView& view) const
250242
{
251243
if (const auto location = AK::find_if(begin(), end(),
@@ -261,15 +253,6 @@ Optional<size_t> StringView::find_first_of(const StringView& view) const
261253
return {};
262254
}
263255

264-
Optional<size_t> StringView::find_last_of(char c) const
265-
{
266-
for (size_t pos = m_length; pos != 0; --pos) {
267-
if (m_characters[pos - 1] == c)
268-
return pos - 1;
269-
}
270-
return {};
271-
}
272-
273256
Optional<size_t> StringView::find_last_of(const StringView& view) const
274257
{
275258
for (size_t pos = m_length; pos != 0; --pos) {

AK/StringView.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,14 @@ class StringView {
8686
[[nodiscard]] String to_lowercase_string() const;
8787
[[nodiscard]] String to_uppercase_string() const;
8888

89-
Optional<size_t> find_first_of(char) const;
90-
Optional<size_t> find_first_of(const StringView&) const;
91-
92-
Optional<size_t> find_last_of(char) const;
93-
Optional<size_t> find_last_of(const StringView&) const;
94-
9589
[[nodiscard]] Optional<size_t> find(char needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); }
9690
[[nodiscard]] Optional<size_t> find(StringView const& needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); }
9791
[[nodiscard]] Optional<size_t> find_last(char needle) const { return StringUtils::find_last(*this, needle); }
9892
// FIXME: Implement find_last(StringView const&) for API symmetry.
9993

94+
[[nodiscard]] Optional<size_t> find_first_of(StringView const&) const;
95+
[[nodiscard]] Optional<size_t> find_last_of(StringView const&) const;
96+
10097
[[nodiscard]] constexpr StringView substring_view(size_t start, size_t length) const
10198
{
10299
if (!is_constant_evaluated())

Tests/AK/TestStringView.cpp

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -104,52 +104,45 @@ TEST_CASE(lines)
104104
EXPECT_EQ(test_string_vector.at(2).is_empty(), true);
105105
}
106106

107-
TEST_CASE(find_first_of)
107+
TEST_CASE(find)
108108
{
109-
String test_string = "aabbcc_xy_ccbbaa";
110-
StringView test_string_view = test_string.view();
111-
112-
EXPECT_EQ(test_string_view.find_first_of('b').has_value(), true);
113-
EXPECT_EQ(test_string_view.find_first_of('b').value(), 2U);
114-
115-
EXPECT_EQ(test_string_view.find_first_of('_').has_value(), true);
116-
EXPECT_EQ(test_string_view.find_first_of('_').value(), 6U);
109+
auto test_string_view = "aabbcc_xy_ccbbaa"sv;
110+
EXPECT_EQ(test_string_view.find('b'), 2U);
111+
EXPECT_EQ(test_string_view.find('_'), 6U);
112+
EXPECT_EQ(test_string_view.find('n').has_value(), false);
113+
}
117114

118-
EXPECT_EQ(test_string_view.find_first_of("bc").has_value(), true);
119-
EXPECT_EQ(test_string_view.find_first_of("bc").value(), 2U);
115+
TEST_CASE(find_last)
116+
{
117+
auto test_string_view = "aabbcc_xy_ccbbaa"sv;
118+
EXPECT_EQ(test_string_view.find_last('b'), 13U);
119+
EXPECT_EQ(test_string_view.find_last('_'), 9U);
120+
EXPECT_EQ(test_string_view.find_last('3').has_value(), false);
120121

121-
EXPECT_EQ(test_string_view.find_first_of("yx").has_value(), true);
122-
EXPECT_EQ(test_string_view.find_first_of("yx").value(), 7U);
122+
test_string_view = "/"sv;
123+
EXPECT_EQ(test_string_view.find_last('/'), 0U);
124+
}
123125

124-
EXPECT_EQ(test_string_view.find_first_of('n').has_value(), false);
126+
TEST_CASE(find_first_of)
127+
{
128+
auto test_string_view = "aabbcc_xy_ccbbaa"sv;
129+
EXPECT_EQ(test_string_view.find_first_of("bc"), 2U);
130+
EXPECT_EQ(test_string_view.find_first_of("yx"), 7U);
125131
EXPECT_EQ(test_string_view.find_first_of("defg").has_value(), false);
132+
133+
test_string_view = "/"sv;
134+
EXPECT_EQ(test_string_view.find_first_of("/"), 0U);
126135
}
127136

128137
TEST_CASE(find_last_of)
129138
{
130-
String test_string = "aabbcc_xy_ccbbaa";
131-
StringView test_string_view = test_string.view();
132-
133-
EXPECT_EQ(test_string_view.find_last_of('b').has_value(), true);
134-
EXPECT_EQ(test_string_view.find_last_of('b').value(), 13U);
135-
136-
EXPECT_EQ(test_string_view.find_last_of('_').has_value(), true);
137-
EXPECT_EQ(test_string_view.find_last_of('_').value(), 9U);
138-
139-
EXPECT_EQ(test_string_view.find_last_of("bc").has_value(), true);
140-
EXPECT_EQ(test_string_view.find_last_of("bc").value(), 13U);
141-
142-
EXPECT_EQ(test_string_view.find_last_of("yx").has_value(), true);
143-
EXPECT_EQ(test_string_view.find_last_of("yx").value(), 8U);
144-
145-
EXPECT_EQ(test_string_view.find_last_of('3').has_value(), false);
139+
auto test_string_view = "aabbcc_xy_ccbbaa"sv;
140+
EXPECT_EQ(test_string_view.find_last_of("bc"), 13U);
141+
EXPECT_EQ(test_string_view.find_last_of("yx"), 8U);
146142
EXPECT_EQ(test_string_view.find_last_of("fghi").has_value(), false);
147143

148-
test_string_view = "/";
149-
EXPECT_EQ(test_string_view.find_last_of('/').has_value(), true);
150-
EXPECT_EQ(test_string_view.find_last_of('/').value(), 0U);
151-
EXPECT_EQ(test_string_view.find_last_of("/").has_value(), true);
152-
EXPECT_EQ(test_string_view.find_last_of("/").value(), 0U);
144+
test_string_view = "/"sv;
145+
EXPECT_EQ(test_string_view.find_last_of("/"), 0U);
153146
}
154147

155148
TEST_CASE(split_view)

Userland/Applications/Browser/CookieJar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ String CookieJar::default_path(const URL& url)
142142
return "/";
143143

144144
StringView uri_path_view = uri_path;
145-
std::size_t last_separator = uri_path_view.find_last_of('/').value();
145+
size_t last_separator = uri_path_view.find_last('/').value();
146146

147147
// 3. If the uri-path contains no more than one %x2F ("/") character, output %x2F ("/") and skip the remaining step.
148148
if (last_separator == 0)

Userland/Applications/SoundPlayer/M3UParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ NonnullOwnPtr<Vector<M3UEntry>> M3UParser::parse(bool include_extended_info)
6464
if (line.starts_with('#') && has_exteded_info_tag) {
6565
if (line.starts_with("#EXTINF")) {
6666
auto data = line.substring_view(8);
67-
auto separator = data.find_first_of(',');
67+
auto separator = data.find(',');
6868
VERIFY(separator.has_value());
6969
auto seconds = data.substring_view(0, separator.value());
7070
VERIFY(!seconds.is_whitespace() && !seconds.is_null() && !seconds.is_empty());

Userland/Applications/Spreadsheet/Spreadsheet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ static size_t convert_from_string(StringView str, unsigned base = 26, StringView
9191

9292
size_t value = 0;
9393
for (size_t i = str.length(); i > 0; --i) {
94-
auto digit_value = map.find_first_of(str[i - 1]).value_or(0);
94+
auto digit_value = map.find(str[i - 1]).value_or(0);
9595
// NOTE: Refer to the note in `String::bijective_base_from()'.
9696
if (i == str.length() && str.length() > 1)
9797
++digit_value;

Userland/Services/LaunchServer/Launcher.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static bool spawn(String executable, const Vector<String>& arguments);
2626

2727
String Handler::name_from_executable(const StringView& executable)
2828
{
29-
auto separator = executable.find_last_of('/');
29+
auto separator = executable.find_last('/');
3030
if (separator.has_value()) {
3131
auto start = separator.value() + 1;
3232
return executable.substring_view(start, executable.length() - start);

Userland/Services/WindowServer/Cursor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ CursorParams CursorParams::parse_from_filename(const StringView& cursor_path, co
1515
{
1616
LexicalPath path(cursor_path);
1717
auto file_title = path.title();
18-
auto last_dot_in_title = StringView(file_title).find_last_of('.');
18+
auto last_dot_in_title = file_title.find_last('.');
1919
if (!last_dot_in_title.has_value() || last_dot_in_title.value() == 0) {
2020
// No encoded params in filename. Not an error, we'll just use defaults
2121
return { default_hotspot };

Userland/Utilities/pro.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ int main(int argc, char** argv)
163163
.value_name = "header-value",
164164
.accept_value = [&](auto* s) {
165165
StringView header { s };
166-
auto split = header.find_first_of(':');
166+
auto split = header.find(':');
167167
if (!split.has_value())
168168
return false;
169169
request_headers.set(header.substring_view(0, split.value()), header.substring_view(split.value() + 1));

0 commit comments

Comments
 (0)