diff options
| author | Nikhil Benesch <nikhil.benesch@gmail.com> | 2020-03-06 15:11:24 -0500 |
|---|---|---|
| committer | Nikhil Benesch <nikhil.benesch@gmail.com> | 2020-03-30 11:10:21 -0400 |
| commit | ac478f2f610bd93c25c82491526ea153ad103ac0 (patch) | |
| tree | bc7425330cc13fef5f5be8176edfc5f677177284 /src/liballoc | |
| parent | e768d6f0bc6db7a46c9ef08254a944ba100bc5dd (diff) | |
| download | rust-ac478f2f610bd93c25c82491526ea153ad103ac0.tar.gz rust-ac478f2f610bd93c25c82491526ea153ad103ac0.zip | |
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.
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/string.rs | 15 |
1 files changed, 15 insertions, 0 deletions
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")] |
