From 28f7890a29bd9531fb91295bc130a911820e9fb6 Mon Sep 17 00:00:00 2001 From: Cameron Steffen Date: Mon, 21 Jun 2021 21:21:18 -0500 Subject: [PATCH] Add char array without ref Pattern impl --- library/core/src/str/pattern.rs | 41 +++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/library/core/src/str/pattern.rs b/library/core/src/str/pattern.rs index 90e5fbdf08e..031fb8e8b21 100644 --- a/library/core/src/str/pattern.rs +++ b/library/core/src/str/pattern.rs @@ -608,6 +608,13 @@ fn matches(&mut self, c: char) -> bool { } } +impl MultiCharEq for [char; N] { + #[inline] + fn matches(&mut self, c: char) -> bool { + self.iter().any(|&m| m == c) + } +} + impl MultiCharEq for &[char; N] { #[inline] fn matches(&mut self, c: char) -> bool { @@ -768,10 +775,36 @@ fn next_reject_back(&mut self) -> Option<(usize, usize)> { /// Associated type for `<[char; N] as Pattern<'a>>::Searcher`. #[derive(Clone, Debug)] -pub struct CharArraySearcher<'a, 'b, const N: usize>( +pub struct CharArraySearcher<'a, const N: usize>( + as Pattern<'a>>::Searcher, +); + +/// Associated type for `<&[char; N] as Pattern<'a>>::Searcher`. +#[derive(Clone, Debug)] +pub struct CharArrayRefSearcher<'a, 'b, const N: usize>( 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 @@ -781,14 +814,14 @@ pub struct CharArraySearcher<'a, 'b, const N: usize>( /// assert_eq!("Hello world".find(&['l', 'l']), Some(2)); /// ``` impl<'a, 'b, const N: usize> Pattern<'a> for &'b [char; N] { - pattern_methods!(CharArraySearcher<'a, 'b, N>, MultiCharEqPattern, CharArraySearcher); + pattern_methods!(CharArrayRefSearcher<'a, 'b, N>, MultiCharEqPattern, CharArrayRefSearcher); } -unsafe impl<'a, 'b, const N: usize> Searcher<'a> for CharArraySearcher<'a, 'b, N> { +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 CharArraySearcher<'a, 'b, N> { +unsafe impl<'a, 'b, const N: usize> ReverseSearcher<'a> for CharArrayRefSearcher<'a, 'b, N> { searcher_methods!(reverse); } -- 2.44.0