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

strings,bytes: improve doc of Split* functions for the empty string case #19726

Closed
dolmen opened this issue Mar 27, 2017 · 7 comments
Closed

Comments

@dolmen
Copy link
Contributor

dolmen commented Mar 27, 2017

In go 1.8 the behaviour of strings.Split{,N,After,AfterN} functions is not explicitly documented for the special case of splitting the empty string "". The functions return a slice with a single element which is the empty string.

The problem is that the Go behaviour is not the same as in other programming languages with functions with a similar name where for the same input the result is a zero-length array.

In Go (playground):

	fmt.Printf("%q\n", strings.SplitAfter("a", ":"))
	fmt.Printf("%q\n", strings.SplitAfter("", ":"))
	// Output:
	// ["a"]
	// [""]

In Perl 5 the result is a zero-length list:

perl -mData::Dumper=Dumper -E 'say Dumper([ split /:/, $_ ]) for "a", ""'
$VAR1 = [
          'a'
        ];

$VAR1 = [];

This leads to wrong expectations from users of the functions (especially when porting code from other languages) and may lead to security issues such as incorrect input validation or deny of service.

The documentation should be improved by documenting specially this corner case.

@dolmen dolmen changed the title Improve doc of strings.Split* functions for the empty string case strings: improve doc of Split* functions for the empty string case Mar 27, 2017
@ALTree ALTree added this to the Go1.9 milestone Mar 27, 2017
@gopherbot
Copy link

CL https://golang.org/cl/38690 mentions this issue.

@ammario
Copy link

ammario commented Mar 27, 2017

I've created a CL addressing this issue by changing the behavior of the Split func to always return a []string{""} if s is empty.

@ianlancetaylor
Copy link
Contributor

The functions have behaved as they do since before the Go 1 release. I think we would need a strong reason to change that behavior, and I don't see that strong reason here. I think we should change the documentation instead.

@mattn
Copy link
Member

mattn commented Mar 27, 2017

I think you should not think the go's behavior as same as perl. In javascript, "".split(",").length == 1. python also return 1 with len("".split(",")).

@ammario
Copy link

ammario commented Mar 27, 2017

What would be a good message to document it? Something like

// If s is empty, SplitAfterN returns a slice with a single empty string.
// If both s and sep are empty, SplitAfterN returns an empty slice.

seems too verbose to explain such an obscure feature.

@dolmen
Copy link
Contributor Author

dolmen commented Mar 27, 2017

@ammario That's because it is obscure that it must be documented.

@dolmen dolmen changed the title strings: improve doc of Split* functions for the empty string case strings,bytes: improve doc of Split* functions for the empty string case Mar 27, 2017
@gopherbot
Copy link

CL https://golang.org/cl/44950 mentions this issue.

@golang golang locked and limited conversation to collaborators Jun 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants