Skip to content

Pattern docs #71097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/liballoc/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1827,7 +1827,13 @@ impl<'a> Extend<Cow<'a, str>> for String {
}
}

/// A convenience impl that delegates to the impl for `&str`
/// A convenience impl that delegates to the impl for `&str`.
///
/// # Examples
///
/// ```
/// assert_eq!(String::from("Hello world").find("world"), Some(6));
/// ```
#[unstable(
feature = "pattern",
reason = "API not fully fleshed out and ready to be stabilized",
Expand Down
36 changes: 31 additions & 5 deletions src/libcore/str/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,13 @@ unsafe impl<'a> ReverseSearcher<'a> for CharSearcher<'a> {

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`.
///
/// # Examples
///
/// ```
/// assert_eq!("Hello world".find('o'), Some(4));
/// ```
impl<'a> Pattern<'a> for char {
type Searcher = CharSearcher<'a>;

Expand Down Expand Up @@ -696,7 +702,14 @@ unsafe impl<'a, 'b> ReverseSearcher<'a> for CharSliceSearcher<'a, 'b> {

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.
///
/// # Examples
///
/// ```
/// assert_eq!("Hello world".find(&['l', 'l'] as &[_]), Some(2));
/// assert_eq!("Hello world".find(&['l', 'l'][..]), Some(2));
/// ```
impl<'a, 'b> Pattern<'a> for &'b [char] {
pattern_methods!(CharSliceSearcher<'a, 'b>, MultiCharEqPattern, CharSliceSearcher);
}
Expand Down Expand Up @@ -738,7 +751,14 @@ where

impl<'a, F> DoubleEndedSearcher<'a> for CharPredicateSearcher<'a, F> where F: FnMut(char) -> bool {}

/// Searches for chars that match the given predicate
/// Searches for chars that match the given predicate.
///
/// # Examples
///
/// ```
/// assert_eq!("Hello world".find(char::is_uppercase), Some(0));
/// assert_eq!("Hello world".find(|c| "aeiou".contains(c)), Some(1));
/// ```
impl<'a, F> Pattern<'a> for F
where
F: FnMut(char) -> bool,
Expand All @@ -763,6 +783,12 @@ impl<'a, 'b, 'c> Pattern<'a> for &'c &'b str {
///
/// Will handle the pattern `""` as returning empty matches at each character
/// boundary.
///
/// # Examples
///
/// ```
/// assert_eq!("Hello world".find("world"), Some(6));
/// ```
impl<'a, 'b> Pattern<'a> for &'b str {
type Searcher = StrSearcher<'a, 'b>;

Expand All @@ -771,7 +797,7 @@ impl<'a, 'b> Pattern<'a> for &'b str {
StrSearcher::new(haystack, self)
}

/// Checks whether the pattern matches at the front of the haystack
/// Checks whether the pattern matches at the front of the haystack.
#[inline]
fn is_prefix_of(self, haystack: &'a str) -> bool {
haystack.as_bytes().starts_with(self.as_bytes())
Expand All @@ -788,7 +814,7 @@ impl<'a, 'b> Pattern<'a> for &'b str {
}
}

/// Checks whether the pattern matches at the back of the haystack
/// Checks whether the pattern matches at the back of the haystack.
#[inline]
fn is_suffix_of(self, haystack: &'a str) -> bool {
haystack.as_bytes().ends_with(self.as_bytes())
Expand Down