[gnome-packagekit] Update egg shared code
- From: Richard Hughes <rhughes src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [gnome-packagekit] Update egg shared code
- Date: Wed, 29 Jul 2009 14:54:28 +0000 (UTC)
commit 94a987cda9c4861b7c1cf4bfbe7eba586f403b4f
Author: Richard Hughes <richard hughsie com>
Date: Wed Jul 29 15:52:40 2009 +0100
Update egg shared code
src/egg-string.c | 191 +++++++++++++++---------------------------------------
src/egg-string.h | 5 --
2 files changed, 52 insertions(+), 144 deletions(-)
---
diff --git a/src/egg-string.c b/src/egg-string.c
index 091795b..6203029 100644
--- a/src/egg-string.c
+++ b/src/egg-string.c
@@ -46,46 +46,9 @@
#include "egg-string.h"
/**
- * egg_strnumber:
- * @text: The text the validate
- *
- * Tests a string to see if it is a number. Both positive and negative numbers
- * are allowed.
- *
- * Return value: %TRUE if the string represents a numeric value
- **/
-gboolean
-egg_strnumber (const gchar *text)
-{
- guint i;
- guint length;
-
- /* check explicitly */
- if (egg_strzero (text))
- return FALSE;
-
- /* max length is 10 */
- length = egg_strlen (text, 10);
- if (length == 10) {
- egg_warning ("input too long: %s", text);
- return FALSE;
- }
-
- for (i=0; i<length; i++) {
- if (i == 0 && text[i] == '-') {
- /* negative sign */
- } else if (g_ascii_isdigit (text[i]) == FALSE) {
- egg_warning ("not a number '%c' in text!", text[i]);
- return FALSE;
- }
- }
- return TRUE;
-}
-
-/**
* egg_strtoint:
* @text: The text the convert
- * @value: The return numeric return value, or 0 if invalid.
+ * @value: The return numeric return value
*
* Converts a string into a signed integer value in a safe way.
*
@@ -94,21 +57,33 @@ egg_strnumber (const gchar *text)
gboolean
egg_strtoint (const gchar *text, gint *value)
{
- gboolean ret;
- ret = egg_strnumber (text);
- if (!ret) {
- *value = 0;
+ gchar *endptr = NULL;
+ gint64 value_raw;
+
+ /* invalid */
+ if (text == NULL)
return FALSE;
- }
- /* ITS4: ignore, we've already checked for validity */
- *value = atoi (text);
+
+ /* parse */
+ value_raw = g_ascii_strtoll (text, &endptr, 10);
+
+ /* parsing error */
+ if (endptr == text)
+ return FALSE;
+
+ /* out of range */
+ if (value_raw > G_MAXINT || value_raw < G_MININT)
+ return FALSE;
+
+ /* cast back down to value */
+ *value = (gint) value_raw;
return TRUE;
}
/**
* egg_strtouint:
* @text: The text the convert
- * @value: The return numeric return value, or 0 if invalid.
+ * @value: The return numeric return value
*
* Converts a string into a unsigned integer value in a safe way.
*
@@ -117,14 +92,26 @@ egg_strtoint (const gchar *text, gint *value)
gboolean
egg_strtouint (const gchar *text, guint *value)
{
- gboolean ret;
- gint temp;
- ret = egg_strtoint (text, &temp);
- if (ret == FALSE || temp < 0) {
- *value = 0;
+ gchar *endptr = NULL;
+ guint64 value_raw;
+
+ /* invalid */
+ if (text == NULL)
return FALSE;
- }
- *value = (guint) temp;
+
+ /* parse */
+ value_raw = g_ascii_strtoull (text, &endptr, 10);
+
+ /* parsing error */
+ if (endptr == text)
+ return FALSE;
+
+ /* out of range */
+ if (value_raw > G_MAXINT)
+ return FALSE;
+
+ /* cast back down to value */
+ *value = (guint) value_raw;
return TRUE;
}
@@ -177,24 +164,6 @@ egg_strlen (const gchar *text, guint len)
}
/**
- * egg_strequal:
- * @id1: the first item of text to test
- * @id2: the second item of text to test
- *
- * This function is a much safer way of doing strcmp as it checks for
- * NULL first, and returns boolean TRUE, not zero for success.
- *
- * Return value: %TRUE if the string are both non-%NULL and the same.
- **/
-gboolean
-egg_strequal (const gchar *id1, const gchar *id2)
-{
- if (id1 == NULL || id2 == NULL)
- return FALSE;
- return (strcmp (id1, id2) == 0);
-}
-
-/**
* egg_strvequal:
* @id1: the first item of text to test
* @id2: the second item of text to test
@@ -284,23 +253,6 @@ egg_string_test (EggTest *test)
return;
/************************************************************
- **************** String equal ******************
- ************************************************************/
- egg_test_title (test, "egg_strequal same argument");
- temp = "dave";
- if (g_strcmp0 (temp, temp) == 0)
- egg_test_success (test, NULL);
- else
- egg_test_failed (test, "incorrect ret when both same");
-
- /************************************************************/
- egg_test_title (test, "egg_strequal both const");
- if (g_strcmp0 ("dave", "dave") == 0)
- egg_test_success (test, NULL);
- else
- egg_test_failed (test, "incorrect ret when both same");
-
- /************************************************************
**************** String array equal ******************
************************************************************/
egg_test_title (test, "egg_strvequal same argument");
@@ -373,12 +325,12 @@ egg_string_test (EggTest *test)
/************************************************************/
egg_test_title (test, "id strcmp pass");
- ret = g_strcmp0 ("moo;0.0.1;i386;fedora", "moo;0.0.1;i386;fedora") == 0;
+ ret = (g_strcmp0 ("moo;0.0.1;i386;fedora", "moo;0.0.1;i386;fedora") == 0);
egg_test_assert (test, ret);
/************************************************************/
egg_test_title (test, "id strcmp fail");
- ret = g_strcmp0 ("moo;0.0.1;i386;fedora", "moo;0.0.2;i386;fedora") == 0;
+ ret = (g_strcmp0 ("moo;0.0.1;i386;fedora", "moo;0.0.2;i386;fedora") == 0);
egg_test_assert (test, !ret);
/************************************************************
@@ -446,53 +398,6 @@ egg_string_test (EggTest *test)
g_free (text_safe);
/************************************************************
- ************** Check for numbers ****************
- ************************************************************/
- egg_test_title (test, "check number valid");
- ret = egg_strnumber ("123");
- egg_test_assert (test, ret);
-
- /************************************************************/
- egg_test_title (test, "check number valid");
- ret = egg_strnumber ("-123");
- egg_test_assert (test, ret);
-
- /************************************************************/
- egg_test_title (test, "check number zero");
- ret = egg_strnumber ("0");
- egg_test_assert (test, ret);
-
- /************************************************************/
- egg_test_title (test, "check number oversize");
- ret = egg_strnumber ("123456891234");
- egg_test_assert (test, !ret);
-
- /************************************************************/
- egg_test_title (test, "check number NULL");
- ret = egg_strnumber (NULL);
- egg_test_assert (test, !ret);
-
- /************************************************************/
- egg_test_title (test, "check number blank");
- ret = egg_strnumber ("");
- egg_test_assert (test, !ret);
-
- /************************************************************/
- egg_test_title (test, "check number not negative");
- ret = egg_strnumber ("503-");
- egg_test_assert (test, !ret);
-
- /************************************************************/
- egg_test_title (test, "check number positive");
- ret = egg_strnumber ("+503");
- egg_test_assert (test, !ret);
-
- /************************************************************/
- egg_test_title (test, "check number random chars");
- ret = egg_strnumber ("dave");
- egg_test_assert (test, !ret);
-
- /************************************************************
************** Convert numbers ****************
************************************************************/
egg_test_title (test, "convert valid number");
@@ -513,12 +418,20 @@ egg_string_test (EggTest *test)
/************************************************************/
egg_test_title (test, "don't convert invalid number");
ret = egg_strtoint ("dave", &value);
- if (ret == FALSE && value == 0)
+ if (!ret)
egg_test_success (test, NULL);
else
egg_test_failed (test, "value is %i", value);
/************************************************************/
+ egg_test_title (test, "convert NULL to a number");
+ ret = egg_strtouint (NULL, &uvalue);
+ if (!ret)
+ egg_test_success (test, NULL);
+ else
+ egg_test_failed (test, "value is %i", uvalue);
+
+ /************************************************************/
egg_test_title (test, "convert valid uint number");
ret = egg_strtouint ("234", &uvalue);
if (ret && uvalue == 234)
@@ -529,7 +442,7 @@ egg_string_test (EggTest *test)
/************************************************************/
egg_test_title (test, "convert invalid uint number");
ret = egg_strtouint ("-234", &uvalue);
- if (ret == FALSE && uvalue == 0)
+ if (ret == FALSE)
egg_test_success (test, NULL);
else
egg_test_failed (test, "value is %i", uvalue);
diff --git a/src/egg-string.h b/src/egg-string.h
index 5414907..668a824 100644
--- a/src/egg-string.h
+++ b/src/egg-string.h
@@ -31,14 +31,9 @@ guint egg_strlen (const gchar *text,
G_GNUC_WARN_UNUSED_RESULT;
gboolean egg_strzero (const gchar *text)
G_GNUC_WARN_UNUSED_RESULT;
-gboolean egg_strequal (const gchar *id1,
- const gchar *id2)
- G_GNUC_WARN_UNUSED_RESULT;
gboolean egg_strvequal (gchar **id1,
gchar **id2)
G_GNUC_WARN_UNUSED_RESULT;
-gboolean egg_strnumber (const gchar *text)
- G_GNUC_WARN_UNUSED_RESULT;
gboolean egg_strtoint (const gchar *text,
gint *value);
gboolean egg_strtouint (const gchar *text,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]