From c2f79aa368030f1409cb342564abd692d1557759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Fri, 3 Jul 2020 17:42:57 +0200 Subject: [PATCH] aquatic_http: reduce allocations in Request::urldecode --- aquatic_http/src/lib/protocol/request.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/aquatic_http/src/lib/protocol/request.rs b/aquatic_http/src/lib/protocol/request.rs index 35cddac..bfbbbc6 100644 --- a/aquatic_http/src/lib/protocol/request.rs +++ b/aquatic_http/src/lib/protocol/request.rs @@ -57,7 +57,8 @@ impl Request { .with_context(|| format!("no key in {}", part))?; let value = key_and_value.next() .with_context(|| format!("no value in {}", part))?; - let value = Self::urldecode(value).to_string(); + let value = Self::urldecode(value)? + .to_string(); if key == "info_hash" { info_hashes.push(value); @@ -119,7 +120,7 @@ impl Request { /// UTF-8 string, meaning that non-ascii bytes are invalid characters. /// Therefore, these bytes must be converted to their equivalent multi-byte /// UTF-8 encodings. - fn urldecode(value: &str) -> String { + fn urldecode(value: &str) -> anyhow::Result { let mut processed = String::new(); for (i, part) in value.split('%').enumerate(){ @@ -127,23 +128,23 @@ impl Request { processed.push_str(part); } else if part.len() >= 2 { let mut two_first = String::with_capacity(2); - let mut rest = String::new(); for (j, c) in part.chars().enumerate(){ - if j < 2 { + if j == 0 { two_first.push(c); + } else if j == 1 { + two_first.push(c); + + let byte = u8::from_str_radix(&two_first, 16)?; + + processed.push(byte as char); } else { - rest.push(c); + processed.push(c); } } - - let byte = u8::from_str_radix(&two_first, 16).unwrap(); - - processed.push(byte as char); - processed.push_str(&rest); } } - processed + Ok(processed) } } \ No newline at end of file