Skip to content

Commit

Permalink
Substring now takes the needle as generic type T instead of &str
Browse files Browse the repository at this point in the history
This allows to either own (consume) the needle or keep a reference to it.
  • Loading branch information
tsnoam committed Aug 25, 2022
1 parent 7a5e775 commit eb07c4b
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,22 @@ impl<T> ByteSubstring<T> where T: AsRef<[u8]> {
/// A convenience type that can be used in a constant or static.
pub type ByteSubstringConst = ByteSubstring<&'static [u8]>;

/// Searches a string for the first occurence of the substring.
pub struct Substring<'a>(ByteSubstring<&'a [u8]>);
/// Searches a string for the first occurrence of the substring.
pub struct Substring<T>(ByteSubstring<T>);

impl<'a> Substring<'a> {
impl<'a> Substring<&'a [u8]> {
pub /* const */ fn new(needle: &'a str) -> Self {
Substring(ByteSubstring::new(needle.as_bytes()))
}
}

impl Substring<Vec<u8>> {
pub fn new_owned(needle: String) -> Self {
Substring(ByteSubstring::new(needle.into_bytes()))
}
}

impl<T> Substring<T> where T: AsRef<[u8]> {
#[cfg(feature = "pattern")]
fn needle_len(&self) -> usize {
self.0.needle_len()
Expand All @@ -344,7 +352,7 @@ impl<'a> Substring<'a> {
}

/// A convenience type that can be used in a constant or static.
pub type SubstringConst = Substring<'static>;
pub type SubstringConst = Substring<&'static str>;

#[cfg(all(test, feature = "benchmarks"))]
mod bench {
Expand Down

0 comments on commit eb07c4b

Please sign in to comment.