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