Avoid non-ANSI pointer comparison. (#54344, Morten Welinder)

Tue Nov 26 09:51:43 2002  Owen Taylor  <otaylor@redhat.com>

        * glib/gstrfuncs.c (g_strchomp): Avoid non-ANSI pointer
        comparison. (#54344, Morten Welinder)

        * tests/strfunc-test.c (main): Add tests for strchomp().
This commit is contained in:
Owen Taylor 2002-11-26 15:04:06 +00:00 committed by Owen Taylor
parent c838b2a071
commit 4d059644f5
2 changed files with 32 additions and 7 deletions

View File

@ -2023,16 +2023,18 @@ g_strchug (gchar *string)
gchar* gchar*
g_strchomp (gchar *string) g_strchomp (gchar *string)
{ {
gchar *s; gsize len;
g_return_val_if_fail (string != NULL, NULL); g_return_val_if_fail (string != NULL, NULL);
if (!*string) len = strlen (string);
return string; while (len--)
{
for (s = string + strlen (string) - 1; s >= string && g_ascii_isspace ((guchar)*s); if (g_ascii_isspace ((guchar) string[len]))
s--) string[len] = '\0';
*s = '\0'; else
break;
}
return string; return string;
} }

View File

@ -92,6 +92,20 @@ str_check (gchar *str,
return ok; 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) \ #define FOR_ALL_CTYPE(macro) \
macro(isalnum) \ macro(isalnum) \
macro(isalpha) \ macro(isalpha) \
@ -331,6 +345,15 @@ main (int argc,
#undef TEST_DIGIT #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 */ /* Tests for g_build_path, g_build_filename */
TEST (NULL, str_check (g_build_path ("", NULL), "")); TEST (NULL, str_check (g_build_path ("", NULL), ""));