Skip to content

Commit

Permalink
save work
Browse files Browse the repository at this point in the history
  • Loading branch information
tbrowder committed May 4, 2024
1 parent 70fe606 commit 6ddffd5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Changes
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{$NEXT}}
- Add new option to sub 'normalize-string':
+ 'no-trim'
- do NOT trim by default
- do NOT trim and collapse spaces by default

3.4.2 2024-04-30T18:12:06-05:00
- Add new options to sub 'normalize-string':
Expand Down
5 changes: 5 additions & 0 deletions docs/README.rakudoc
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ Collapse to a newline:
say normalize-string($s, :c<n>) # OUTPUT: «1\n2\n3␤»
=end code

Notice that in the normalization
routines, spaces (' ') are always
normalized, even when tabs and
newlines are normalized separately.

=head3 sort-list

=begin code
Expand Down
6 changes: 4 additions & 2 deletions lib/Text/Utils.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,9 @@ sub normalize-string(
--> Str
) is export(:normalize-string) {
# default is to always trim first
$str .= trim;
unless $no-trim {
$str .= trim;
}
# then normalize all space characters
$str ~~ s:g/ $WS ** 2..* /$WS/;
Expand Down Expand Up @@ -475,10 +477,10 @@ sub normalize-string(
}
}
else {
$str .= trim;
$str ~~ s:g/ \s ** 2..* /$WS/;
}
=begin comment
else {
#$str .= trim;
Expand Down
9 changes: 8 additions & 1 deletion t/14-normalize-string.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use Text::Utils :ALL;
#say "constant ws = '$WS'";
#say $WS;
#exit;
plan 26;

plan 27;

# normalize these strings
my $str1 = '1 2 3 4 5 6 7 8 9 0';
Expand All @@ -22,6 +23,9 @@ my $sT = " 1 \t\t 2 \t 3 ";
my $sTn = "1 \t\t 2 \t 3";
is normalize-string($sT, :tabs<k>), $sTn, "keep tabs";

# test no-default option
is normalize-string($sT, :no-trim), $sT, "no-default";

# strings with newlines
my $sN = " 1 \n\n 2 \n 3 ";
# normalized
Expand Down Expand Up @@ -103,3 +107,6 @@ is $str4, $n;
# check for failing on a non-string
$str1 = 3;
dies-ok { $str1 = normalize-string $str1 }, 'Fails on a non-string';

done-testing;

0 comments on commit 6ddffd5

Please sign in to comment.