Version.pm 9.9 KB

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