From ac478f2f610bd93c25c82491526ea153ad103ac0 Mon Sep 17 00:00:00 2001 From: Nikhil Benesch Date: Fri, 6 Mar 2020 15:11:24 -0500 Subject: Optimize strip_prefix and strip_suffix with str patterns Constructing a Searcher in strip_prefix and strip_suffix is unnecessarily slow when the pattern is a fixed-length string. Add strip_prefix and strip_suffix methods to the Pattern trait, and add optimized implementations of these methods in the str implementation. The old implementation is retained as the default for these methods. --- src/liballoc/string.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/liballoc/string.rs') diff --git a/src/liballoc/string.rs b/src/liballoc/string.rs index 7c89d38caa4..1e5fe125c55 100644 --- a/src/liballoc/string.rs +++ b/src/liballoc/string.rs @@ -1849,6 +1849,21 @@ impl<'a, 'b> Pattern<'a> for &'b String { fn is_prefix_of(self, haystack: &'a str) -> bool { self[..].is_prefix_of(haystack) } + + #[inline] + fn strip_prefix_of(self, haystack: &'a str) -> Option<&'a str> { + self[..].strip_prefix_of(haystack) + } + + #[inline] + fn is_suffix_of(self, haystack: &'a str) -> bool { + self[..].is_suffix_of(haystack) + } + + #[inline] + fn strip_suffix_of(self, haystack: &'a str) -> Option<&'a str> { + self[..].strip_suffix_of(haystack) + } } #[stable(feature = "rust1", since = "1.0.0")] -- cgit 1.4.1-3-g733a5