| 1 | # Number::Range::Regex::Range | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Copyright 2012 Brian Szymanski. All rights reserved. This module is | ||||||||||||||||||||||||||||
| 4 | # free software; you can redistribute it and/or modify it under the same | ||||||||||||||||||||||||||||
| 5 | # terms as Perl itself. | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | package Number::Range::Regex::Range; | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 14 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 14 | ▶ | use vars qw ( @ISA @EXPORT @EXPORT_OK $VERSION | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 14 | $default_opts @STANDARD_DIGIT_ORDER ); | |||||||||||||||||||||||||||
| 12 | 14 | eval { require warnings; }; #it's ok if we can't load warnings | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 14 | require Exporter; | |||||||||||||||||||||||||||
| 15 | 14 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 14 | @ISA = qw( Exporter ); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 14 | $VERSION = '0.32'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 14 | ▶ | use Number::Range::Regex::CompoundRange; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 14 | ▶ | use Number::Range::Regex::SimpleRange; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 14 | ▶ | use Number::Range::Regex::TrivialRange; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 14 | ▶ | use Number::Range::Regex::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 14 | $default_opts = { | |||||||||||||||||||||||||||
| 26 | allow_wildcard => 0, | ||||||||||||||||||||||||||||
| 27 | autoswap => 0, | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | base => 10, | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | range_operator => '..', | ||||||||||||||||||||||||||||
| 32 | range_separator => ',', | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | no_leading_zeroes => 0, | ||||||||||||||||||||||||||||
| 35 | no_sign => 0, | ||||||||||||||||||||||||||||
| 36 | comment => 1, | ||||||||||||||||||||||||||||
| 37 | readable => 0, | ||||||||||||||||||||||||||||
| 38 | }; | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 14 | my $opt_aliases = { | |||||||||||||||||||||||||||
| 41 | 'us_number' => { digitgroup => { max => 999, separator => ',', padding => '0' } }, | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 'dotted_quad' => 'ipv4', | ||||||||||||||||||||||||||||
| 44 | 'ipv4' => { range_operator => '-', digitgroup => { max => 255, separator => '.', padding => '' } }, | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 'mac_addr' => 'mac_addr', | ||||||||||||||||||||||||||||
| 47 | 'mac' => { base => 16, digitgroup => { max => 'ff', separator => ':', padding => '0' } }, | ||||||||||||||||||||||||||||
| 48 | }; | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 14 | ▶ | while (my ($key, $value) = each %$opt_aliases) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 70 | ▶ | unless(ref $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 28 | $opt_aliases->{$key} = $opt_aliases->{$value}; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 14 | @STANDARD_DIGIT_ORDER = (0..9, 'a'..'z'); #TODO: maybe use constant? | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 153 | ▶ | use overload bool => sub { return $_[0] }, | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | 6 | ▶ | '""' => sub { return $_[0]->overload_string() }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | 1062 | ▶ | 'qr' => sub { return $_[0]->regex() }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | 'ne' => sub { return !_equals( @_ ) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 63 | 14 | 'eq' => \&_equals; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # note: we don't use overload's fallback feature for eq/ne because | ||||||||||||||||||||||||||||
| 66 | # doing so would also define lt, le, gt, ge, and cmp which are | ||||||||||||||||||||||||||||
| 67 | # suspect in this context - if you are trying it without ->to_string()ing | ||||||||||||||||||||||||||||
| 68 | # first, you're probably doing something wrong... | ||||||||||||||||||||||||||||
| 69 | 1 | ▶ | sub _equals { | ||||||||||||||||||||||||||
|
Subroutine
_equals: called CC=5 SCAR=19.4
| |||||||||||||||||||||||||||||
| 70 | 1 | my ($a, $b) = @_; | |||||||||||||||||||||||||||
| 71 | 1 | ▶ | $a = $a->to_string() if ref($a) && $a->isa('Number::Range::Regex::Range'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $a and $a->isa("Number::Range::Regex::Range")
MC/DC0%0/2
ref $a and $a->isa("Number::Range::Regex::Range")
ref $a
$a->isa("Number::Range::Regex::Range")
Truth table33%1/3
A and B
A= ref $aB= $a->isa("Number::Range::Regex::Range")
| |||||||||||||||||||||||||||||
| 72 | 1 | ▶ | $b = $b->to_string() if ref($b) && $b->isa('Number::Range::Regex::Range'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $b and $b->isa("Number::Range::Regex::Range")
MC/DC0%0/2
ref $b and $b->isa("Number::Range::Regex::Range")
ref $b
$b->isa("Number::Range::Regex::Range")
Truth table33%1/3
A and B
A= ref $bB= $b->isa("Number::Range::Regex::Range")
| |||||||||||||||||||||||||||||
| 73 | 1 | return $a eq $b; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 6 | ▶ | sub overload_string { | ||||||||||||||||||||||||||
|
Subroutine
overload_string: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||
| 78 | 6 | ▶ | $self->{has_regex_overloading} = has_regex_overloading() unless defined $self->{has_regex_overloading}; # be cheap, save some sub calls | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | # if we can distinguish regex from string context, then return a | ||||||||||||||||||||||||||||
| 80 | # human-friendly format. otherwise, return the (probably hairy) regex | ||||||||||||||||||||||||||||
| 81 | 6 | ▶ | return $self->{has_regex_overloading} ? $self->to_string() : $self->regex(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 12 | ▶ | sub iterator { | ||||||||||||||||||||||||||
|
Subroutine
iterator: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 85 | 12 | my ($self) = @_; | |||||||||||||||||||||||||||
| 86 | 12 | return Number::Range::Regex::Iterator->new( $self ); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub new { die "called abstract Range->new() on a ".ref($_[0]) } | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | sub to_string { die "called abstract Range->to_string() on a ".ref($_[0]) } | ||||||||||||||||||||||||||
|
Subroutine
to_string: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | sub regex { die "called abstract Range->regex() on a ".ref($_[0]) } | ||||||||||||||||||||||||||
|
Subroutine
regex: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | sub union { die "called abstract Range->union() on a ".ref($_[0]) } | ||||||||||||||||||||||||||
|
Subroutine
union: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 93 | 9 | ▶ | sub intersect { shift->intersection(@_); } | ||||||||||||||||||||||||||
|
Subroutine
intersect: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | sub intersection { die "called abstract Range->intersection() on a ".ref($_[0]) } | ||||||||||||||||||||||||||
|
Subroutine
intersection: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 95 | 38 | ▶ | sub minus { shift->subtract(@_); } | ||||||||||||||||||||||||||
|
Subroutine
minus: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | sub subtraction { shift->subtract(@_); } | ||||||||||||||||||||||||||
|
Subroutine
subtraction: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | sub subtract { die "called abstract Range->subtract() on a ".ref($_[0]) } | ||||||||||||||||||||||||||
|
Subroutine
subtract: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub xor { die "called abstract Range->xor() on a ".ref($_[0]) } | ||||||||||||||||||||||||||
|
Subroutine
xor: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 99 | 62 | ▶ | sub not { shift->invert(@_); } | ||||||||||||||||||||||||||
|
Subroutine
not: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 100 | ## relative complemet == subtraction, absolute complement == invert | ||||||||||||||||||||||||||||
| 101 | ## but it probably will cause more confusion to include this than not | ||||||||||||||||||||||||||||
| 102 | #sub complement { | ||||||||||||||||||||||||||||
| 103 | # my ($self, $other) = @_; | ||||||||||||||||||||||||||||
| 104 | # return $self->subtract( $other ) if $other; | ||||||||||||||||||||||||||||
| 105 | # return $self->invert(); | ||||||||||||||||||||||||||||
| 106 | #} | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | sub invert { die "called abstract Range->invert() on a ".ref($_[0]) } | ||||||||||||||||||||||||||
|
Subroutine
invert: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub touches { die "called abstract Range->touches() on a ".ref($_[0]) } | ||||||||||||||||||||||||||
|
Subroutine
touches: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | sub contains { die "called abstract Range->contains() on a ".ref($_[0]) } | ||||||||||||||||||||||||||
|
Subroutine
contains: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | sub has_lower_bound { die "called abstract Range->has_lower_bound() on a ".ref($_[0]) } | ||||||||||||||||||||||||||
|
Subroutine
has_lower_bound: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub has_upper_bound { die "called abstract Range->has_upper_bound() on a ".ref($_[0]) } | ||||||||||||||||||||||||||
|
Subroutine
has_upper_bound: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | sub is_infinite { die "called abstract Range->is_infinite() on a ".ref($_[0]) } | ||||||||||||||||||||||||||
|
Subroutine
is_infinite: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | sub is_empty { die "called abstract Range->is_empty() on a ".ref($_[0]) } | ||||||||||||||||||||||||||
|
Subroutine
is_empty: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 14 | 1; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||