comparison scripts/miscellaneous/compare_versions.m @ 6045:421d8a903df7

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