| 1 | # Number::Range::Regex | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # Copyright 2012 Brian Szymanski. All rights reserved. This module is | ||||||||||||||||||||||||||||
| 5 | # free software; you can redistribute it and/or modify it under the same | ||||||||||||||||||||||||||||
| 6 | # terms as Perl itself. | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | package Number::Range::Regex; | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 14 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 14 | ▶ | use Number::Range::Regex::Range; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 14 | ▶ | use Number::Range::Regex::Iterator; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 14 | ▶ | use Number::Range::Regex::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 14 | ▶ | use Number::Range::Regex::Util::inf qw( neg_inf pos_inf ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 14 | ▶ | use vars qw ( @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 14 | eval { require warnings; }; #it's ok if we can't load warnings | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 14 | require Exporter; | |||||||||||||||||||||||||||
| 19 | 14 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 14 | @ISA = qw( Exporter ); | |||||||||||||||||||||||||||
| 21 | 14 | @EXPORT = qw( range rangespec ); | |||||||||||||||||||||||||||
| 22 | 14 | @EXPORT_OK = qw ( init regex_range ) ; | |||||||||||||||||||||||||||
| 23 | 14 | %EXPORT_TAGS = ( all => [ @EXPORT, @EXPORT_OK ] ); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 14 | $VERSION = '0.32'; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 14 | my $init_opts = $Number::Range::Regex::Range::default_opts; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 4 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 30 | 4 | my ($self, @opts) = @_; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # vestigial limb: init( foo => "bar" ) == init( { foo => "bar" } ); | ||||||||||||||||||||||||||||
| 33 | 4 | ▶ | my %opts = (@opts == 1) ? %{$opts[0]} : | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 34 | (@opts % 2 == 0) ? @opts : | ||||||||||||||||||||||||||||
| 35 | die 'usage: init( $options_ref )'; | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 4 | $init_opts = $Number::Range::Regex::Range::default_opts; | |||||||||||||||||||||||||||
| 38 | # override any values of init_opts that were passed to init | ||||||||||||||||||||||||||||
| 39 | 4 | ▶ | while (my ($key, $value) = each %opts) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 3 | $init_opts->{$key} = $value; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # regex_range( $min, $max ); #undef = no limit, so. e.g. | ||||||||||||||||||||||||||||
| 45 | # regex_range(3, undef) yields the equivalent of qr/[+]?[3-9]|\d+/; | ||||||||||||||||||||||||||||
| 46 | 271 | ▶ | sub regex_range { | ||||||||||||||||||||||||||
|
Subroutine
regex_range: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 271 | my ($min, $max, $passed_opts) = @_; | |||||||||||||||||||||||||||
| 48 | 271 | my $opts = option_mangler( $init_opts, $passed_opts ); | |||||||||||||||||||||||||||
| 49 | 270 | return range($min, $max, $opts)->regex(); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 345 | ▶ | sub range { | ||||||||||||||||||||||||||
|
Subroutine
range: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 345 | ▶ | my $opts = option_mangler( ref $_[-1] eq 'HASH' ? pop : undef ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 345 | my ($min, $max) = @_; | |||||||||||||||||||||||||||
| 55 | 345 | ▶ | if(!defined $min && !defined $max) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $min or defined $max
MC/DC100%2/2
defined $min or defined $max
defined $min
defined $max
Truth table100%3/3
A or B
A= defined $minB= defined $max
| |||||||||||||||||||||||||||||
| 56 | 3 | ▶ | die "for the set of all integers, you must specify min as '-inf' and max as '+inf', or use the allow_wildcard argument" if !$opts->{allow_wildcard}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 344 | ▶ | $min = neg_inf if !defined $min; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 344 | ▶ | $max = pos_inf if !defined $max; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 344 | return rangespec( "$min..$max", $opts ); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 491 | ▶ | sub rangespec { | ||||||||||||||||||||||||||
|
Subroutine
rangespec: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | 491 | ▶ | my $opts = option_mangler( ref $_[-1] eq 'HASH' ? pop : undef ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | # we allow (but do not like) e.g. rangespec(5,7,10..18); | ||||||||||||||||||||||||||||
| 66 | # we don't like it because it can make us run out of memory for | ||||||||||||||||||||||||||||
| 67 | # large ranges. preferred: rangespec('5,7,10..18'); | ||||||||||||||||||||||||||||
| 68 | 491 | my $spec; | |||||||||||||||||||||||||||
| 69 | 491 | ▶ | if(@_ > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 1 | warn "passed literal range to rangespec!\n"; | |||||||||||||||||||||||||||
| 71 | 1 | $spec = join $opts->{range_separator}, @_; | |||||||||||||||||||||||||||
| 72 | } else { | ||||||||||||||||||||||||||||
| 73 | 490 | $spec = $_[0]; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 491 | my $base = $opts->{base}; | |||||||||||||||||||||||||||
| 77 | 491 | my $base_digits = base_digits($base); | |||||||||||||||||||||||||||
| 78 | 491 | my $base_max = substr($base_digits, -1); | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 491 | my $digits_validate = "[$base_digits]+"; | |||||||||||||||||||||||||||
| 81 | 491 | my $range_operator = '\s*'.quotemeta( $opts->{range_operator} ).'\s*'; | |||||||||||||||||||||||||||
| 82 | 491 | my $range_separator = '\s*'.quotemeta( $opts->{range_separator} ).'\s*'; | |||||||||||||||||||||||||||
| 83 | 491 | my $section_validate = qr/(?:-?$digits_validate|(?:-?$digits_validate|-inf)$range_operator(?:\+?inf|-?$digits_validate))/; | |||||||||||||||||||||||||||
| 84 | 491 | my $range_validate = qr/(?:|$section_validate(?:$range_separator$section_validate)*)/; | |||||||||||||||||||||||||||
| 85 | 491 | ▶ | die "invalid rangespec '$spec' !~ /$range_validate/" unless $spec =~ /^$range_validate$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 487 | my @sections = split /$range_separator/, $spec; | |||||||||||||||||||||||||||
| 88 | 487 | my @ranges; | |||||||||||||||||||||||||||
| 89 | 487 | foreach my $section (@sections) { | |||||||||||||||||||||||||||
| 90 | 617 | ▶ | if($section =~ /^(-?$digits_validate)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 64 | push @ranges, Number::Range::Regex::SimpleRange->new( $1, $1, $opts ); | |||||||||||||||||||||||||||
| 92 | } else { | ||||||||||||||||||||||||||||
| 93 | 553 | my ($min, $max) = map { s/^\s+//; s/\s+$//; $_ } split /$range_operator/, $section, 2; | |||||||||||||||||||||||||||
| 94 | 553 | push @ranges, Number::Range::Regex::SimpleRange->new( $min, $max, $opts ); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | my $warn_overlap = defined $opts->{warn_overlap} ? | ||||||||||||||||||||||||||||
| 98 | 485 | ▶ | $opts->{warn_overlap} : 'rangespec'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | # note: multi_union() will have the side effect of sorting | ||||||||||||||||||||||||||||
| 100 | # and de-overlap-ify-ing the input ranges | ||||||||||||||||||||||||||||
| 101 | 485 | return multi_union( @ranges, { warn_overlap => $warn_overlap } ); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 14 | 1; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | __END__ | ||||||||||||||||||||||||||||