Refactored core::str::pattern to become a user-facing module and hide away
CharEq.
This commit is contained in:
@@ -58,6 +58,8 @@ use core::iter::{Iterator, Extend};
|
|||||||
use core::option::Option::{self, Some, None};
|
use core::option::Option::{self, Some, None};
|
||||||
use core::result::Result;
|
use core::result::Result;
|
||||||
use core::str as core_str;
|
use core::str as core_str;
|
||||||
|
use core::str::pattern::Pattern;
|
||||||
|
use core::str::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher};
|
||||||
use unicode::str::{UnicodeStr, Utf16Encoder};
|
use unicode::str::{UnicodeStr, Utf16Encoder};
|
||||||
|
|
||||||
use core::convert::AsRef;
|
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, Chars, CharIndices, Bytes};
|
||||||
pub use core::str::{from_utf8_unchecked, ParseBoolError};
|
pub use core::str::{from_utf8_unchecked, ParseBoolError};
|
||||||
pub use unicode::str::{Words, Graphemes, GraphemeIndices};
|
pub use unicode::str::{Words, Graphemes, GraphemeIndices};
|
||||||
pub use core::str::Pattern;
|
pub use core::str::pattern;
|
||||||
pub use core::str::{Searcher, ReverseSearcher, DoubleEndedSearcher, SearchStep};
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Section: Creating a string
|
Section: Creating a string
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ use core::mem;
|
|||||||
use core::ops::{self, Deref, Add, Index};
|
use core::ops::{self, Deref, Add, Index};
|
||||||
use core::ptr;
|
use core::ptr;
|
||||||
use core::slice;
|
use core::slice;
|
||||||
use core::str::Pattern;
|
use core::str::pattern::Pattern;
|
||||||
use unicode::str as unicode_str;
|
use unicode::str as unicode_str;
|
||||||
use unicode::str::Utf16Item;
|
use unicode::str::Utf16Item;
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,8 @@
|
|||||||
#![doc(primitive = "str")]
|
#![doc(primitive = "str")]
|
||||||
|
|
||||||
use self::OldSearcher::{TwoWay, TwoWayLong};
|
use self::OldSearcher::{TwoWay, TwoWayLong};
|
||||||
|
use self::pattern::Pattern;
|
||||||
|
use self::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher};
|
||||||
|
|
||||||
use char::CharExt;
|
use char::CharExt;
|
||||||
use clone::Clone;
|
use clone::Clone;
|
||||||
@@ -34,10 +36,7 @@ use result::Result::{self, Ok, Err};
|
|||||||
use slice::{self, SliceExt};
|
use slice::{self, SliceExt};
|
||||||
use usize;
|
use usize;
|
||||||
|
|
||||||
pub use self::pattern::Pattern;
|
pub mod pattern;
|
||||||
pub use self::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher, SearchStep};
|
|
||||||
|
|
||||||
mod pattern;
|
|
||||||
|
|
||||||
/// A trait to abstract the idea of creating a new instance of a type from a
|
/// A trait to abstract the idea of creating a new instance of a type from a
|
||||||
/// string.
|
/// string.
|
||||||
|
|||||||
@@ -471,29 +471,28 @@ fn str_search_step<F, G>(mut m: &mut StrSearcher,
|
|||||||
|
|
||||||
macro_rules! pattern_methods {
|
macro_rules! pattern_methods {
|
||||||
($t:ty, $pmap:expr, $smap:expr) => {
|
($t:ty, $pmap:expr, $smap:expr) => {
|
||||||
// FIXME: #22463
|
type Searcher = $t;
|
||||||
//type Searcher = $t;
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn into_searcher(self, haystack: &'a str) -> $t {
|
fn into_searcher(self, haystack: &'a str) -> $t {
|
||||||
$smap($pmap(self).into_searcher(haystack))
|
($smap)(($pmap)(self).into_searcher(haystack))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn is_contained_in(self, haystack: &'a str) -> bool {
|
fn is_contained_in(self, haystack: &'a str) -> bool {
|
||||||
$pmap(self).is_contained_in(haystack)
|
($pmap)(self).is_contained_in(haystack)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn is_prefix_of(self, haystack: &'a str) -> bool {
|
fn is_prefix_of(self, haystack: &'a str) -> bool {
|
||||||
$pmap(self).is_prefix_of(haystack)
|
($pmap)(self).is_prefix_of(haystack)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn is_suffix_of(self, haystack: &'a str) -> bool
|
fn is_suffix_of(self, haystack: &'a str) -> bool
|
||||||
where $t: ReverseSearcher<'a>
|
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
|
/// Searches for chars that are equal to a given char
|
||||||
impl<'a> Pattern<'a> for char {
|
impl<'a> Pattern<'a> for char {
|
||||||
type Searcher = CharSearcher<'a>;
|
|
||||||
pattern_methods!(CharSearcher<'a>, CharEqPattern, CharSearcher);
|
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
|
/// Searches for chars that are equal to any of the chars in the array
|
||||||
impl<'a, 'b> Pattern<'a> for &'b [char] {
|
impl<'a, 'b> Pattern<'a> for &'b [char] {
|
||||||
type Searcher = CharSliceSearcher<'a, 'b>;
|
|
||||||
pattern_methods!(CharSliceSearcher<'a, 'b>, CharEqPattern, CharSliceSearcher);
|
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
|
/// Searches for chars that match the given predicate
|
||||||
impl<'a, F> Pattern<'a> for F where F: FnMut(char) -> bool {
|
impl<'a, F> Pattern<'a> for F where F: FnMut(char) -> bool {
|
||||||
type Searcher = CharPredicateSearcher<'a, F>;
|
|
||||||
pattern_methods!(CharPredicateSearcher<'a, F>, CharEqPattern, CharPredicateSearcher);
|
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 {
|
mod pattern {
|
||||||
use std::str::Pattern;
|
use std::str::pattern::Pattern;
|
||||||
use std::str::{Searcher, ReverseSearcher};
|
use std::str::pattern::{Searcher, ReverseSearcher};
|
||||||
use std::str::SearchStep::{self, Match, Reject, Done};
|
use std::str::pattern::SearchStep::{self, Match, Reject, Done};
|
||||||
|
|
||||||
macro_rules! make_test {
|
macro_rules! make_test {
|
||||||
($name:ident, $p:expr, $h:expr, [$($e:expr,)*]) => {
|
($name:ident, $p:expr, $h:expr, [$($e:expr,)*]) => {
|
||||||
mod $name {
|
mod $name {
|
||||||
use std::str::SearchStep::{Match, Reject};
|
use std::str::pattern::SearchStep::{Match, Reject};
|
||||||
use super::{cmp_search_to_vec};
|
use super::{cmp_search_to_vec};
|
||||||
#[test]
|
#[test]
|
||||||
fn fwd() {
|
fn fwd() {
|
||||||
|
|||||||
Reference in New Issue
Block a user