Version.pm 11 KB

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