diff options
author | Richard Hughes <richard@hughsie.com> | 2015-09-24 09:25:49 +0100 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2015-09-24 09:25:49 +0100 |
commit | a833a4c4e6d7274e51f7bab19e4e828f25afb0f7 (patch) | |
tree | c09a221f8602124cdb9c8dc77e6f150ae035d4ba | |
parent | a0f2dda9e0d81ea4931e2fc677d069f3d8a2f1f2 (diff) | |
download | appstream-glib-wip/hughsie/dotted-decimal.tar.gz |
-rw-r--r-- | libappstream-glib/as-release.c | 12 | ||||
-rw-r--r-- | libappstream-glib/as-self-test.c | 63 | ||||
-rw-r--r-- | libappstream-glib/as-utils.c | 64 | ||||
-rw-r--r-- | libappstream-glib/as-utils.h | 3 |
4 files changed, 89 insertions, 53 deletions
diff --git a/libappstream-glib/as-release.c b/libappstream-glib/as-release.c index 3c82ea2..bcb00f1 100644 --- a/libappstream-glib/as-release.c +++ b/libappstream-glib/as-release.c @@ -320,17 +320,7 @@ as_release_set_version (AsRelease *release, const gchar *version) { AsReleasePrivate *priv = GET_PRIVATE (release); g_free (priv->version); - - /* convert 0x prefixed strings to dotted decimal */ - if (g_str_has_prefix (version, "0x")) { - guint64 tmp; - tmp = g_ascii_strtoull (version + 2, NULL, 16); - priv->version = as_utils_int_to_dotted_decimal (tmp); - return; - } - - /* no special rule */ - priv->version = g_strdup (version); + priv->version = as_utils_version_parse (version); } /** diff --git a/libappstream-glib/as-self-test.c b/libappstream-glib/as-self-test.c index 467c828..920cc12 100644 --- a/libappstream-glib/as-self-test.c +++ b/libappstream-glib/as-self-test.c @@ -378,7 +378,7 @@ as_test_release_func (void) /* verify converting hex prefix */ as_release_set_version (release, "0x600100"); - g_assert_cmpstr (as_release_get_version (release), ==, "6291712"); + g_assert_cmpstr (as_release_get_version (release), ==, "96.1.0"); } static void @@ -3519,23 +3519,49 @@ as_test_utils_func (void) g_assert_cmpstr (tokens[1], ==, "stupid"); g_assert_cmpstr (tokens[2], ==, NULL); g_strfreev (tokens); +} + +static void +as_test_utils_version_func (void) +{ + guint i; + struct { + guint32 val; + const gchar *ver; + } version_from_uint32[] = { + { 0x0, "0" }, + { 0xff, "255" }, + { 0xff01, "255.1" }, + { 0xff0001, "255.0.1" }, + { 0xff000100, "255.0.1.0" }, + { NULL, NULL } + }; + struct { + const gchar *old; + const gchar *new; + } version_parse[] = { + { "0", "0" }, + { "257", "1.1" }, + { "1.2.3", "1.2.3" }, + { "0xff0001", "255.0.1" }, + { "16711681", "255.0.1" }, + { "dave", "dave" }, + { NULL, NULL } + }; /* check version conversion */ - tmp = as_utils_int_to_dotted_decimal (0x0); - g_assert_cmpstr (tmp, ==, "0"); - g_free (tmp); - tmp = as_utils_int_to_dotted_decimal (0xff); - g_assert_cmpstr (tmp, ==, "255"); - g_free (tmp); - tmp = as_utils_int_to_dotted_decimal (0xff01); - g_assert_cmpstr (tmp, ==, "255.1"); - g_free (tmp); - tmp = as_utils_int_to_dotted_decimal (0xff0001); - g_assert_cmpstr (tmp, ==, "255.0.1"); - g_free (tmp); - tmp = as_utils_int_to_dotted_decimal (0xff000100); - g_assert_cmpstr (tmp, ==, "255.0.1.0"); - g_free (tmp); + for (i = 0; version_from_uint32[i].ver != NULL; i++) { + g_autofree gchar *ver = NULL; + ver = as_utils_version_from_uint32 (version_from_uint32[i].val); + g_assert_cmpstr (ver, ==, version_from_uint32[i].ver); + } + + /* check version parsing */ + for (i = 0; version_parse[i].old != NULL; i++) { + g_autofree gchar *ver = NULL; + ver = as_utils_version_parse (version_parse[i].old); + g_assert_cmpstr (ver, ==, version_parse[i].new); + } } static void @@ -3837,8 +3863,8 @@ as_test_utils_vercmp_func (void) g_assert_cmpint (as_utils_vercmp ("1.2.3", "1.2.3"), ==, 0); /* same, not dotted decimal */ - g_assert_cmpint (as_utils_vercmp ("1.2.3", "10203"), ==, 0); - g_assert_cmpint (as_utils_vercmp ("10203", "10203"), ==, 0); + g_assert_cmpint (as_utils_vercmp ("1.2.3", "0x10203"), ==, 0); + g_assert_cmpint (as_utils_vercmp ("0x10203", "0x10203"), ==, 0); /* upgrade and downgrade */ g_assert_cmpint (as_utils_vercmp ("1.2.3", "1.2.4"), <, 0); @@ -4274,6 +4300,7 @@ main (int argc, char **argv) g_test_add_func ("/AppStream/node{intltool}", as_test_node_intltool_func); g_test_add_func ("/AppStream/node{sort}", as_test_node_sort_func); g_test_add_func ("/AppStream/utils", as_test_utils_func); + g_test_add_func ("/AppStream/utils{version}", as_test_utils_version_func); g_test_add_func ("/AppStream/utils{guid}", as_test_utils_guid_func); g_test_add_func ("/AppStream/utils{icons}", as_test_utils_icons_func); g_test_add_func ("/AppStream/utils{spdx-token}", as_test_utils_spdx_token_func); diff --git a/libappstream-glib/as-utils.c b/libappstream-glib/as-utils.c index 9e9dcd5..0192142 100644 --- a/libappstream-glib/as-utils.c +++ b/libappstream-glib/as-utils.c @@ -1374,25 +1374,6 @@ as_utils_search_tokenize (const gchar *search) } /** - * as_utils_convert_version: - */ -static gchar * -as_utils_convert_version (const gchar *version) -{ - guint64 tmp; - - /* already dotted decimal */ - if (g_strstr_len (version, -1, ".") != NULL) - return g_strdup (version); - - /* convert */ - tmp = g_ascii_strtoull (version, NULL, 16); - if (tmp < 0xff) - return g_strdup (version); - return as_utils_int_to_dotted_decimal (tmp); -} - -/** * as_utils_vercmp: * @version_a: the release version, e.g. 1.2.3 * @version_b: the release version, e.g. 1.2.3.1 @@ -1426,8 +1407,8 @@ as_utils_vercmp (const gchar *version_a, const gchar *version_b) return 0; /* split into sections, and try to parse */ - str_a = as_utils_convert_version (version_a); - str_b = as_utils_convert_version (version_b); + str_a = as_utils_version_parse (version_a); + str_b = as_utils_version_parse (version_b); split_a = g_strsplit (str_a, ".", -1); split_b = g_strsplit (str_b, ".", -1); longest_split = MAX (g_strv_length (split_a), g_strv_length (split_b)); @@ -1555,7 +1536,7 @@ as_utils_guid_from_string (const gchar *str) } /** - * as_utils_int_to_dotted_decimal: + * as_utils_version_from_uint32: * @val: A uint32le version number * * Returns a dotted decimal version string from a 32 bit number. @@ -1565,7 +1546,7 @@ as_utils_guid_from_string (const gchar *str) * Since: 0.5.2 **/ gchar * -as_utils_int_to_dotted_decimal (guint32 val) +as_utils_version_from_uint32 (guint32 val) { GString *str; gboolean valid = FALSE; @@ -1587,3 +1568,40 @@ as_utils_int_to_dotted_decimal (guint32 val) return g_string_free (str, FALSE); } + +/** + * as_utils_version_parse: + */ +gchar * +as_utils_version_parse (const gchar *version) +{ + gchar *endptr = NULL; + guint64 tmp; + guint base; + guint i; + + /* already dotted decimal */ + if (g_strstr_len (version, -1, ".") != NULL) + return g_strdup (version); + + /* convert 0x prefixed strings to dotted decimal */ + if (g_str_has_prefix (version, "0x")) { + version += 2; + base = 16; + } else { + /* for non-numeric content, just return the string */ + for (i = 0; version[i] != '\0'; i++) { + if (!g_ascii_isdigit (version[i])) + return g_strdup (version); + } + base = 10; + } + + /* convert */ + tmp = g_ascii_strtoull (version, &endptr, base); + if (endptr != NULL && endptr[0] != '\0') + return g_strdup (version); + if (tmp == 0 || tmp < 0xff) + return g_strdup (version); + return as_utils_version_from_uint32 (tmp); +} diff --git a/libappstream-glib/as-utils.h b/libappstream-glib/as-utils.h index c15edde..8e8d5b3 100644 --- a/libappstream-glib/as-utils.h +++ b/libappstream-glib/as-utils.h @@ -125,7 +125,8 @@ gint as_utils_vercmp (const gchar *version_a, const gchar *version_b); gboolean as_utils_guid_is_valid (const gchar *guid); gchar *as_utils_guid_from_string (const gchar *str); -gchar *as_utils_int_to_dotted_decimal (guint32 val); +gchar *as_utils_version_from_uint32 (guint32 val); +gchar *as_utils_version_parse (const gchar *version); G_END_DECLS |