Skip to content

Commit fa86569

Browse files
committed
Document all old/new features of Str.split
1 parent bbc2e39 commit fa86569

File tree

1 file changed

+66
-22
lines changed

1 file changed

+66
-22
lines changed

doc/Type/Str.pod

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -177,42 +177,86 @@ Examples:
177177
178178
=head2 routine split
179179
180-
multi sub split( Str:D $delimiter, Str:D $input, $limit = Inf, :$all) returns Positional
181-
multi sub split(Regex:D $delimiter, Str:D $input, $limit = Inf, :$all) returns Positional
182-
multi method split(Str:D $input: Str:D $delimiter, $limit = Inf, :$all) returns Positional
183-
multi method split(Str:D $input: Regex:D $delimiter, $limit = Inf, :$all) returns Positional
180+
multi sub split( Str:D $delimiter, Str:D $input, $limit = Inf,
181+
:$skip-empty, :$v, :$k, :$kv, :$p) returns Positional
182+
multi sub split(Regex:D $delimiter, Str:D $input, $limit = Inf,
183+
:$skip-empty, :$v, :$k, :$kv, :$p) returns Positional
184+
multi sub split(List:D $delimiters, Str:D $input, $limit = Inf,
185+
:$skip-empty, :$v, :$k, :$kv, :$p) returns Positional
186+
multi method split(Str:D: Str:D $delimiter, $limit = Inf,
187+
:$skip-empty, :$v, :$k, :$kv, :$p) returns Positional
188+
multi method split(Str:D: Regex:D $delimiter, $limit = Inf,
189+
:$skip-empty, :$v, :$k, :$kv, :$p) returns Positional
190+
multi method split(Str:D: List:D $delimiters, $limit = Inf,
191+
:$skip-empty, :$v, :$k, :$kv, :$p) returns Positional
184192
185193
Usage:
186194
187-
split( DELIMITER, STRING [, LIMIT] [, :all])
188-
split( /PATTERN/, STRING [, LIMIT] [, :all])
189-
STRING.split( DELIMITER [, LIMIT] [, :all])
190-
STRING.split( /PATTERN/ [, LIMIT] [, :all])
195+
split( DELIMITER, STRING [,LIMIT] [,:v] [,:k] [,:kv] [,:p] [,:skip-empty] )
196+
split( /PATTERN/, STRING [,LIMIT] [,:v] [,:k] [,:kv] [,:p] [,:skip-empty] )
197+
split( DELIMITERS, STRING [,LIMIT] [,:v] [,:k] [,:kv] [,:p] [,:skip-empty] )
198+
STRING.split( DELIMITER [,LIMIT] [,:v] [,:k] [,:kv] [,:p] [,:skip-empty] )
199+
STRING.split( /PATTERN/ [,LIMIT] [,:v] [,:k] [,:kv] [,:p] [,:skip-empty] )
200+
STRING.split( DELIMITERS [,LIMIT] [,:v] [,:k] [,:kv] [,:p] [,:skip-empty] )
191201
192202
Splits a string up into pieces based on delimiters found in the string.
193203
194204
If C<DELIMITER> is a string, it is searched for literally and not treated
195-
as a regex.
205+
as a regex. If C<DELIMITER> is the empty string, it effectively returns all
206+
characters of the string separately (plus an empty string at the begin and at
207+
the end). If C<PATTERN> is a regular expression, then that will be used
208+
to split up the string. If C<DELIMITERS> is a list, then all of its elements
209+
will be considered a delimiter (either a string or a regular expression) to
210+
split the string on.
196211
197-
If the named parameter C<:all> is passed, the matches from C<DELIMITER>
198-
are included in the result list.
212+
The optional C<LIMIT> indicates in how many segments the string should be
213+
split, if possible. It defaults to B<Inf> (or B<*>, whichever way you look at
214+
it), which means "as many as possible".
199215
200-
Note that unlike in Perl 5, empty chunks are not removed from the result list.
201-
If you want that behavior, consider using L<comb> instead.
216+
A number of optional named parameters can be specified, which alter the
217+
result being returned. The C<:v>, C<:k>, C<:kv> and C<:p> named parameters
218+
all perform a special action with regards to the delimiter found.
202219
203-
=comment TODO Describe behavior of LIMIT
220+
=item :skip-empty
221+
222+
If specified, do not return empty strings before or after a delimiter.
223+
224+
=item :v
225+
226+
Also return the delimiter. If the delimiter was a regular expression, then
227+
this will be the associated C<Match> object. Since this stringifies as the
228+
delimiter string found, you can always assume it is the delimiter string if
229+
you're not interested in further information about that particular match.
230+
231+
=item :k
232+
233+
Also return the B<index> of the delimiter. Only makes sense if a list of
234+
delimiters was specified: in all other cases, this will be B<0>.
235+
236+
=item :kv
237+
238+
Also return both the B<index> of the delimiter, as well as the delimiter.
239+
240+
=item :p
241+
242+
Also return the B<index> of the delimiter and the delimiter as a C<Pair>.
204243
205244
Examples:
206245
207246
=begin code
208-
say split(';', "a;b;c").perl; # ("a", "b", "c").list
209-
say split(';', "a;b;c", :all).perl; # (("a", ";"), ("b", ";"), "c").list
210-
say split(';', "a;b;c", 2).perl; # ("a", "b;c").list
211-
say split(';', "a;b;c", 2, :all).perl; # (("a", ";"), "b;c").list
212-
213-
say split(';', "a;b;c,d").perl; # ("a", "b", "c,d").list
214-
say split(/\;/, "a;b;c,d").perl; # ("a", "b", "c,d").list
215-
say split(/<[;,]>/, "a;b;c,d").perl; # ("a", "b", "c", "d").list
247+
say split(";", "a;b;c") # (a b c)
248+
say split(";", "a;b;c", :v) # (a ; b ; c)
249+
say split(";", "a;b;c", 2) # (a b;c)
250+
say split(";", "a;b;c", 2, :v) # (a ; b;c)
251+
say split(";", "a;b;c,d") # (a b c,d)
252+
say split(/\;/, "a;b;c,d") # (a b c,d)
253+
say split(<; ,>, "a;b;c,d") # (a b c d)
254+
say split(/<[;,]>/, "a;b;c,d") # (a b c d)
255+
say split(<; ,>, "a;b;c,d", :k) # (a 0 b 0 c 1 d)
256+
say split(<; ,>, "a;b;c,d", :kv) # (a 0 ; b 0 ; c 1 , d)
257+
258+
say "abcde".split("") # ( a b c d e )
259+
say "abcde".split("",:skip-empty) # (a b c d e)
216260
=end code
217261
218262
=head2 routine comb

0 commit comments

Comments
 (0)