From 5db1eb996173b34960f48c7ea8ce4fd33acde157 Mon Sep 17 00:00:00 2001 From: MacDue Date: Thu, 13 Apr 2023 23:29:51 +0100 Subject: 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 +- 3 files changed, 9 insertions(+), 1 deletion(-) (limited to 'AK') 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(); -- cgit v1.2.3