Skip to content

Commit

Permalink
api: fix for splitn("a", 2) returning extra ""
Browse files Browse the repository at this point in the history
Corrects `/-/.splitn("a", 2)` to return `["a"]` instead of `["a", ""]`.
(`/-/` is shorthand for `Regex::new("-").unwrap()`.)

Fixes #521, Closes #606, Closes #628
  • Loading branch information
danielparks authored and BurntSushi committed Jan 9, 2020
1 parent 17d68dc commit 3a41061
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ New features:

Bug fixes:

* [BUG #521](https://github.com/rust-lang/regex/issues/521):
Corrects `/-/.splitn("a", 2)` to return `["a"]` instead of `["a", ""]`.
* [BUG #594](https://github.com/rust-lang/regex/pull/594):
Improve error reporting when writing `\p\`.
* [BUG #627](https://github.com/rust-lang/regex/issues/627):
Corrects `re.split("a-")` to return `["a", ""]` instead of `["a"]`.
Corrects `/-/.split("a-")` to return `["a", ""]` instead of `["a"]`.
* [BUG #633](https://github.com/rust-lang/regex/pull/633):
Squash deprecation warnings for the `std::error::Error::description` method.

Expand Down
15 changes: 11 additions & 4 deletions src/re_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,12 +774,19 @@ impl<'r, 't> Iterator for SplitN<'r, 't> {
if self.n == 0 {
return None;
}

self.n -= 1;
if self.n == 0 {
let text = self.splits.finder.0.text();
Some(&text[self.splits.last..])
if self.n > 0 {
return self.splits.next();
}

let text = self.splits.finder.0.text();
if self.splits.last > text.len() {
// We've already returned all substrings.
None
} else {
self.splits.next()
// self.n == 0, so future calls will return None immediately
Some(&text[self.splits.last..])
}
}
}
Expand Down
15 changes: 11 additions & 4 deletions src/re_unicode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -814,12 +814,19 @@ impl<'r, 't> Iterator for SplitN<'r, 't> {
if self.n == 0 {
return None;
}

self.n -= 1;
if self.n == 0 {
let text = self.splits.finder.0.text();
Some(&text[self.splits.last..])
if self.n > 0 {
return self.splits.next();
}

let text = self.splits.finder.0.text();
if self.splits.last > text.len() {
// We've already returned all substrings.
None
} else {
self.splits.next()
// self.n == 0, so future calls will return None immediately
Some(&text[self.splits.last..])
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,7 @@ split!(split_trailing_blank, r"-", r"a-", &[t!("a"), t!("")]);
split!(split_trailing_blanks, r"-", r"a--", &[t!("a"), t!(""), t!("")]);
split!(split_empty, r"-", r"", &[t!("")]);

// See: https://github.com/rust-lang/regex/issues/521
// splitn!(splitn_below_limit, r"-", r"a", 2, &[t!("a")]);

splitn!(splitn_below_limit, r"-", r"a", 2, &[t!("a")]);
splitn!(splitn_at_limit, r"-", r"a-b", 2, &[t!("a"), t!("b")]);
splitn!(splitn_above_limit, r"-", r"a-b-c", 2, &[t!("a"), t!("b-c")]);
splitn!(splitn_zero_limit, r"-", r"a-b", 0, empty_vec!());
Expand Down

0 comments on commit 3a41061

Please sign in to comment.