6045
|
1 ## Copyright (C) 2006 Bill Denney <denney@seas.upenn.edu> |
|
2 ## |
6440
|
3 ## This file is part of Octave. |
6045
|
4 ## |
6440
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
6045
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
6440
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
6045
|
19 |
|
20 ## -*- texinfo -*- |
6248
|
21 ## @deftypefn {Function File} {} compare_versions (@var{v1}, @var{v2}, @var{operator}) |
6045
|
22 ## Compares to version strings using the given @var{operator}. |
|
23 ## |
|
24 ## This function assumes that versions @var{v1} and @var{v2} are |
|
25 ## arbitrarily long strings made of numeric and period characters |
|
26 ## possibly followed by an arbitrary string (e.g. "1.2.3", "0.3", |
|
27 ## "0.1.2+", or "1.2.3.4-test1"). |
|
28 ## |
|
29 ## The version is first split into the numeric and the character parts |
|
30 ## then the parts are padded to be the same length (i.e. "1.1" would be |
|
31 ## padded to be like "1.1.0" when being compared with "1.1.1", and |
|
32 ## separately, the character parts of the strings are padded with |
|
33 ## nulls). |
|
34 ## |
|
35 ## The operator can be any logical operator from the set |
|
36 ## |
|
37 ## @itemize @bullet |
|
38 ## @item |
|
39 ## "==" |
|
40 ## equal |
|
41 ## @item |
|
42 ## "<" |
|
43 ## less than |
|
44 ## @item |
|
45 ## "<=" |
|
46 ## less than or equal to |
|
47 ## @item |
|
48 ## ">" |
|
49 ## greater than |
|
50 ## @item |
|
51 ## ">=" |
|
52 ## greater than or equal to |
|
53 ## @item |
|
54 ## "!=" |
|
55 ## not equal |
|
56 ## @item |
|
57 ## "~=" |
|
58 ## not equal |
|
59 ## @end itemize |
|
60 ## |
|
61 ## Note that version "1.1-test2" would compare as greater than |
|
62 ## "1.1-test10". Also, since the numeric part is compared first, "a" |
|
63 ## compares less than "1a" because the second string starts with a |
|
64 ## numeric part even though double("a") is greater than double("1"). |
|
65 ## @end deftypefn |
|
66 |
|
67 ## TODO?: This allows a single equal sign "=" to indicate equality, do |
|
68 ## we want to require a double equal since that is the boolean operator? |
|
69 |
|
70 function out = compare_versions(v1, v2, operator) |
|
71 |
|
72 ## make sure that the version numbers are valid |
|
73 if ~ (ischar (v1) && ischar (v2)) |
|
74 error ("Both version numbers must be strings"); |
|
75 elseif (size (v1, 1) ~= 1) || (size (v2, 1) ~= 1) |
|
76 error ("Version numbers must be a single row") |
|
77 endif |
|
78 |
|
79 ## check and make sure that the operator is valid |
|
80 if (~ ischar (operator)) |
|
81 error("Operator must be a character string"); |
|
82 elseif (numel (operator) > 2) |
|
83 error("Operator cannot be more than 2 characters long"); |
|
84 endif |
|
85 |
|
86 ## trim off any character data that is not part of a normal version |
|
87 ## number |
|
88 numbers = "0123456789."; |
|
89 v1firstchar = find(~ ismember(v1, numbers), 1); |
|
90 v2firstchar = find(~ ismember(v2, numbers), 1); |
|
91 if ~ isempty (v1firstchar) |
|
92 v1c = v1(v1firstchar:length(v1)); |
|
93 v1nochar = v1(1:v1firstchar-1); |
|
94 else |
|
95 v1c = ""; |
|
96 v1nochar = v1; |
|
97 endif |
|
98 if ~ isempty (v2firstchar) |
|
99 v2c = v2(v2firstchar:length(v2)); |
|
100 v2nochar = v2(1:v2firstchar-1); |
|
101 else |
|
102 v2c = ""; |
|
103 v2nochar = v2; |
|
104 endif |
|
105 |
|
106 v1n = str2num (split (v1nochar, '.')); |
|
107 v2n = str2num (split (v2nochar, '.')); |
|
108 if ((isempty (v1n) && isempty (v1c)) || (isempty (v2n) && isempty(v2c))) |
|
109 error ("Given version strings are not valid: %s %s", v1, v2); |
|
110 endif |
|
111 |
|
112 ## assume that any additional elements would be 0 if one is longer |
|
113 ## than the other |
|
114 maxnumlen = max ([length(v1n) length(v2n)]); |
|
115 if (length (v1n) < maxnumlen) |
|
116 v1n(length(v1n)+1:maxnumlen) = 0; |
|
117 endif |
|
118 if (length (v2n) < maxnumlen) |
|
119 v2n(length(v2n)+1:maxnumlen) = 0; |
|
120 endif |
|
121 |
|
122 ## assume that any additional character elements would be 0 if one is |
|
123 ## longer than the other |
|
124 maxcharlen = max ([length(v1c) length(v2c)]); |
|
125 if (length (v1c) < maxcharlen) |
|
126 v1c(length(v1c)+1:maxcharlen) = "\0"; |
|
127 endif |
|
128 if (length (v2c) < maxcharlen) |
|
129 v2c(length(v2c)+1:maxcharlen) = "\0"; |
|
130 endif |
|
131 |
|
132 ## determine the operator |
|
133 if any (ismember (operator, "=")) |
|
134 equal_op = true; |
|
135 else |
|
136 equal_op = false; |
|
137 end |
|
138 if any (ismember (operator, "~!")) |
|
139 not_op = true; |
|
140 else |
|
141 not_op = false; |
|
142 endif |
|
143 if any (ismember (operator, "<")) |
|
144 lt_op = true; |
|
145 else |
|
146 lt_op = false; |
|
147 endif |
|
148 if any (ismember (operator, ">")) |
|
149 gt_op = true; |
|
150 else |
|
151 gt_op = false; |
|
152 endif |
|
153 |
|
154 ## make sure that we don't have conflicting operators |
|
155 if (gt_op && lt_op) |
|
156 error("Operator cannot contain both greater and less than symbols"); |
|
157 elseif ((gt_op || lt_op) && not_op) |
|
158 error("Operator cannot contain not and greater than or less than symbols"); |
|
159 endif |
|
160 |
|
161 ## compare the versions (making sure that they're the same shape) |
|
162 vcmp = v1n(:) - v2n(:); |
|
163 vcmp = [vcmp; (v1c - v2c)(:)]; |
|
164 if (lt_op) |
|
165 ## so that we only need to check for the output being greater than 1 |
|
166 vcmp = -vcmp; |
|
167 endif |
|
168 firstdiff = find(vcmp != 0, 1); |
|
169 |
|
170 if isempty (firstdiff) |
|
171 ## they're equal |
|
172 out = equal_op; |
|
173 elseif (lt_op || gt_op) |
|
174 ## they're correctly less than or greater than |
|
175 out = (vcmp(firstdiff) > 0); |
|
176 else |
|
177 ## they're not correctly less than or greater than, and they're not equal |
|
178 out = false; |
|
179 endif |
|
180 |
|
181 ## reverse the output if not is given |
|
182 out = xor (not_op, out); |
|
183 endfunction |
|
184 |
|
185 ## tests |
|
186 ## test both equality symbols |
|
187 %!assert(compare_versions("1", "1", "="), true) |
|
188 ## test arbitrarily long equality |
|
189 %!assert(compare_versions("1.1.0.0.0", "1.1", "=="), true) |
|
190 %!assert(compare_versions("1", "1.1", "<"), true) |
|
191 %!assert(compare_versions("1.1", "1.1", "<="), true) |
|
192 %!assert(compare_versions("1.1", "1.1.1", "<="), true) |
|
193 %!assert(compare_versions("1.23", "1.24", "=<"), true) |
|
194 ## test different length numbers |
|
195 %!assert(compare_versions("23.2000", "23.1", ">"), true) |
|
196 %!assert(compare_versions("0.0.2", "0.0.1", ">="), true) |
|
197 %!assert(compare_versions("0.2", "0.0.100", "=>"), true) |
|
198 %!assert(compare_versions("0.1", "0.2", "!="), true) |
|
199 %!assert(compare_versions("0.1", "0.2", "~="), true) |
|
200 |
|
201 ## test alphanumeric strings |
|
202 %!assert(compare_versions("1a", "1b", "<"), true) |
|
203 %!assert(compare_versions("a", "1", "<"), true) |
|
204 %!assert(compare_versions("1a", "1b", ">"), false) |
|
205 %!assert(compare_versions("a", "1", ">"), false) |
|
206 %!assert(compare_versions("1.1.0a", "1.1.0b", "=="), false) |
|
207 %!assert(compare_versions("1.1.0a", "1.1.0b", "!="), true) |
|
208 %!assert(compare_versions("1.1.0test", "1.1.0b", "=="), false) |
|
209 %!assert(compare_versions("1.1.0test", "1.1.0test", "=="), true) |
|
210 |
|
211 ## make sure that it won't just give true output |
|
212 %!assert(compare_versions("1", "0", "="), false) |
|
213 ## test arbitrarily long equality |
|
214 %!assert(compare_versions("1.1.1.0.0", "1.1", "=="), false) |
|
215 %!assert(compare_versions("1.1", "1", "<"), false) |
|
216 %!assert(compare_versions("2", "1.1", "<="), false) |
|
217 %!assert(compare_versions("1.1.1", "1.1", "<="), false) |
|
218 %!assert(compare_versions("1.25", "1.24", "=<"), false) |
|
219 ## test different length numbers |
|
220 %!assert(compare_versions("23.2", "23.100", ">"), false) |
|
221 %!assert(compare_versions("0.0.0.2", "0.0.1", ">="), false) |
|
222 %!assert(compare_versions("0.0.20", "0.10.2", "=>"), false) |
|
223 %!assert(compare_versions("0.1", "0.1", "!="), false) |
|
224 %!assert(compare_versions("0.1", "0.1", "~="), false) |
|
225 |
|
226 ## FIXME: how do we check to make sure that it gives errors when it |
|
227 ## should |