Skip to content

Commit

Permalink
Fix substring helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Skittyblock committed Jul 28, 2024
1 parent 6b714d8 commit a7974a9
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions crates/helpers/src/substring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,47 +27,42 @@ use core::str::pattern::{Pattern, ReverseSearcher, Searcher};

pub trait Substring {
/// Returns a substring before the first occurence of pattern.
fn substring_before<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<&'a str>;
fn substring_before<'a, P: Pattern>(&'a self, pat: P) -> Option<&'a str>;

Check warning on line 30 in crates/helpers/src/substring.rs

View workflow job for this annotation

GitHub Actions / clippy

the following explicit lifetimes could be elided: 'a

warning: the following explicit lifetimes could be elided: 'a --> crates/helpers/src/substring.rs:30:25 | 30 | fn substring_before<'a, P: Pattern>(&'a self, pat: P) -> Option<&'a str>; | ^^ ^^ ^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes = note: `#[warn(clippy::needless_lifetimes)]` on by default help: elide the lifetimes | 30 - fn substring_before<'a, P: Pattern>(&'a self, pat: P) -> Option<&'a str>; 30 + fn substring_before<P: Pattern>(&self, pat: P) -> Option<&str>; |

/// Returns a substring before the last occurence of pattern.
fn substring_before_last<'a, P>(&'a self, pat: P) -> Option<&'a str>
fn substring_before_last<P: Pattern>(&self, pat: P) -> Option<&str>
where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: ReverseSearcher<'a>;
for<'a> P::Searcher<'a>: ReverseSearcher<'a>;

/// Returns a substring after the first occurence of pattern.
fn substring_after<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<&'a str>;
fn substring_after<'a, P: Pattern>(&'a self, pat: P) -> Option<&'a str>;

Check warning on line 38 in crates/helpers/src/substring.rs

View workflow job for this annotation

GitHub Actions / clippy

the following explicit lifetimes could be elided: 'a

warning: the following explicit lifetimes could be elided: 'a --> crates/helpers/src/substring.rs:38:24 | 38 | fn substring_after<'a, P: Pattern>(&'a self, pat: P) -> Option<&'a str>; | ^^ ^^ ^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes help: elide the lifetimes | 38 - fn substring_after<'a, P: Pattern>(&'a self, pat: P) -> Option<&'a str>; 38 + fn substring_after<P: Pattern>(&self, pat: P) -> Option<&str>; |

/// Returns a substring after the last occurence of pattern.
fn substring_after_last<'a, P>(&'a self, pat: P) -> Option<&'a str>
where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: ReverseSearcher<'a>;
P: Pattern,
<P as Pattern>::Searcher<'a>: ReverseSearcher<'a>;
}

impl Substring for str {
#[inline]
fn substring_before<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<&'a str> {
fn substring_before<'a, P: Pattern>(&'a self, pat: P) -> Option<&'a str> {

Check warning on line 49 in crates/helpers/src/substring.rs

View workflow job for this annotation

GitHub Actions / clippy

the following explicit lifetimes could be elided: 'a

warning: the following explicit lifetimes could be elided: 'a --> crates/helpers/src/substring.rs:49:25 | 49 | fn substring_before<'a, P: Pattern>(&'a self, pat: P) -> Option<&'a str> { | ^^ ^^ ^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes help: elide the lifetimes | 49 - fn substring_before<'a, P: Pattern>(&'a self, pat: P) -> Option<&'a str> { 49 + fn substring_before<P: Pattern>(&self, pat: P) -> Option<&str> { |
match self.find(pat) {
Some(i) => Some(&self[..i]),
None => None,
}
}

#[inline]
fn substring_before_last<'a, P>(&'a self, pat: P) -> Option<&'a str>
fn substring_before_last<P: Pattern>(&self, pat: P) -> Option<&str>
where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: ReverseSearcher<'a>,
for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
{
match self.rfind(pat) {
Some(i) => Some(&self[..i]),
None => None,
}
self.rfind(pat).map(|idx| &self[..idx])
}

#[inline]
fn substring_after<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<&'a str> {
fn substring_after<'a, P: Pattern>(&'a self, pat: P) -> Option<&'a str> {

Check warning on line 65 in crates/helpers/src/substring.rs

View workflow job for this annotation

GitHub Actions / clippy

the following explicit lifetimes could be elided: 'a

warning: the following explicit lifetimes could be elided: 'a --> crates/helpers/src/substring.rs:65:24 | 65 | fn substring_after<'a, P: Pattern>(&'a self, pat: P) -> Option<&'a str> { | ^^ ^^ ^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes help: elide the lifetimes | 65 - fn substring_after<'a, P: Pattern>(&'a self, pat: P) -> Option<&'a str> { 65 + fn substring_after<P: Pattern>(&self, pat: P) -> Option<&str> { |
match pat.into_searcher(self).next_match() {
Some((_, end)) => Some(&self[end..]),
None => None,
Expand All @@ -77,8 +72,8 @@ impl Substring for str {
#[inline]
fn substring_after_last<'a, P>(&'a self, pat: P) -> Option<&'a str>
where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: ReverseSearcher<'a>,
P: Pattern,
<P as Pattern>::Searcher<'a>: ReverseSearcher<'a>,
{
match pat.into_searcher(self).next_match_back() {
Some((_, end)) => Some(&self[end..]),
Expand Down

0 comments on commit a7974a9

Please sign in to comment.