From f47282093a9de71da52bbb66750251e9808d7476 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 4 Sep 2021 00:24:48 +0200 Subject: [PATCH] String allocation not needed --- lychee-lib/src/helpers/url.rs | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/lychee-lib/src/helpers/url.rs b/lychee-lib/src/helpers/url.rs index b00624d..fe275f6 100644 --- a/lychee-lib/src/helpers/url.rs +++ b/lychee-lib/src/helpers/url.rs @@ -2,12 +2,12 @@ use linkify::LinkFinder; /// Remove all GET parameters from a URL. /// The link is not a URL but a String as it may not have a base domain. -pub(crate) fn remove_get_params(url: &str) -> String { +pub(crate) fn remove_get_params(url: &str) -> &str { let path = match url.split_once('?') { Some((path, _params)) => path, None => url, }; - path.to_string() + path } /// Determine if an element's attribute contains a link / URL. @@ -43,26 +43,17 @@ mod test_fs_tree { #[test] fn test_remove_get_params() { - assert_eq!(remove_get_params("/"), "/".to_string()); - assert_eq!( - remove_get_params("index.html?foo=bar"), - "index.html".to_string() - ); - assert_eq!( - remove_get_params("/index.html?foo=bar"), - "/index.html".to_string() - ); + assert_eq!(remove_get_params("/"), "/"); + assert_eq!(remove_get_params("index.html?foo=bar"), "index.html"); + assert_eq!(remove_get_params("/index.html?foo=bar"), "/index.html"); assert_eq!( remove_get_params("/index.html?foo=bar&baz=zorx?bla=blub"), - "/index.html".to_string() + "/index.html" ); assert_eq!( remove_get_params("https://example.org/index.html?foo=bar"), - "https://example.org/index.html".to_string() - ); - assert_eq!( - remove_get_params("test.png?foo=bar"), - "test.png".to_string() + "https://example.org/index.html" ); + assert_eq!(remove_get_params("test.png?foo=bar"), "test.png"); } }