2 * Copyright (c) 2007-2009, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
22 #if defined(DEBIAN) || defined(MULTI_SEMANTICS)
24 /* debian type version compare */
26 solv_vercmp_deb(const char *s1, const char *q1, const char *s2, const char *q2)
31 c1 = s1 < q1 ? *(const unsigned char *)s1++ : 0;
32 c2 = s2 < q2 ? *(const unsigned char *)s2++ : 0;
33 if ((c1 >= '0' && c1 <= '9') && (c2 >= '0' && c2 <= '9'))
36 c1 = s1 < q1 ? *(const unsigned char *)s1++ : 0;
38 c2 = s2 < q2 ? *(const unsigned char *)s2++ : 0;
40 while ((c1 >= '0' && c1 <= '9') && (c2 >= '0' && c2 <= '9'))
44 c1 = s1 < q1 ? *(const unsigned char *)s1++ : 0;
45 c2 = s2 < q2 ? *(const unsigned char *)s2++ : 0;
47 if (c1 >= '0' && c1 <= '9')
49 if (c2 >= '0' && c2 <= '9')
52 return r < 0 ? -1 : 1;
54 c1 = c1 == '~' ? -1 : !c1 || (c1 >= '0' && c1 <= '9') || (c1 >= 'A' && c1 <= 'Z') || (c1 >= 'a' && c1 <= 'z') ? c1 : c1 + 256;
55 c2 = c2 == '~' ? -1 : !c2 || (c2 >= '0' && c2 <= '9') || (c2 >= 'A' && c2 <= 'Z') || (c2 >= 'a' && c2 <= 'z') ? c2 : c2 + 256;
58 return r < 0 ? -1 : 1;
66 #if !defined(DEBIAN) || defined(MULTI_SEMANTICS)
68 /* rpm type version compare */
69 /* note: the code assumes that *q1 and *q2 are not alphanumeric! */
72 solv_vercmp_rpm(const char *s1, const char *q1, const char *s2, const char *q2)
79 while (s1 < q1 && !(*s1 >= '0' && *s1 <= '9') &&
80 !(*s1 >= 'a' && *s1 <= 'z') && !(*s1 >= 'A' && *s1 <= 'Z') && *s1 != '~')
82 while (s2 < q2 && !(*s2 >= '0' && *s2 <= '9') &&
83 !(*s2 >= 'a' && *s2 <= 'z') && !(*s2 >= 'A' && *s2 <= 'Z') && *s2 != '~')
85 if (s1 < q1 && *s1 == '~')
87 if (s2 < q2 && *s2 == '~')
95 if (s2 < q2 && *s2 == '~')
97 if (s1 >= q1 || s2 >= q2)
99 if ((*s1 >= '0' && *s1 <= '9') || (*s2 >= '0' && *s2 <= '9'))
101 while (*s1 == '0' && s1[1] >= '0' && s1[1] <= '9')
103 while (*s2 == '0' && s2[1] >= '0' && s2[1] <= '9')
105 for (e1 = s1; *e1 >= '0' && *e1 <= '9'; )
107 for (e2 = s2; *e2 >= '0' && *e2 <= '9'; )
109 r = (e1 - s1) - (e2 - s2);
111 r = strncmp(s1, s2, e1 - s1);
113 return r > 0 ? 1 : -1;
117 for (e1 = s1; (*e1 >= 'a' && *e1 <= 'z') || (*e1 >= 'A' && *e1 <= 'Z'); )
119 for (e2 = s2; (*e2 >= 'a' && *e2 <= 'z') || (*e2 >= 'A' && *e2 <= 'Z'); )
121 r = (e1 - s1) - (e2 - s2);
124 r = strncmp(s1, s2, e2 - s2);
125 return r >= 0 ? 1 : -1;
129 r = strncmp(s1, s2, e1 - s1);
130 return r <= 0 ? -1 : 1;
132 r = strncmp(s1, s2, e1 - s1);
134 return r > 0 ? 1 : -1;
139 return s1 < q1 ? 1 : s2 < q2 ? -1 : 0;
143 solv_vercmp_rpm_notilde(const char *s1, const char *q1, const char *s2, const char *q2)
148 while (s1 < q1 && s2 < q2)
150 while (s1 < q1 && !(*s1 >= '0' && *s1 <= '9') &&
151 !(*s1 >= 'a' && *s1 <= 'z') && !(*s1 >= 'A' && *s1 <= 'Z'))
153 while (s2 < q2 && !(*s2 >= '0' && *s2 <= '9') &&
154 !(*s2 >= 'a' && *s2 <= 'z') && !(*s2 >= 'A' && *s2 <= 'Z'))
156 if ((*s1 >= '0' && *s1 <= '9') || (*s2 >= '0' && *s2 <= '9'))
158 while (*s1 == '0' && s1[1] >= '0' && s1[1] <= '9')
160 while (*s2 == '0' && s2[1] >= '0' && s2[1] <= '9')
162 for (e1 = s1; *e1 >= '0' && *e1 <= '9'; )
164 for (e2 = s2; *e2 >= '0' && *e2 <= '9'; )
166 r = (e1 - s1) - (e2 - s2);
168 r = strncmp(s1, s2, e1 - s1);
170 return r > 0 ? 1 : -1;
174 for (e1 = s1; (*e1 >= 'a' && *e1 <= 'z') || (*e1 >= 'A' && *e1 <= 'Z'); )
176 for (e2 = s2; (*e2 >= 'a' && *e2 <= 'z') || (*e2 >= 'A' && *e2 <= 'Z'); )
178 r = (e1 - s1) - (e2 - s2);
181 r = strncmp(s1, s2, e2 - s2);
182 return r >= 0 ? 1 : -1;
186 r = strncmp(s1, s2, e1 - s1);
187 return r <= 0 ? -1 : 1;
189 r = strncmp(s1, s2, e1 - s1);
191 return r > 0 ? 1 : -1;
196 return s1 < q1 ? 1 : s2 < q2 ? -1 : 0;
204 solv_cmp_version_part_haiku(const char *s1, const char *q1, const char *s2,
207 while (s1 < q1 && s2 < q2)
210 const char *part1 = s1, *part2 = s2;
212 /* compare non-number part */
213 while (s1 < q1 && !isdigit(*s1))
215 while (s2 < q2 && !isdigit(*s2))
225 cmp = strncmp(part1, part2, len1 < len2 ? len1 : len2);
231 else if (part2 != s2)
234 /* compare number part */
238 while (s1 < q1 && isdigit(*s1))
240 while (s2 < q2 && isdigit(*s2))
243 while (part1 + 1 < s1 && *part1 == '0')
245 while (part2 + 1 < s1 && *part2 == '0')
255 cmp = strncmp(part1, part2, len1);
260 return s1 < q1 ? 1 : s2 < q2 ? -1 : 0;
264 solv_vercmp_haiku(const char *s1, const char *q1, const char *s2, const char *q2)
266 const char *pre1 = s1;
267 const char *pre2 = s2;
270 /* find pre-release separator */
271 while (pre1 != q1 && *pre1 != '/')
273 while (pre2 != q2 && *pre2 != '/')
276 /* compare main versions */
277 cmp = solv_cmp_version_part_haiku(s1, pre1, s2, pre2);
279 return cmp < 0 ? -1 : 1; /* must return -1, 0, or 1 */
281 /* main versions are equal -- compare pre-release (none is greatest) */
283 return pre2 == q2 ? 0 : 1;
287 return solv_cmp_version_part_haiku(pre1 + 1, q1, pre2 + 1, q2);
288 cmp = solv_cmp_version_part_haiku(pre1 + 1, q1, pre2 + 1, q2);
289 return cmp == 0 ? 0 : cmp < 0 ? -1 : 1; /* must return -1, 0, or 1 */
296 * the solv_vercmp variant your system uses.
299 solv_vercmp(const char *s1, const char *q1, const char *s2, const char *q2)
302 return solv_vercmp_deb(s1, q1, s2, q2);
303 #elif defined(ARCHLINUX)
304 return solv_vercmp_rpm_notilde(s1, q1, s2, q2);
306 return solv_vercmp_haiku(s1, q1, s2, q2);
308 return solv_vercmp_rpm(s1, q1, s2, q2);
312 #if defined(MULTI_SEMANTICS)
313 # define solv_vercmp (*(pool->disttype == DISTTYPE_DEB ? &solv_vercmp_deb : &solv_ver##cmp_rpm))
314 #elif defined(DEBIAN)
315 # define solv_vercmp solv_vercmp_deb
316 #elif defined(ARCHLINUX)
317 # define solv_vercmp solv_vercmp_rpm_notilde
319 # define solv_vercmp solv_vercmp_rpm
322 /* edition (e:v-r) compare */
324 pool_evrcmp_str(const Pool *pool, const char *evr1, const char *evr2, int mode)
334 POOL_DEBUG(DEBUG_EVRCMP, "evrcmp %s %s mode=%d\n", evr1, evr2, mode);
336 for (s1 = evr1; *s1 >= '0' && *s1 <= '9'; s1++)
338 for (s2 = evr2; *s2 >= '0' && *s2 <= '9'; s2++)
340 if (mode == EVRCMP_MATCH && (*evr1 == ':' || *evr2 == ':'))
342 /* empty epoch, skip epoch check */
351 /* compare the epoch */
352 if (s1 == evr1 || *s1 != ':')
354 if (s2 == evr2 || *s2 != ':')
358 r = solv_vercmp(evr1, s1, evr2, s2);
366 if (!pool->promoteepoch)
384 /* same epoch, now split into version/release */
385 for (s1 = evr1, r1 = 0; *s1; s1++)
388 for (s2 = evr2, r2 = 0; *s2; s2++)
393 if (mode != EVRCMP_MATCH || (evr1 != (r1 ? r1 : s1) && evr2 != (r2 ? r2 : s2)))
394 r = solv_vercmp(evr1, r1 ? r1 : s1, evr2, r2 ? r2 : s2);
398 if (mode == EVRCMP_COMPARE)
405 if (mode == EVRCMP_COMPARE_EVONLY)
407 if (mode == EVRCMP_MATCH_RELEASE)
409 /* rpm treats empty releases as missing, i.e "foo = 4-" is the same as "foo = 4" */
410 if (r1 && r1 + 1 == s1)
412 if (r2 && r2 + 1 == s2)
419 if (mode != EVRCMP_MATCH || (s1 != r1 && s2 != r2))
421 if (pool->havedistepoch)
424 for (d1 = r1; d1 < s1; d1++)
427 for (d2 = r2; d2 < s2; d2++)
430 /* XXX: promote just in one direction? */
431 r = solv_vercmp(r1, d1 ? d1 : s1, r2, d2 ? d2 : s2);
432 if (r == 0 && d1 < s1 && d2 < s2)
433 r = solv_vercmp(d1 + 1, s1, d2 + 1, s2);
436 r = solv_vercmp(r1, s1, r2, s2);
439 else if (mode == EVRCMP_MATCH_RELEASE)
450 pool_evrcmp(const Pool *pool, Id evr1id, Id evr2id, int mode)
452 const char *evr1, *evr2;
453 if (evr1id == evr2id)
455 evr1 = pool_id2str(pool, evr1id);
456 evr2 = pool_id2str(pool, evr2id);
457 return pool_evrcmp_str(pool, evr1, evr2, mode);
461 pool_evrmatch(const Pool *pool, Id evrid, const char *epoch, const char *version, const char *release)
468 evr1 = pool_id2str(pool, evrid);
469 for (s1 = evr1; *s1 >= '0' && *s1 <= '9'; s1++)
471 if (s1 != evr1 && *s1 == ':')
475 r = solv_vercmp(evr1, s1, epoch, epoch + strlen(epoch));
483 while (*epoch == '0')
488 for (s1 = evr1, r1 = 0; *s1; s1++)
493 r = solv_vercmp(evr1, r1 ? r1 : s1, version, version + strlen(version));
501 r = solv_vercmp(r1 + 1, s1, release, release + strlen(release));