diff options
author | MacDue <macdue@dueutil.tech> | 2023-04-13 23:29:51 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-04-15 06:37:04 +0200 |
commit | 5db1eb996173b34960f48c7ea8ce4fd33acde157 (patch) | |
tree | 3821497e14788bebd9a675ca44d73d574cb3f420 | |
parent | 35612c6a7f7602ff46d56bbbbc8b33b28b411409 (diff) | |
download | serenity-5db1eb996173b34960f48c7ea8ce4fd33acde157.zip |
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).
-rw-r--r-- | AK/URL.cpp | 6 | ||||
-rw-r--r-- | AK/URL.h | 2 | ||||
-rw-r--r-- | AK/URLParser.cpp | 2 | ||||
-rw-r--r-- | Tests/AK/TestURL.cpp | 26 | ||||
-rw-r--r-- | Userland/Applications/Assistant/Providers.cpp | 2 | ||||
-rw-r--r-- | 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) @@ -65,6 +65,8 @@ public: DeprecatedString query(ApplyPercentDecoding = ApplyPercentDecoding::No) const; DeprecatedString fragment(ApplyPercentDecoding = ApplyPercentDecoding::Yes) const; Optional<u16> 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<URL> 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<void(Vector<Nonn url.set_scheme("http"); if (url.host().is_empty()) url.set_host(query); - if (url.paths().is_empty()) + if (url.path_segment_count() == 0) url.set_paths({ "" }); if (!url.is_valid()) diff --git a/Userland/Libraries/LibManual/Node.cpp b/Userland/Libraries/LibManual/Node.cpp index 3b48ae90c0..4c89ff8a7a 100644 --- a/Userland/Libraries/LibManual/Node.cpp +++ b/Userland/Libraries/LibManual/Node.cpp @@ -70,11 +70,10 @@ ErrorOr<NonnullRefPtr<Node const>> 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<NonnullRefPtr<Node const>> Node::try_find_from_help_url(URL const& url) NonnullRefPtr<Node const> 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; } |