| 1 | # Data::Hopen::Util::NameSet - set of strings and regexps | ||||||||||||||||||||||
| 2 | package Data::Hopen::Util::NameSet; | ||||||||||||||||||||||
| 3 | 23 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | 23 | ▶ | use Data::Hopen::Base; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | |||||||||||||||||||||||
| 6 | 23 | our $VERSION = '0.000021'; | |||||||||||||||||||||
| 7 | |||||||||||||||||||||||
| 8 | # Docs {{{1 | ||||||||||||||||||||||
| 9 | |||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||
| 11 | |||||||||||||||||||||||
| 12 | Data::Hopen::Util::NameSet - set of names (strings or regexps) | ||||||||||||||||||||||
| 13 | |||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||
| 15 | |||||||||||||||||||||||
| 16 | NameSet stores strings and regexps, and can quickly tell you whether | ||||||||||||||||||||||
| 17 | a given string matches one of the stored strings or regexps. | ||||||||||||||||||||||
| 18 | |||||||||||||||||||||||
| 19 | =cut | ||||||||||||||||||||||
| 20 | |||||||||||||||||||||||
| 21 | # }}}1 | ||||||||||||||||||||||
| 22 | |||||||||||||||||||||||
| 23 | =head1 FUNCTIONS | ||||||||||||||||||||||
| 24 | |||||||||||||||||||||||
| 25 | =head2 new | ||||||||||||||||||||||
| 26 | |||||||||||||||||||||||
| 27 | Create a new instance. Usage: C<< Data::Hopen::Util::Nameset->new(...) >>. | ||||||||||||||||||||||
| 28 | The parameters are as L</add>. | ||||||||||||||||||||||
| 29 | |||||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||||
| 31 | |||||||||||||||||||||||
| 32 | 53 | ▶ | sub new { | ||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||
| 33 | 53 | ▶ | my $class = shift or croak 'Call as ' . __PACKAGE__ . '->new(...)'; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 34 | 52 | my $self = bless { _strings => [], _regexps => [], _RE => undef }, $class; | |||||||||||||||||||||
| 35 | 52 | ▶ | $self->add(@_) if @_; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 36 | 51 | return $self; | |||||||||||||||||||||
| 37 | } #new() | ||||||||||||||||||||||
| 38 | |||||||||||||||||||||||
| 39 | =head2 add | ||||||||||||||||||||||
| 40 | |||||||||||||||||||||||
| 41 | Add one or more strings or regexps to the NameSet. Usage: | ||||||||||||||||||||||
| 42 | |||||||||||||||||||||||
| 43 | $instance->add(x1, x2, ...) | ||||||||||||||||||||||
| 44 | |||||||||||||||||||||||
| 45 | where each C<xn> can be a scalar, regexp, arrayref (processed recursively) | ||||||||||||||||||||||
| 46 | or hashref (the keys are added and the values are ignored). | ||||||||||||||||||||||
| 47 | |||||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||||
| 49 | |||||||||||||||||||||||
| 50 | 40 | ▶ | sub add { | ||||||||||||||||||||
|
Subroutine
add: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||
| 51 | 40 | ▶ | my $self = shift or croak 'Need an instance'; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 52 | 39 | ▶ | return unless @_; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 53 | 38 | $self->{_RE} = undef; # dirty the instance | |||||||||||||||||||||
| 54 | |||||||||||||||||||||||
| 55 | 38 | foreach my $arg (@_) { | |||||||||||||||||||||
| 56 | 61 | ▶ | if(!ref $arg) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 57 | 24 | push @{$self->{_strings}}, "$arg"; | |||||||||||||||||||||
| 58 | } elsif(ref $arg eq 'Regexp') { | ||||||||||||||||||||||
| 59 | 29 | push @{$self->{_regexps}}, $arg; | |||||||||||||||||||||
| 60 | } elsif(ref $arg eq 'ARRAY') { | ||||||||||||||||||||||
| 61 | 4 | $self->add(@$arg); | |||||||||||||||||||||
| 62 | } elsif(ref $arg eq 'HASH') { | ||||||||||||||||||||||
| 63 | 3 | $self->add(keys %$arg); | |||||||||||||||||||||
| 64 | } else { | ||||||||||||||||||||||
| 65 | 23 | ▶ | use Data::Dumper; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 66 | 1 | croak "I don't know how to handle this: " . Dumper($arg) | |||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||
| 69 | } #add() | ||||||||||||||||||||||
| 70 | |||||||||||||||||||||||
| 71 | =head2 contains | ||||||||||||||||||||||
| 72 | |||||||||||||||||||||||
| 73 | Return truthy if the NameSet contains the argument. Usage: | ||||||||||||||||||||||
| 74 | C<< $set->contains('foo') >>. | ||||||||||||||||||||||
| 75 | |||||||||||||||||||||||
| 76 | =cut | ||||||||||||||||||||||
| 77 | |||||||||||||||||||||||
| 78 | 74 | ▶ | sub contains { | ||||||||||||||||||||
|
Subroutine
contains: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||
| 79 | 74 | ▶ | my $self = shift or croak 'Need an instance'; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 80 | 73 | ▶ | $self->{_RE} = $self->_build unless $self->{_RE}; # Clean | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 81 | #say STDERR $self->{_RE}; | ||||||||||||||||||||||
| 82 | 73 | return shift =~ $self->{_RE}; | |||||||||||||||||||||
| 83 | } #contains() | ||||||||||||||||||||||
| 84 | |||||||||||||||||||||||
| 85 | =head2 strings | ||||||||||||||||||||||
| 86 | |||||||||||||||||||||||
| 87 | Accessor for the strings in the set. Returns an arrayref. | ||||||||||||||||||||||
| 88 | |||||||||||||||||||||||
| 89 | =cut | ||||||||||||||||||||||
| 90 | |||||||||||||||||||||||
| 91 | 2 | ▶ | sub strings { (shift)->{_strings} } | ||||||||||||||||||||
|
Subroutine
strings: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 92 | |||||||||||||||||||||||
| 93 | =head2 regexps | ||||||||||||||||||||||
| 94 | |||||||||||||||||||||||
| 95 | Accessor for the regexps in the set. Returns an arrayref. | ||||||||||||||||||||||
| 96 | |||||||||||||||||||||||
| 97 | =cut | ||||||||||||||||||||||
| 98 | |||||||||||||||||||||||
| 99 | 2 | ▶ | sub regexps { (shift)->{_regexps} } | ||||||||||||||||||||
|
Subroutine
regexps: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 100 | |||||||||||||||||||||||
| 101 | =head2 complex | ||||||||||||||||||||||
| 102 | |||||||||||||||||||||||
| 103 | Returns truthy if the nameset has any regular expressions. | ||||||||||||||||||||||
| 104 | |||||||||||||||||||||||
| 105 | =cut | ||||||||||||||||||||||
| 106 | |||||||||||||||||||||||
| 107 | 4 | ▶ | sub complex { @{(shift)->{_regexps}} > 0 } | ||||||||||||||||||||
|
Subroutine
complex: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 108 | |||||||||||||||||||||||
| 109 | =head2 _build | ||||||||||||||||||||||
| 110 | |||||||||||||||||||||||
| 111 | (Internal) Build a regex from all the strings and regexps in the set. | ||||||||||||||||||||||
| 112 | Returns the new regexp --- does not mutate $self. | ||||||||||||||||||||||
| 113 | |||||||||||||||||||||||
| 114 | In the current implementation, strings are matched case-sensitively. | ||||||||||||||||||||||
| 115 | Regexps are matched with whatever flags they were compiled with. | ||||||||||||||||||||||
| 116 | |||||||||||||||||||||||
| 117 | =cut | ||||||||||||||||||||||
| 118 | |||||||||||||||||||||||
| 119 | 8 | ▶ | sub _build { | ||||||||||||||||||||
|
Subroutine
_build: called CC=4 SCAR=13.9
| |||||||||||||||||||||||
| 120 | 8 | ▶ | my $self = shift or croak 'Need an instance'; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 121 | |||||||||||||||||||||||
| 122 | 7 | my @quoted_strs; | |||||||||||||||||||||
| 123 | 7 | ▶ | if(@{$self->{_strings}}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 124 | push @quoted_strs, | ||||||||||||||||||||||
| 125 | 3 | join '|', map { quotemeta } @{$self->{_strings}}; | |||||||||||||||||||||
| 126 | # TODO should I be using qr/\Q$_\E/ instead, since quotemeta | ||||||||||||||||||||||
| 127 | # isn't quite right on 5.14? Or should I be using 5.16+? | ||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||
| 129 | |||||||||||||||||||||||
| 130 | 7 | my $pattern = join '|', @{$self->{_regexps}}, @quoted_strs; | |||||||||||||||||||||
| 131 | # Each regexp stringifies with surrounding parens, so we | ||||||||||||||||||||||
| 132 | # don't need to add any. | ||||||||||||||||||||||
| 133 | |||||||||||||||||||||||
| 134 | 7 | ▶ | return $pattern ? qr/\A(?:$pattern)\z/ : qr/(*FAIL)/; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 135 | # If $pattern is empty, the nameset is empty (`(*FAIL)`). Without the | ||||||||||||||||||||||
| 136 | # ?:, qr// would match anything, when we want to match nothing. | ||||||||||||||||||||||
| 137 | } #_build() | ||||||||||||||||||||||
| 138 | |||||||||||||||||||||||
| 139 | 23 | 1; | |||||||||||||||||||||
| 140 | __END__ | ||||||||||||||||||||||