Raw String Literal Library Support
Jonathan Bluett-Duncan
jbluettduncan at gmail.com
Wed Mar 14 00:12:47 UTC 2018
Paul,
AFAICT, one sort of behaviour which String.split() allows which
Pattern.splitAsStream() and the proposed String.splits() don't is allowing
a negative limit, e.g. String.split(string, -1).
Over at http://errorprone.info/bugpattern/StringSplitter, they argue that a
limit of -1 has less surprising behaviour than the default of 0, because
e.g. "".split(":") produces [] (empty array), whereas ":".split(":")
produces [""] (array with an empty string), which IMO is not consistent.
This compares with ":".split(":", -1) and "".split(":", -1) which produce
["", ""] (array with two empty strings, each representing ends of `:`) and
[] (empty array) respectively - more consistent IMO.
Should String.splits(`\n|\r\n?`) follow the behaviour of String.split(...,
0) or String.split(..., -1)? I'd personally argue for the latter.
Cheers,
Jonathan
On 13 March 2018 at 23:22, Paul Sandoz <paul.sandoz at oracle.com> wrote:
>
>
> > On Mar 13, 2018, at 3:49 PM, John Rose <john.r.rose at oracle.com> wrote:
> >
> > On Mar 13, 2018, at 6:47 AM, Jim Laskey <james.laskey at oracle.com> wrote:
> >>
> >> …
> >> A. Line support.
> >>
> >> public Stream<String> lines()
> >>
> >
> > Suggest factoring this as:
> >
> > public Stream<String> splits(String regex) { }
>
> +1
>
> This is a natural companion to the existing array returning method (as it
> was the case on Pattern when we added splitAsStream), where one can use a
> limit() operation to achieve the same effect as the limit parameter on the
> array returning method.
>
>
> > public Stream<String> lines() { return splits(`\n|\r\n?`); }
> >
>
> See also Files/BufferedReader.lines. (Without going into details
> Files.lines has some interesting optimizations.)
>
> Paul.
More information about the core-libs-dev
mailing list