Refactored core::str::pattern to become a user-facing module and hide away
CharEq.
This commit is contained in:
parent
91d1aa71f6
commit
c04f22a667
@ -58,6 +58,8 @@ use core::iter::{Iterator, Extend};
|
||||
use core::option::Option::{self, Some, None};
|
||||
use core::result::Result;
|
||||
use core::str as core_str;
|
||||
use core::str::pattern::Pattern;
|
||||
use core::str::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher};
|
||||
use unicode::str::{UnicodeStr, Utf16Encoder};
|
||||
|
||||
use core::convert::AsRef;
|
||||
@ -78,8 +80,7 @@ pub use core::str::{MatchIndices, RMatchIndices};
|
||||
pub use core::str::{from_utf8, Chars, CharIndices, Bytes};
|
||||
pub use core::str::{from_utf8_unchecked, ParseBoolError};
|
||||
pub use unicode::str::{Words, Graphemes, GraphemeIndices};
|
||||
pub use core::str::Pattern;
|
||||
pub use core::str::{Searcher, ReverseSearcher, DoubleEndedSearcher, SearchStep};
|
||||
pub use core::str::pattern;
|
||||
|
||||
/*
|
||||
Section: Creating a string
|
||||
|
@ -24,7 +24,7 @@ use core::mem;
|
||||
use core::ops::{self, Deref, Add, Index};
|
||||
use core::ptr;
|
||||
use core::slice;
|
||||
use core::str::Pattern;
|
||||
use core::str::pattern::Pattern;
|
||||
use unicode::str as unicode_str;
|
||||
use unicode::str::Utf16Item;
|
||||
|
||||
|
@ -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.
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -185,14 +185,14 @@ fn trim_ws() {
|
||||
}
|
||||
|
||||
mod pattern {
|
||||
use std::str::Pattern;
|
||||
use std::str::{Searcher, ReverseSearcher};
|
||||
use std::str::SearchStep::{self, Match, Reject, Done};
|
||||
use std::str::pattern::Pattern;
|
||||
use std::str::pattern::{Searcher, ReverseSearcher};
|
||||
use std::str::pattern::SearchStep::{self, Match, Reject, Done};
|
||||
|
||||
macro_rules! make_test {
|
||||
($name:ident, $p:expr, $h:expr, [$($e:expr,)*]) => {
|
||||
mod $name {
|
||||
use std::str::SearchStep::{Match, Reject};
|
||||
use std::str::pattern::SearchStep::{Match, Reject};
|
||||
use super::{cmp_search_to_vec};
|
||||
#[test]
|
||||
fn fwd() {
|
||||
|
Loading…
Reference in New Issue
Block a user