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

Introduce String::remove_matches to remove all matches of a pattern in a string. #50015

Closed
wants to merge 1 commit into from

Conversation

frewsxcv
Copy link
Member

@frewsxcv frewsxcv commented Apr 17, 2018

Like replace(..., ""), but doesn't result in any allocations.

@rust-highfive
Copy link
Collaborator

r? @aidanhs

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 17, 2018
let s = self.clone();
// FIXME: this is inefficient because we'll search from the beginning of the string in
// . each iteration of the loop
let mut searcher = pat.into_searcher(&s);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that this doesn't compile, because into_searcher immutably borrows the string, and i can't call self.vec.set_len below if it's borrowed. not sure how to proceed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also into_searcher consumes itself, so there's no way to call it again on a new string slice

@rust-highfive
Copy link
Collaborator

Your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
[00:00:50] configure: rust.quiet-tests     := True
---
[00:03:25] error[E0597]: `s` does not live long enough
[00:03:25]     --> liballoc/string.rs:1229:51
[00:03:25]      |
[00:03:25] 1229 |             let mut searcher = pat.into_searcher(&s);
[00:03:25]      |                                                   ^ borrowed value does not live long enough
[00:03:25] ...
[00:03:25] 1243 |         }
[00:03:25]      |         - borrowed value only lives until here
[00:03:25]      |
[00:03:25] note: borrowed value must be valid for the lifetime 'a as defined on the method body at 1217:5...
[00:03:25]     --> liballoc/string.rs:1217:5
[00:03:25]      |
[00:03:25] 1217 | /     pub fn remove_matches<'a, P>(&'a mut self, pat: P)
[00:03:25] 1218 | |     where
[00:03:25] 1219 | |         P: Pattern<'a>,
[00:03:25] 1220 | |     {
[00:03:25] ...    |
[00:03:25] 1243 | |         }
[00:03:25] 1244 | |     }
[00:03:25]      | |_____^
[00:03:25]
[00:03:25] error[E0382]: use of moved value: `pat`
[00:03:25]     --> liballoc/string.rs:1229:32
[00:03:25]      |
[00:03:25] 1229 |             let mut searcher = pat.into_searcher(&s);
[00:03:25]      |                                ^^^ value moved here in previous iteration of loop
[00:03:25]      |
[00:03:25]      = note: move occurs because `pat` has type `P`, which does not implement the `Copy` trait
[00:03:25]
[00:03:25] error: aborting due to 2 previous errors
[00:03:25]
[00:03:25] Some errors occurred: E0382, E0597.
[00:03:25] For more information about an error, try `rustc --explain E0382`.
[00:03:25] error: Could not compile `alloc`.
[00:03:25]
[00:03:25] Caused by:
[00:03:25]   process didn't exit successfully: `/checkout/obj/build/bootstrap/debug/rustc --crate-name alloc liballoc/lib.rs --color always --error-format json --crate-type lib --emit=dep-info,link -C opt-level=2 -C metadata=d3eb5884eb70b0b8 -C extra-filename=-d3eb5884eb70b0b8 --out-dir /checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/x86_64-unknown-linux-gnu/release/deps --target x86_64-unknown-linux-gnu -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/x86_64-unknown-linux-gnu/release/deps -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/release/deps --extern compiler_builtins=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/x86_64-unknown-linux-gnu/release/deps/libcompiler_builtins-a2d7f090df844ebf.rlib --extern core=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/x86_64-unknown-linux-gnu/release/deps/libcore-1b8789e893adb899.rlib -L native=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/x86_64-unknown-linux-gnu/release/build/compiler_builtins-6789d0ad544f7553/out` (exit code: 101)
---
$ dmesg | grep -i kill
[   10.758087] init: failsafe main process (1096) killed by TERM signal

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

@frewsxcv frewsxcv changed the title Introduce String::remove_matches to remove all matches of patterns in a string. Introduce String::remove_matches to remove all matches of a pattern in a string. Apr 17, 2018
@frewsxcv frewsxcv added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Apr 17, 2018
@TimNN TimNN added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 24, 2018
@TimNN
Copy link
Contributor

TimNN commented Apr 24, 2018

@frewsxcv: How do you want to proceed with this PR? You mention that it currently doesn't compile, is that a problem with the pattern API or something in this PR that can be fixed?

@frewsxcv
Copy link
Member Author

convertinig this to a github issue #50206

@frewsxcv frewsxcv closed this Apr 25, 2018
@frewsxcv frewsxcv deleted the frewsxcv-remove-matches branch December 3, 2018 15:45
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Mar 18, 2021
…joshtriplett

Implement String::remove_matches

Closes rust-lang#50206.

I lifted the function help from `@frewsxcv's` original PR (rust-lang#50015), hope they don't mind.

I'm also wondering whether it would be useful for `remove_matches` to collect up the removed substrings into a `Vec` and return them, right now they're just overwritten by the copy and lost.
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Mar 18, 2021
…joshtriplett

Implement String::remove_matches

Closes rust-lang#50206.

I lifted the function help from ``@frewsxcv's`` original PR (rust-lang#50015), hope they don't mind.

I'm also wondering whether it would be useful for `remove_matches` to collect up the removed substrings into a `Vec` and return them, right now they're just overwritten by the copy and lost.
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Mar 19, 2021
…joshtriplett

Implement String::remove_matches

Closes rust-lang#50206.

I lifted the function help from `@frewsxcv's` original PR (rust-lang#50015), hope they don't mind.

I'm also wondering whether it would be useful for `remove_matches` to collect up the removed substrings into a `Vec` and return them, right now they're just overwritten by the copy and lost.
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 19, 2021
…shtriplett

Implement String::remove_matches

Closes rust-lang#50206.

I lifted the function help from `@frewsxcv's` original PR (rust-lang#50015), hope they don't mind.

I'm also wondering whether it would be useful for `remove_matches` to collect up the removed substrings into a `Vec` and return them, right now they're just overwritten by the copy and lost.
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-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.

4 participants