diff options
| author | bors <bors@rust-lang.org> | 2021-10-31 15:45:39 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2021-10-31 15:45:39 +0000 |
| commit | c7e4740ec18996e082fe6e29ebf7efdc7dda418f (patch) | |
| tree | ce985db1c3f3cb7ef1b5ffb73d573ee9d3157dc4 | |
| parent | 68b554e6af18726fe6fa8de2134c59c441e0b019 (diff) | |
| parent | 28f7890a29bd9531fb91295bc130a911820e9fb6 (diff) | |
| download | rust-c7e4740ec18996e082fe6e29ebf7efdc7dda418f.tar.gz rust-c7e4740ec18996e082fe6e29ebf7efdc7dda418f.zip | |
Auto merge of #86336 - camsteffen:char-array-pattern, r=joshtriplett
impl Pattern for char array Closes #39511 Closes #86329
| -rw-r--r-- | library/core/src/str/pattern.rs | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/library/core/src/str/pattern.rs b/library/core/src/str/pattern.rs index 55ac1aa765c..031fb8e8b21 100644 --- a/library/core/src/str/pattern.rs +++ b/library/core/src/str/pattern.rs @@ -22,6 +22,8 @@ //! assert_eq!(s.find("you"), Some(4)); //! // char pattern //! assert_eq!(s.find('n'), Some(2)); +//! // array of chars pattern +//! assert_eq!(s.find(&['a', 'e', 'i', 'o', 'u']), Some(1)); //! // slice of chars pattern //! assert_eq!(s.find(&['a', 'e', 'i', 'o', 'u'][..]), Some(1)); //! // closure pattern @@ -79,6 +81,11 @@ use crate::slice::memchr; /// assert_eq!("abaaa".find('b'), Some(1)); /// assert_eq!("abaaa".find('c'), None); /// +/// // &[char; N] +/// assert_eq!("ab".find(&['b', 'a']), Some(0)); +/// assert_eq!("abaaa".find(&['a', 'z']), Some(0)); +/// assert_eq!("abaaa".find(&['c', 'd']), None); +/// /// // &[char] /// assert_eq!("ab".find(&['b', 'a'][..]), Some(0)); /// assert_eq!("abaaa".find(&['a', 'z'][..]), Some(0)); @@ -601,6 +608,20 @@ where } } +impl<const N: usize> MultiCharEq for [char; N] { + #[inline] + fn matches(&mut self, c: char) -> bool { + self.iter().any(|&m| m == c) + } +} + +impl<const N: usize> MultiCharEq for &[char; N] { + #[inline] + fn matches(&mut self, c: char) -> bool { + self.iter().any(|&m| m == c) + } +} + impl MultiCharEq for &[char] { #[inline] fn matches(&mut self, c: char) -> bool { @@ -752,6 +773,58 @@ macro_rules! searcher_methods { }; } +/// Associated type for `<[char; N] as Pattern<'a>>::Searcher`. +#[derive(Clone, Debug)] +pub struct CharArraySearcher<'a, const N: usize>( + <MultiCharEqPattern<[char; N]> as Pattern<'a>>::Searcher, +); + +/// Associated type for `<&[char; N] as Pattern<'a>>::Searcher`. +#[derive(Clone, Debug)] +pub struct CharArrayRefSearcher<'a, 'b, const N: usize>( + <MultiCharEqPattern<&'b [char; N]> as Pattern<'a>>::Searcher, +); + +/// Searches for chars that are equal to any of the [`char`]s in the array. +/// +/// # Examples +/// +/// ``` +/// assert_eq!("Hello world".find(['l', 'l']), Some(2)); +/// assert_eq!("Hello world".find(['l', 'l']), Some(2)); +/// ``` +impl<'a, const N: usize> Pattern<'a> for [char; N] { + pattern_methods!(CharArraySearcher<'a, N>, MultiCharEqPattern, CharArraySearcher); +} + +unsafe impl<'a, const N: usize> Searcher<'a> for CharArraySearcher<'a, N> { + searcher_methods!(forward); +} + +unsafe impl<'a, const N: usize> ReverseSearcher<'a> for CharArraySearcher<'a, N> { + searcher_methods!(reverse); +} + +/// Searches for chars that are equal to any of the [`char`]s in the array. +/// +/// # Examples +/// +/// ``` +/// assert_eq!("Hello world".find(&['l', 'l']), Some(2)); +/// assert_eq!("Hello world".find(&['l', 'l']), Some(2)); +/// ``` +impl<'a, 'b, const N: usize> Pattern<'a> for &'b [char; N] { + pattern_methods!(CharArrayRefSearcher<'a, 'b, N>, MultiCharEqPattern, CharArrayRefSearcher); +} + +unsafe impl<'a, 'b, const N: usize> Searcher<'a> for CharArrayRefSearcher<'a, 'b, N> { + searcher_methods!(forward); +} + +unsafe impl<'a, 'b, const N: usize> ReverseSearcher<'a> for CharArrayRefSearcher<'a, 'b, N> { + searcher_methods!(reverse); +} + ///////////////////////////////////////////////////////////////////////////// // Impl for &[char] ///////////////////////////////////////////////////////////////////////////// |
