diff --git a/Changes b/Changes index 900f5b7..f6e2907 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,10 @@ Revision history for String-Utils {{$NEXT}} +0.0.27 2024-08-18T12:20:10+02:00 + - Add support for ":Pair" named argument to "paragraphs" to set + the class with which to create the objects + 0.0.26 2024-08-16T13:53:24+02:00 - Add support for "regexify" diff --git a/META6.json b/META6.json index f7d87a5..a51bac7 100644 --- a/META6.json +++ b/META6.json @@ -30,5 +30,5 @@ ], "test-depends": [ ], - "version": "0.0.26" + "version": "0.0.27" } diff --git a/README.md b/README.md index 633059e..aca936e 100644 --- a/README.md +++ b/README.md @@ -360,6 +360,13 @@ Lazily produces a `Seq` of `Pairs` with paragraphs from a `Seq` or string in whi The optional second argument can be used to indicate the ordinal number of the first line in the string. +```raku +my class A is Pair { } +.say for paragraphs("a\n\nb", 1, :Pair(A)); # 1 => a␤3 => b␤ +``` + +Also takes an optional named argument `:Pair` that indicates the class with which the objects should be created. This defailts to the core `Pair` class. + regexify -------- diff --git a/doc/String-Utils.rakudoc b/doc/String-Utils.rakudoc index 960c608..16e8867 100644 --- a/doc/String-Utils.rakudoc +++ b/doc/String-Utils.rakudoc @@ -423,6 +423,17 @@ and the value is the paragraph (without trailing newline). The optional second argument can be used to indicate the ordinal number of the first line in the string. +=begin code :lang + +my class A is Pair { } +.say for paragraphs("a\n\nb", 1, :Pair(A)); # 1 => a␤3 => b␤ + +=end code + +Also takes an optional named argument C<:Pair> that indicates the class +with which the objects should be created. This defailts to the core +C class. + =head2 regexify =begin code :lang diff --git a/lib/String/Utils.rakumod b/lib/String/Utils.rakumod index 7d4afc6..fba7bda 100644 --- a/lib/String/Utils.rakumod +++ b/lib/String/Utils.rakumod @@ -396,15 +396,17 @@ my sub all-same(str $string) { } my proto sub paragraphs(|) {*} -my multi sub paragraphs(@source, Int:D $initial = 0) { +my multi sub paragraphs(@source, Int:D $initial = 0, :$Pair = Pair) { my class Paragraphs does Iterator { has $!iterator; has int $!line; + has $!Pair; - method new($iterator, int $line) { + method new($iterator, int $line, $Pair) { my $self := nqp::create(self); nqp::bindattr( $self,Paragraphs,'$!iterator',$iterator); nqp::bindattr_i($self,Paragraphs,'$!line', $line - 1); + nqp::bindattr( $self,Paragraphs,'$!Pair', $Pair); $self } @@ -419,7 +421,7 @@ my multi sub paragraphs(@source, Int:D $initial = 0) { my sub paragraph() { $!line = $line; - Pair.new( + $!Pair.new( $line - nqp::elems($collected), nqp::join("\n", $collected) ) @@ -456,10 +458,10 @@ my multi sub paragraphs(@source, Int:D $initial = 0) { } # Produce the sequence - Seq.new: Paragraphs.new(@source.iterator, $initial) + Seq.new: Paragraphs.new(@source.iterator, $initial, $Pair) } -my multi sub paragraphs(Cool:D $string, Int:D $initial = 0) { - paragraphs $string.Str.lines, $initial +my multi sub paragraphs(Cool:D $string, Int:D $initial = 0, :$Pair = Pair) { + paragraphs $string.Str.lines, $initial, :$Pair } my sub regexify(str $spec, *%_) {