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>
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.
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.
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/>.
19 package Dpkg::Version;
24 our $VERSION = '1.01';
27 version_compare_relation
28 version_normalize_relation
29 version_compare_string
40 use Exporter qw(import);
44 use Dpkg::ErrorHandling;
55 '<=>' => \&_comparison,
56 'cmp' => \&_comparison,
57 '""' => sub { return $_[0]->as_string(); },
58 'bool' => sub { return $_[0]->as_string() if $_[0]->is_valid(); },
65 Dpkg::Version - handling and comparing dpkg-style version numbers
69 The Dpkg::Version module provides pure-Perl routines to compare
70 dpkg-style version numbers (as used in Debian packages) and also
71 an object oriented interface overriding perl operators
72 to do the right thing when you compare Dpkg::Version object between
79 =item $v = Dpkg::Version->new($version, %opts)
81 Create a new Dpkg::Version object corresponding to the version indicated in
82 the string (scalar) $version. By default it will accepts any string
83 and consider it as a valid version. If you pass the option "check => 1",
84 it will return undef if the version is invalid (see version_check for
87 You can always call $v->is_valid() later on to verify that the version is
93 my ($this, $ver, %opts) = @_;
94 my $class = ref($this) || $this;
95 $ver = "$ver" if ref($ver); # Try to stringify objects
98 return unless version_check($ver);
102 if ($ver =~ /^([^:]*):(.+)$/) {
107 $self->{no_epoch} = 1;
109 if ($ver =~ /(.*)-(.*)$/) {
110 $self->{version} = $1;
111 $self->{revision} = $2;
113 $self->{version} = $ver;
114 $self->{revision} = 0;
115 $self->{no_revision} = 1;
118 return bless $self, $class;
121 =item boolean evaluation
123 When the Dpkg::Version object is used in a boolean evaluation (for example
124 in "if ($v)" or "$v || 'default'") it returns its string representation
125 if the version stored is valid ($v->is_valid()) and undef otherwise.
129 Returns true if the version is valid, false otherwise.
135 return scalar version_check($self);
138 =item $v->epoch(), $v->version(), $v->revision()
140 Returns the corresponding part of the full version string.
146 return $self->{epoch};
151 return $self->{version};
156 return $self->{revision};
159 =item $v->is_native()
161 Returns true if the version is native, false if it has a revision.
167 return $self->{no_revision};
170 =item $v1 <=> $v2, $v1 < $v2, $v1 <= $v2, $v1 > $v2, $v1 >= $v2
172 Numerical comparison of various versions numbers. One of the two operands
173 needs to be a Dpkg::Version, the other one can be anything provided that
174 its string representation is a version number.
179 my ($a, $b, $inverted) = @_;
180 if (not ref($b) or not $b->isa('Dpkg::Version')) {
181 $b = Dpkg::Version->new($b);
183 ($a, $b) = ($b, $a) if $inverted;
184 my $r = version_compare_part($a->epoch(), $b->epoch());
186 $r = version_compare_part($a->version(), $b->version());
188 return version_compare_part($a->revision(), $b->revision());
191 =item "$v", $v->as_string(), $v->as_string(%options)
193 Accepts an optional option hash reference, affecting the string conversion.
199 =item omit_epoch (defaults to 0)
201 Omit the epoch, if present, in the output string.
203 =item omit_revision (defaults to 0)
205 Omit the revision, if present, in the output string.
209 Returns the string representation of the version number.
214 my ($self, %opts) = @_;
215 my $no_epoch = $opts{omit_epoch} || $self->{no_epoch};
216 my $no_revision = $opts{omit_revision} || $self->{no_revision};
219 $str .= $self->{epoch} . ':' unless $no_epoch;
220 $str .= $self->{version};
221 $str .= '-' . $self->{revision} unless $no_revision;
229 All the functions are exported by default.
233 =item version_compare($a, $b)
235 Returns -1 if $a is earlier than $b, 0 if they are equal and 1 if $a
238 If $a or $b are not valid version numbers, it dies with an error.
242 sub version_compare($$) {
244 my $va = Dpkg::Version->new($a, check => 1);
245 defined($va) || error(g_('%s is not a valid version'), "$a");
246 my $vb = Dpkg::Version->new($b, check => 1);
247 defined($vb) || error(g_('%s is not a valid version'), "$b");
251 =item version_compare_relation($a, $rel, $b)
253 Returns the result (0 or 1) of the given comparison operation. This
254 function is implemented on top of version_compare().
256 Allowed values for $rel are the exported constants REL_GT, REL_GE,
257 REL_EQ, REL_LE, REL_LT. Use version_normalize_relation() if you
258 have an input string containing the operator.
262 sub version_compare_relation($$$) {
263 my ($a, $op, $b) = @_;
264 my $res = version_compare($a, $b);
268 } elsif ($op eq REL_GE) {
270 } elsif ($op eq REL_EQ) {
272 } elsif ($op eq REL_LE) {
274 } elsif ($op eq REL_LT) {
277 croak "unsupported relation for version_compare_relation(): '$op'";
281 =item $rel = version_normalize_relation($rel_string)
283 Returns the normalized constant of the relation $rel (a value
284 among REL_GT, REL_GE, REL_EQ, REL_LE and REL_LT). Supported
285 relations names in input are: "gt", "ge", "eq", "le", "lt", ">>", ">=",
286 "=", "<=", "<<". ">" and "<" are also supported but should not be used as
287 they are obsolete aliases of ">=" and "<=".
291 sub version_normalize_relation($) {
294 warning('relation %s is deprecated: use %s or %s',
295 $op, "$op$op", "$op=") if ($op eq '>' or $op eq '<');
297 if ($op eq '>>' or $op eq 'gt') {
299 } elsif ($op eq '>=' or $op eq 'ge' or $op eq '>') {
301 } elsif ($op eq '=' or $op eq 'eq') {
303 } elsif ($op eq '<=' or $op eq 'le' or $op eq '<') {
305 } elsif ($op eq '<<' or $op eq 'lt') {
308 croak "bad relation '$op'";
312 =item version_compare_string($a, $b)
314 String comparison function used for comparing non-numerical parts of version
315 numbers. Returns -1 if $a is earlier than $b, 0 if they are equal and 1 if $a
318 The "~" character always sort lower than anything else. Digits sort lower
319 than non-digits. Among remaining characters alphabetic characters (A-Z, a-z)
320 sort lower than the other ones. Within each range, the ASCII decimal value
321 of the character is used to sort between characters.
330 } elsif ($x =~ /^\d$/) {
332 } elsif ($x =~ /^[A-Za-z]$/) {
335 return ord($x) + 256;
339 sub version_compare_string($$) {
340 my @a = map { _version_order($_) } split(//, shift);
341 my @b = map { _version_order($_) } split(//, shift);
343 my ($a, $b) = (shift @a, shift @b);
344 return 0 if not defined($a) and not defined($b);
345 $a ||= 0; # Default order for "no character"
348 return -1 if $a < $b;
352 =item version_compare_part($a, $b)
354 Compare two corresponding sub-parts of a version number (either upstream
355 version or debian revision).
357 Each parameter is split by version_split_digits() and resulting items
358 are compared together. As soon as a difference happens, it returns -1 if
359 $a is earlier than $b, 0 if they are equal and 1 if $a is later than $b.
363 sub version_compare_part($$) {
364 my @a = version_split_digits(shift);
365 my @b = version_split_digits(shift);
367 my ($a, $b) = (shift @a, shift @b);
368 return 0 if not defined($a) and not defined($b);
369 $a ||= 0; # Default value for lack of version
371 if ($a =~ /^\d+$/ and $b =~ /^\d+$/) {
372 # Numerical comparison
377 my $cmp = version_compare_string($a, $b);
383 =item @items = version_split_digits($version)
385 Splits a string in items that are each entirely composed either
386 of digits or of non-digits. For instance for "1.024~beta1+svn234" it would
387 return ("1", ".", "024", "~beta", "1", "+svn", "234").
391 sub version_split_digits($) {
394 return split /(?<=\d)(?=\D)|(?<=\D)(?=\d)/, $version;
397 =item ($ok, $msg) = version_check($version)
399 =item $ok = version_check($version)
401 Checks the validity of $version as a version number. Returns 1 in $ok
402 if the version is valid, 0 otherwise. In the latter case, $msg
403 contains a description of the problem with the $version scalar.
407 sub version_check($) {
410 if (defined $version) {
412 $version = Dpkg::Version->new($str) unless ref($version);
414 if (not defined($str) or not length($str)) {
415 my $msg = g_('version number cannot be empty');
416 return (0, $msg) if wantarray;
419 if (not defined $version->epoch() or not length $version->epoch()) {
420 my $msg = sprintf(g_('epoch part of the version number cannot be empty'));
421 return (0, $msg) if wantarray;
424 if (not defined $version->version() or not length $version->version()) {
425 my $msg = g_('upstream version cannot be empty');
426 return (0, $msg) if wantarray;
429 if (not defined $version->revision() or not length $version->revision()) {
430 my $msg = sprintf(g_('revision cannot be empty'));
431 return (0, $msg) if wantarray;
434 if ($version->version() =~ m/^[^\d]/) {
435 my $msg = g_('version number does not start with digit');
436 return (0, $msg) if wantarray;
439 if ($str =~ m/([^-+:.0-9a-zA-Z~])/o) {
440 my $msg = sprintf g_("version number contains illegal character '%s'"), $1;
441 return (0, $msg) if wantarray;
444 if ($version->epoch() !~ /^\d*$/) {
445 my $msg = sprintf(g_('epoch part of the version number ' .
446 "is not a number: '%s'"), $version->epoch());
447 return (0, $msg) if wantarray;
450 return (1, '') if wantarray;
458 =head2 Version 1.01 (dpkg 1.17.0)
460 New argument: Accept an options argument in $v->as_string().
462 New method: $v->is_native().
464 =head2 Version 1.00 (dpkg 1.15.6)
466 Mark the module as public.