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

(#521, #627) Fixes for split() and splitn() #628

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
Unreleased
==================

* [BUG #627](https://github.com/rust-lang/regex/issues/627):
Corrects `/-/.split("a-")` to return `["a", ""]` correctly instead of `["a"]`
(where `/-/` is a placeholder for code generate a regex).
* [BUG #521](https://github.com/rust-lang/regex/issues/521):
Corrects `/-/.splitn("a", 2)` to return `["a"]` correctly instead of
`["a", ""]` (where `/-/` is a placeholder for code generate a regex).

1.3.1 (2019-09-04)
==================
This is a maintenance release with no changes in order to try to work-around
Expand Down
19 changes: 13 additions & 6 deletions src/re_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,11 +726,11 @@ impl<'r, 't> Iterator for Split<'r, 't> {
let text = self.finder.0.text();
match self.finder.next() {
None => {
if self.last >= text.len() {
if self.last > text.len() {
None
} else {
let s = &text[self.last..];
self.last = text.len();
self.last = text.len() + 1; // Next call will return None
Some(s)
}
}
Expand Down Expand Up @@ -761,12 +761,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
19 changes: 13 additions & 6 deletions src/re_unicode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,11 +766,11 @@ impl<'r, 't> Iterator for Split<'r, 't> {
let text = self.finder.0.text();
match self.finder.next() {
None => {
if self.last >= text.len() {
if self.last > text.len() {
None
} else {
let s = &text[self.last..];
self.last = text.len();
self.last = text.len() + 1; // Next call will return None
Some(s)
}
}
Expand Down Expand Up @@ -801,12 +801,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
16 changes: 14 additions & 2 deletions tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,18 @@ split!(
split2,
r"(?-u)\b",
"a b c",
&[t!(""), t!("a"), t!(" "), t!("b"), t!(" "), t!("c")]
&[t!(""), t!("a"), t!(" "), t!("b"), t!(" "), t!("c"), t!("")]
);
split!(split3, r"a$", "a", &[t!("")]);
split!(split3, r"a$", "a", &[t!(""), t!("")]);
split!(split_none, r"-", r"a", &[t!("a")]);
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!("")]);

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!());
splitn!(splitn_trailing_blank, r"-", r"a-", 2, &[t!("a"), t!("")]);
splitn!(splitn_trailing_separator, r"-", r"a--", 2, &[t!("a"), t!("-")]);
splitn!(splitn_empty, r"-", r"", 1, &[t!("")]);
11 changes: 11 additions & 0 deletions tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,14 @@ macro_rules! split {
}
}
}

macro_rules! splitn {
($name:ident, $re:expr, $text:expr, $limit:expr, $expected:expr) => {
#[test]
fn $name() {
let re = regex!($re);
let splitted: Vec<_> = re.splitn(t!($text), $limit).collect();
assert_eq!($expected, &*splitted);
}
}
}
1 change: 1 addition & 0 deletions tests/macros_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ macro_rules! text { ($text:expr) => { $text.as_bytes() } }
macro_rules! t { ($re:expr) => { text!($re) } }
macro_rules! match_text { ($text:expr) => { $text.as_bytes() } }
macro_rules! use_ { ($($path: tt)*) => { use regex::bytes::$($path)*; } }
macro_rules! empty_vec { () => { <Vec<&[u8]>>::new() } }

macro_rules! bytes { ($text:expr) => { $text } }

Expand Down
1 change: 1 addition & 0 deletions tests/macros_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ macro_rules! text { ($text:expr) => { $text } }
macro_rules! t { ($text:expr) => { text!($text) } }
macro_rules! match_text { ($text:expr) => { $text.as_str() } }
macro_rules! use_ { ($($path: tt)*) => { use regex::$($path)*; } }
macro_rules! empty_vec { () => { <Vec<&str>>::new() } }

macro_rules! no_expand {
($text:expr) => {{
Expand Down