blib/lib/SemVer.pm untested

filter: Statementstmt 0.0% 0 / 144 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 21 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 244 SCAR 65.5
CC
26
Cov
0%
vcmp
306.0
new
20.0
declare
12.0
CRAP 702.0
1 package SemVer;
2
3 use 5.008001;
4 use strict;
5 use version 0.82;
6 use Scalar::Util ();
7
8 use overload (
9 '""' => 'stringify',
10 '<=>' => 'vcmp',
11 'cmp' => 'vcmp',
12 );
13
14 our @ISA = qw(version);
15 our $VERSION = '0.7.0'; # For Module::Build
16
17 sub _die { require Carp; Carp::croak(@_) }
18
19 # Prevent version.pm from mucking with our internals.
20 sub import {}
21
22 # Adapted from version.pm.
23 my $STRICT_INTEGER_PART = qr/0|[1-9][0-9]*/;
24 my $DOT_SEPARATOR = qr/\./;
25 my $PLUS_SEPARATOR = qr/\+/;
26 my $DASH_SEPARATOR = qr/-/;
27 my $STRICT_DOTTED_INTEGER_PART = qr/$DOT_SEPARATOR$STRICT_INTEGER_PART/;
28 my $STRICT_DOTTED_INTEGER_VERSION = qr/ $STRICT_INTEGER_PART $STRICT_DOTTED_INTEGER_PART{2,} /x;
29 my $IDENTIFIER = qr/[-0-9A-Za-z]+/;
30 my $DOTTED_IDENTIFIER = qr/(?:$DOT_SEPARATOR$IDENTIFIER)*/;
31 my $PRERELEASE = qr/$IDENTIFIER$DOTTED_IDENTIFIER/;
32 my $METADATA = qr/$IDENTIFIER$DOTTED_IDENTIFIER/;
33
34 my $OPTIONAL_EXTRA_PART = qr/$PRERELEASE($PLUS_SEPARATOR$METADATA)?/;
35
36 sub new {
37 my ($class, $ival) = @_;
38
39 # Handle vstring.
40 return $class->SUPER::new($ival) if Scalar::Util::isvstring($ival);
41
42 # Let version handle cloning.
43 if (eval { $ival->isa('version') }) {
44 my $self = $class->SUPER::new($ival);
45 $self->{extra} = $ival->{extra};
46 $self->{dash} = $ival->{dash};
47 $self->_evalPreRelease($self->{extra});
48 return $self;
49 }
50
51 my ($val, $dash, $extra) = (
52 $ival =~ /^v?($STRICT_DOTTED_INTEGER_VERSION)(?:($DASH_SEPARATOR)($OPTIONAL_EXTRA_PART))?$/
53 );
54 _die qq{Invalid semantic version string format: "$ival"}
55 unless defined $val;
56
57 my $self = $class->SUPER::new($val);
58 $self->{dash} = $dash;
59 $self->{extra} = $extra;
60 $self->_evalPreRelease($self->{extra});
61
62 return $self;
63 }
64
65 # Internal function to split up given string into prerelease- and patch-components
66 sub _evalPreRelease {
67 no warnings 'uninitialized';
68 my $self = shift;
69 my $v = shift;
70 my ($preRelease, $plus, $patch) = (
71 $v =~ /^($PRERELEASE)(?:($PLUS_SEPARATOR)($METADATA))?$/
72 );
73 @{$self->{prerelease}} = split $DOT_SEPARATOR,$preRelease;
74 $self->{plus} = $plus;
75 @{$self->{patch}} = (split $DOT_SEPARATOR, $patch || undef);
76 return;
77 }
78
79 $VERSION = __PACKAGE__->new($VERSION); # For ourselves.
80
81 sub declare {
82 my ($class, $ival) = @_;
83 return $class->new($ival) if Scalar::Util::isvstring($ival)
84 or eval { $ival->isa('version') };
85
86 (my $v = $ival) =~ s/^v?$STRICT_DOTTED_INTEGER_VERSION(?:($DASH_SEPARATOR)($OPTIONAL_EXTRA_PART))[[:space:]]*$//;
87 my $dash = $1;
88 my $extra = $2;
89 $v += 0 if $v =~ s/_//g; # ignore underscores.
90 my $self = $class->SUPER::declare($v);
91 $self->{dash} = $dash;
92 $self->{extra} = $extra;
93 $self->_evalPreRelease($self->{extra});
94 return $self;
95 }
96
97 sub parse {
98 my ($class, $ival) = @_;
99 return $class->new($ival) if Scalar::Util::isvstring($ival)
100 or eval { $ival->isa('version') };
101
102 (my $v = $ival) =~ s/^v?$STRICT_DOTTED_INTEGER_VERSION(?:($DASH_SEPARATOR)($OPTIONAL_EXTRA_PART))[[:space:]]*$//;
103 my $dash = $1;
104 my $extra = $2;
105 $v += 0 if $v =~ s/_//g; # ignore underscores.
106 my $self = $class->SUPER::parse($v);
107 $self->{dash} = $dash;
108 $self->{extra} = $extra;
109 $self->_evalPreRelease($self->{extra});
110 return $self;
111 }
112
113 sub stringify {
114 my $self = shift;
115 my $str = $self->SUPER::stringify;
116 # This is purely for SemVers constructed from version objects.
117 $str += 0 if $str =~ s/_//g; # ignore underscores.
118 return $str . ($self->{dash} || '') . ($self->{extra} || '');
119 }
120
121 sub normal {
122 my $self = shift;
123 (my $norm = $self->SUPER::normal) =~ s/^v//;
124 $norm =~ s/_/./g;
125 return $norm . ($self->{extra} ? "-$self->{extra}" : '');
126 }
127
128 sub numify { _die 'Semantic versions cannot be numified'; }
129 sub is_alpha { !!shift->{extra} }
130
131
132 # Sort Ordering:
133 # Precedence refers to how versions are compared to each other when ordered. Precedence MUST be calculated by
134 # separating the version into major, minor, patch and pre-release identifiers in that order (Build metadata does not figure into precedence).
135 # Precedence is determined by the first difference when comparing each of these identifiers from left to right as follows:
136 # 1. Major, minor, and patch versions are always compared numerically. Example: 1.0.0 < 2.0.0 < 2.1.0 < 2.1.1.
137 # 2. When major, minor, and patch are equal, a pre-release version has lower precedence than a normal version.
138 # Example: 1.0.0-alpha < 1.0.0.
139 # 3. Precedence for two pre-release versions with the same major, minor, and patch version MUST be determined by
140 # comparing each dot separated identifier from left to right until a difference is found as follows:
141 # 3.a. identifiers consisting of only digits are compared numerically and identifiers with letters or hyphens are
142 # compared lexically in ASCII sort order.
143 # 3.b. Numeric identifiers always have lower precedence than non-numeric identifiers.
144 # 3.c. A larger set of pre-release fields has a higher precedence than a smaller set, if all of the preceding identifiers are equal.
145 # Example: 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0.
146 sub vcmp {
147 my $left = shift;
148 my $right = ref($left)->declare(shift);
149
150 # Reverse?
151 ($left, $right) = shift() ? ($right, $left): ($left, $right);
152
153 # Major and minor win. - case 1.
154 if (my $ret = $left->SUPER::vcmp($right, 0)) {
155 return $ret;
156 } else { #cases 2, 3
157 my $lenLeft = 0;
158 my $lenRight = 0;
159 if (defined $left->{prerelease}) {
160 $lenLeft = scalar(@{$left->{prerelease}});
161 }
162 if (defined $right->{prerelease}) {
163 $lenRight = scalar(@{$right->{prerelease}});
164 }
165 my $lenMin = ($lenLeft, $lenRight)[$lenLeft > $lenRight];
166 if ( $lenLeft == 0) {
167 if ($lenRight == 0) {
168 return 0; # Neither LEFT nor RIGHT have prerelease identifiers - versions are equal
169 } else {
170 # Case 2: When major, minor, and patch are equal, a pre-release version has lower precedence than a normal version.
171 return 1; # Only RIGHT has prelease - not LEFT -> LEFT wins
172 }
173 } else {
174 if ($lenRight == 0) {
175 # Case 2: When major, minor, and patch are equal, a pre-release version has lower precedence than a normal version.
176 return -1; # Only LEFT has prelease identifiers - not RIGHT -> RIGHT wins
177 } else {
178 # LEFT and RIGHT have prelease identifiers - compare each part separately
179 for (my $i = 0; $i < $lenMin; $i++) {
180 my $isNumLeft = Scalar::Util::looks_like_number($left->{prerelease}->[$i]);
181 my $isNumRight = Scalar::Util::looks_like_number($right->{prerelease}->[$i]);
182 # Case 3.b: Numeric identifiers always have lower precedence than non-numeric identifiers
183 if (!$isNumLeft && $isNumRight) {
184 return 1; # LEFT identifier is Non-numeric - RIGHT identifier is numeric -> LEFT wins
185 } elsif ($isNumLeft && !$isNumRight) {
186 return -1; # LEFT identifier is numeric - RIGHT identifier is non-numeric -> RIGHT wins
187 } elsif ($isNumLeft && $isNumRight) {
188 # Case 3.a.1: identifiers consisting of only digits are compared numerically
189 if ($left->{prerelease}->[$i] == $right->{prerelease}->[$i] ) {
190 next; # LEFT identifier and RIGHT identifier are equal - step to next part
191 } elsif ($left->{prerelease}->[$i] > $right->{prerelease}->[$i] ) {
192 return 1; # LEFT identifier is bigger than RIGHT identifier -> LEFT wins
193 } else {
194 return -1; return 1; # LEFT identifier is smaller than RIGHT identifier -> RIGHT wins
195 }
196 } else {
197 # Case 3.a.2: identifiers with letters or hyphens are compared lexically in ASCII sort order.
198 if (lc $left->{prerelease}->[$i] eq lc $right->{prerelease}->[$i] ) {
199 next; # LEFT identifier and RIGHT identifier are equal - step to next part
200 } elsif (lc $left->{prerelease}->[$i] gt lc $right->{prerelease}->[$i] ) {
201 return 1; # LEFT identifier is bigger than RIGHT identifier -> LEFT wins
202 } else {
203 return -1; return 1; # LEFT identifier is smaller than RIGHT identifier -> RIGHT wins
204 }
205 }
206 }
207 # Case 3.c: A larger set of pre-release fields has a higher precedence than a smaller set, if all of the preceding identifiers are equal
208 if ($lenLeft > $lenRight) {
209 return 1; # All existing identifiers are equal, but LEFT has more identifiers -> LEFT wins
210 } elsif ($lenLeft < $lenRight) {
211 return -1; # All existing identifiers are equal, but RIGHT has more identifiers -> RIGHT wins
212 }
213 # All identifiers are equal
214 return 0;
215 }
216 }
217 }
218 }
219
220 1;
221 __END__