From 5db1eb996173b34960f48c7ea8ce4fd33acde157 Mon Sep 17 00:00:00 2001 From: MacDue Date: Thu, 13 Apr 2023 23:29:51 +0100 Subject: [PATCH] AK+Everywhere: Replace URL::paths() with path_segment_at_index() This allows accessing and looping over the path segments in a URL without necessarily allocating a new vector if you want them percent decoded too (which path_segment_at_index() has an option for). --- AK/URL.cpp | 6 +++++ AK/URL.h | 2 ++ AK/URLParser.cpp | 2 +- Tests/AK/TestURL.cpp | 26 +++++++++---------- Userland/Applications/Assistant/Providers.cpp | 2 +- Userland/Libraries/LibManual/Node.cpp | 11 ++++---- 6 files changed, 28 insertions(+), 21 deletions(-) diff --git a/AK/URL.cpp b/AK/URL.cpp index 0aa17f6c52..c50aa6a5d0 100644 --- a/AK/URL.cpp +++ b/AK/URL.cpp @@ -45,6 +45,12 @@ DeprecatedString URL::password(ApplyPercentDecoding apply_percent_decoding) cons return apply_percent_decoding == ApplyPercentDecoding::Yes ? percent_decode(m_password) : m_password; } +DeprecatedString URL::path_segment_at_index(size_t index, ApplyPercentDecoding apply_percent_decoding) const +{ + VERIFY(index < path_segment_count()); + return apply_percent_decoding == ApplyPercentDecoding::Yes ? percent_decode(m_paths[index]) : m_paths[index]; +} + DeprecatedString URL::basename(ApplyPercentDecoding apply_percent_decoding) const { if (!m_valid) diff --git a/AK/URL.h b/AK/URL.h index 85f2f68042..a71533b51d 100644 --- a/AK/URL.h +++ b/AK/URL.h @@ -65,6 +65,8 @@ public: DeprecatedString query(ApplyPercentDecoding = ApplyPercentDecoding::No) const; DeprecatedString fragment(ApplyPercentDecoding = ApplyPercentDecoding::Yes) const; Optional port() const { return m_port; } + DeprecatedString path_segment_at_index(size_t index, ApplyPercentDecoding = ApplyPercentDecoding::Yes) const; + size_t path_segment_count() const { return m_paths.size(); } u16 port_or_default() const { return m_port.value_or(default_port_for_scheme(m_scheme)); } bool cannot_be_a_base_url() const { return m_cannot_be_a_base_url; } diff --git a/AK/URLParser.cpp b/AK/URLParser.cpp index 842f7f4a0a..0cfb341fd5 100644 --- a/AK/URLParser.cpp +++ b/AK/URLParser.cpp @@ -536,7 +536,7 @@ URL URLParser::parse(StringView raw_input, Optional const& base_url, Option url->m_query = {}; auto substring_from_pointer = input.substring_view(iterator - input.begin()).as_string(); if (!starts_with_windows_drive_letter(substring_from_pointer)) { - if (!url->paths().is_empty() && !(url->scheme() == "file" && url->paths().size() == 1 && is_normalized_windows_drive_letter(url->paths()[0]))) + if (!url->m_paths.is_empty() && !(url->scheme() == "file" && url->m_paths.size() == 1 && is_normalized_windows_drive_letter(url->m_paths[0]))) url->m_paths.remove(url->m_paths.size() - 1); } else { report_validation_error(); diff --git a/Tests/AK/TestURL.cpp b/Tests/AK/TestURL.cpp index c886b313f1..52024c64a0 100644 --- a/Tests/AK/TestURL.cpp +++ b/Tests/AK/TestURL.cpp @@ -203,8 +203,8 @@ TEST_CASE(mailto_url) EXPECT_EQ(url.scheme(), "mailto"); EXPECT(url.host().is_null()); EXPECT_EQ(url.port_or_default(), 0); - EXPECT_EQ(url.paths().size(), 1u); - EXPECT_EQ(url.paths()[0], "mail@example.com"); + EXPECT_EQ(url.path_segment_count(), 1u); + EXPECT_EQ(url.path_segment_at_index(0), "mail@example.com"); EXPECT(url.query().is_null()); EXPECT(url.fragment().is_null()); EXPECT_EQ(url.serialize(), "mailto:mail@example.com"); @@ -329,20 +329,20 @@ TEST_CASE(create_with_file_scheme) EXPECT(url.is_valid()); EXPECT_EQ(url.scheme(), "file"); EXPECT_EQ(url.port_or_default(), 0); - EXPECT_EQ(url.paths().size(), 3u); - EXPECT_EQ(url.paths()[0], "home"); - EXPECT_EQ(url.paths()[1], "anon"); - EXPECT_EQ(url.paths()[2], "README.md"); + EXPECT_EQ(url.path_segment_count(), 3u); + EXPECT_EQ(url.path_segment_at_index(0), "home"); + EXPECT_EQ(url.path_segment_at_index(1), "anon"); + EXPECT_EQ(url.path_segment_at_index(2), "README.md"); EXPECT_EQ(url.serialize_path(), "/home/anon/README.md"); EXPECT(url.query().is_null()); EXPECT(url.fragment().is_null()); url = URL::create_with_file_scheme("/home/anon/"); EXPECT(url.is_valid()); - EXPECT_EQ(url.paths().size(), 3u); - EXPECT_EQ(url.paths()[0], "home"); - EXPECT_EQ(url.paths()[1], "anon"); - EXPECT_EQ(url.paths()[2], ""); + EXPECT_EQ(url.path_segment_count(), 3u); + EXPECT_EQ(url.path_segment_at_index(0), "home"); + EXPECT_EQ(url.path_segment_at_index(1), "anon"); + EXPECT_EQ(url.path_segment_at_index(2), ""); EXPECT_EQ(url.serialize_path(), "/home/anon/"); url = URL("file:///home/anon/"sv); @@ -399,9 +399,9 @@ TEST_CASE(complete_file_url_with_base) URL url { "file:///home/index.html" }; EXPECT(url.is_valid()); EXPECT_EQ(url.serialize_path(), "/home/index.html"); - EXPECT_EQ(url.paths().size(), 2u); - EXPECT_EQ(url.paths()[0], "home"); - EXPECT_EQ(url.paths()[1], "index.html"); + EXPECT_EQ(url.path_segment_count(), 2u); + EXPECT_EQ(url.path_segment_at_index(0), "home"); + EXPECT_EQ(url.path_segment_at_index(1), "index.html"); auto sub_url = url.complete_url("js/app.js"sv); EXPECT(sub_url.is_valid()); diff --git a/Userland/Applications/Assistant/Providers.cpp b/Userland/Applications/Assistant/Providers.cpp index d1cbbadfcf..72413b4988 100644 --- a/Userland/Applications/Assistant/Providers.cpp +++ b/Userland/Applications/Assistant/Providers.cpp @@ -226,7 +226,7 @@ void URLProvider::query(DeprecatedString const& query, Function> Node::try_find_from_help_url(URL const& url) { if (url.host() != "man") return Error::from_string_view("Bad help operation"sv); - if (url.paths().size() < 2) + if (url.path_segment_count() < 2) return Error::from_string_view("Bad help page URL"sv); - auto paths = url.paths(); - auto const section = paths.take_first(); + auto const section = url.path_segment_at_index(0); auto maybe_section_number = section.to_uint(); if (!maybe_section_number.has_value()) return Error::from_string_view("Bad section number"sv); @@ -84,16 +83,16 @@ ErrorOr> Node::try_find_from_help_url(URL const& url) NonnullRefPtr current_node = sections[section_number - 1]; - while (!paths.is_empty()) { - auto next_path_segment = TRY(String::from_deprecated_string(paths.take_first())); + for (size_t i = 1; i < url.path_segment_count(); i++) { auto children = TRY(current_node->children()); for (auto const& child : children) { - if (TRY(child->name()) == next_path_segment) { + if (TRY(child->name()) == url.path_segment_at_index(i).view()) { current_node = child; break; } } } + return current_node; }