Skip to content

Commit 75d07fc

Browse files
committed
changes for new release and API
1 parent 6833e74 commit 75d07fc

File tree

5 files changed

+162
-14
lines changed

5 files changed

+162
-14
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [v2.0.0] - 2019-12-18
8+
- add multi version of strip-comment routine
9+
- update API to 2
10+
- deprecate the original signature;
11+
it will be removed in version 3.0.0.
12+
- improve robustness of strip-comment to multi-char comment marks
13+
- add tests for it
14+
715
## [v1.0.0] - 2019-12-17
816
- started a renamed version of module Text::More (which is now deprecated)
917
- improved routine 'strip-comment' to allow returning the comment as well

META6.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"perl" : "6.*",
33
"name" : "Text::Utils",
4-
"version" : "1.0.0",
5-
"api" : "1",
4+
"version" : "2.0.0",
5+
"api" : "2",
66
"description" : "Provides some additional text functions (replaces module Text::More)",
77
"auth" : "github:tbrowder",
88
"author" : "github:tbrowder",

README.md

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,62 @@
55
This model provides some miscellaneous text processing routines not
66
provided by core Raku. (Note it replaces the now-deprecated `Text::More` module.)
77

8+
Note this is version 2.0.0 which introduces a new API 2 for
9+
the 'strip-comment' routine. See the examples below for its use.
10+
The old signature is still usable, but it is deprecated
11+
and will be removed in version 3.0.0.
12+
813
## Synopsis
914

10-
```Raku
15+
```raku
1116
use Text::More :ALL;
1217
```
1318
Note that individual subroutines
1419
may also be exported:
1520

16-
```Raku
21+
```raku
1722
use Text::More :strip-comment;
23+
# the '#' is the default comment character
24+
my $line = " some text # a comment";
25+
$line = strip-comment $line;
26+
say $line # output: << some text >>
27+
```
28+
If you want to be fancier:
29+
30+
```raku
31+
# define your own comment character(s)
32+
# save the comment and normalize the returned strings
33+
my ($line, $comm) = strip-comment $line, :mark<%%>, :save-comment, :normalize;
34+
say $line # output: <<some text>>
35+
say $comm # output: <<a comment>>
36+
```
37+
38+
The default behavior is to find the first comment character in the input
39+
string, but you may choose to start the search from the end of the
40+
input string:
41+
42+
```raku
43+
my $line = "text 1 # text 2 # comment";
44+
$line = strip-comment $line, :reverse;
45+
say $line # output: <<text 1 # text 2 >>
46+
```
47+
48+
Note that the routine is line oriented, so embedded newlines
49+
may give unexpected results:
50+
51+
```raku
52+
my $line = q:to/HERE/;
53+
text 1 # comment 1
54+
text 2 # comment 2
55+
HERE
56+
$line = strip-comment $line
57+
say $line # output: <<text 1 >>
1858
```
1959

60+
61+
62+
63+
2064
## Installation
2165

2266
``` Raku
@@ -38,7 +82,7 @@ p6doc Text::Utils
3882
- `Text::Emotion`
3983
- `Text::Levenshtein::Damerau`
4084
- `Text::MiscUtils`
41-
- `Text::More (deprecated)`
85+
- `Text::More` (deprecated)
4286
- `Text::Table::List`
4387
- `Text::Table::Simple`
4488
- `Text::Tabs`

lib/Text/Utils.pm6

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,62 @@ sub count-substrs(Str:D $ip, Str:D $substr --> UInt) is export(:count-substrs) {
3131
#| Purpose : Strip comments from an input text line, save comment if
3232
#| requested, normalize returned text if requested
3333
#| Params : String of text, comment char ('#' is default),
34+
#| option to return the comment, option to normalize
35+
#| the returned strings, option to use the last comment char
36+
#| instead of the first
3437
#| Returns : String of text with any comment stripped off. Note that the
3538
#| designated char will trigger the strip even though it is
3639
#| escaped or included in quotes.
3740
#| Also returns the comment if requested.
3841
#| All returned text is normalized if requested.
39-
sub strip-comment($line is copy, #= string of text with possible comment
40-
$comment-char = '#', #= desired comment char indicator
41-
:$save-comment, #= if true, return the comment
42-
:$normalize, #= if true, normalize returned strings
43-
) is export(:strip-comment) {
42+
multi strip-comment($line is copy, #= string of text with possible comment
43+
:$mark = '#', #= desired comment char indicator
44+
:$save-comment, #= if true, return the comment
45+
:$normalize, #= if true, normalize returned strings
46+
:$last, #= if true, use the last instead of first comment char
47+
) is export(:strip-comment) {
48+
# failure condition:
49+
if $line eq $mark {
50+
die "FATAL: The input line is the same as the comment char: '$line'";
51+
}
52+
my $comment = '';
53+
my $clen = $mark.chars;
54+
my $idx = $last ?? rindex $line, $mark
55+
!! index $line, $mark;
56+
if $idx.defined {
57+
$comment = substr $line, $idx+$clen; #= don't want the comment char
58+
$line = substr $line, 0, $idx;
59+
}
60+
if $normalize {
61+
$line = normalize-string $line;
62+
$comment = normalize-string $comment;
63+
}
64+
if $save-comment {
65+
return $line, $comment;
66+
}
67+
$line;
68+
}
69+
70+
#| NOTE THE FOLLOWING SIGNATURE IS DEPRECATED
71+
multi strip-comment($line is copy, #= string of text with possible comment
72+
$comment-char = '#', #= desired comment char indicator
73+
:$save-comment, #= if true, return the comment
74+
:$normalize, #= if true, normalize returned strings
75+
:$last, #= if true, use the last instead of first comment char
76+
) is export(:strip-comment) {
77+
# failure condition:
78+
if $line eq $comment-char {
79+
note "FATAL: The input line is the same as the comment char: '$line'";
80+
note " This signature is deprecated.";
81+
note " Use the ':mark<$line>' named param for the comment char.";
82+
die "DEPRECATED SIGNATURE--USE THE MARK NOTATION IN THE FUTURE";
83+
}
4484
my $comment = '';
45-
my $idx = index $line, $comment-char;
85+
my $clen = $comment-char.chars;
86+
my $idx = $last ?? rindex $line, $comment-char
87+
!! index $line, $comment-char;
4688
if $idx.defined {
47-
$comment = substr $line, $idx+1; #= don't want the comment char
89+
$comment = substr $line, $idx+$clen; #= don't want the comment char
4890
$line = substr $line, 0, $idx;
4991
}
5092
if $normalize {

t/020-strip-comment.t

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use Test;
33

44
use Text::Utils :ALL;
55

6-
plan 12;
6+
plan 30;
77

88
my (@s, @stripped);
99
# comment char is default '#'
@@ -21,7 +21,6 @@ my (@s, @stripped);
2121
'more text'
2222
);
2323

24-
2524
# return the stripped strings
2625
for 0..^+@s -> $i {
2726
my $line = strip-comment(@s[$i]);
@@ -62,4 +61,59 @@ is $comm, ' some comment';
6261
is $text, 'some text';
6362
is $comm, 'some comment';
6463

64+
# test empty comment
65+
$tstr = ' some text ';
66+
($text, $comm) = strip-comment $tstr, :save-comment;
67+
is $text, ' some text ';
68+
is $comm, '';
69+
70+
($text, $comm) = strip-comment $tstr, :save-comment, :normalize;
71+
is $text, 'some text';
72+
is $comm, '';
73+
74+
# default is to take the first comment char found
75+
# note multi-char comment char is allowed
76+
$tstr = ' some text %% text %% some comment ';
77+
($text, $comm) = strip-comment $tstr, '%%', :save-comment;
78+
is $text, ' some text ';
79+
is $comm, ' text %% some comment ';
80+
81+
#===== ORIGINAL SIGNATURE IS DEPRECATED =====
82+
# testing the original signature which is now deprecated
83+
# and will be removed in version 3.0.0
84+
($text, $comm) = strip-comment $tstr, '%%', :save-comment, :normalize;
85+
is $text, 'some text';
86+
is $comm, 'text %% some comment';
87+
88+
$text = strip-comment $tstr, '%%', :last;
89+
is $text, ' some text %% text ';
90+
91+
($text, $comm) = strip-comment $tstr, '%%', :last, :save-comment;
92+
is $text, ' some text %% text ';
93+
is $comm, ' some comment ';
94+
95+
($text, $comm) = strip-comment $tstr, '%%', :last, :save-comment, :normalize;
96+
is $text, 'some text %% text';
97+
is $comm, 'some comment';
98+
99+
# bad input by user
100+
dies-ok { $text = strip-comment '#'; }, 'dies when line is same as comment char';
101+
102+
#===== END OF ORIGINAL SIGNATURE IS DEPRECATED =====
103+
104+
# test the new signature
105+
106+
# bad input by user
107+
dies-ok { $text = strip-comment '%', :mark<%>; }, 'dies when line is same as comment (:mark) char';
108+
109+
($text, $comm) = strip-comment $tstr, :mark<%%>, :last, :save-comment, :normalize;
110+
is $text, 'some text %% text';
111+
is $comm, 'some comment';
65112

113+
# watch out for embedded newlines
114+
my $s = q:to/HERE/;
115+
text 1 # comment 1
116+
text 2 # comment 2
117+
HERE
118+
$s = strip-comment $s;
119+
is $s, 'text 1 ';

0 commit comments

Comments
 (0)