Fix g_strreverse to handle empty strings
authorRolf Bjarne Kvinge <rolf@xamarin.com>
Fri, 30 Mar 2012 11:40:01 +0000 (13:40 +0200)
committerRolf Bjarne Kvinge <rolf@xamarin.com>
Fri, 30 Mar 2012 11:44:03 +0000 (13:44 +0200)
eglib/src/gstr.c
eglib/test/string-util.c

index fccf2651db26b05678dea8a5e4b1472f6d5a0753..0f66a43372e96e0f2ad6db4ac0ec3ef76290ecd6 100644 (file)
@@ -357,6 +357,9 @@ g_strreverse (gchar *str)
        if (str == NULL)
                return NULL;
 
+       if (*str == 0)
+               return str;
+
        for (i = 0, j = strlen (str) - 1; i < j; i++, j--) {
                c = str [i];
                str [i] = str [j];
index 14d24d6ac772f2933455747c1247f7adb2589eff..3dbd738aba7fc7de580460d2a1942b9b573eab87 100644 (file)
@@ -296,27 +296,37 @@ test_split_set ()
 RESULT
 test_strreverse ()
 {
+       RESULT res = OK;
        gchar *a = g_strdup ("onetwothree");
        gchar *a_target = "eerhtowteno";
        gchar *b = g_strdup ("onetwothre");
        gchar *b_target = "erhtowteno";
+       gchar *c = g_strdup ("");
+       gchar *c_target = "";
 
        g_strreverse (a);
        if (strcmp (a, a_target)) {
-               g_free (b);
-               g_free (a);
-               return FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", a, a_target);
+               res = FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", a, a_target);
+               goto cleanup;
        }
 
        g_strreverse (b);
        if (strcmp (b, b_target)) {
-               g_free (b);
-               g_free (a);
-               return FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", b, b_target);
+               res = FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", b, b_target);
+               goto cleanup;
        }
+
+       g_strreverse (c);
+       if (strcmp (c, c_target)) {
+               res = FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", b, b_target);
+               goto cleanup;
+       }
+
+cleanup:
+       g_free (c);
        g_free (b);
        g_free (a);
-       return OK;
+       return res;
 }
 
 RESULT