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

str: add cut and cutn methods #49027

Closed
wants to merge 1 commit into from

Conversation

hotandcoldfever
Copy link

These methods just wrap replace/replacen and provide a more concise way to remove patterns from strs

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @bluss (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 14, 2018
@kennytm kennytm added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Mar 14, 2018
@jfager
Copy link
Contributor

jfager commented Mar 14, 2018

This isn’t what I’d expect a string op named cut to do.

@hotandcoldfever
Copy link
Author

hotandcoldfever commented Mar 14, 2018

I don't think there's any general expectation for rust methods to have the same behaviour as similarly named unix commands, and I think the name cut does make sense for this since it effectively cuts content out of the string according to the provided pattern. But I'm down w/ considering other names. I originally was gonna use remove but decided against it when I remembered String's remove method

@leoyvens
Copy link
Contributor

Is there any previous discussion on this? Seems too trivial to be in std.

@jfager
Copy link
Contributor

jfager commented Mar 14, 2018

There's no general expectation for rust methods to match unix commands, but they usually either have some precedent or near-match in other ecosystems, or, conversely, conform to Rust's naming conventions for the sorts of operations that are unique to the language.

Afaik 'cut' doesn't show up in many (any?) other languages' standard libraries, while the unix command is used widely in a similar enough context that I think it's reasonable to hold up as the prevailing prior art for the name in this particular case.

@clarfonthey
Copy link
Contributor

IMO replace returning String instead of Cow<str> is a mistake, and I'd prefer that this method do that.

Also, acut_in_place would be nice, using &mut str.

@bors
Copy link
Contributor

bors commented Mar 15, 2018

☔ The latest upstream changes (presumably #47813) made this pull request unmergeable. Please resolve the merge conflicts.

@hotandcoldfever
Copy link
Author

hotandcoldfever commented Mar 15, 2018

@jfager Point taken. Maybe a name like purge, delete, snip, blank?
@clarcharr So somethin like

fn cut<'a, P: Pattern<'a>>(&'a self, pattern: P) -> Cow<'a, str> {
    let result = self.replace(pattern, "");

    match result == self {
        false => result.into(),
        true => self.into()
    }
}

? cut_in_place could be useful I guess and I could take a crack at it if cut and cutn get merged
@leodasvacas Yea this is pretty trivial but it'd just be nice to have a more concise way to remove things from strings, and I don't think it's any more trivial than having flatten when you can use flat_map(|x| x) or vec![a, b] when you can use let mut vec = Vec::new(); vec.push(a); vec.push(b); etc. As far as I'm aware there's no previous discussions about this

@clarfonthey
Copy link
Contributor

@cool-cool-sweat yes, but please don't actually write that code; it's less efficient because it still allocates unconditionally.

As far as naming goes, I like remove_all and remove_n

@pietroalbini
Copy link
Member

Thank you for this PR @cool-cool-sweat! The triage team will periodically check in to make sure the PR is properly reviewed.

Ping from triage @bluss! This PR needs your review.

@hotandcoldfever
Copy link
Author

Gonna close this since there ain't much interest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants