| 1 | # Build::Hopen::Util::NameSet - set of strings and regexps | ||||||||||||||||||||||
| 2 | package Build::Hopen::Util::NameSet; | ||||||||||||||||||||||
| 3 | 16 | ▶ | use Build::Hopen::Base; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | |||||||||||||||||||||||
| 5 | 16 | our $VERSION = '0.000008'; # TRIAL | |||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | # Docs {{{1 | ||||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||
| 10 | |||||||||||||||||||||||
| 11 | Build::Hopen::Util::NameSet - set of names (strings or regexps) | ||||||||||||||||||||||
| 12 | |||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||
| 14 | |||||||||||||||||||||||
| 15 | NameSet stores strings and regexps, and can quickly tell you whether | ||||||||||||||||||||||
| 16 | a given string matches one of the stored strings or regexps. | ||||||||||||||||||||||
| 17 | |||||||||||||||||||||||
| 18 | =cut | ||||||||||||||||||||||
| 19 | |||||||||||||||||||||||
| 20 | # }}}1 | ||||||||||||||||||||||
| 21 | |||||||||||||||||||||||
| 22 | =head1 FUNCTIONS | ||||||||||||||||||||||
| 23 | |||||||||||||||||||||||
| 24 | =head2 new | ||||||||||||||||||||||
| 25 | |||||||||||||||||||||||
| 26 | Create a new instance. Usage: C<< Build::Hopen::Util::Nameset->new(...) >>. | ||||||||||||||||||||||
| 27 | The parameters are as L</add>. | ||||||||||||||||||||||
| 28 | |||||||||||||||||||||||
| 29 | =cut | ||||||||||||||||||||||
| 30 | |||||||||||||||||||||||
| 31 | 36 | ▶ | sub new { | ||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||
| 32 | 36 | ▶ | my $class = shift or croak 'Call as ' . __PACKAGE__ . '->new(...)'; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 33 | 36 | my $self = bless { _strings => [], _regexps => [], _RE => undef }, $class; | |||||||||||||||||||||
| 34 | 36 | ▶ | $self->add(@_) if @_; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 35 | 36 | return $self; | |||||||||||||||||||||
| 36 | } #new() | ||||||||||||||||||||||
| 37 | |||||||||||||||||||||||
| 38 | =head2 add | ||||||||||||||||||||||
| 39 | |||||||||||||||||||||||
| 40 | Add one or more strings or regexps to the NameSet. Usage: | ||||||||||||||||||||||
| 41 | |||||||||||||||||||||||
| 42 | $instance->add(x1, x2, ...) | ||||||||||||||||||||||
| 43 | |||||||||||||||||||||||
| 44 | where each C<xn> can be a scalar, regexp, arrayref (processed recursively) | ||||||||||||||||||||||
| 45 | or hashref (the keys are added and the values are ignored). | ||||||||||||||||||||||
| 46 | |||||||||||||||||||||||
| 47 | =cut | ||||||||||||||||||||||
| 48 | |||||||||||||||||||||||
| 49 | 27 | ▶ | sub add { | ||||||||||||||||||||
|
Subroutine
add: called CC=8 SCAR=21.1
Pod
documented
| |||||||||||||||||||||||
| 50 | 27 | ▶ | my $self = shift or croak 'Need an instance'; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 51 | 27 | ▶ | return unless @_; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 52 | 27 | $self->{_RE} = undef; # dirty the instance | |||||||||||||||||||||
| 53 | |||||||||||||||||||||||
| 54 | 27 | foreach my $arg (@_) { | |||||||||||||||||||||
| 55 | 49 | ▶ | if(!ref $arg) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 56 | 22 | push @{$self->{_strings}}, "$arg"; | |||||||||||||||||||||
| 57 | } elsif(ref $arg eq 'Regexp') { | ||||||||||||||||||||||
| 58 | 21 | push @{$self->{_regexps}}, $arg; | |||||||||||||||||||||
| 59 | } elsif(ref $arg eq 'ARRAY') { | ||||||||||||||||||||||
| 60 | 4 | $self->add(@$arg); | |||||||||||||||||||||
| 61 | } elsif(ref $arg eq 'HASH') { | ||||||||||||||||||||||
| 62 | 2 | $self->add(keys %$arg); | |||||||||||||||||||||
| 63 | } else { | ||||||||||||||||||||||
| 64 | 16 | ▶ | use Data::Dumper; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 65 | 0 | croak "I don't know how to handle this: " . Dumper($arg) | |||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||
| 68 | } #add() | ||||||||||||||||||||||
| 69 | |||||||||||||||||||||||
| 70 | =head2 contains | ||||||||||||||||||||||
| 71 | |||||||||||||||||||||||
| 72 | Return truthy if the NameSet contains the argument. Usage: | ||||||||||||||||||||||
| 73 | C<< $set->contains('foo') >>. | ||||||||||||||||||||||
| 74 | |||||||||||||||||||||||
| 75 | =cut | ||||||||||||||||||||||
| 76 | |||||||||||||||||||||||
| 77 | 90 | ▶ | sub contains { | ||||||||||||||||||||
|
Subroutine
contains: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||
| 78 | 90 | ▶ | my $self = shift or croak 'Need an instance'; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 79 | 90 | ▶ | $self->{_RE} = $self->_build unless $self->{_RE}; # Clean | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 80 | #say STDERR $self->{_RE}; | ||||||||||||||||||||||
| 81 | 90 | return shift =~ $self->{_RE}; | |||||||||||||||||||||
| 82 | } #contains() | ||||||||||||||||||||||
| 83 | |||||||||||||||||||||||
| 84 | =head2 smartmatch overload | ||||||||||||||||||||||
| 85 | |||||||||||||||||||||||
| 86 | For convenience, C<< 'foo' ~~ $nameset >> invokes | ||||||||||||||||||||||
| 87 | C<< $nameset->contains('foo') >>. This is inspired by the Raku behaviour, | ||||||||||||||||||||||
| 88 | in which C<< $x ~~ $y >> calls C<< $y.ACCEPTS($x) >> | ||||||||||||||||||||||
| 89 | |||||||||||||||||||||||
| 90 | NOTE: C<< $nameset ~~ 'foo' >> (object first) is officially not supported by | ||||||||||||||||||||||
| 91 | this module. This form is possible in stable perls at least through 5.26. | ||||||||||||||||||||||
| 92 | However, the changes (since reverted) in 5.27.7 would not have supported this | ||||||||||||||||||||||
| 93 | form. See | ||||||||||||||||||||||
| 94 | L<http://blogs.perl.org/users/leon_timmermans/2017/12/smartmatch-in-5277.html>. | ||||||||||||||||||||||
| 95 | However, as far as I can tell, even 5.27.7 would have supported the | ||||||||||||||||||||||
| 96 | C<< 'foo' ~~ $nameset >> form. | ||||||||||||||||||||||
| 97 | |||||||||||||||||||||||
| 98 | =cut | ||||||||||||||||||||||
| 99 | |||||||||||||||||||||||
| 100 | use overload | ||||||||||||||||||||||
| 101 | 16 | ▶ | fallback => 1, | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 102 | 69 | ▶ | '~~' => sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 103 | #my ($self, $other, $swap) = @_; | ||||||||||||||||||||||
| 104 | 69 | $_[0]->contains($_[1]) | |||||||||||||||||||||
| 105 | 16 | }; | |||||||||||||||||||||
| 106 | |||||||||||||||||||||||
| 107 | =head2 strings | ||||||||||||||||||||||
| 108 | |||||||||||||||||||||||
| 109 | Accessor for the strings in the set. Returns an arrayref. | ||||||||||||||||||||||
| 110 | |||||||||||||||||||||||
| 111 | =cut | ||||||||||||||||||||||
| 112 | |||||||||||||||||||||||
| 113 | 2 | ▶ | sub strings { (shift)->{_strings} } | ||||||||||||||||||||
|
Subroutine
strings: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 114 | |||||||||||||||||||||||
| 115 | =head2 regexps | ||||||||||||||||||||||
| 116 | |||||||||||||||||||||||
| 117 | Accessor for the regexps in the set. Returns an arrayref. | ||||||||||||||||||||||
| 118 | |||||||||||||||||||||||
| 119 | =cut | ||||||||||||||||||||||
| 120 | |||||||||||||||||||||||
| 121 | 2 | ▶ | sub regexps { (shift)->{_regexps} } | ||||||||||||||||||||
|
Subroutine
regexps: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 122 | |||||||||||||||||||||||
| 123 | =head2 complex | ||||||||||||||||||||||
| 124 | |||||||||||||||||||||||
| 125 | Returns truthy if the nameset has any regular expressions. | ||||||||||||||||||||||
| 126 | |||||||||||||||||||||||
| 127 | =cut | ||||||||||||||||||||||
| 128 | |||||||||||||||||||||||
| 129 | 4 | ▶ | sub complex { @{(shift)->{_regexps}} > 0 } | ||||||||||||||||||||
|
Subroutine
complex: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 130 | |||||||||||||||||||||||
| 131 | =head2 _build | ||||||||||||||||||||||
| 132 | |||||||||||||||||||||||
| 133 | (Internal) Build a regex from all the strings and regexps in the set. | ||||||||||||||||||||||
| 134 | Returns the new regexp --- does not mutate $self. | ||||||||||||||||||||||
| 135 | |||||||||||||||||||||||
| 136 | In the current implementation, strings are matched case-sensitively. | ||||||||||||||||||||||
| 137 | Regexps are matched with whatever flags they were compiled with. | ||||||||||||||||||||||
| 138 | |||||||||||||||||||||||
| 139 | =cut | ||||||||||||||||||||||
| 140 | |||||||||||||||||||||||
| 141 | 5 | ▶ | sub _build { | ||||||||||||||||||||
|
Subroutine
_build: called CC=4 SCAR=14.0
| |||||||||||||||||||||||
| 142 | 5 | ▶ | my $self = shift or croak 'Need an instance'; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 143 | |||||||||||||||||||||||
| 144 | 5 | my $strs = join '|', map { quotemeta } @{$self->{_strings}}; | |||||||||||||||||||||
| 145 | # TODO should I be using qr/\Q$_\E/ instead, since quotemeta | ||||||||||||||||||||||
| 146 | # isn't quite right on 5.14? Or should I be using 5.16+? | ||||||||||||||||||||||
| 147 | # See how the cpantesters results for t/008 turn out on 5.14. | ||||||||||||||||||||||
| 148 | 5 | ▶ | my $str = join '|', @{$self->{_regexps}}, ($strs || ()); | ||||||||||||||||||||
|
Condition66%2/3
$strs || ()
MC/DC0%0/2
$strs || ()
$strs
()
Truth table66%2/3
A || B
A= $strsB= ()
| |||||||||||||||||||||||
| 149 | # Each regexp stringifies with surrounding parens, so we | ||||||||||||||||||||||
| 150 | # don't need to add any. | ||||||||||||||||||||||
| 151 | |||||||||||||||||||||||
| 152 | 5 | ▶ | return $str ? qr/\A(?:$str)\z/ : qr/(*FAIL)/; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 153 | # If $str is empty, the nameset is empty (`(*FAIL)`). Without the ?: , | ||||||||||||||||||||||
| 154 | # qr// would match anything, when we want to match nothing. | ||||||||||||||||||||||
| 155 | } #_build() | ||||||||||||||||||||||
| 156 | |||||||||||||||||||||||
| 157 | 16 | 1; | |||||||||||||||||||||
| 158 | __END__ | ||||||||||||||||||||||