Refactored core::str::pattern to become a user-facing module and hide away

CharEq.
This commit is contained in:
Marvin Löbel
2015-03-23 14:21:42 +01:00
parent 91d1aa71f6
commit c04f22a667
5 changed files with 25 additions and 20 deletions

View File

@@ -17,6 +17,8 @@
#![doc(primitive = "str")]
use self::OldSearcher::{TwoWay, TwoWayLong};
use self::pattern::Pattern;
use self::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher};
use char::CharExt;
use clone::Clone;
@@ -34,10 +36,7 @@ use result::Result::{self, Ok, Err};
use slice::{self, SliceExt};
use usize;
pub use self::pattern::Pattern;
pub use self::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher, SearchStep};
mod pattern;
pub mod pattern;
/// A trait to abstract the idea of creating a new instance of a type from a
/// string.

View File

@@ -471,29 +471,28 @@ fn str_search_step<F, G>(mut m: &mut StrSearcher,
macro_rules! pattern_methods {
($t:ty, $pmap:expr, $smap:expr) => {
// FIXME: #22463
//type Searcher = $t;
type Searcher = $t;
#[inline]
fn into_searcher(self, haystack: &'a str) -> $t {
$smap($pmap(self).into_searcher(haystack))
($smap)(($pmap)(self).into_searcher(haystack))
}
#[inline]
fn is_contained_in(self, haystack: &'a str) -> bool {
$pmap(self).is_contained_in(haystack)
($pmap)(self).is_contained_in(haystack)
}
#[inline]
fn is_prefix_of(self, haystack: &'a str) -> bool {
$pmap(self).is_prefix_of(haystack)
($pmap)(self).is_prefix_of(haystack)
}
#[inline]
fn is_suffix_of(self, haystack: &'a str) -> bool
where $t: ReverseSearcher<'a>
{
$pmap(self).is_suffix_of(haystack)
($pmap)(self).is_suffix_of(haystack)
}
}
}
@@ -553,7 +552,6 @@ impl<'a> DoubleEndedSearcher<'a> for CharSearcher<'a> {}
/// Searches for chars that are equal to a given char
impl<'a> Pattern<'a> for char {
type Searcher = CharSearcher<'a>;
pattern_methods!(CharSearcher<'a>, CharEqPattern, CharSearcher);
}
@@ -579,7 +577,6 @@ impl<'a, 'b> DoubleEndedSearcher<'a> for CharSliceSearcher<'a, 'b> {}
/// Searches for chars that are equal to any of the chars in the array
impl<'a, 'b> Pattern<'a> for &'b [char] {
type Searcher = CharSliceSearcher<'a, 'b>;
pattern_methods!(CharSliceSearcher<'a, 'b>, CharEqPattern, CharSliceSearcher);
}
@@ -609,6 +606,14 @@ impl<'a, F> DoubleEndedSearcher<'a> for CharPredicateSearcher<'a, F>
/// Searches for chars that match the given predicate
impl<'a, F> Pattern<'a> for F where F: FnMut(char) -> bool {
type Searcher = CharPredicateSearcher<'a, F>;
pattern_methods!(CharPredicateSearcher<'a, F>, CharEqPattern, CharPredicateSearcher);
}
/////////////////////////////////////////////////////////////////////////////
// Impl for &&str
/////////////////////////////////////////////////////////////////////////////
/// Delegates to the `&str` impl.
impl<'a, 'b> Pattern<'a> for &'b &'b str {
pattern_methods!(StrSearcher<'a, 'b>, |&s| s, |s| s);
}