Dpkg/Version.pm

filter: Statementstmt 96.9% 160 / 165 Branchbran 86.2% 88 / 102 Conditioncond 86.1% 56 / 65 MC/DCmcdc 75.0% 30 / 40 Subroutinesub 96.4% 27 / 28 Podpod 100% 16 / 16 totaltotal 90.6% 377 / 416 CC 75 SCAR 23.5
CC
75
Cov
92%
version_check
23.6
version_normalize_relation
15.1
version_compare_part
9.0
CRAP 10.4
« Dpkg/Conf.pm ↑ Summary Dpkg/Checksums.pm »
1 # Copyright © Colin Watson <cjwatson@debian.org>
2 # Copyright © Ian Jackson <ijackson@chiark.greenend.org.uk>
3 # Copyright © 2007 Don Armstrong <don@donarmstrong.com>.
4 # Copyright © 2009 Raphaël Hertzog <hertzog@debian.org>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <https://www.gnu.org/licenses/>.
18  
19 =encoding utf8
20  
21 =head1 NAME
22  
23 Dpkg::Version - handling and comparing dpkg-style version numbers
24  
25 =head1 DESCRIPTION
26  
27 The Dpkg::Version module provides pure-Perl routines to compare
28 dpkg-style version numbers (as used in Debian packages) and also
29 an object oriented interface overriding perl operators
30 to do the right thing when you compare Dpkg::Version object between
31 them.
32  
33 =cut
34  
35 package Dpkg::Version 1.05;
36  
37 525 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 # Currently unused, but not removed to not generate warnings on users.
40 525 ▶use warnings::register qw(semantic_change::overload::bool);
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 525 our @EXPORT = qw(
43 version_compare
44 version_compare_relation
45 version_normalize_relation
46 version_compare_string
47 version_compare_part
48 version_split_digits
49 version_check
50 REL_LT
51 REL_LE
52 REL_EQ
53 REL_GE
54 REL_GT
55 );
56  
57 525 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
58 525 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 525 ▶use Dpkg::Gettext;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 525 ▶use Dpkg::ErrorHandling;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 525 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
64 525 REL_LT => '<<',
65 REL_LE => '<=',
66 REL_EQ => '=',
67 REL_GE => '>=',
68 REL_GT => '>>',
69 525 };
70  
71 use overload
72 525 ▶ '<=>' => \&_comparison,
Subroutine BEGIN: called CC=1 SCAR=0.0
73 'cmp' => \&_comparison,
74 29052 ▶ '""' => sub { return $_[0]->as_string(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
75 605 ▶ 'bool' => sub { return $_[0]->is_valid(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
76 525 'fallback' => 1;
77  
78 =head1 METHODS
79  
80 =over 4
81  
82 =item $v = Dpkg::Version->new($version, %opts)
83  
84 Create a new Dpkg::Version object corresponding to the version indicated in
85 the string (scalar) $version. By default it will accept any string
86 and consider it as a valid version, but this can be controlled with the
87 "check" option.
88  
89 You can always call $v->is_valid() later on to verify that the version is
90 valid.
91  
92 Options:
93  
94 =over
95  
96 =item B<check>
97  
98 Setting this option to true will return undef if the version is invalid
99 (see $v->version_check() for details).
100  
101 =back
102  
103 =cut
104  
105 674745 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
106 674745 my ($this, $ver, %opts) = @_;
107 674745 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
06747450
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
108 # Try to stringify objects.
109 674745 ▶ $ver = "$ver" if ref($ver);
Branch: 1 branch
BranchTrueFalseTotal
if ref $ver 3 674742 674745
110  
111 674745 ▶ if ($opts{check}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"check"}) 331737 343008 674745
112 331737 ▶ return unless version_check($ver);
Branch: 1 branch
BranchTrueFalseTotal
unless version_check($ver) 0 331737 331737
113 }
114  
115 674745 my $self = {};
116 674745 ▶ if ($ver =~ /^([^:]*):(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ver =~ /^([^:]*):(.+)$/u) { } 82215 592530 674745
117 82215 $self->{epoch} = $1;
118 82215 $ver = $2;
119 } else {
120 592530 $self->{epoch} = 0;
121 592530 $self->{no_epoch} = 1;
122 }
123 674745 ▶ if ($ver =~ /(.*)-(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ver =~ /(.*)-(.*)$/u) { } 277905 396840 674745
124 277905 $self->{version} = $1;
125 277905 $self->{revision} = $2;
126 } else {
127 396840 $self->{version} = $ver;
128 396840 $self->{revision} = 0;
129 396840 $self->{no_revision} = 1;
130 }
131  
132 674745 return bless $self, $class;
133 }
134  
135 =item boolean evaluation
136  
137 When the Dpkg::Version object is used in a boolean evaluation (for example
138 in "if ($v)" or "$v ? \"$v\" : 'default'") it returns true if the version
139 stored is valid ($v->is_valid()) and false otherwise.
140  
141 B<Notice>: Between dpkg 1.15.7.2 and 1.19.1 this overload used to return
142 $v->as_string() if $v->is_valid(), a breaking change in behavior that caused
143 "0" versions to be evaluated as false. To catch any possibly intended code
144 that relied on those semantics, this overload emitted a warning with category
145 "Dpkg::Version::semantic_change::overload::bool" between dpkg 1.19.1 and
146 1.20.0. Once fixed, or for already valid code the warning could be quiesced
147 for that specific versions with
148  
149 no if $Dpkg::Version::VERSION eq '1.02',
150 warnings => qw(Dpkg::Version::semantic_change::overload::bool);
151  
152 added after the C<use Dpkg::Version>.
153  
154 =item $bool = $v->is_valid()
155  
156 Returns true if the version is valid, false otherwise.
157  
158 =cut
159  
160 5308 ▶sub is_valid {
Subroutine is_valid: called CC=1 SCAR=0.0
Pod documented
161 5308 my $self = shift;
162 5308 return scalar version_check($self);
163 }
164  
165 =item $string = $v->epoch()
166  
167 Returns the epoch part of the full version string.
168  
169 If the version parsed did not have an epoch, the value returned will be 0.
170  
171 =cut
172  
173 1614711 ▶sub epoch {
Subroutine epoch: called CC=1 SCAR=0.0
Pod documented
174 1614711 my $self = shift;
175 1614711 return $self->{epoch};
176 }
177  
178 =item $string = $v->version()
179  
180 Returns the upstream version part of the full version string.
181  
182 =cut
183  
184 1567322 ▶sub version {
Subroutine version: called CC=1 SCAR=0.0
Pod documented
185 1567322 my $self = shift;
186 1567322 return $self->{version};
187 }
188  
189 =item $string = $v->revision()
190  
191 Returns the revision part of the full version string.
192  
193 If the version parsed did not have a revision, the value returned will be 0.
194  
195 =cut
196  
197 873892 ▶sub revision {
Subroutine revision: called CC=1 SCAR=0.0
Pod documented
198 873892 my $self = shift;
199 873892 return $self->{revision};
200 }
201  
202 =item $bool = $v->is_native()
203  
204 Returns true if the version is native, false if it has a revision.
205  
206 B<Note>: On some vendors, dpkg has been made incoherent, and a native
207 source package format can end up having a non-native version, adding
208 to the confusion of the whole concept. And while this method would
209 be fine to be used on vendors that have a coherent native source concept,
210 this is not a pattern that will be portably relied on. Thus this method
211 is being deprecated. Depending on the context, using the negated result of
212 the $v->has_revision() method might be sensible.
213  
214 =cut
215  
216 0 ▶sub is_native {
Subroutine is_native: not called CC=1 SCAR=6.9
Pod documented
217 0 my $self = shift;
218 0 warnings::warnif('deprecated',
219 'using Dpkg::Version->is_native() has been made incoherent and ' .
220 'confusing on some dpkg vendors; it is deprecated as not having ' .
221 'portable semantics anymore; depending on the context, using ' .
222 'the negated result of the has_revision() method might be sensible');
223 0 return ! $self->has_revision();
224 }
225  
226 =item $v->has_epoch()
227  
228 Returns true if the version has an epoch, false otherwise.
229  
230 =cut
231  
232 2585 ▶sub has_epoch($self)
Subroutine has_epoch: called CC=1 SCAR=0.0
Pod documented
233 {
234 2585 return ! $self->{no_epoch};
235 }
236  
237 =item $v->has_revision()
238  
239 Returns true if the version has a revision, false otherwise.
240  
241 =cut
242  
243 2585 ▶sub has_revision($self)
Subroutine has_revision: called CC=1 SCAR=0.0
Pod documented
244 {
245 2585 return ! $self->{no_revision};
246 }
247  
248 =item $cmp = $v1 <=> $v2
249  
250 =item $cmp = $v1 < $v2
251  
252 =item $cmp = $v1 <= $v2
253  
254 =item $cmp = $v1 > $v2
255  
256 =item $cmp = $v1 >= $v2
257  
258 Numerical comparison of various versions numbers. One of the two operands
259 needs to be a Dpkg::Version, the other one can be anything provided that
260 its string representation is a version number.
261  
262 =cut
263  
264 303481 ▶sub _comparison {
Subroutine _comparison: called CC=6 SCAR=18.0
265 303481 my ($a, $b, $inverted) = @_;
266 303481 ▶ if (not ref($b) or not $b->isa('Dpkg::Version')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $b and $b->isa("Dpkg::Version")) 3686 299795 303481
Condition66%2/3
ref $b and $b->isa("Dpkg::Version")
!ll&&!rl&&r
36860299795
MC/DC50%1/2
ref $b and $b->isa("Dpkg::Version")
ref $b $b->isa("Dpkg::Version")
Truth table66%2/3
A and B
A= ref $bB= $b->isa("Dpkg::Version")
ABresult
0X0
100
111
267 3686 $b = Dpkg::Version->new($b);
268 }
269 303481 ▶ ($a, $b) = ($b, $a) if $inverted;
Branch: 1 branch
BranchTrueFalseTotal
if $inverted 0 303481 303481
270 303481 my $r = version_compare_part($a->epoch(), $b->epoch());
271 303481 ▶ return $r if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 23436 280045 303481
272 280045 $r = version_compare_part($a->version(), $b->version());
273 280045 ▶ return $r if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 178326 101719 280045
274 101719 return version_compare_part($a->revision(), $b->revision());
275 }
276  
277 =item $string = "$v"
278  
279 =item $string = $v->as_string(%opts)
280  
281 Accepts an options %opts hash, affecting the string conversion.
282  
283 Options:
284  
285 =over 8
286  
287 =item B<omit_epoch> (defaults to 0)
288  
289 Omit the epoch, if present, in the output string.
290  
291 =item B<omit_revision> (defaults to 0)
292  
293 Omit the revision, if present, in the output string.
294  
295 =back
296  
297 Returns the string representation of the version number.
298  
299 =cut
300  
301 33276 ▶sub as_string {
Subroutine as_string: called CC=5 SCAR=16.1
Pod documented
302 33276 my ($self, %opts) = @_;
303 33276 ▶ my $no_epoch = $opts{omit_epoch} || $self->{no_epoch};
Condition66%2/3
$opts{'omit_epoch'} || $self->{'no_epoch'}
l!l&&r!l&&!r
0253367940
MC/DC50%1/2
$opts{'omit_epoch'} || $self->{'no_epoch'}
$opts{'omit_epoch'} $self->{'no_epoch'}
Truth table66%2/3
A || B
A= $opts{'omit_epoch'}B= $self->{'no_epoch'}
ABresult
1X1
011
000
304 33276 ▶ my $no_revision = $opts{omit_revision} || $self->{no_revision};
Condition100%3/3
$opts{'omit_revision'} || $self->{'no_revision'}
l!l&&r!l&&!r
31842314850
MC/DC100%2/2
$opts{'omit_revision'} || $self->{'no_revision'}
$opts{'omit_revision'} $self->{'no_revision'}
Truth table100%3/3
A || B
A= $opts{'omit_revision'}B= $self->{'no_revision'}
ABresult
1X1
011
000
305  
306 33276 my $str = '';
307 33276 ▶ $str .= $self->{epoch} . ':' unless $no_epoch;
Branch: 1 branch
BranchTrueFalseTotal
unless $no_epoch 7940 25336 33276
308 33276 $str .= $self->{version};
309 33276 ▶ $str .= '-' . $self->{revision} unless $no_revision;
Branch: 1 branch
BranchTrueFalseTotal
unless $no_revision 14850 18426 33276
310 33276 return $str;
311 }
312  
313 =back
314  
315 =head1 FUNCTIONS
316  
317 All the functions are exported by default.
318  
319 =over 4
320  
321 =item $cmp = version_compare($a, $b)
322  
323 Returns -1 if $a is earlier than $b, 0 if they are equal and 1 if $a
324 is later than $b.
325  
326 If $a or $b are not valid version numbers, it dies with an error.
327  
328 =cut
329  
330 154472 ▶sub version_compare :prototype($$)
Subroutine version_compare: called CC=3 SCAR=11.2
Pod documented
331 {
332 154472 my ($a, $b) = @_;
333 154472 my $va = Dpkg::Version->new($a, check => 1);
334 154472 ▶ defined($va) || error(g_('%s is not a valid version'), "$a");
Branch: 1 branch
BranchTrueFalseTotal
defined $va or error(g_("%s is not a valid version"), "$a") 154472 0 154472
335 154472 my $vb = Dpkg::Version->new($b, check => 1);
336 154472 ▶ defined($vb) || error(g_('%s is not a valid version'), "$b");
Branch: 1 branch
BranchTrueFalseTotal
defined $vb or error(g_("%s is not a valid version"), "$b") 154472 0 154472
337 154472 return $va <=> $vb;
338 }
339  
340 =item $bool = version_compare_relation($a, $rel, $b)
341  
342 Returns the result (0 or 1) of the given comparison operation. This
343 function is implemented on top of version_compare().
344  
345 Allowed values for $rel are the exported constants REL_GT, REL_GE,
346 REL_EQ, REL_LE, REL_LT. Use version_normalize_relation() if you
347 have an input string containing the operator.
348  
349 =cut
350  
351 133919 ▶sub version_compare_relation {
Subroutine version_compare_relation: called CC=6 SCAR=18.0
Pod documented
352 133919 my ($a, $op, $b) = @_;
353 133919 my $res = version_compare($a, $b);
354  
355 133919 ▶ if ($op eq REL_GT) {
Branch: 5 branches
BranchTrueFalseTotal
if ($op eq '>>') { } 21887 112032 133919
elsif ($op eq '>=') { } 33156 78876 112032
elsif ($op eq '=') { } 22317 56559 78876
elsif ($op eq '<=') { } 33884 22675 56559
elsif ($op eq '<<') { } 22675 0 22675
356 21887 return $res > 0;
357 } elsif ($op eq REL_GE) {
358 33156 return $res >= 0;
359 } elsif ($op eq REL_EQ) {
360 22317 return $res == 0;
361 } elsif ($op eq REL_LE) {
362 33884 return $res <= 0;
363 } elsif ($op eq REL_LT) {
364 22675 return $res < 0;
365 } else {
366 0 croak "unsupported relation for version_compare_relation(): '$op'";
367 }
368 }
369  
370 =item $rel = version_normalize_relation($rel_string)
371  
372 Returns the normalized constant of the relation $rel (a value
373 among REL_GT, REL_GE, REL_EQ, REL_LE and REL_LT). Supported
374 relations names in input are: "gt", "ge", "eq", "le", "lt", ">>", ">=",
375 "=", "<=", "<<". ">" and "<" are also supported but should not be used as
376 they are obsolete aliases of ">=" and "<=".
377  
378 =cut
379  
380 133955 ▶sub version_normalize_relation {
Subroutine version_normalize_relation: called CC=15 SCAR=27.1
Pod documented
381 133955 my $op = shift;
382  
383 133955 ▶ warning('relation %s is deprecated: use %s or %s',
Branch: 1 branch
BranchTrueFalseTotal
if $op eq ">" or $op eq "<" 22403 111552 133955
Condition100%3/3
$op eq ">" or $op eq "<"
l!l&&r!l&&!r
1100811395111552
MC/DC100%2/2
$op eq ">" or $op eq "<"
$op eq ">" $op eq "<"
Truth table100%3/3
A or B
A= $op eq ">"B= $op eq "<"
ABresult
1X1
011
000
384 $op, "$op$op", "$op=") if ($op eq '>' or $op eq '<');
385  
386 133955 ▶ if ($op eq '>>' or $op eq 'gt') {
Branch: 5 branches
BranchTrueFalseTotal
if ($op eq '>>' or $op eq 'gt') { } 21891 112064 133955
elsif ($op eq '>=' or $op eq 'ge' or $op eq '>') { } 33179 78885 112064
elsif ($op eq '=' or $op eq 'eq') { } 22333 56552 78885
elsif ($op eq '<=' or $op eq 'le' or $op eq '<') { } 33888 22664 56552
elsif ($op eq '<<' or $op eq 'lt') { } 22664 0 22664
Condition100%3/3
$op eq ">>" or $op eq "gt"
l!l&&r!l&&!r
1096910922112064
MC/DC100%2/2
$op eq ">>" or $op eq "gt"
$op eq ">>" $op eq "gt"
Truth table100%3/3
A or B
A= $op eq ">>"B= $op eq "gt"
ABresult
1X1
011
000
387 21891 return REL_GT;
388 ▶ } elsif ($op eq '>=' or $op eq 'ge' or $op eq '>') {
Condition100%6/6
$op eq ">=" or $op eq "ge" or $op eq ">"
l!l&&r!l&&!r
221711100878885
$op eq ">=" or $op eq "ge"
l!l&&r!l&&!r
111201105189893
MC/DC100%3/3
$op eq ">=" or $op eq "ge" or $op eq ">"
$op eq ">=" $op eq "ge" $op eq ">"
Truth table100%4/4
A or B or C
A= $op eq ">="B= $op eq "ge"C= $op eq ">"
ABCresult
1XX1
01X1
0011
0000
389 33179 return REL_GE;
390 ▶ } elsif ($op eq '=' or $op eq 'eq') {
Condition100%3/3
$op eq "=" or $op eq "eq"
l!l&&r!l&&!r
111961113756552
MC/DC100%2/2
$op eq "=" or $op eq "eq"
$op eq "=" $op eq "eq"
Truth table100%3/3
A or B
A= $op eq "="B= $op eq "eq"
ABresult
1X1
011
000
391 22333 return REL_EQ;
392 ▶ } elsif ($op eq '<=' or $op eq 'le' or $op eq '<') {
Condition100%6/6
$op eq "<=" or $op eq "le" or $op eq "<"
l!l&&r!l&&!r
224931139522664
$op eq "<=" or $op eq "le"
l!l&&r!l&&!r
112701122334059
MC/DC100%3/3
$op eq "<=" or $op eq "le" or $op eq "<"
$op eq "<=" $op eq "le" $op eq "<"
Truth table100%4/4
A or B or C
A= $op eq "<="B= $op eq "le"C= $op eq "<"
ABCresult
1XX1
01X1
0011
0000
393 33888 return REL_LE;
394 ▶ } elsif ($op eq '<<' or $op eq 'lt') {
Condition66%2/3
$op eq "<<" or $op eq "lt"
l!l&&r!l&&!r
11355113090
MC/DC0%0/2
$op eq "<<" or $op eq "lt"
$op eq "<<" $op eq "lt"
Truth table66%2/3
A or B
A= $op eq "<<"B= $op eq "lt"
ABresult
1X1
011
000
395 22664 return REL_LT;
396 } else {
397 0 croak "bad relation '$op'";
398 }
399 }
400  
401 =item $cmp = version_compare_string($a, $b)
402  
403 String comparison function used for comparing non-numerical parts of version
404 numbers. Returns -1 if $a is earlier than $b, 0 if they are equal and 1 if $a
405 is later than $b.
406  
407 The "~" character always sort lower than anything else. Digits sort lower
408 than non-digits. Among remaining characters alphabetic characters (A-Z, a-z)
409 sort lower than the other ones. Within each range, the ASCII decimal value
410 of the character is used to sort between characters.
411  
412 =cut
413  
414 1988152 ▶sub _version_order {
Subroutine _version_order: called CC=4 SCAR=13.9
415 1988152 my $x = shift;
416  
417 1988152 ▶ if ($x eq '~') {
Branch: 3 branches
BranchTrueFalseTotal
if ($x eq '~') { } 93859 1894293 1988152
elsif ($x =~ /^\d$/u) { } 50367 1843926 1894293
elsif ($x =~ /^[A-Za-z]$/u) { } 1467781 376145 1843926
418 93859 return -1;
419 } elsif ($x =~ /^\d$/) {
420 50367 return $x * 1 + 1;
421 } elsif ($x =~ /^[A-Za-z]$/) {
422 1467781 return ord($x);
423 } else {
424 376145 return ord($x) + 256;
425 }
426 }
427  
428 326828 ▶sub version_compare_string :prototype($$)
Subroutine version_compare_string: called CC=7 SCAR=19.5
Pod documented
429 {
430 326828 my @a = map { _version_order($_) } split(//, shift);
431 326828 my @b = map { _version_order($_) } split(//, shift);
432 326828 while (1) {
433 1124352 my ($a, $b) = (shift @a, shift @b);
434 1124352 ▶ return 0 if not defined($a) and not defined($b);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a or defined $b 202048 922304 1124352
Condition100%3/3
defined $a or defined $b
l!l&&r!l&&!r
90691615388202048
MC/DC100%2/2
defined $a or defined $b
defined $a defined $b
Truth table100%3/3
A or B
A= defined $aB= defined $b
ABresult
1X1
011
000
435 # Default order for "no character".
436 922304 ▶ $a ||= 0;
Condition100%2/2
$a ||= 0
l!l
90691615388
MC/DC100%1/1
$a ||= 0
$a
Truth table100%2/2
A
A= $a
Aresult
11
00
437 922304 ▶ $b ||= 0;
Condition100%2/2
$b ||= 0
l!l
88227440030
MC/DC100%1/1
$b ||= 0
$b
Truth table100%2/2
A
A= $b
Aresult
11
00
438 922304 ▶ return 1 if $a > $b;
Branch: 1 branch
BranchTrueFalseTotal
if $a > $b 57295 865009 922304
439 865009 ▶ return -1 if $a < $b;
Branch: 1 branch
BranchTrueFalseTotal
if $a < $b 67485 797524 865009
440 }
441 }
442  
443 =item $cmp = version_compare_part($a, $b)
444  
445 Compare two corresponding sub-parts of a version number (either upstream
446 version or debian revision).
447  
448 Each parameter is split by version_split_digits() and resulting items
449 are compared together. As soon as a difference happens, it returns -1 if
450 $a is earlier than $b, 0 if they are equal and 1 if $a is later than $b.
451  
452 =cut
453  
454 685254 ▶sub version_compare_part :prototype($$)
Subroutine version_compare_part: called CC=9 SCAR=22.0
Pod documented
455 {
456 685254 my @a = version_split_digits(shift);
457 685254 my @b = version_split_digits(shift);
458 685254 while (1) {
459 1621795 my ($a, $b) = (shift @a, shift @b);
460 1621795 ▶ return 0 if not defined($a) and not defined($b);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a or defined $b 467239 1154556 1621795
Condition100%3/3
defined $a or defined $b
l!l&&r!l&&!r
113667217884467239
MC/DC100%2/2
defined $a or defined $b
defined $a defined $b
Truth table100%3/3
A or B
A= defined $aB= defined $b
ABresult
1X1
011
000
461 # Default value for lack of version
462 1154556 ▶ $a ||= 0;
Condition100%2/2
$a ||= 0
l!l
580347574209
MC/DC100%1/1
$a ||= 0
$a
Truth table100%2/2
A
A= $a
Aresult
11
00
463 1154556 ▶ $b ||= 0;
Condition100%2/2
$b ||= 0
l!l
566915587641
MC/DC100%1/1
$b ||= 0
$b
Truth table100%2/2
A
A= $b
Aresult
11
00
464 1154556 ▶ if ($a =~ /^\d+$/ and $b =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a =~ /^\d+$/u and $b =~ /^\d+$/u) { } 827739 326817 1154556
Condition100%3/3
$a =~ /^\d+$/u and $b =~ /^\d+$/u
!ll&&!rl&&r
30893317884827739
MC/DC100%2/2
$a =~ /^\d+$/u and $b =~ /^\d+$/u
$a =~ /^\d+$/u $b =~ /^\d+$/u
Truth table100%3/3
A and B
A= $a =~ /^\d+$/uB= $b =~ /^\d+$/u
ABresult
0X0
100
111
465 # Numerical comparison.
466 827739 my $cmp = $a <=> $b;
467 827739 ▶ return $cmp if $cmp;
Branch: 1 branch
BranchTrueFalseTotal
if $cmp 93246 734493 827739
468 } else {
469 # String comparison.
470 326817 my $cmp = version_compare_string($a, $b);
471 326817 ▶ return $cmp if $cmp;
Branch: 1 branch
BranchTrueFalseTotal
if $cmp 124769 202048 326817
472 }
473 }
474 }
475  
476 =item @items = version_split_digits($version)
477  
478 Splits a string in items that are each entirely composed either
479 of digits or of non-digits. For instance for "1.024~beta1+svn234" it would
480 return ("1", ".", "024", "~beta", "1", "+svn", "234").
481  
482 =cut
483  
484 1370508 ▶sub version_split_digits {
Subroutine version_split_digits: called CC=1 SCAR=0.0
Pod documented
485 1370508 my $version = shift;
486  
487 1370508 return split /(?<=\d)(?=\D)|(?<=\D)(?=\d)/, $version;
488 }
489  
490 =item ($ok, $msg) = version_check($version)
491  
492 =item $ok = version_check($version)
493  
494 Checks the validity of $version as a version number. Returns 1 in $ok
495 if the version is valid, 0 otherwise. In the latter case, $msg
496 contains a description of the problem with the $version scalar.
497  
498 =cut
499  
500 337295 ▶sub version_check {
Subroutine version_check: called CC=22 SCAR=31.6
Pod documented
501 337295 my $version = shift;
502 337295 my $str;
503 337295 ▶ if (defined $version) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $version) 337295 0 337295
504 337295 $str = "$version";
505 337295 ▶ $version = Dpkg::Version->new($str) unless ref($version);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $version 331737 5558 337295
506 }
507 337295 ▶ if (not defined($str) or not length($str)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $str and length $str) 517 336778 337295
Condition66%2/3
defined $str and length $str
!ll&&!rl&&r
0517336778
MC/DC50%1/2
defined $str and length $str
defined $str length $str
Truth table66%2/3
A and B
A= defined $strB= length $str
ABresult
0X0
100
111
508 517 my $msg = g_('version number cannot be empty');
509 517 ▶ return (0, $msg) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 517 517
510 517 return 0;
511 }
512 336778 ▶ if (not defined $version->epoch() or not length $version->epoch()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $version->epoch and length $version->epoch) 517 336261 336778
Condition66%2/3
defined $version->epoch and length $version->epoch
!ll&&!rl&&r
0517336261
MC/DC50%1/2
defined $version->epoch and length $version->epoch
defined $version->epoch length $version->epoch
Truth table66%2/3
A and B
A= defined $version->epochB= length $version->epoch
ABresult
0X0
100
111
513 517 my $msg = sprintf(g_('epoch part of the version number cannot be empty'));
514 517 ▶ return (0, $msg) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 517 517
515 517 return 0;
516 }
517 336261 ▶ if (not defined $version->version() or not length $version->version()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $version->version and length $version->version) 1034 335227 336261
Condition66%2/3
defined $version->version and length $version->version
!ll&&!rl&&r
01034335227
MC/DC50%1/2
defined $version->version and length $version->version
defined $version->version length $version->version
Truth table66%2/3
A and B
A= defined $version->versionB= length $version->version
ABresult
0X0
100
111
518 1034 my $msg = g_('upstream version cannot be empty');
519 1034 ▶ return (0, $msg) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 1034 1034
520 1034 return 0;
521 }
522 335227 ▶ if (not defined $version->revision() or not length $version->revision()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $version->revision and length $version->revision) 517 334710 335227
Condition66%2/3
defined $version->revision and length $version->revision
!ll&&!rl&&r
0517334710
MC/DC50%1/2
defined $version->revision and length $version->revision
defined $version->revision length $version->revision
Truth table66%2/3
A and B
A= defined $version->revisionB= length $version->revision
ABresult
0X0
100
111
523 517 my $msg = sprintf(g_('revision cannot be empty'));
524 517 ▶ return (0, $msg) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 517 517
525 517 return 0;
526 }
527 334710 ▶ if ($version->version() =~ m/^[^\d]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version->version =~ /^[^\d]/u) 1034 333676 334710
528 1034 my $msg = g_('version number does not start with digit');
529 1034 ▶ return (0, $msg) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 1034 1034
530 1034 return 0;
531 }
532 333676 ▶ if ($str =~ m/([^-+:.0-9a-zA-Z~])/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /([^-+:.0-9a-zA-Z~])/ou) 517 333159 333676
533 517 my $msg = sprintf g_("version number contains invalid character '%s'"), $1;
534 517 ▶ return (0, $msg) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 517 517
535 517 return 0;
536 }
537 333159 ▶ if ($version->epoch() !~ /^\d*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($version->epoch =~ /^\d*$/u) 1034 332125 333159
538 1034 my $msg = sprintf(g_('epoch part of the version number ' .
539 "is not a number: '%s'"), $version->epoch());
540 1034 ▶ return (0, $msg) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 1034 1034
541 1034 return 0;
542 }
543 332125 ▶ return (1, '') if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 250 331875 332125
544 331875 return 1;
545 }
546  
547 =back
548  
549 =head1 CHANGES
550  
551 =head2 Version 1.05 (dpkg 1.23.3)
552  
553 New methods: $v->has_epoch(), $v->has_revision().
554  
555 =head2 Version 1.04 (dpkg 1.23.0)
556  
557 Deprecated method: $v->is_native().
558  
559 =head2 Version 1.03 (dpkg 1.20.0)
560  
561 Remove deprecation warning from semantic change in 1.02.
562  
563 =head2 Version 1.02 (dpkg 1.19.1)
564  
565 Semantic change: bool evaluation semantics restored to their original behavior.
566  
567 =head2 Version 1.01 (dpkg 1.17.0)
568  
569 New argument: Accept an options argument in $v->as_string().
570  
571 New method: $v->is_native().
572  
573 =head2 Version 1.00 (dpkg 1.15.6)
574  
575 Mark the module as public.
576  
577 =cut
578  
579 525 1;
« Dpkg/Conf.pm ↑ Summary Dpkg/Checksums.pm »