From 4d059644f582c938387583b15050f4ae7895e736 Mon Sep 17 00:00:00 2001 From: Owen Taylor Date: Tue, 26 Nov 2002 15:04:06 +0000 Subject: [PATCH] Avoid non-ANSI pointer comparison. (#54344, Morten Welinder) Tue Nov 26 09:51:43 2002 Owen Taylor * glib/gstrfuncs.c (g_strchomp): Avoid non-ANSI pointer comparison. (#54344, Morten Welinder) * tests/strfunc-test.c (main): Add tests for strchomp(). --- glib/gstrfuncs.c | 16 +++++++++------- tests/strfunc-test.c | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/glib/gstrfuncs.c b/glib/gstrfuncs.c index 579ef0fc6..06e168fe2 100644 --- a/glib/gstrfuncs.c +++ b/glib/gstrfuncs.c @@ -2023,16 +2023,18 @@ g_strchug (gchar *string) gchar* g_strchomp (gchar *string) { - gchar *s; + gsize len; g_return_val_if_fail (string != NULL, NULL); - if (!*string) - return string; - - for (s = string + strlen (string) - 1; s >= string && g_ascii_isspace ((guchar)*s); - s--) - *s = '\0'; + len = strlen (string); + while (len--) + { + if (g_ascii_isspace ((guchar) string[len])) + string[len] = '\0'; + else + break; + } return string; } diff --git a/tests/strfunc-test.c b/tests/strfunc-test.c index 1f13831e4..eeb7c9705 100644 --- a/tests/strfunc-test.c +++ b/tests/strfunc-test.c @@ -92,6 +92,20 @@ str_check (gchar *str, return ok; } +static gboolean +strchomp_check (gchar *str, + gchar *expected) +{ + gchar *tmp = strdup (str); + gboolean ok; + + g_strchomp (tmp); + ok = (strcmp (tmp, expected) == 0); + g_free (tmp); + + return ok; +} + #define FOR_ALL_CTYPE(macro) \ macro(isalnum) \ macro(isalpha) \ @@ -331,6 +345,15 @@ main (int argc, #undef TEST_DIGIT + /* Tests for strchomp () */ + TEST (NULL, strchomp_check ("", "")); + TEST (NULL, strchomp_check (" ", "")); + TEST (NULL, strchomp_check (" \t\r\n", "")); + TEST (NULL, strchomp_check ("a ", "a")); + TEST (NULL, strchomp_check ("a ", "a")); + TEST (NULL, strchomp_check ("a a", "a a")); + TEST (NULL, strchomp_check ("a a ", "a a")); + /* Tests for g_build_path, g_build_filename */ TEST (NULL, str_check (g_build_path ("", NULL), ""));