Skip to content
Merged
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 @@ -394,7 +394,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 @@ -482,7 +487,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 @@ -700,3 +710,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"))
}