Version.pm 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. # Copyright © Colin Watson <cjwatson@debian.org>
  2. # Copyright © Ian Jackson <iwj@debian.org>
  3. # Copyright © 2007 Don Armstrong <don@donarmstrong.com>.
  4. # Copyright © 2009 Raphaël Hertzog <hertzog@debian.org>
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package Dpkg::Version;
  16. use strict;
  17. use warnings;
  18. use Dpkg::ErrorHandling;
  19. use Dpkg::Gettext;
  20. use base qw(Exporter);
  21. our @EXPORT = qw(version_compare version_compare_relation
  22. version_normalize_relation version_compare_string
  23. version_compare_part version_split_digits version_check
  24. REL_LT REL_LE REL_EQ REL_GE REL_GT);
  25. use constant {
  26. REL_LT => '<<',
  27. REL_LE => '<=',
  28. REL_EQ => '=',
  29. REL_GE => '>=',
  30. REL_GT => '>>',
  31. };
  32. use overload
  33. '<=>' => \&comparison,
  34. 'cmp' => \&comparison,
  35. '""' => \&as_string,
  36. 'bool' => sub { return 1 };
  37. =head1 NAME
  38. Dpkg::Version - handling and comparing dpkg-style version numbers
  39. =head1 DESCRIPTION
  40. The Dpkg::Version module provides pure-Perl routines to compare
  41. dpkg-style version numbers (as used in Debian packages) and also
  42. an object oriented interface overriding perl operators
  43. to do the right thing when you compare Dpkg::Version object between
  44. them.
  45. =head1 OBJECT INTERFACE
  46. =over 4
  47. =item my $v = Dpkg::Version->new($version)
  48. Create a new Dpkg::Version object corresponding to the version indicated in
  49. the string (scalar) $version. Returns undef if the string doesn't contain
  50. a valid version (see version_check for details).
  51. =cut
  52. sub new {
  53. my ($this, $ver) = @_;
  54. my $class = ref($this) || $this;
  55. $ver = "$ver" if ref($ver); # Try to stringify objects
  56. return undef unless version_check($ver);
  57. my $self = {};
  58. if ($ver =~ /^(\d*):(.+)$/) {
  59. $self->{'epoch'} = $1;
  60. $ver = $2;
  61. } else {
  62. $self->{'epoch'} = 0;
  63. $self->{'no_epoch'} = 1;
  64. }
  65. if ($ver =~ /(.+)-(.*)$/) {
  66. $self->{'version'} = $1;
  67. $self->{'revision'} = $2;
  68. } else {
  69. $self->{'version'} = $ver;
  70. $self->{'revision'} = 0;
  71. $self->{'no_revision'} = 1;
  72. }
  73. return bless $self, $class;
  74. }
  75. =item $v->epoch(), $v->version(), $v->revision()
  76. Returns the corresponding part of the full version string.
  77. =cut
  78. sub epoch {
  79. my $self = shift;
  80. return $self->{'epoch'};
  81. }
  82. sub version {
  83. my $self = shift;
  84. return $self->{'version'};
  85. }
  86. sub revision {
  87. my $self = shift;
  88. return $self->{'revision'};
  89. }
  90. =item $v1 <=> $v2, $v1 < $v2, $v1 <= $v2, $v1 > $v2, $v1 >= $v2
  91. Numerical comparison of various versions numbers. One of the two operands
  92. needs to be a Dpkg::Version, the other one can be anything provided that
  93. its string representation is a version number.
  94. =cut
  95. sub comparison {
  96. my ($a, $b, $inverted) = @_;
  97. if (not ref($b) or not $b->isa("Dpkg::Version")) {
  98. $b = Dpkg::Version->new($b);
  99. }
  100. ($a, $b) = ($b, $a) if $inverted;
  101. my $r = $a->epoch() <=> $b->epoch();
  102. return $r if $r;
  103. $r = version_compare_part($a->version(), $b->version());
  104. return $r if $r;
  105. return version_compare_part($a->revision(), $b->revision());
  106. }
  107. =item "$v"
  108. =item $v->as_string()
  109. Returns the string representation of the version number.
  110. =cut
  111. sub as_string {
  112. my ($self) = @_;
  113. my $str = "";
  114. $str .= $self->{epoch} . ":" unless $self->{no_epoch};
  115. $str .= $self->{version};
  116. $str .= "-" . $self->{revision} unless $self->{no_revision};
  117. return $str;
  118. }
  119. =back
  120. =head1 FUNCTIONS
  121. All the functions are exported by default.
  122. =over 4
  123. =item version_compare($a, $b)
  124. Returns -1 is $a is smaller than $b, 0 if they are equal and 1 if $a
  125. is bigger than $b.
  126. If $a or $b are not valid version numbers, it dies with an error.
  127. =cut
  128. sub version_compare($$) {
  129. my ($a, $b) = @_;
  130. my $va = Dpkg::Version->new($a) || error(_g("%s is not a valid version"), "$a");
  131. my $vb = Dpkg::Version->new($b) || error(_g("%s is not a valid version"), "$b");
  132. return $va <=> $vb;
  133. }
  134. =item version_compare_relation($a, $rel, $b)
  135. Returns the result (0 or 1) of the given comparison operation. This
  136. function is implemented on top of version_compare().
  137. Allowed values for $rel are the exported constants REL_GT, REL_GE,
  138. REL_EQ, REL_LE, REL_LT. Use version_normalize_relation() if you
  139. have an input string containing the operator.
  140. =cut
  141. sub version_compare_relation($$$) {
  142. my ($a, $op, $b) = @_;
  143. my $res = version_compare($a, $b);
  144. if ($op eq REL_GT) {
  145. return $res > 0;
  146. } elsif ($op eq REL_GE) {
  147. return $res >= 0;
  148. } elsif ($op eq REL_EQ) {
  149. return $res == 0;
  150. } elsif ($op eq REL_LE) {
  151. return $res <= 0;
  152. } elsif ($op eq REL_LT) {
  153. return $res < 0;
  154. } else {
  155. internerr("unsupported relation for version_compare_relation(): '$op'");
  156. }
  157. }
  158. =item my $rel = version_normalize_relation($rel_string)
  159. Returns the normalized constant of the relation $rel (a value
  160. among REL_GT, REL_GE, REL_EQ, REL_LE and REL_LT). Supported
  161. relations names in input are: "gt", "ge", "eq", "le", "lt", ">>", ">=",
  162. "=", "<=", "<<". ">" and "<" are also supported but should not be used as
  163. they are obsolete aliases of ">=" and "<=".
  164. =cut
  165. sub version_normalize_relation($) {
  166. my $op = shift;
  167. warning("relation %s is deprecated: use %s or %s",
  168. $op, "$op$op", "$op=") if ($op eq '>' or $op eq '<');
  169. if ($op eq '>>' or $op eq 'gt') {
  170. return REL_GT;
  171. } elsif ($op eq '>=' or $op eq 'ge' or $op eq '>') {
  172. return REL_GE;
  173. } elsif ($op eq '=' or $op eq 'eq') {
  174. return REL_EQ;
  175. } elsif ($op eq '<=' or $op eq 'le' or $op eq '<') {
  176. return REL_LE;
  177. } elsif ($op eq '<<' or $op eq 'lt') {
  178. return REL_LT;
  179. } else {
  180. internerr("bad relation '$op'");
  181. }
  182. }
  183. =item version_compare_string($a, $b)
  184. String comparison function used for comparing non-numerical parts of version
  185. numbers. Returns -1 is $a is smaller than $b, 0 if they are equal and 1 if $a
  186. is bigger than $b.
  187. The "~" character always sort lower than anything else. Digits sort lower
  188. than non-digits. Among remaining characters alphabetic characters (A-Za-z)
  189. sort lower than the other ones. Within each range, the ASCII decimal value
  190. of the character is used to sort between characters.
  191. =cut
  192. sub version_compare_string($$) {
  193. sub order(_) {
  194. my ($x) = @_;
  195. if ($x eq '~') {
  196. return -1;
  197. } elsif ($x =~ /^\d$/) {
  198. return $x * 1 + 1;
  199. } elsif ($x =~ /^[A-Za-z]$/) {
  200. return ord($x);
  201. } else {
  202. return ord($x) + 256;
  203. }
  204. }
  205. my @a = map(order, split(//, shift));
  206. my @b = map(order, split(//, shift));
  207. while (1) {
  208. my ($a, $b) = (shift @a, shift @b);
  209. return 0 if not defined($a) and not defined($b);
  210. $a ||= 0; # Default order for "no character"
  211. $b ||= 0;
  212. return 1 if $a > $b;
  213. return -1 if $a < $b;
  214. }
  215. }
  216. =item version_compare_part($a, $b)
  217. Compare two corresponding sub-parts of a version number (either upstream
  218. version or debian revision).
  219. Each parameter is split by version_split_digits() and resulting items
  220. are compared together.in digits and non-digits items that are compared
  221. together. As soon as a difference happens, it returns -1 if $a is smaller
  222. than $b, 0 if they are equal and 1 if $a is bigger than $b.
  223. =cut
  224. sub version_compare_part($$) {
  225. my @a = version_split_digits(shift);
  226. my @b = version_split_digits(shift);
  227. while (1) {
  228. my ($a, $b) = (shift @a, shift @b);
  229. return 0 if not defined($a) and not defined($b);
  230. $a ||= 0; # Default value for lack of version
  231. $b ||= 0;
  232. if ($a =~ /^\d+$/ and $b =~ /^\d+$/) {
  233. # Numerical comparison
  234. my $cmp = $a <=> $b;
  235. return $cmp if $cmp;
  236. } else {
  237. # String comparison
  238. my $cmp = version_compare_string($a, $b);
  239. return $cmp if $cmp;
  240. }
  241. }
  242. }
  243. =item my @items = version_split_digits($version)
  244. Splits a string in items that are each entirely composed either
  245. of digits or of non-digits. For instance for "1.024~beta1+svn234" it would
  246. return ("1", ".", "024", "~beta", "1", "+svn", "234").
  247. =cut
  248. sub version_split_digits($) {
  249. return split(/(?<=\d)(?=\D)|(?<=\D)(?=\d)/, $_[0]);
  250. }
  251. =item my ($ok, $msg) = version_check($version)
  252. =item my $ok = version_check($version)
  253. Checks the validity of $version as a version number. Returns 1 in $ok
  254. if the version is valid, 0 otherwise. In the latter case, $msg
  255. contains a description of the problem with the $version scalar.
  256. =cut
  257. sub version_check($) {
  258. my $version = shift;
  259. $version = "$version" if ref($version);
  260. if (not defined($version) or not length($version)) {
  261. my $msg = _g("version number cannot be empty");
  262. return (0, $msg) if wantarray;
  263. return 0;
  264. }
  265. if ($version =~ m/([^-+:.0-9a-zA-Z~])/o) {
  266. my $msg = sprintf(_g("version number contains illegal character `%s'"), $1);
  267. return (0, $msg) if wantarray;
  268. return 0;
  269. }
  270. if ($version =~ /:/ and $version !~ /^\d*:/) {
  271. $version =~ /^([^:]*):/;
  272. my $msg = sprintf(_g("epoch part of the version number " .
  273. "is not a number: '%s'"), $1);
  274. return (0, $msg) if wantarray;
  275. return 0;
  276. }
  277. return (1, "") if wantarray;
  278. return 1;
  279. }
  280. =back
  281. =head1 AUTHOR
  282. Don Armstrong <don@donarmstrong.com>, Colin Watson
  283. <cjwatson@debian.org> and Raphaël Hertzog <hertzog@debian.org>, based on
  284. the implementation in C<dpkg/lib/vercmp.c> by Ian Jackson and others.
  285. =cut
  286. 1;