On x86 compilers without fastcall, simulate it when invoking traces and un-simulate...
[wine-gecko.git] / xpcom / glue / nsVersionComparator.cpp
blobfdbcc6b8887b29468cc8777a1427a726cb319311
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is Mozilla XPCOM.
16 * The Initial Developer of the Original Code is
17 * Benjamin Smedberg <benjamin@smedbergs.us>.
19 * Portions created by the Initial Developer are Copyright (C) 2005
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsVersionComparator.h"
40 #include <stdlib.h>
41 #include <string.h>
43 struct VersionPart {
44 PRInt32 numA;
46 const char *strB; // NOT null-terminated, can be a null pointer
47 PRUint32 strBlen;
49 PRInt32 numC;
51 char *extraD; // null-terminated
54 /**
55 * Parse a version part into a number and "extra text".
57 * @returns A pointer to the next versionpart, or null if none.
59 static char*
60 ParseVP(char *part, VersionPart &result)
62 char *dot;
64 result.numA = 0;
65 result.strB = nsnull;
66 result.strBlen = 0;
67 result.numC = 0;
68 result.extraD = nsnull;
70 if (!part)
71 return part;
73 dot = strchr(part, '.');
74 if (dot)
75 *dot = '\0';
77 if (part[0] == '*' && part[1] == '\0') {
78 result.numA = PR_INT32_MAX;
79 result.strB = "";
81 else {
82 result.numA = strtol(part, const_cast<char**>(&result.strB), 10);
85 if (!*result.strB) {
86 result.strB = nsnull;
87 result.strBlen = 0;
89 else {
90 if (result.strB[0] == '+') {
91 static const char kPre[] = "pre";
93 ++result.numA;
94 result.strB = kPre;
95 result.strBlen = sizeof(kPre) - 1;
97 else {
98 const char *numstart = strpbrk(result.strB, "0123456789+-");
99 if (!numstart) {
100 result.strBlen = strlen(result.strB);
102 else {
103 result.strBlen = numstart - result.strB;
105 result.numC = strtol(numstart, &result.extraD, 10);
106 if (!*result.extraD)
107 result.extraD = nsnull;
112 if (dot) {
113 ++dot;
115 if (!*dot)
116 dot = nsnull;
119 return dot;
122 // compare two null-terminated strings, which may be null pointers
123 static PRInt32
124 ns_strcmp(const char *str1, const char *str2)
126 // any string is *before* no string
127 if (!str1)
128 return str2 != 0;
130 if (!str2)
131 return -1;
133 return strcmp(str1, str2);
136 // compare two length-specified string, which may be null pointers
137 static PRInt32
138 ns_strnncmp(const char *str1, PRUint32 len1, const char *str2, PRUint32 len2)
140 // any string is *before* no string
141 if (!str1)
142 return str2 != 0;
144 if (!str2)
145 return -1;
147 for (; len1 && len2; --len1, --len2, ++str1, ++str2) {
148 if (*str1 < *str2)
149 return -1;
151 if (*str1 > *str2)
152 return 1;
155 if (len1 == 0)
156 return len2 == 0 ? 0 : -1;
158 return 1;
161 // compare two PRInt32
162 static PRInt32
163 ns_cmp(PRInt32 n1, PRInt32 n2)
165 if (n1 < n2)
166 return -1;
168 return n1 != n2;
172 * Compares two VersionParts
174 static PRInt32
175 CompareVP(VersionPart &v1, VersionPart &v2)
177 PRInt32 r = ns_cmp(v1.numA, v2.numA);
178 if (r)
179 return r;
181 r = ns_strnncmp(v1.strB, v1.strBlen, v2.strB, v2.strBlen);
182 if (r)
183 return r;
185 r = ns_cmp(v1.numC, v2.numC);
186 if (r)
187 return r;
189 return ns_strcmp(v1.extraD, v2.extraD);
192 PRInt32
193 NS_CompareVersions(const char *A, const char *B)
195 char *A2 = strdup(A);
196 if (!A2)
197 return 1;
199 char *B2 = strdup(B);
200 if (!B2) {
201 free(A2);
202 return 1;
205 PRInt32 result;
206 char *a = A2, *b = B2;
208 do {
209 VersionPart va, vb;
211 a = ParseVP(a, va);
212 b = ParseVP(b, vb);
214 result = CompareVP(va, vb);
215 if (result)
216 break;
218 } while (a || b);
220 free(A2);
221 free(B2);
223 return result;