mirror of
https://github.com/keymanapp/keyman.git
synced 2026-09-22 07:37:40 +00:00
refactor(linux): Deal with invalid version numbers
This commit is contained in:
parent
02008dc585
commit
13eb1002ae
2 changed files with 45 additions and 19 deletions
|
|
@ -212,27 +212,41 @@ get_engine_for_language(
|
|||
return engine_desc;
|
||||
}
|
||||
|
||||
int
|
||||
keyman_compare_version(const gchar *version1str, const gchar *version2str) {
|
||||
size_t len1 = strlen(version1str);
|
||||
size_t len2 = strlen(version2str);
|
||||
for (size_t i = 0, j = 0; i < len1 || j < len2; i++, j++) {
|
||||
int version1 = 0, version2 = 0;
|
||||
int _get_version(const gchar **pver) {
|
||||
g_assert(pver);
|
||||
const gchar *ver = *pver;
|
||||
int version = 0;
|
||||
|
||||
while (i < len1 && version1str[i] != '.') {
|
||||
version1 = version1 * 10 + (version1str[i] - '0');
|
||||
i++;
|
||||
}
|
||||
while (j < len2 && version2str[j] != '.') {
|
||||
version2 = version2 * 10 + (version2str[j] - '0');
|
||||
j++;
|
||||
}
|
||||
|
||||
if (version1 < version2)
|
||||
return -1;
|
||||
if (version1 > version2)
|
||||
return +1;
|
||||
while (*ver && *ver != '.') {
|
||||
if (*ver >= '0' && *ver <= '9') {
|
||||
version = version * 10 + (*ver - '0');
|
||||
ver++;
|
||||
} else {
|
||||
// stop comparison on first non-digit
|
||||
while (*ver)
|
||||
ver++;
|
||||
}
|
||||
}
|
||||
*pver = ver;
|
||||
return version;
|
||||
}
|
||||
|
||||
int
|
||||
keyman_compare_version(const gchar *ver1, const gchar *ver2) {
|
||||
for (; *ver1 || *ver2; ) {
|
||||
int version1 = _get_version(&ver1);
|
||||
int version2 = _get_version(&ver2);
|
||||
|
||||
if (version1 < version2)
|
||||
return -1;
|
||||
if (version1 > version2)
|
||||
return +1;
|
||||
|
||||
if (*ver1)
|
||||
ver1++;
|
||||
if (*ver2)
|
||||
ver2++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1043,6 +1043,16 @@ test_keyman_compare_version_equal_with_patch() {
|
|||
g_assert_cmpint(keyman_compare_version("1.0", "1.0.0"), ==, 0);
|
||||
}
|
||||
|
||||
void
|
||||
test_keyman_compare_version_equal_nondigit() {
|
||||
g_assert_cmpint(keyman_compare_version("1.1", "1.1-beta2"), ==, 0);
|
||||
}
|
||||
|
||||
void
|
||||
test_keyman_compare_version_equal_doubledot() {
|
||||
g_assert_cmpint(keyman_compare_version("1..2", "1.0.2"), ==, 0);
|
||||
}
|
||||
|
||||
void
|
||||
test_keyman_compare_version_less() {
|
||||
g_assert_cmpint(keyman_compare_version("1.0", "1.1"), <, 0);
|
||||
|
|
@ -1162,6 +1172,8 @@ int main(int argc, char* argv[]) {
|
|||
|
||||
g_test_add_func("/keymanutil/keyman_compare_version/1.0==1.0", test_keyman_compare_version_equal);
|
||||
g_test_add_func("/keymanutil/keyman_compare_version/1.0==1.0.0", test_keyman_compare_version_equal_with_patch);
|
||||
g_test_add_func("/keymanutil/keyman_compare_version/1.1==1.1-beta", test_keyman_compare_version_equal_nondigit);
|
||||
g_test_add_func("/keymanutil/keyman_compare_version/1..2==1.0.2", test_keyman_compare_version_equal_doubledot);
|
||||
g_test_add_func("/keymanutil/keyman_compare_version/1.0<1.1", test_keyman_compare_version_less);
|
||||
g_test_add_func("/keymanutil/keyman_compare_version/1.0.1<1.1", test_keyman_compare_version_minor_less_with_patch);
|
||||
g_test_add_func("/keymanutil/keyman_compare_version/1.0<1.0.1", test_keyman_compare_version_patch_less);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue