-
Notifications
You must be signed in to change notification settings - Fork 20
slice contains subslice #499
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
Comments
iirc let array = [Default::default(); 5];
// if this acts like `str::Pattern` then it matches a
// single character that is either 'a' or 'b'.
// if this acts like is proposed for `slice::pattern::Pattern`, then
// it matches the sequence 'a' followed by 'b' and forces `array: [char; 5]`.
array.contains(&['a', 'b']); |
I don't think std should be in the business of providing arbitrary subsequence search. I would be in favor of adding byte-substring search though, which appears to satisfy your use case. It is unclear why you took the step to generalize. Could you say why you wrote naive substring search instead of using the |
Hmm yeah if that's still the plan then e.g.
What makes it different from string subsequence search to you?
Yes I think
Well using a dependency just really didn't seem worth it here. I'm looking for something that is clear and quick and not terrible performance-wise. Something like |
Everything. It's an entirely different problem. Most substring search algorithms are tuned to small alphabets. As you point out, you would need specialization for the bytes case anyway. And on top of that, arbitrary subsequence search is an extremely niche problem. That should absolutely be in a crate, not std. |
In today's @rust-lang/libs-api meeting, we discussed at length the possible paths forward for this. We had consensus that we do want to add most methods from We also observed that this was closely related to the similar desire on We spent a lot of time bikeshedding a few possible interfaces, and looking at the methods provided by Attempting to separate and summarize the different axes of the discussion (and using phrasing like "some folks felt that" here because in some cases not all of the separated-out aspects had clear positions from everyone present, so I'll leave it to individual members to comment on which positions they prefer): Where to put the methods (which interacts with what to name them)
What the methods should accept
|
Taking off my team hat, and speaking personally: For where to put the methods, I'd advocate approach 1b (add as inherent with For how to define the methods, I'm fine with either approach. I think for simplicity it's easy enough to add |
That broadly sounds good to me. Approach 1b covers the vast majority of use cases. I do think that when the signature is limited to just However, it looks like no consensus was reached on a particular way forward. So, what is stopping this from sitting around for another 6 years like the original rustc issue? |
To elaborate a bit more on this option, I'm open to the idea of adding I like 1b, and the shenanigans with I do sympathize with the notion that if we provided arbitrary subsequence search, then we could probably use better (i.e., more general) names. But I am very skeptical about adding those to std. While I might have a lot of experience with the |
My position on this is that if the methods only take a plain needle, i.e.
Agreed that we likely won't achieve optimal performance in the general case if we support just searching for any So providing a better-than-naive-but-not-perfect implementation can still be worthwhile if it provides enough convenience. |
I don't think enough convenience is provided by such an API. General subsequence search is incredibly niche, unlike iterators. I don't think I've reached for it once in my decades of programming at least. A cursory search of crates.io seems to support this. The only crates I could find providing such functionality are It's worth noting that if we do go with I'm kind of meh on the |
We discussed this in today's @rust-lang/libs-api meeting, and we agreed that we should go forward with methods-1b (add byte-specific methods with Note that |
Marking as accepted in that form. |
@Amanieu wrote up a quick list of potential candidate methods:
In general, we should consider any reasonable method from |
Minor comment: I think these sound better with the wording flipped, |
I made the tracking issue rust-lang/rust#134149, and I'll start the implementation work soon. Likely first getting the trait right with only a couple methods, then fleshing out the rest of the api separately. |
Proposal
Problem statement
Determining whether a slice is contained within another slice, analogous to
"foobar".contains("foo")
, but for slices.Motivating examples or use cases
I recently had cause to write
Partially the problem here is the limited API on
OsStr
(and similarlyPath
andCStr
), but I've wanted a "contains slice" operation on just standard slices too. It is especially odd that the contains operation is defined on&str
, but not when you drop down to raw byte values.I see two problems
Solution sketch
I think the nicest solution is to mirror the
core::str::pattern
design, so that we could haveThis appears to work out
And it looks backwards-compatible to me, but I'm not 100% sure that it is.
Some final notes:
u8
andT: Copy
)Alternatives
This rustc issue rust-lang/rust#54961 proposed instead
This idea works fine too if the pattern idea above has backwards compatibility issues. (though I'd suggest
find_subslice
for consistency).Links and related work
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: