Skip to content

Allow specification of starting index in str::find #11986

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

Closed
xales opened this issue Feb 2, 2014 · 7 comments
Closed

Allow specification of starting index in str::find #11986

xales opened this issue Feb 2, 2014 · 7 comments

Comments

@xales
Copy link
Contributor

xales commented Feb 2, 2014

The find family of string functions currently do not have a way of specifying the starting index. This makes it difficult to find the nth occurrence of something.

cc @cmr

@huonw
Copy link
Member

huonw commented Feb 2, 2014

The API is designed to be composable: string.slice_from(n).find(...).

Is that what you are describing?

(I think the .match_offsets (can't remember the exact name) iterator is probably a better way of iterating through the matches of a string, too.)

@xales
Copy link
Contributor Author

xales commented Feb 2, 2014

Yes. In particular

let y = x.find('"').unwrap()+1; x.slice(y, y + x.slice_from(y+1).find('"').unwrap()+1)

(intended to find string between first two double-quote characters)
could be simplified as

let y = x.find('"').unwrap()+1; x.slice(y, x.find('"', y).unwrap())

While it's possible to do using composition, it feels somewhat clumsy, and using an iterator probably overkill for small matches like this.

@huonw
Copy link
Member

huonw commented Feb 2, 2014

I'm personally nervous about adding more arguments for things like this; e.g. why don't we have an argument counting how many matches should be ignored; and the ending index, and then the same for the reverse versions.

This specific case may be fixed by something like adding a string.offsets(char) which returns Iterator<uint> where the uints are the positions of the found codepoints:

let mut it = x.offsets('"');
x.slice(it.next().unwrap(), it.next().unwrap() + 1)

That said, this sort of stuff is not great Unicode-practice.

@emberian
Copy link
Member

emberian commented Feb 2, 2014

I was just thinking a find_from. But, it is really awful Unicode hygiene...
I hate strings.

On Sun, Feb 2, 2014 at 7:29 AM, Huon Wilson [email protected]:

I'm personally nervous about adding more arguments for things like this;
e.g. why don't we have an argument counting how many matches should be
ignored; and the ending index, and then the same for the reverse versions.

This specific case may be fixed by something like adding a
string.offsets(char) which returns Iterator where the uints are the
positions of the found codepoints:

let mut it = x.offsets('"');x.slice(it.next().unwrap(), it.next().unwrap() + 1)

That said, this sort of stuff is not great Unicode-practice.


Reply to this email directly or view it on GitHubhttps://github.com//issues/11986#issuecomment-33899182
.

@treeman
Copy link
Contributor

treeman commented Sep 2, 2014

Triage bump. Needs an RFC as find is reachable by the prelude?

@steveklabnik
Copy link
Member

This is bad unicode practice, and changes to the libraries will need an RFC, so yes, closing.

@stej
Copy link

stej commented Jul 7, 2015

I tried to use the proposed solution, but it looks that the API changed significantly. What is the current way how to find a string starting from given index?

bors pushed a commit to rust-lang-ci/rust that referenced this issue Jul 25, 2022
bors added a commit to rust-lang-ci/rust that referenced this issue Jul 25, 2022
fix rust-lang#11986 Aliases break resolution of qualified variants in patterns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants