Skip to content

Commit 3a41061

Browse files
danielparksBurntSushi
authored andcommitted
api: fix for splitn("a", 2) returning extra ""
Corrects `/-/.splitn("a", 2)` to return `["a"]` instead of `["a", ""]`. (`/-/` is shorthand for `Regex::new("-").unwrap()`.) Fixes #521, Closes #606, Closes #628
1 parent 17d68dc commit 3a41061

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ New features:
99

1010
Bug fixes:
1111

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

src/re_bytes.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -774,12 +774,19 @@ impl<'r, 't> Iterator for SplitN<'r, 't> {
774774
if self.n == 0 {
775775
return None;
776776
}
777+
777778
self.n -= 1;
778-
if self.n == 0 {
779-
let text = self.splits.finder.0.text();
780-
Some(&text[self.splits.last..])
779+
if self.n > 0 {
780+
return self.splits.next();
781+
}
782+
783+
let text = self.splits.finder.0.text();
784+
if self.splits.last > text.len() {
785+
// We've already returned all substrings.
786+
None
781787
} else {
782-
self.splits.next()
788+
// self.n == 0, so future calls will return None immediately
789+
Some(&text[self.splits.last..])
783790
}
784791
}
785792
}

src/re_unicode.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -814,12 +814,19 @@ impl<'r, 't> Iterator for SplitN<'r, 't> {
814814
if self.n == 0 {
815815
return None;
816816
}
817+
817818
self.n -= 1;
818-
if self.n == 0 {
819-
let text = self.splits.finder.0.text();
820-
Some(&text[self.splits.last..])
819+
if self.n > 0 {
820+
return self.splits.next();
821+
}
822+
823+
let text = self.splits.finder.0.text();
824+
if self.splits.last > text.len() {
825+
// We've already returned all substrings.
826+
None
821827
} else {
822-
self.splits.next()
828+
// self.n == 0, so future calls will return None immediately
829+
Some(&text[self.splits.last..])
823830
}
824831
}
825832
}

tests/api.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,7 @@ split!(split_trailing_blank, r"-", r"a-", &[t!("a"), t!("")]);
213213
split!(split_trailing_blanks, r"-", r"a--", &[t!("a"), t!(""), t!("")]);
214214
split!(split_empty, r"-", r"", &[t!("")]);
215215

216-
// See: https://github.com/rust-lang/regex/issues/521
217-
// splitn!(splitn_below_limit, r"-", r"a", 2, &[t!("a")]);
218-
216+
splitn!(splitn_below_limit, r"-", r"a", 2, &[t!("a")]);
219217
splitn!(splitn_at_limit, r"-", r"a-b", 2, &[t!("a"), t!("b")]);
220218
splitn!(splitn_above_limit, r"-", r"a-b-c", 2, &[t!("a"), t!("b-c")]);
221219
splitn!(splitn_zero_limit, r"-", r"a-b", 0, empty_vec!());

0 commit comments

Comments
 (0)