Skip to content

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

Description

@dolmen

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions