Skip to content

Commit 2be3680

Browse files
author
Tom O'Hara
committed
warns about achors occuring outside of start or end
1 parent b490f38 commit 2be3680

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

count_it.perl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,9 @@ BEGIN
133133
# See if regex has line achor (^ or $)
134134
## TODO: exclude \$ at end of string (e.g., "100\$")
135135
## TODO: add unit test: (echo "12 34 56 78" | count_it.perl '\d{2}' | wc -l) => 4
136+
## TODO2: allow for line archor within grouping parenthesis
136137
my($has_line_anchor) = ((index($pattern, "^") == 0)
137-
|| (rindex($pattern, "\$") == length($pattern)));
138+
|| (rindex($pattern, "\$") == length($pattern)));
138139
&init_var(*one_per_line, $has_line_anchor); # only count one instance of the pattern per line
139140
&init_var(*multi_per_line, ! $one_per_line); # count multiple instance of the pattern per line (even when ^ and $ are specified)
140141
&assertion(! ($one_per_line && $multi_per_line));
@@ -145,7 +146,10 @@ BEGIN
145146
eval "use locale;'"
146147
}
147148

148-
# Sanity check for whether one-per-line option might be needed
149+
# Sanity checks for whether -one-per-line option might be needed
150+
if ((! $one_per_line) && (! $has_line_anchor) && ($pattern =~ /[^\][\^\$]/)) {
151+
&warning("Anchors currently start or end line: consider -one_per_line\n");
152+
}
149153
# NOTE: checks against pattern need to occur prior to modification (e.g., paren addition)
150154
# TODO: handle multiple patterns per line (e.g., set line break to null); likewise check for multiple end-of-line matching
151155
if ((&DEBUG_LEVEL > 3) && ($pattern =~ /^\^/) && ($pattern !~ /[\$\n]$/) && (! $multi_per_line)) {

0 commit comments

Comments
 (0)