| 1 | # Number::Range::Regex::TrivialRange | ||||||||||||||
| 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::TrivialRange; | ||||||||||||||
| 8 | |||||||||||||||
| 9 | # one range, expressible in the form $header.$range.$trailer, where | ||||||||||||||
| 10 | # header = \d+ | ||||||||||||||
| 11 | # range = [\d-\d] | ||||||||||||||
| 12 | # trailer = \\d+ | ||||||||||||||
| 13 | # e.g. 12[3-8]\d\d | ||||||||||||||
| 14 | |||||||||||||||
| 15 | 14 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 14 | ▶ | use vars qw ( @ISA @EXPORT @EXPORT_OK $VERSION ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 14 | eval { require warnings; }; #it's ok if we can't load warnings | |||||||||||||
| 18 | |||||||||||||||
| 19 | 14 | require Exporter; | |||||||||||||
| 20 | 14 | ▶ | use base 'Exporter'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | 14 | @ISA = qw( Exporter Number::Range::Regex::SimpleRange ); | |||||||||||||
| 22 | |||||||||||||||
| 23 | 14 | $VERSION = '0.32'; | |||||||||||||
| 24 | |||||||||||||||
| 25 | 14 | ▶ | use Number::Range::Regex::SimpleRange; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 26 | 14 | ▶ | use Number::Range::Regex::Util ':all'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 27 | 14 | ▶ | use Number::Range::Regex::Util::inf qw ( neg_inf pos_inf _is_negative ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 28 | |||||||||||||||
| 29 | 968 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 30 | 968 | my ($class, $min, $max, $passed_opts) = @_; | |||||||||||||
| 31 | |||||||||||||||
| 32 | 968 | my $opts = option_mangler( $passed_opts ); | |||||||||||||
| 33 | |||||||||||||||
| 34 | 968 | my $base = $opts->{base}; | |||||||||||||
| 35 | 968 | my $base_digits = $opts->{base_digits} = base_digits($base); | |||||||||||||
| 36 | 968 | my $base_max = $opts->{base_max} = substr($base_digits, -1); | |||||||||||||
| 37 | 968 | my $base_digits_regex = $opts->{base_digits_regex} = _calculate_digit_range( 0, $base_max, $base_digits ); | |||||||||||||
| 38 | |||||||||||||||
| 39 | 968 | return bless { min => $min, max => $max, opts => $opts }, $class; | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | |||||||||||||||
| 42 | 5475 | ▶ | sub regex { | ||||||||||||
|
Subroutine
regex: called CC=16 SCAR=27.8
Pod
undocumented
| |||||||||||||||
| 43 | 5475 | my ($self, $passed_opts) = @_; | |||||||||||||
| 44 | |||||||||||||||
| 45 | 5475 | my $opts = option_mangler( $self->{opts}, $passed_opts ); | |||||||||||||
| 46 | |||||||||||||||
| 47 | 5475 | ▶ | my $zeroes_maybe = $opts->{no_leading_zeroes} ? '' : '0*'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 48 | |||||||||||||||
| 49 | 5475 | ▶ | if( _is_negative( $self->{min} ) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 50 | 134 | my $pmin = abs $self->{max}; | |||||||||||||
| 51 | # -'-inf' == 'inf' according to perl. that's no good for us | ||||||||||||||
| 52 | #TODO: is the above still true with Util/inf.pm ? | ||||||||||||||
| 53 | 134 | ▶ | my $pmax = ($self->{min} == neg_inf) ? pos_inf : abs $self->{min}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 54 | 134 | my $re_part = Number::Range::Regex::TrivialRange->new( $pmin, $pmax )-> | |||||||||||||
| 55 | regex( { no_leading_zeroes => 1, no_sign => 1 } ); | ||||||||||||||
| 56 | 134 | return qr/-$zeroes_maybe$re_part/; | |||||||||||||
| 57 | } else { | ||||||||||||||
| 58 | 5341 | ▶ | my $sign_maybe = $opts->{no_sign} ? '' : '[+]?'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | 5341 | ▶ | if($self->{min} eq $self->{max}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 60 | 512 | return qr/$sign_maybe$zeroes_maybe$self->{min}/; | |||||||||||||
| 61 | } else { | ||||||||||||||
| 62 | #note: because of the nature of a trivial range, max must also be positive | ||||||||||||||
| 63 | 4829 | my $ndigits = length $self->{min}; | |||||||||||||
| 64 | 4829 | ▶ | if($self->{max} == pos_inf) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 65 | # for a trivial range extending to +inf, min must be /^10+$/ | ||||||||||||||
| 66 | 77 | my $trailer; | |||||||||||||
| 67 | 77 | ▶ | if($opts->{no_leading_zeroes}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 68 | 55 | ▶ | die "internal error" if $ndigits <= 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 69 | 55 | $ndigits--; #change the first '\d' to '[1-9]' | |||||||||||||
| 70 | 55 | $trailer = "[1-$opts->{base_max}]"; | |||||||||||||
| 71 | 55 | $zeroes_maybe = ''; | |||||||||||||
| 72 | } else { | ||||||||||||||
| 73 | 22 | $trailer = ''; | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | $trailer .= $ndigits == 0 ? '' : | ||||||||||||||
| 76 | $ndigits == 1 ? $opts->{base_digits_regex} : | ||||||||||||||
| 77 | 77 | ▶ | "$opts->{base_digits_regex}\{$ndigits,\}"; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 78 | 77 | return qr/$sign_maybe$zeroes_maybe$trailer/; | |||||||||||||
| 79 | } else { | ||||||||||||||
| 80 | 4752 | ▶ | die "internal error" if $ndigits != length $self->{max}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 81 | 4752 | my $nsame = 0; | |||||||||||||
| 82 | 4752 | ▶ | for(; $nsame<$ndigits; $nsame++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 83 | 10576 | ▶ | last if substr($self->{min}, $nsame, 1) ne substr($self->{max}, $nsame, 1); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 84 | } | ||||||||||||||
| 85 | 4752 | my $static_header = substr($self->{min}, 0, $nsame); | |||||||||||||
| 86 | 4752 | my $dig_min = substr($self->{min}, $nsame, 1); | |||||||||||||
| 87 | 4752 | my $dig_max = substr($self->{max}, $nsame, 1); | |||||||||||||
| 88 | 4752 | my $digit_range = "[$dig_min-$dig_max]"; | |||||||||||||
| 89 | 4752 | my $extra_digits = $ndigits-$nsame-1; | |||||||||||||
| 90 | my $trailer = $extra_digits == 0 ? '' : | ||||||||||||||
| 91 | $extra_digits == 1 ? $opts->{base_digits_regex} : | ||||||||||||||
| 92 | 4752 | ▶ | "$opts->{base_digits_regex}\{$extra_digits\}"; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 93 | 4752 | return qr/$sign_maybe$zeroes_maybe$static_header$digit_range$trailer/; | |||||||||||||
| 94 | } | ||||||||||||||
| 95 | } | ||||||||||||||
| 96 | } | ||||||||||||||
| 97 | } | ||||||||||||||
| 98 | |||||||||||||||
| 99 | # touches/union/intersect/subtract/etc. inherit from SimpleRange.pm | ||||||||||||||
| 100 | |||||||||||||||
| 101 | 14 | 1; | |||||||||||||
| 102 | |||||||||||||||