Version.pm 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. # Copyright Colin Watson <cjwatson@debian.org>
  2. # Copyright Ian Jackson <iwj@debian.org>
  3. # Copyright 2007 by Don Armstrong <don@donarmstrong.com>.
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License along
  13. # with this program; if not, write to the Free Software Foundation, Inc.,
  14. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. package Dpkg::Version;
  16. use strict;
  17. use warnings;
  18. use Dpkg::ErrorHandling qw(error);
  19. use Exporter;
  20. our @ISA = qw(Exporter);
  21. our @EXPORT_OK = qw(vercmp compare_versions check_version parseversion);
  22. =head1 NAME
  23. Dpkg::Version - pure-Perl dpkg-style version comparison
  24. =head1 DESCRIPTION
  25. The Dpkg::Version module provides pure-Perl routines to compare
  26. dpkg-style version numbers, as used in Debian packages. If you have the
  27. libapt-pkg Perl bindings available (Debian package libapt-pkg-perl), they
  28. may offer better performance.
  29. =head1 METHODS
  30. =over 8
  31. =cut
  32. sub parseversion ($)
  33. {
  34. my $ver = shift;
  35. my %verhash;
  36. if ($ver =~ /:/)
  37. {
  38. $ver =~ /^(\d+):(.+)/ or die "bad version number '$ver'";
  39. $verhash{epoch} = $1;
  40. $ver = $2;
  41. }
  42. else
  43. {
  44. $verhash{epoch} = 0;
  45. }
  46. if ($ver =~ /(.+)-(.+)$/)
  47. {
  48. $verhash{version} = $1;
  49. $verhash{revision} = $2;
  50. }
  51. else
  52. {
  53. $verhash{version} = $ver;
  54. $verhash{revision} = 0;
  55. }
  56. return %verhash;
  57. }
  58. # verrevcmp
  59. # This function is almost exactly equivalent
  60. # to dpkg's verrevcmp function, including the
  61. # order subroutine which it uses.
  62. sub verrevcmp($$)
  63. {
  64. sub order{
  65. my ($x) = @_;
  66. ##define order(x) ((x) == '~' ? -1 \
  67. # : cisdigit((x)) ? 0 \
  68. # : !(x) ? 0 \
  69. # : cisalpha((x)) ? (x) \
  70. # : (x) + 256)
  71. # This comparison is out of dpkg's order to avoid
  72. # comparing things to undef and triggering warnings.
  73. if (not defined $x) {
  74. return 0;
  75. }
  76. elsif ($x eq '~') {
  77. return -1;
  78. }
  79. elsif ($x =~ /^\d$/) {
  80. return 0;
  81. }
  82. elsif ($x =~ /^[A-Z]$/i) {
  83. return ord($x);
  84. }
  85. else {
  86. return ord($x) + 256;
  87. }
  88. }
  89. sub next_elem(\@){
  90. my $a = shift;
  91. return @{$a} ? shift @{$a} : undef;
  92. }
  93. my ($val, $ref) = @_;
  94. $val = "" if not defined $val;
  95. $ref = "" if not defined $ref;
  96. my @val = split //,$val;
  97. my @ref = split //,$ref;
  98. my $vc = next_elem @val;
  99. my $rc = next_elem @ref;
  100. while (defined $vc or defined $rc) {
  101. my $first_diff = 0;
  102. while ((defined $vc and $vc !~ /^\d$/) or
  103. (defined $rc and $rc !~ /^\d$/)) {
  104. my $vo = order($vc); my $ro = order($rc);
  105. # Unlike dpkg's verrevcmp, we only return 1 or -1 here.
  106. return (($vo - $ro > 0) ? 1 : -1) if $vo != $ro;
  107. $vc = next_elem @val; $rc = next_elem @ref;
  108. }
  109. while (defined $vc and $vc eq '0') {
  110. $vc = next_elem @val;
  111. }
  112. while (defined $rc and $rc eq '0') {
  113. $rc = next_elem @ref;
  114. }
  115. while (defined $vc and $vc =~ /^\d$/ and
  116. defined $rc and $rc =~ /^\d$/) {
  117. $first_diff = ord($vc) - ord($rc) if !$first_diff;
  118. $vc = next_elem @val; $rc = next_elem @ref;
  119. }
  120. return 1 if defined $vc and $vc =~ /^\d$/;
  121. return -1 if defined $rc and $rc =~ /^\d$/;
  122. return $first_diff if $first_diff;
  123. }
  124. return 0;
  125. }
  126. =item vercmp
  127. Compare the two arguments as dpkg-style version numbers. Returns -1 if the
  128. first argument represents a lower version number than the second, 1 if the
  129. first argument represents a higher version number than the second, and 0 if
  130. the two arguments represent equal version numbers.
  131. =cut
  132. sub vercmp ($$)
  133. {
  134. my %version = parseversion $_[0];
  135. my %refversion = parseversion $_[1];
  136. return 1 if $version{epoch} > $refversion{epoch};
  137. return -1 if $version{epoch} < $refversion{epoch};
  138. my $r = verrevcmp($version{version}, $refversion{version});
  139. return $r if $r;
  140. return verrevcmp($version{revision}, $refversion{revision});
  141. }
  142. =item compare_versions
  143. Emulates dpkg --compare-versions. Takes two versions as arguments
  144. one and three and one operator as argument two. Supports the following
  145. operators: 'gt', 'ge', 'eq', 'le', 'lt', and '>>', '>=', '=', '<=', '<<'.
  146. Returns a true value if the specified condition is true, a false value
  147. otherwise.
  148. =cut
  149. sub compare_versions ($$$)
  150. {
  151. my $rel = $_[1];
  152. my $res = vercmp($_[0], $_[2]);
  153. if ($rel eq 'gt' or $rel eq ">" or $rel eq ">>") {
  154. return $res > 0;
  155. } elsif ($rel eq 'ge' or $rel eq '>=') {
  156. return $res >= 0;
  157. } elsif ($rel eq 'eq' or $rel eq '=') {
  158. return $res == 0;
  159. } elsif ($rel eq 'le' or $rel eq '<=') {
  160. return $res <= 0;
  161. } elsif ($rel eq 'lt' or $rel eq "<" or $rel eq "<<") {
  162. return $res < 0;
  163. } else {
  164. die "bad relation '$rel'";
  165. }
  166. }
  167. =item check_version($version)
  168. Check the version string and fails it it's invalid.
  169. =cut
  170. sub check_version ($) {
  171. my $version = shift || '';
  172. $version =~ m/[^-+:.0-9a-zA-Z~]/o &&
  173. error(_g("version number contains illegal character `%s'"), $&);
  174. }
  175. =back
  176. =head1 AUTHOR
  177. Don Armstrong <don@donarmstrong.com> and Colin Watson
  178. E<lt>cjwatson@debian.orgE<gt>, based on the implementation in
  179. C<dpkg/lib/vercmp.c> by Ian Jackson and others.
  180. =cut
  181. 1;