Skip to content

Commit 7d8b76a

Browse files
committed
Revising Str.match, subst and subst-mutate.
* All match adverbs and cases are mentioned. * All subst adverbs are also mentioned in its own place. I have changed the ordering of the sections. * subst-mutate is linked from subst. So I would say the concerns of #919 have been adressed. Closes #919. (some minor reflow in this and faq file too)
1 parent 1b0518b commit 7d8b76a

File tree

2 files changed

+58
-41
lines changed

2 files changed

+58
-41
lines changed

doc/Language/faq.pod6

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,9 @@ them offer all.
736736
737737
=item Phasers (like BEGIN / END) allow code to be executed at scope entry / exit, loop first / last / next and many more special contexts.
738738
739-
=item High level concurrency model, both for implicit as well as explicit multi-processing, which goes way beyond primitive threads and locks. Perl 6's concurrency offers a rich set of (composable) tools.
739+
=item High level concurrency model, both for implicit as well as explicit
740+
multi-processing, which goes way beyond primitive threads and locks. Perl 6's
741+
concurrency offers a rich set of (composable) tools.
740742
741743
=item Multiple-core computers are getting used more and more, and with Perl 6
742744
these can be used thanks to parallelism, both implicit (e.g., with the C«>>».
@@ -757,11 +759,14 @@ Fibonacci sequence, or all prime numbers.
757759
758760
=item Native data types for faster, closer to the metal, processing.
759761
760-
=item Interfacing to external libraries in C / C++ are trivially simple with NativeCall.
762+
=item Interfacing to external libraries in C / C++ are trivially simple with
763+
NativeCall.
761764
762-
=item Interfacing with Perl 5 (CPAN) / Python modules trivially simple with Inline::Perl5 and Inline::Python.
765+
=item Interfacing with Perl 5 (CPAN) / Python modules trivially simple with
766+
Inline::Perl5 and Inline::Python.
763767
764-
=item Can have multiple versions of a module installed and loaded simultaneously.
768+
=item Can have multiple versions of a module installed and loaded
769+
simultaneously.
765770
766771
=item System administration simplified due to simpler update/upgrade policies.
767772

doc/Type/Str.pod6

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -186,34 +186,40 @@ after the previous match.
186186
187187
method match($pat, :continue(:$c), :pos(:$p), :global(:$g), :overlap(:$ov), :exhaustive(:$ex), :st(:$nd), :rd(:$th), :$nth, :$x --> Match)
188188
189-
Performs a match of the string against C<$pat> and returns a L<Match> object if there is a successful match,
190-
and C<(Any)> otherwise. Matches are stored in C<$/>. If C<$pat> is not a L<Regex> object, match will coerce
191-
the argument to a Str and then perform a literal match against C<$pat>.
189+
Performs a match of the string against C<$pat> and returns a L<Match> object if
190+
there is a successful match; it returns C<(Any)> otherwise. Matches are stored in the
191+
L<default match variable C<$/>|/language/variables#index-entry-match_variable>.
192+
If C<$pat> is not a L<Regex> object, match will coerce the argument to a
193+
Str and then perform a literal match against C<$pat>.
192194
193195
A number of optional named parameters can be specified, which alter how the match is performed.
194196
195197
=item :continue
196198
197-
The C<:continue> adverb takes as an argument the position where the regex should start to search.
198-
If no position is specified for C<:c> it will default to 0 unless C<$/> is set, in which case it defaults to C<$/.to>.
199+
The C<:continue> adverb takes as an argument the position where the regex should
200+
start to search. If no position is specified for C<:c> it will default to 0
201+
unless C<$/> is set, in which case it defaults to C<$/.to>.
199202
200203
=item :pos
201204
202205
Takes a position as an argument. Fails if regex cannot be matched from that position, unlike C<:continue>.
203206
204207
=item :global
205208
206-
Instead of searching for just one match and returning a C<Match> object, search for every non-overlapping match and return them in a C<List>.
209+
Instead of searching for just one match and returning a C<Match> object, search
210+
for every non-overlapping match and return them in a C<List>.
207211
208212
=item :overlap
209213
210-
Finds all matches including overlapping matches, but only returns one match from each starting position.
214+
Finds all matches including overlapping matches, but only returns one match from
215+
each starting position.
211216
212217
=item :exhaustive
213218
214-
Finds all possible matches of a regex, including overlapping matches and matches that start at the same position.
219+
Finds all possible matches of a regex, including overlapping matches and matches
220+
that start at the same position.
215221
216-
=item :st, :nd, rd, nth
222+
=item :st, :nd, :rd, :nth
217223
218224
Returns the nth match in the string. The argument can be a L<Numeric> or
219225
an L<Iterable> producing monotonically increasing numbers (that is, the
@@ -226,9 +232,10 @@ will be set to a possibly-empty L<List> of L<Match> objects.
226232
227233
=item :x
228234
229-
Takes as an argument the number of matches to return, stopping once the specified number of matches has been reached. The value must be a L<Numeric>
230-
or a L<Range>; other values will cause C<.match> to return a L<Failure>
231-
containing C<X::Str::Match::x> exception.
235+
Takes as an argument the number of matches to return, stopping once the
236+
specified number of matches has been reached. The value must be a L<Numeric> or
237+
a L<Range>; other values will cause C<.match> to return a L<Failure> containing
238+
C<X::Str::Match::x> exception.
232239
233240
Examples:
234241
=begin code
@@ -843,8 +850,11 @@ Returns C<True> if the invocant is identical to or ends with C<$needle>.
843850
Returns the invocant string where C<$matcher> is replaced by C<$replacement>
844851
(or the original string, if no match was found).
845852
846-
There is an in-place syntactic variant of C<subst> spelled L«C<s/matcher/replacement/>|/syntax/s$SOLIDUS%20$SOLIDUS%20$SOLIDUS»
847-
as well as the L«C<.subst-mutate> method|/routine/subst-mutate».
853+
There is an in-place syntactic variant of C<subst> spelled
854+
L«C<s/matcher/replacement/>|/syntax/s$SOLIDUS%20$SOLIDUS%20$SOLIDUS» and with
855+
adverb following the C<s> or inside the matcher, as well as the
856+
L«C<.subst-mutate> method|/routine/subst-mutate» which modifies the string
857+
in-place.
848858
849859
C<$matcher> can be a L<Regex>, or a literal C<Str>. Non-Str matcher arguments
850860
of type L<Cool> are coerced to C<Str> for literal matching.
@@ -861,10 +871,10 @@ multi-match options like C<:g> are used).
861871
862872
=head3 Callable
863873
864-
The replacement can be a L<Callable> in which the current L<Match> object will be
865-
placed in the C<$/> variable, as well as the C<$_> topic variable.
866-
Using a L<Callable> as replacement is how you can refer to any of the captures
867-
created in the regex:
874+
The replacement can be a L<Callable> in which the current L<Match> object will
875+
be placed in the C<$/> variable, as well as the C<$_> topic variable. Using a
876+
L<Callable> as replacement is how you can refer to any of the captures created
877+
in the regex:
868878
869879
# Using capture from $/ variable (the $0 is the first positional capture)
870880
say 'abc123defg'.subst(/(\d+)/, { " before $0 after " });
@@ -884,25 +894,6 @@ created in the regex:
884894
say $str.subst(/secret/, {++$i}); # The answer to everything
885895
# OUTPUT: «The answer is 42.␤»
886896
887-
=head3 More Examples
888-
889-
Here are other examples of usage:
890-
891-
my $str = "Hey foo foo foo";
892-
$str.subst(/foo/, "bar", :g); # global substitution - returns Hey bar bar bar
893-
894-
$str.subst(/foo/, "no subst", :x(0)); # targeted substitution. Number of times to substitute. Returns back unmodified.
895-
$str.subst(/foo/, "bar", :x(1)); #replace just the first occurrence.
896-
897-
$str.subst(/foo/, "bar", :nth(3)); # replace nth match alone. Replaces the third foo. Returns Hey foo foo bar
898-
899-
The C<:nth> adverb has readable English-looking variants:
900-
901-
say 'ooooo'.subst: 'o', 'x', :1st; # OUTPUT: «xoooo␤»
902-
say 'ooooo'.subst: 'o', 'x', :2nd; # OUTPUT: «oxooo␤»
903-
say 'ooooo'.subst: 'o', 'x', :3rd; # OUTPUT: «ooxoo␤»
904-
say 'ooooo'.subst: 'o', 'x', :4th; # OUTPUT: «oooxo␤»
905-
906897
=head3 Adverbs
907898
908899
The following adverbs are supported
@@ -924,6 +915,27 @@ Note that only in the C<s///> form C<:ii> implies C<:i> and C<:ss> implies
924915
C<:s>. In the method form, the C<:s> and C<:i> modifiers must be added to the
925916
regex, not the C<subst> method call.
926917
918+
919+
=head3 More Examples
920+
921+
Here are other examples of usage:
922+
923+
my $str = "Hey foo foo foo";
924+
$str.subst(/foo/, "bar", :g); # global substitution - returns Hey bar bar bar
925+
926+
$str.subst(/foo/, "no subst", :x(0)); # targeted substitution. Number of times to substitute. Returns back unmodified.
927+
$str.subst(/foo/, "bar", :x(1)); #replace just the first occurrence.
928+
929+
$str.subst(/foo/, "bar", :nth(3)); # replace nth match alone. Replaces the third foo. Returns Hey foo foo bar
930+
931+
The C<:nth> adverb has readable English-looking variants:
932+
933+
say 'ooooo'.subst: 'o', 'x', :1st; # OUTPUT: «xoooo␤»
934+
say 'ooooo'.subst: 'o', 'x', :2nd; # OUTPUT: «oxooo␤»
935+
say 'ooooo'.subst: 'o', 'x', :3rd; # OUTPUT: «ooxoo␤»
936+
say 'ooooo'.subst: 'o', 'x', :4th; # OUTPUT: «oooxo␤»
937+
938+
927939
=head2 method subst-mutate
928940
929941
Where L<subst> returns the modified string and leaves the original

0 commit comments

Comments
 (0)