-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Empty strings in result of "abc".split("") #33882
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
Comments
I guess it's sort of in the same category as:
and
Which is in the documentation here. |
I am on my phone at the moment, but it is expected and documented IIRC. On May 26, 2016, 12:39 -0400, Corey [email protected], wrote:
|
The examples given in the documentation are let x = "||||a||b|c".to_string();
let d: Vec<_> = x.split('|').collect();
assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]); and let x = " a b c".to_string();
let d: Vec<_> = x.split(' ').collect();
assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]); The text doesn't go further than "will contain empty strings", but the examples suggest that each time the separator is directly followed by another occurence of it in the string, an empty string will be the output. From reading that specific section of the documentation, I can't really find out the output of the split method for the empty string. The empty string is a real edge case and at least deserves to be documented IMO, or, if it is considered invalid input, it should panic. |
I agree with this. Especially since doing |
JavaScript: "abc".split("")
["a", "b", "c"] Python: >>> "abc".split("")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: empty separator Ruby: irb(main):002:0> "abc".split("")
=> ["a", "b", "c"] |
C/C++: #include <string.h>
#include <stdio.h>
int main(void)
{
char input[] = "abc";
char delim[] = "";
char *token = strtok(input, delim);
printf("[");
while (token) {
printf("\"%s\"", token);
token = strtok(NULL, delim);
if (token) {
printf(", ");
}
}
printf("]\n");
} outputs
|
The behaviour is expected (algorithm and behaviour we implement is greedy). Consider It should be documented if it isn’t already. (To me it looks like it isn’t) |
This seems like an easy enough to do change, thus marking as E-easy. This is the documentation that needs to be edited, other examples for the function should be used as a reference. The algorithm behaviour is described in the comment above. In case anything is not clear, ask either here or on IRC (#rust-internals/#rust-libs). |
Previous related discussion in #25986 |
…uillaumeGomez Added examples/docs to split in str.rs Added documentation clarifying the behavior of split when used with the empty string and contiguous separators. Addresses issue [33882](rust-lang#33882). This is my first time contributing to rust, so forgive me if I'm skipping any of the contribution steps. Fixes rust-lang#33882
…uillaumeGomez Added examples/docs to split in str.rs Added documentation clarifying the behavior of split when used with the empty string and contiguous separators. Addresses issue [33882](rust-lang#33882). This is my first time contributing to rust, so forgive me if I'm skipping any of the contribution steps. Fixes rust-lang#33882
…uillaumeGomez Added examples/docs to split in str.rs Added documentation clarifying the behavior of split when used with the empty string and contiguous separators. Addresses issue [33882](rust-lang#33882). This is my first time contributing to rust, so forgive me if I'm skipping any of the contribution steps. Fixes rust-lang#33882
it's best to not surprise people |
Determining what is surprising is not an easy problem |
Is this expected behavior? If so, it should probably be documented.
The text was updated successfully, but these errors were encountered: