| 1 | package Range::Object::Serial; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # This is basically what common::sense does, but without the pragma itself | ||||||||||||||||||||||||||||
| 4 | # to remain compatible with Perls older than 5.8 | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | no warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use warnings qw(FATAL closed internal debugging pack malloc portable | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | prototype inplace io pipe unpack deprecated glob digit | ||||||||||||||||||||||||||||
| 10 | 1 | printf reserved taint closure semicolon); | |||||||||||||||||||||||||||
| 11 | 1 | ▶ | no warnings qw(exec newline unopened); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use List::Util qw( first ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use base qw(Range::Object); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # Overload definitions | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use overload q{""} => 'stringify_collapsed', | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 1 | fallback => 1; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | ### PUBLIC INSTANCE METHOD ### | ||||||||||||||||||||||||||||
| 24 | # | ||||||||||||||||||||||||||||
| 25 | # Returns regex for matching unsigned integer numbers. | ||||||||||||||||||||||||||||
| 26 | # | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 308 | ▶ | sub pattern { | ||||||||||||||||||||||||||
|
Subroutine
pattern: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 308 | my ($self) = @_; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 308 | my $delimiter = $self->delimiter(); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 308 | return qr/ | |||||||||||||||||||||||||||
| 34 | (?<! \A -) # Can't have negative numbers | ||||||||||||||||||||||||||||
| 35 | \s* # optional greedy whitespace | ||||||||||||||||||||||||||||
| 36 | [0-9]+ # then one or more digits | ||||||||||||||||||||||||||||
| 37 | (?: # and finally this group consisting of | ||||||||||||||||||||||||||||
| 38 | (?: $delimiter) # ... either delimiter | ||||||||||||||||||||||||||||
| 39 | | # or | ||||||||||||||||||||||||||||
| 40 | (?: \s) # whitespace | ||||||||||||||||||||||||||||
| 41 | | # or | ||||||||||||||||||||||||||||
| 42 | \z # end of string | ||||||||||||||||||||||||||||
| 43 | ) # ... end of group | ||||||||||||||||||||||||||||
| 44 | /xms; | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | ### PRIVATE INSTANCE METHOD ### | ||||||||||||||||||||||||||||
| 48 | # | ||||||||||||||||||||||||||||
| 49 | # Returns regex that is used to separate items in a range list. | ||||||||||||||||||||||||||||
| 50 | # Default for Serial is comma (,) or semicolon (;). | ||||||||||||||||||||||||||||
| 51 | # | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 308 | ▶ | sub separator { | ||||||||||||||||||||||||||
|
Subroutine
separator: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 308 | return qr/ | |||||||||||||||||||||||||||
| 55 | [,;] # comma or semicolon | ||||||||||||||||||||||||||||
| 56 | \s* # if there is whitespace, eat it all | ||||||||||||||||||||||||||||
| 57 | /xms; | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | ### PUBLIC INSTANCE METHOD ### | ||||||||||||||||||||||||||||
| 61 | # | ||||||||||||||||||||||||||||
| 62 | # Returns range delimiter; Serial uses default dash (-). | ||||||||||||||||||||||||||||
| 63 | # | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1127 | ▶ | sub delimiter { '-' } | ||||||||||||||||||||||||||
|
Subroutine
delimiter: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | ############## PRIVATE METHODS BELOW ############## | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | ### PRIVATE INSTANCE METHOD ### | ||||||||||||||||||||||||||||
| 70 | # | ||||||||||||||||||||||||||||
| 71 | # Tests if a sigle value is in current range. Serial uses numeric | ||||||||||||||||||||||||||||
| 72 | # comparison. | ||||||||||||||||||||||||||||
| 73 | # | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 582 | ▶ | sub _search_range { | ||||||||||||||||||||||||||
|
Subroutine
_search_range: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 76 | 582 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | return | ||||||||||||||||||||||||||||
| 79 | 4198 | ▶ | first { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 80 | 4198 | ▶ | ref($_) ? (($value >= $_->{start}) && ($value <= $_->{end})) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$value >= $_->{"start"} and $value <= $_->{"end"}
MC/DC100%2/2
$value >= $_->{"start"} and $value <= $_->{"end"}
$value >= $_->{"start"}
$value <= $_->{"end"}
Truth table100%3/3
A and B
A= $value >= $_->{"start"}B= $value <= $_->{"end"}
| |||||||||||||||||||||||||||||
| 81 | : $_ == $value | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 582 | @{ $self->{range} }; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | ### PRIVATE INSTANCE METHOD ### | ||||||||||||||||||||||||||||
| 87 | # | ||||||||||||||||||||||||||||
| 88 | # Returns sorted list of all single items within current range. | ||||||||||||||||||||||||||||
| 89 | # Serial uses numeric sorting. | ||||||||||||||||||||||||||||
| 90 | # | ||||||||||||||||||||||||||||
| 91 | # Works in list context only, croaks if called otherwise. | ||||||||||||||||||||||||||||
| 92 | # | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 321 | ▶ | sub _sort_range { | ||||||||||||||||||||||||||
|
Subroutine
_sort_range: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 95 | 321 | my ($self, @range) = @_; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 321 | ▶ | croak "_sort_range can only be used in list context" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | unless wantarray; | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 321 | ▶ | return sort { $a <=> $b } @range ? @range : $self->_full_range(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | ### PRIVATE INSTANCE METHOD ### | ||||||||||||||||||||||||||||
| 104 | # | ||||||||||||||||||||||||||||
| 105 | # Tests if two values are equal. | ||||||||||||||||||||||||||||
| 106 | # | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 46 | ▶ | sub _equal_value { | ||||||||||||||||||||||||||
|
Subroutine
_equal_value: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 109 | 46 | my ($self, $first, $last) = @_; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 46 | return !!($first == $last); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | ### PRIVATE INSTANCE METHOD ### | ||||||||||||||||||||||||||||
| 115 | # | ||||||||||||||||||||||||||||
| 116 | # Tests if two values are consequent. | ||||||||||||||||||||||||||||
| 117 | # | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 148 | ▶ | sub _next_in_range { | ||||||||||||||||||||||||||
|
Subroutine
_next_in_range: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 120 | 148 | my ($self, $first, $last) = @_; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 148 | return !!($last == $first + 1); | |||||||||||||||||||||||||||
| 123 | }; | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 1 | 1; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | __END__ | ||||||||||||||||||||||||||||