Skip to content

Commit

Permalink
core: convert Pattern<'a> into Pattern<H: Haystack> (RFC 2295)
Browse files Browse the repository at this point in the history
As per RFC 2295, add a Haystack trait describing something that can be
searched in and make core::str::Pattern (and related types) generic on
that trait.  This will allow Pattern to be used for types other than
str (most notably OsStr).

The change mostly follows the RFC though there are some differences in
Haystack trait.

Most notably, instead of separate StartCursor and EndCursors types,
there’s one Cursor type instead.  This eliminate the need for methods
converting between the two types of cursors.

Conversion from cursor to offset isn’t included either since as far as
I can tell it’s not needed.  A generic code operating on Haystack
doesn’t need a concept of offset.  It can operate on cursors instead.

Lastly, rather than range_to_self as suggested in RFC this commit
implements split_at_cursor_unchecked which simplifies default
implementation of strip_prefix_of and strip_suffix_of.

For now leave Pattern, Haystack et al in core::str::pattern.  Since
they are no longer str-specific, I’ll move them to core::pattern in
future commit.  This one leaves them in place to make the diff
smaller.

Issue: rust-lang#49802
  • Loading branch information
mina86 committed Feb 16, 2023
1 parent c528357 commit 4391e6a
Show file tree
Hide file tree
Showing 11 changed files with 229 additions and 182 deletions.
4 changes: 2 additions & 2 deletions library/alloc/src/str.rs
Original file line number Diff line number Diff line change
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
8 changes: 4 additions & 4 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1371,7 +1371,7 @@ 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;

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
4 changes: 2 additions & 2 deletions library/alloc/tests/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1879,7 +1879,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 @@ -2143,7 +2143,7 @@ fn different_str_pattern_forwarding_lifetimes() {

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
Loading

0 comments on commit 4391e6a

Please sign in to comment.