Skip to content

Commit 767db96

Browse files
committed
Got NOT highlighting going OK
1 parent 029fc9b commit 767db96

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

andor.pl

+28-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
MAIN: {
13-
my @words = @ARGV;
13+
my @words = @ARGV or die "No words";
1414

1515
my @lines = <DATA>;
1616
chomp @lines;
@@ -25,7 +25,7 @@
2525
my $hl_re = join( '|', @words );
2626
$hl_re = qr/($hl_re)/;
2727

28-
_iter( $match_and_re, $hl_re, @lines );
28+
_iter( $match_and_re, undef, $hl_re, @lines );
2929
}
3030

3131
OR: {
@@ -37,7 +37,21 @@
3737
my $hl_re = join( '|', @words );
3838
$hl_re = qr/($hl_re)/;
3939

40-
_iter( $match_or_re, $hl_re, @lines );
40+
_iter( $match_or_re, undef, $hl_re, @lines );
41+
}
42+
43+
NOT: {
44+
say '';
45+
say "NOT";
46+
my ($keep, @not) = @words;
47+
my $not_re;
48+
if ( @not ) {
49+
$not_re = join( '|', @not );
50+
$not_re = qr/$not_re/;
51+
}
52+
my $hl_re = qr/($keep)/;
53+
54+
_iter( $keep, $not_re, $hl_re, @lines );
4155
}
4256
}
4357

@@ -47,16 +61,22 @@
4761

4862
sub _iter {
4963
my $match_re = shift;
64+
my $not_re = shift;
5065
my $hl_re = shift;
5166
my @lines = @_;
5267

53-
say "match=$match_re";
54-
say "hlite=$hl_re";
68+
say " match=$match_re";
69+
say " notre=", ($not_re // 'undef');
70+
say " hlite=$hl_re";
5571

5672
for ( @lines ) {
5773
my $line = "$_";
5874

59-
if ( $line =~ /$match_re/ ) {
75+
my $match = $line =~ /$match_re/;
76+
if ( $match && defined $not_re ) {
77+
$match = $line !~ /$not_re/;
78+
}
79+
if ( $match ) {
6080
while ( $line =~ /$hl_re/g ) {
6181
my $match_start = $-[0] // next;
6282
my $match_end = $+[0];
@@ -86,3 +106,5 @@ sub _iter {
86106
sox got no
87107
table and apple and penny
88108
table only
109+
penny saved an apple
110+
penny sat at the table

0 commit comments

Comments
 (0)