Skip to content

fix(string): split and then join should be identity #2020

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 5 additions & 7 deletions string/methods.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -664,11 +664,7 @@ pub fn View::split(self : View, sep : View) -> Iter[View] {
}
view = view.view(start_offset=end + sep_len)
} else {
if view.is_empty() {
IterContinue
} else {
yield_(view)
}
yield_(view)
}
})
}
Expand Down Expand Up @@ -701,12 +697,14 @@ test "split" {
assert_eq!(",a,b,c".split(",").map(View::to_string).collect(), [
"", "a", "b", "c",
])
assert_eq!("a,b,c,".split(",").map(View::to_string).collect(), ["a", "b", "c"])
assert_eq!("a,b,c,".split(",").map(View::to_string).collect(), [
"a", "b", "c", "",
])
assert_eq!("a,b,c".split("").map(View::to_string).collect(), [
"a", ",", "b", ",", "c",
])
assert_eq!("".split("").map(View::to_string).collect(), [])
assert_eq!("".split(",").map(View::to_string).collect(), [])
assert_eq!("".split(",").map(View::to_string).collect(), [""])
assert_eq!("😀,😃,😄".split(",").map(View::to_string).collect(), [
"😀", "😃", "😄",
])
Expand Down
30 changes: 28 additions & 2 deletions string/string_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,12 @@ test "split" {
,
)
inspect!("".split(""), content="[]")
inspect!("".split("x"), content="[]")
inspect!(
"".split("x"),
content=
#|[""]
,
)
inspect!(
"你 wow 好 wow 月 wow 兔".split(" wow "),
content=
Expand Down Expand Up @@ -512,7 +517,12 @@ test "split" {
,
)
inspect!("".split(""), content="[]")
inspect!("".split("x"), content="[]")
inspect!(
"".split("x"),
content=
#|[""]
,
)
inspect!(
"你 wow 好 wow 月 wow 兔".split(" wow "),
content=
Expand Down Expand Up @@ -730,3 +740,19 @@ test "offset_of_nth_char property negative" {
assert_eq!(s.unsafe_char_at(index), s.rev_iter().nth(i - 1).unwrap())
}
}

///|
test "split and then join should be identity" {
let string = ""
assert_eq!(string, string.split("").map(View::to_string).join(""))
assert_eq!(string, string.split("a").map(View::to_string).join("a"))
let string = "a,b,c,"
assert_eq!(string, string.split("").map(View::to_string).join(""))
assert_eq!(string, string.split(",").map(View::to_string).join(","))
let string = "a b c"
assert_eq!(string, string.split("").map(View::to_string).join(""))
assert_eq!(string, string.split(" ").map(View::to_string).join(" "))
let string = "aaaaa"
assert_eq!(string, string.split("").map(View::to_string).join(""))
assert_eq!(string, string.split("a").map(View::to_string).join("a"))
}
Loading