Skip to content

Commit 28f7890

Browse files
committed
Add char array without ref Pattern impl
1 parent 5e4f212 commit 28f7890

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

Diff for: library/core/src/str/pattern.rs

+37-4
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,13 @@ where
608608
}
609609
}
610610

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+
611618
impl<const N: usize> MultiCharEq for &[char; N] {
612619
#[inline]
613620
fn matches(&mut self, c: char) -> bool {
@@ -768,10 +775,36 @@ macro_rules! searcher_methods {
768775

769776
/// Associated type for `<[char; N] as Pattern<'a>>::Searcher`.
770777
#[derive(Clone, Debug)]
771-
pub struct CharArraySearcher<'a, 'b, const N: usize>(
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>(
772785
<MultiCharEqPattern<&'b [char; N]> as Pattern<'a>>::Searcher,
773786
);
774787

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+
775808
/// Searches for chars that are equal to any of the [`char`]s in the array.
776809
///
777810
/// # Examples
@@ -781,14 +814,14 @@ pub struct CharArraySearcher<'a, 'b, const N: usize>(
781814
/// assert_eq!("Hello world".find(&['l', 'l']), Some(2));
782815
/// ```
783816
impl<'a, 'b, const N: usize> Pattern<'a> for &'b [char; N] {
784-
pattern_methods!(CharArraySearcher<'a, 'b, N>, MultiCharEqPattern, CharArraySearcher);
817+
pattern_methods!(CharArrayRefSearcher<'a, 'b, N>, MultiCharEqPattern, CharArrayRefSearcher);
785818
}
786819

787-
unsafe impl<'a, 'b, const N: usize> Searcher<'a> for CharArraySearcher<'a, 'b, N> {
820+
unsafe impl<'a, 'b, const N: usize> Searcher<'a> for CharArrayRefSearcher<'a, 'b, N> {
788821
searcher_methods!(forward);
789822
}
790823

791-
unsafe impl<'a, 'b, const N: usize> ReverseSearcher<'a> for CharArraySearcher<'a, 'b, N> {
824+
unsafe impl<'a, 'b, const N: usize> ReverseSearcher<'a> for CharArrayRefSearcher<'a, 'b, N> {
792825
searcher_methods!(reverse);
793826
}
794827

0 commit comments

Comments
 (0)