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

replace has problems with empty strings #394

Closed
fulara opened this issue Aug 20, 2017 · 2 comments
Closed

replace has problems with empty strings #394

fulara opened this issue Aug 20, 2017 · 2 comments
Labels

Comments

@fulara
Copy link
Contributor

fulara commented Aug 20, 2017

Seems like a replace has problems processing empty strings. example:

extern crate regex;
use regex::Regex;
fn main() {
    let regex = Regex::new("\\d*").unwrap();
    let s = regex.replace("", "replaced").into_owned();
    assert_eq!("replaced", s);
}

That fails. however:

extern crate regex;
use regex::Regex;
fn main() {
    let regex = Regex::new("\\d*").unwrap();
    let s = regex.find("");
    assert!(s.is_some());
}

That one passes.
Thanks to lapinot I've workaround here:

extern crate regex;
use regex::Regex;
fn main() {
    let regex = Regex::new("\\d*").unwrap();
    let s = regex.replace("", |caps: &regex::Captures| { "replaced".to_string() }).into_owned();
    assert_eq!("replaced", s);
}
@BurntSushi
Copy link
Member

This is probably a dupe of #393, but we can leave this open until a fix confirms.

@BurntSushi BurntSushi added the bug label Aug 20, 2017
Lapin0t added a commit to Lapin0t/regex that referenced this issue Aug 20, 2017
Lapin0t added a commit to Lapin0t/regex that referenced this issue Oct 15, 2017
bors added a commit that referenced this issue Nov 9, 2017
Fix `Regex::replacen` when replacing an empty match.

Fixes #393, #394.

There was some logic error when deciding to return `Cow::Borrowed` vs `Cow::Owned` in the fast-path. I took the same solution as the slow-path, ie use `peekable` on the iterator. I'm not sure this is the most efficient way (maybe just add `mut did_replace = false` and set it to true in the loop), but it does the job.
ethanpailes pushed a commit to ethanpailes/regex that referenced this issue Nov 29, 2017
@BurntSushi
Copy link
Member

Fixed by #395

ethanpailes pushed a commit to ethanpailes/regex that referenced this issue Dec 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants