Skip to content

push-wlxvvvxzrsly - #161755

Draft
pacak wants to merge 6 commits into
rust-lang:mainfrom
pacak:push-wlxvvvxzrsly
Draft

push-wlxvvvxzrsly#161755
pacak wants to merge 6 commits into
rust-lang:mainfrom
pacak:push-wlxvvvxzrsly

Conversation

@pacak

@pacak pacak commented Aug 25, 2026

Copy link
Copy Markdown
Contributor
  • fa667f5e core: add core::pattern::EmptyNeedleSearcher internal type

    Introduce core::pattern::EmptyNeedleSearcher internal type which
    implements logic for matching an empty pattern against a haystack.
    Convert core::str::pattern::StrSearcher to use it. In future more
    implementations will take advantage of it.

    Also adapt and rework TwoWayStrategy into an internal SearchResult
    trait which abstracts differences between Searcher’s next, next_match
    and next_rejects methods. It makes it simpler to write a single generic
    method implementing optimised versions of all those calls.


    @pacak:\
  • Fixed a few typos.\
  • There's no H::Cursor parameter so code gets a bit simplified.\
  • Added a test to assert how TwoWaySearcher runs with
    EmptyNeedleSearcher
  • e9f97dd3 Revert "Add ByteNeedle search in StrSearcherImpl"

    This reverts commit 85cf233ced0d0fe02734c8a83b6d79ccc5432d06.

    Gone for now, I'll reimplement it later in str_bytes.rs, will confirm
    with the benchmarks included that the optimization still applies

This PR is part of a stack containing 16 PRs:

  1. main
  2. coretests: Add more pattern tests. #161596
  3. generalize-pattern #161606 (Compare)
  4. core: introduce internal core::pattern::{Split,SplitN} types #161754 (Compare)
  5. "push-wlxvvvxzrsly" (this PR) (Compare)
  6. core: add try_next_code_point{,_reverse} internal functions #161756 (Compare)
  7. core: add internal core::str_bytes module handling string-like slices #161757 (Compare)
  8. coretests: Add a few tests for backward multibyte predicate #161758 (Compare)
  9. core: add concept of Flavour to core::str_bytes #161759 (Compare)
  10. core: add memchr-based single-ascii byte search to str_bytes #161760 (Compare)
  11. core/std: inline small functions #161761 (Compare)
  12. core::str_bytes: use unsafe for indexing #161762 (Compare)
  13. core: skip emitting rejects if result doesn't cary it #161608 (Compare)
  14. std: add pattern matching to OsStr #161763 (Compare)
  15. core: add core::pattern::Predicate wrapper type #161764 (Compare)
  16. core, std: support various [char] matchers for Pattern<&OsStr> #161765 (Compare)
  17. std: add OsStr pattern benchmarks #161610 (Compare)

pacak added 6 commits August 25, 2026 08:13
Right now things are undertested and underspecified.

Some of the library code would get in a loop if searcher starts
returning empty rejects.

And there's no tests for backwards multi byte char matchers. Pull
request I'm reviving had a problem implementing that, so making sure
it's tested before the actual code lands.

Right now it is possible to break both tests (and user code) without
breaking anything else in the test suite I think.
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).

This somewhat follows the Pattern API 2.0 design.  While that design is
apparently abandoned (?), it is somewhat helpful when going for patterns
on OsStr, so I’m going with it unless someone tells me otherwise. ;)

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.

@pacak: I moved some (or all new) of the `P: Pattern<&'a str>
constraints into where clause to keep things narrower:

```
pub fn foo<'a, P: Pattern<&'a str>>(&'a self, pat: P, ...) ...
```

to

```
pub fn replacen<'a, P>(&'a self, pat: P, ...) ...
     where
         P: Pattern<&'a str>,
```

Original code had indices in Haystack abstracted as an associated type
Cursor. Replaced with usize - Cursor adds noise with not much value.

Changed wording in 2-3 places - for example Searcher is generic over a
few types so it makes more sense to talk about split points in general
with utf8 split points as an example for `&str`.
Pattern is no longer str-specific, so move it from core::str::pattern
module to a new core::pattern module.  This introduces no changes in
behaviour or implementation.  Just moves stuff around and adjusts
documentation.
Introduce core::pattern::Split and core::pattern::SplitN internal types
which can be used to implement iterators splitting haystack into parts.
Convert str’s Split-family of iterators to use them.  In the future,
more haystacks will use those internal types.

Co-authored-by: Peter Jaszkowiak <p.jaszkow@gmail.com>

@pacak: Fixed some typos, added a few `#[inline]`. Since there's no
`H::Cursor` - I had to add `ctx: PhantomData<H>`.
This reverts commit 85cf233ced0d0fe02734c8a83b6d79ccc5432d06.

Gone for now, I'll reimplement it later in str_bytes.rs, will confirm
with the benchmarks included that the optimization still applies
Introduce core::pattern::EmptyNeedleSearcher internal type which
implements logic for matching an empty pattern against a haystack.
Convert core::str::pattern::StrSearcher to use it.  In future more
implementations will take advantage of it.

Also adapt and rework TwoWayStrategy into an internal SearchResult
trait  which abstracts differences between Searcher’s next, next_match
and next_rejects methods.  It makes it simpler to write a single generic
method implementing optimised versions of all those calls.


@pacak:
- Fixed a few typos.
- There's no H::Cursor parameter so code gets a bit simplified.
- Added a test to assert how TwoWaySearcher runs with
  EmptyNeedleSearcher
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants