From 4d449218853ac84619262575303e476e27051dff Mon Sep 17 00:00:00 2001 From: Bakyt Niiazaliev Date: Fri, 4 Jul 2025 01:57:31 +0700 Subject: [PATCH] fix: correct condition order in quote function and implement PercentEncoded trait for String and &str --- src/urlparse.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/urlparse.rs b/src/urlparse.rs index e628cd80..4e6181c3 100644 --- a/src/urlparse.rs +++ b/src/urlparse.rs @@ -52,7 +52,7 @@ pub fn quote(string: &str, safe: &str) -> String { let mut start = 0; let mut i = 0; while i < s.len() { - if i + 2 < s.len() && s[i] == b'%' && is_percent_encoded(&s[i..i + 3]) { + if s[i] == b'%' && i + 2 < s.len() && is_percent_encoded(&s[i..i + 3]) { if start < i { result.push_str(&percent_encoded(&string[start..i], safe)); } @@ -70,3 +70,19 @@ pub fn quote(string: &str, safe: &str) -> String { result } + +pub(crate) trait PercentEncoded { + fn percent_encoded(&self, safe: &str) -> String; +} + +impl PercentEncoded for String { + fn percent_encoded(&self, safe: &str) -> String { + quote(self, safe) + } +} + +impl PercentEncoded for &str { + fn percent_encoded(&self, safe: &str) -> String { + quote(self, safe) + } +}