Skip to content
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

Add pattern matching API to OsStr #109350

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
6 changes: 3 additions & 3 deletions library/alloc/src/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use core::borrow::{Borrow, BorrowMut};
use core::iter::FusedIterator;
use core::mem;
use core::pattern::{DoubleEndedSearcher, Pattern, ReverseSearcher, Searcher};
use core::ptr;
use core::str::pattern::{DoubleEndedSearcher, Pattern, ReverseSearcher, Searcher};
use core::unicode::conversions;

use crate::borrow::ToOwned;
Expand Down Expand Up @@ -268,7 +268,7 @@ impl str {
without modifying the original"]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn replace<'a, P: Pattern<'a>>(&'a self, from: P, to: &str) -> String {
pub fn replace<'a, P: Pattern<&'a str>>(&'a self, from: P, to: &str) -> String {
let mut result = String::new();
let mut last_end = 0;
for (start, part) in self.match_indices(from) {
Expand Down Expand Up @@ -308,7 +308,7 @@ impl str {
#[must_use = "this returns the replaced string as a new allocation, \
without modifying the original"]
#[stable(feature = "str_replacen", since = "1.16.0")]
pub fn replacen<'a, P: Pattern<'a>>(&'a self, pat: P, to: &str, count: usize) -> String {
pub fn replacen<'a, P: Pattern<&'a str>>(&'a self, pat: P, to: &str, count: usize) -> String {
// Hope to reduce the times of re-allocation
let mut result = String::with_capacity(32);
let mut last_end = 0;
Expand Down
12 changes: 6 additions & 6 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ use core::ops::AddAssign;
#[cfg(not(no_global_oom_handling))]
use core::ops::Bound::{Excluded, Included, Unbounded};
use core::ops::{self, Index, IndexMut, Range, RangeBounds};
use core::pattern::Pattern;
use core::ptr;
use core::slice;
use core::str::pattern::Pattern;
#[cfg(not(no_global_oom_handling))]
use core::str::Utf8Chunks;

Expand Down Expand Up @@ -1371,9 +1371,9 @@ impl String {
#[unstable(feature = "string_remove_matches", reason = "new API", issue = "72826")]
pub fn remove_matches<'a, P>(&'a mut self, pat: P)
where
P: for<'x> Pattern<'x>,
P: for<'x> Pattern<&'x str>,
{
use core::str::pattern::Searcher;
use core::pattern::Searcher;

let rejections = {
let mut searcher = pat.into_searcher(self);
Expand Down Expand Up @@ -2174,10 +2174,10 @@ impl<'a> Extend<Cow<'a, str>> for String {
reason = "API not fully fleshed out and ready to be stabilized",
issue = "27721"
)]
impl<'a, 'b> Pattern<'a> for &'b String {
type Searcher = <&'b str as Pattern<'a>>::Searcher;
impl<'a, 'b> Pattern<&'a str> for &'b String {
type Searcher = <&'b str as Pattern<&'a str>>::Searcher;

fn into_searcher(self, haystack: &'a str) -> <&'b str as Pattern<'a>>::Searcher {
fn into_searcher(self, haystack: &'a str) -> <&'b str as Pattern<&'a str>>::Searcher {
self[..].into_searcher(haystack)
}

Expand Down
18 changes: 9 additions & 9 deletions library/alloc/tests/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1868,14 +1868,14 @@ fn test_repeat() {
}

mod pattern {
use std::str::pattern::SearchStep::{self, Done, Match, Reject};
use std::str::pattern::{Pattern, ReverseSearcher, Searcher};
use core::pattern::SearchStep::{self, Done, Match, Reject};
use core::pattern::{Pattern, ReverseSearcher, Searcher};

macro_rules! make_test {
($name:ident, $p:expr, $h:expr, [$($e:expr,)*]) => {
#[allow(unused_imports)]
mod $name {
use std::str::pattern::SearchStep::{Match, Reject};
use core::pattern::SearchStep::{Match, Reject};
use super::{cmp_search_to_vec};
#[test]
fn fwd() {
Expand All @@ -1891,7 +1891,7 @@ mod pattern {

fn cmp_search_to_vec<'a>(
rev: bool,
pat: impl Pattern<'a, Searcher: ReverseSearcher<'a>>,
pat: impl Pattern<&'a str, Searcher: ReverseSearcher<&'a str>>,
haystack: &'a str,
right: Vec<SearchStep>,
) {
Expand Down Expand Up @@ -1972,7 +1972,7 @@ mod pattern {
str_searcher_multibyte_haystack,
" ",
"├──",
[Reject(0, 3), Reject(3, 6), Reject(6, 9),]
[Reject(0, 9),]
);
make_test!(
str_searcher_empty_needle_multibyte_haystack,
Expand Down Expand Up @@ -2008,13 +2008,13 @@ mod pattern {
char_searcher_multibyte_haystack,
' ',
"├──",
[Reject(0, 3), Reject(3, 6), Reject(6, 9),]
[Reject(0, 9),]
);
make_test!(
char_searcher_short_haystack,
'\u{1F4A9}',
"* \t",
[Reject(0, 1), Reject(1, 2), Reject(2, 3),]
[Reject(0, 3),]
);

// See #85462
Expand Down Expand Up @@ -2151,11 +2151,11 @@ generate_iterator_test! {

#[test]
fn different_str_pattern_forwarding_lifetimes() {
use std::str::pattern::Pattern;
use core::pattern::Pattern;

fn foo<'a, P>(p: P)
where
for<'b> &'b P: Pattern<'a>,
for<'b> &'b P: Pattern<&'a str>,
{
for _ in 0..3 {
"asdf".find(&p);
Expand Down
3 changes: 3 additions & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,11 @@ pub mod sync;

pub mod fmt;
pub mod hash;
pub mod pattern;
pub mod slice;
pub mod str;
#[allow(missing_docs)]
pub mod str_bytes;
pub mod time;

pub mod unicode;
Expand Down
Loading