Skip to content

Commit c7e4740

Browse files
committedOct 31, 2021
Auto merge of #86336 - camsteffen:char-array-pattern, r=joshtriplett
impl Pattern for char array Closes #39511 Closes #86329
2 parents 68b554e + 28f7890 commit c7e4740

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
 

‎library/core/src/str/pattern.rs

+73
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
//! assert_eq!(s.find("you"), Some(4));
2323
//! // char pattern
2424
//! assert_eq!(s.find('n'), Some(2));
25+
//! // array of chars pattern
26+
//! assert_eq!(s.find(&['a', 'e', 'i', 'o', 'u']), Some(1));
2527
//! // slice of chars pattern
2628
//! assert_eq!(s.find(&['a', 'e', 'i', 'o', 'u'][..]), Some(1));
2729
//! // closure pattern
@@ -79,6 +81,11 @@ use crate::slice::memchr;
7981
/// assert_eq!("abaaa".find('b'), Some(1));
8082
/// assert_eq!("abaaa".find('c'), None);
8183
///
84+
/// // &[char; N]
85+
/// assert_eq!("ab".find(&['b', 'a']), Some(0));
86+
/// assert_eq!("abaaa".find(&['a', 'z']), Some(0));
87+
/// assert_eq!("abaaa".find(&['c', 'd']), None);
88+
///
8289
/// // &[char]
8390
/// assert_eq!("ab".find(&['b', 'a'][..]), Some(0));
8491
/// assert_eq!("abaaa".find(&['a', 'z'][..]), Some(0));
@@ -601,6 +608,20 @@ where
601608
}
602609
}
603610

611+
impl<const N: usize> MultiCharEq for [char; N] {
612+
#[inline]
613+
fn matches(&mut self, c: char) -> bool {
614+
self.iter().any(|&m| m == c)
615+
}
616+
}
617+
618+
impl<const N: usize> MultiCharEq for &[char; N] {
619+
#[inline]
620+
fn matches(&mut self, c: char) -> bool {
621+
self.iter().any(|&m| m == c)
622+
}
623+
}
624+
604625
impl MultiCharEq for &[char] {
605626
#[inline]
606627
fn matches(&mut self, c: char) -> bool {
@@ -752,6 +773,58 @@ macro_rules! searcher_methods {
752773
};
753774
}
754775

776+
/// Associated type for `<[char; N] as Pattern<'a>>::Searcher`.
777+
#[derive(Clone, Debug)]
778+
pub struct CharArraySearcher<'a, const N: usize>(
779+
<MultiCharEqPattern<[char; N]> as Pattern<'a>>::Searcher,
780+
);
781+
782+
/// Associated type for `<&[char; N] as Pattern<'a>>::Searcher`.
783+
#[derive(Clone, Debug)]
784+
pub struct CharArrayRefSearcher<'a, 'b, const N: usize>(
785+
<MultiCharEqPattern<&'b [char; N]> as Pattern<'a>>::Searcher,
786+
);
787+
788+
/// Searches for chars that are equal to any of the [`char`]s in the array.
789+
///
790+
/// # Examples
791+
///
792+
/// ```
793+
/// assert_eq!("Hello world".find(['l', 'l']), Some(2));
794+
/// assert_eq!("Hello world".find(['l', 'l']), Some(2));
795+
/// ```
796+
impl<'a, const N: usize> Pattern<'a> for [char; N] {
797+
pattern_methods!(CharArraySearcher<'a, N>, MultiCharEqPattern, CharArraySearcher);
798+
}
799+
800+
unsafe impl<'a, const N: usize> Searcher<'a> for CharArraySearcher<'a, N> {
801+
searcher_methods!(forward);
802+
}
803+
804+
unsafe impl<'a, const N: usize> ReverseSearcher<'a> for CharArraySearcher<'a, N> {
805+
searcher_methods!(reverse);
806+
}
807+
808+
/// Searches for chars that are equal to any of the [`char`]s in the array.
809+
///
810+
/// # Examples
811+
///
812+
/// ```
813+
/// assert_eq!("Hello world".find(&['l', 'l']), Some(2));
814+
/// assert_eq!("Hello world".find(&['l', 'l']), Some(2));
815+
/// ```
816+
impl<'a, 'b, const N: usize> Pattern<'a> for &'b [char; N] {
817+
pattern_methods!(CharArrayRefSearcher<'a, 'b, N>, MultiCharEqPattern, CharArrayRefSearcher);
818+
}
819+
820+
unsafe impl<'a, 'b, const N: usize> Searcher<'a> for CharArrayRefSearcher<'a, 'b, N> {
821+
searcher_methods!(forward);
822+
}
823+
824+
unsafe impl<'a, 'b, const N: usize> ReverseSearcher<'a> for CharArrayRefSearcher<'a, 'b, N> {
825+
searcher_methods!(reverse);
826+
}
827+
755828
/////////////////////////////////////////////////////////////////////////////
756829
// Impl for &[char]
757830
/////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)