[gnome-power-manager] Egg shared code updates
- From: Richard Hughes <rhughes src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [gnome-power-manager] Egg shared code updates
- Date: Wed, 29 Jul 2009 15:12:45 +0000 (UTC)
commit 72104c40988a858a8d18fd38464653707e45974b
Author: Richard Hughes <richard hughsie com>
Date: Wed Jul 29 15:53:57 2009 +0100
Egg shared code updates
src/egg-string.c | 201 +++++++++++++++--------------------------------------
src/egg-string.h | 5 --
2 files changed, 57 insertions(+), 149 deletions(-)
---
diff --git a/src/egg-string.c b/src/egg-string.c
index 2e860df..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
@@ -226,7 +195,7 @@ egg_strvequal (gchar **id1, gchar **id2)
/* text equal each one */
for (i=0; i<length1; i++) {
- if (!egg_strequal (id1[i], id2[i]))
+ if (g_strcmp0 (id1[i], id2[i]) != 0)
return FALSE;
}
@@ -284,23 +253,6 @@ egg_string_test (EggTest *test)
return;
/************************************************************
- **************** String equal ******************
- ************************************************************/
- egg_test_title (test, "egg_strequal same argument");
- temp = "dave";
- if (egg_strequal (temp, temp))
- egg_test_success (test, NULL);
- else
- egg_test_failed (test, "incorrect ret when both same");
-
- /************************************************************/
- egg_test_title (test, "egg_strequal both const");
- if (egg_strequal ("dave", "dave"))
- 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 = egg_strequal ("moo;0.0.1;i386;fedora", "moo;0.0.1;i386;fedora");
+ 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 = egg_strequal ("moo;0.0.1;i386;fedora", "moo;0.0.2;i386;fedora");
+ ret = (g_strcmp0 ("moo;0.0.1;i386;fedora", "moo;0.0.2;i386;fedora") == 0);
egg_test_assert (test, !ret);
/************************************************************
@@ -412,7 +364,7 @@ egg_string_test (EggTest *test)
************************************************************/
egg_test_title (test, "replace start");
text_safe = egg_strreplace ("richard\nhughes", "r", "e");
- if (egg_strequal (text_safe, "eichaed\nhughes"))
+ if (g_strcmp0 (text_safe, "eichaed\nhughes") == 0)
egg_test_success (test, NULL);
else
egg_test_failed (test, "failed the replace '%s'", text_safe);
@@ -421,7 +373,7 @@ egg_string_test (EggTest *test)
/************************************************************/
egg_test_title (test, "replace none");
text_safe = egg_strreplace ("richard\nhughes", "dave", "e");
- if (egg_strequal (text_safe, "richard\nhughes"))
+ if (g_strcmp0 (text_safe, "richard\nhughes") == 0)
egg_test_success (test, NULL);
else
egg_test_failed (test, "failed the replace '%s'", text_safe);
@@ -430,7 +382,7 @@ egg_string_test (EggTest *test)
/************************************************************/
egg_test_title (test, "replace end");
text_safe = egg_strreplace ("richard\nhughes", "s", "e");
- if (egg_strequal (text_safe, "richard\nhughee"))
+ if (g_strcmp0 (text_safe, "richard\nhughee") == 0)
egg_test_success (test, NULL);
else
egg_test_failed (test, "failed the replace '%s'", text_safe);
@@ -439,60 +391,13 @@ egg_string_test (EggTest *test)
/************************************************************/
egg_test_title (test, "replace unicode");
text_safe = egg_strreplace ("richard\n- hughes", "\n- ", "\nâ?¢ ");
- if (egg_strequal (text_safe, "richard\nâ?¢ hughes"))
+ if (g_strcmp0 (text_safe, "richard\nâ?¢ hughes") == 0)
egg_test_success (test, NULL);
else
egg_test_failed (test, "failed the replace '%s'", text_safe);
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]