diff options
author | Owen Taylor <otaylor@redhat.com> | 2002-07-26 19:58:07 +0000 |
---|---|---|
committer | Owen Taylor <otaylor@src.gnome.org> | 2002-07-26 19:58:07 +0000 |
commit | 23a2a8ad3471df359f4548c4964843c1e265f2ce (patch) | |
tree | 78ac87abc25d1a66fbe9cffc2ed83693dc2d8fcf | |
parent | 1a8852d37483500adc0dfe79339c0f08ccb10d16 (diff) |
Clarify the behavior of g_build_path() for empty elements and for leading
Thu Jul 25 14:23:15 2002 Owen Taylor <otaylor@redhat.com>
* glib/gfileutils.c: Clarify the behavior of g_build_path()
for empty elements and for leading and trailing copies
of the separator in the docs.
* glib/gfileutils.c: Fix problems with leading elements
consisting only of "/" characters. (#85928, Guillaume Chazarain)
* tests/strfunc-test.c (main): Add more test cases
for g_build_filename().
-rw-r--r-- | ChangeLog | 12 | ||||
-rw-r--r-- | ChangeLog.pre-2-10 | 12 | ||||
-rw-r--r-- | ChangeLog.pre-2-12 | 12 | ||||
-rw-r--r-- | ChangeLog.pre-2-2 | 12 | ||||
-rw-r--r-- | ChangeLog.pre-2-4 | 12 | ||||
-rw-r--r-- | ChangeLog.pre-2-6 | 12 | ||||
-rw-r--r-- | ChangeLog.pre-2-8 | 12 | ||||
-rw-r--r-- | glib/gfileutils.c | 86 | ||||
-rw-r--r-- | tests/strfunc-test.c | 38 |
9 files changed, 193 insertions, 15 deletions
@@ -1,3 +1,15 @@ +Thu Jul 25 14:23:15 2002 Owen Taylor <otaylor@redhat.com> + + * glib/gfileutils.c: Clarify the behavior of g_build_path() + for empty elements and for leading and trailing copies + of the separator in the docs. + + * glib/gfileutils.c: Fix problems with leading elements + consisting only of "/" characters. (#85928, Guillaume Chazarain) + + * tests/strfunc-test.c (main): Add more test cases + for g_build_filename(). + Thu Jul 25 20:57:20 2002 Owen Taylor <otaylor@redhat.com> * glib/giochannel.c (g_io_channel_read_line): Fix diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 31bc9c591..613041628 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,15 @@ +Thu Jul 25 14:23:15 2002 Owen Taylor <otaylor@redhat.com> + + * glib/gfileutils.c: Clarify the behavior of g_build_path() + for empty elements and for leading and trailing copies + of the separator in the docs. + + * glib/gfileutils.c: Fix problems with leading elements + consisting only of "/" characters. (#85928, Guillaume Chazarain) + + * tests/strfunc-test.c (main): Add more test cases + for g_build_filename(). + Thu Jul 25 20:57:20 2002 Owen Taylor <otaylor@redhat.com> * glib/giochannel.c (g_io_channel_read_line): Fix diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 31bc9c591..613041628 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,15 @@ +Thu Jul 25 14:23:15 2002 Owen Taylor <otaylor@redhat.com> + + * glib/gfileutils.c: Clarify the behavior of g_build_path() + for empty elements and for leading and trailing copies + of the separator in the docs. + + * glib/gfileutils.c: Fix problems with leading elements + consisting only of "/" characters. (#85928, Guillaume Chazarain) + + * tests/strfunc-test.c (main): Add more test cases + for g_build_filename(). + Thu Jul 25 20:57:20 2002 Owen Taylor <otaylor@redhat.com> * glib/giochannel.c (g_io_channel_read_line): Fix diff --git a/ChangeLog.pre-2-2 b/ChangeLog.pre-2-2 index 31bc9c591..613041628 100644 --- a/ChangeLog.pre-2-2 +++ b/ChangeLog.pre-2-2 @@ -1,3 +1,15 @@ +Thu Jul 25 14:23:15 2002 Owen Taylor <otaylor@redhat.com> + + * glib/gfileutils.c: Clarify the behavior of g_build_path() + for empty elements and for leading and trailing copies + of the separator in the docs. + + * glib/gfileutils.c: Fix problems with leading elements + consisting only of "/" characters. (#85928, Guillaume Chazarain) + + * tests/strfunc-test.c (main): Add more test cases + for g_build_filename(). + Thu Jul 25 20:57:20 2002 Owen Taylor <otaylor@redhat.com> * glib/giochannel.c (g_io_channel_read_line): Fix diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index 31bc9c591..613041628 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,3 +1,15 @@ +Thu Jul 25 14:23:15 2002 Owen Taylor <otaylor@redhat.com> + + * glib/gfileutils.c: Clarify the behavior of g_build_path() + for empty elements and for leading and trailing copies + of the separator in the docs. + + * glib/gfileutils.c: Fix problems with leading elements + consisting only of "/" characters. (#85928, Guillaume Chazarain) + + * tests/strfunc-test.c (main): Add more test cases + for g_build_filename(). + Thu Jul 25 20:57:20 2002 Owen Taylor <otaylor@redhat.com> * glib/giochannel.c (g_io_channel_read_line): Fix diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index 31bc9c591..613041628 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,15 @@ +Thu Jul 25 14:23:15 2002 Owen Taylor <otaylor@redhat.com> + + * glib/gfileutils.c: Clarify the behavior of g_build_path() + for empty elements and for leading and trailing copies + of the separator in the docs. + + * glib/gfileutils.c: Fix problems with leading elements + consisting only of "/" characters. (#85928, Guillaume Chazarain) + + * tests/strfunc-test.c (main): Add more test cases + for g_build_filename(). + Thu Jul 25 20:57:20 2002 Owen Taylor <otaylor@redhat.com> * glib/giochannel.c (g_io_channel_read_line): Fix diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index 31bc9c591..613041628 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,15 @@ +Thu Jul 25 14:23:15 2002 Owen Taylor <otaylor@redhat.com> + + * glib/gfileutils.c: Clarify the behavior of g_build_path() + for empty elements and for leading and trailing copies + of the separator in the docs. + + * glib/gfileutils.c: Fix problems with leading elements + consisting only of "/" characters. (#85928, Guillaume Chazarain) + + * tests/strfunc-test.c (main): Add more test cases + for g_build_filename(). + Thu Jul 25 20:57:20 2002 Owen Taylor <otaylor@redhat.com> * glib/giochannel.c (g_io_channel_read_line): Fix diff --git a/glib/gfileutils.c b/glib/gfileutils.c index 4108e197b..86ef6ea98 100644 --- a/glib/gfileutils.c +++ b/glib/gfileutils.c @@ -770,7 +770,10 @@ g_build_pathv (const gchar *separator, GString *result; gint separator_len = strlen (separator); gboolean is_first = TRUE; + gboolean have_leading = FALSE; + const gchar *single_element = NULL; const gchar *next_element; + const gchar *last_trailing = NULL; result = g_string_new (NULL); @@ -790,36 +793,69 @@ g_build_pathv (const gchar *separator, else break; - start = element; + /* Ignore empty elements */ + if (!*element) + continue; - if (is_first) - is_first = FALSE; - else if (separator_len) + start = element; + + if (separator_len) { while (start && strncmp (start, separator, separator_len) == 0) start += separator_len; - } + } end = start + strlen (start); - if (next_element && separator_len) + if (separator_len) { - while (end > start + separator_len && + while (end >= start + separator_len && strncmp (end - separator_len, separator, separator_len) == 0) end -= separator_len; + + last_trailing = end; + while (last_trailing >= element + separator_len && + strncmp (last_trailing - separator_len, separator, separator_len) == 0) + last_trailing -= separator_len; + + if (!have_leading) + { + /* If the leading and trailing separator strings are in the + * same element and overlap, the result is exactly that element + */ + if (last_trailing <= start) + single_element = element; + + g_string_append_len (result, element, start - element); + have_leading = TRUE; + } + else + single_element = NULL; } - if (end > start) - { - if (result->len > 0) - g_string_append (result, separator); + if (end == start) + continue; - g_string_append_len (result, start, end - start); - } + if (!is_first) + g_string_append (result, separator); + + g_string_append_len (result, start, end - start); + is_first = FALSE; } + + if (single_element) + { + g_string_free (result, TRUE); + return g_strdup (single_element); + } + else + { + if (last_trailing) + g_string_append (result, last_trailing); - return g_string_free (result, FALSE); + return g_string_free (result, FALSE); + } } /** @@ -833,6 +869,28 @@ g_build_pathv (const gchar *separator, * any trailing occurrences of separator in the first element, or * leading occurrences of separator in the second element are removed * and exactly one copy of the separator is inserted. + * + * Empty elements are ignored. + * + * The number of leading copies of the separator on the result is + * the same as the number of leading copies of the separator on + * the first non-empty element. + * + * The number of trailing copies of the separator on the result is + * the same as the number of trailing copies of the separator on + * the last non-empty element. (Determination of the number of + * trailing copies is done without stripping leading copies, so + * if the separator is <literal>ABA</literal>, <literal>ABABA</literal> + * has 1 trailing copy.) + * + * However, if there is only a single non-empty element, and there + * are no characters in that element not part of the leading or + * trailing separators, then the result is exactly the original value + * of that element. + * + * Other than for determination of the number of leading and trailing + * copies of the separator, elements consisting only of copies + * of the separator are ignored. * * Return value: a newly-allocated string that must be freed with g_free(). **/ diff --git a/tests/strfunc-test.c b/tests/strfunc-test.c index cd20b9a9d..1f13831e4 100644 --- a/tests/strfunc-test.c +++ b/tests/strfunc-test.c @@ -41,7 +41,7 @@ if (failed) \ { if (!m) \ g_print ("(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \ else \ - g_print ("(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \ + g_print ("(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), m ? (gchar*)m : ""); \ fflush (stdout); \ any_failed = TRUE; \ } \ @@ -343,25 +343,50 @@ main (int argc, TEST (NULL, str_check (g_build_path (":", ":", NULL), ":")); TEST (NULL, str_check (g_build_path (":", ":x", NULL), ":x")); TEST (NULL, str_check (g_build_path (":", "x:", NULL), "x:")); + TEST (NULL, str_check (g_build_path (":", "", "x", NULL), "x")); + TEST (NULL, str_check (g_build_path (":", "", ":x", NULL), ":x")); + TEST (NULL, str_check (g_build_path (":", ":", "x", NULL), ":x")); + TEST (NULL, str_check (g_build_path (":", "::", "x", NULL), "::x")); + TEST (NULL, str_check (g_build_path (":", "x", "", NULL), "x")); + TEST (NULL, str_check (g_build_path (":", "x:", "", NULL), "x:")); + TEST (NULL, str_check (g_build_path (":", "x", ":", NULL), "x:")); + TEST (NULL, str_check (g_build_path (":", "x", "::", NULL), "x::")); TEST (NULL, str_check (g_build_path (":", "x", "y", NULL), "x:y")); TEST (NULL, str_check (g_build_path (":", ":x", "y", NULL), ":x:y")); TEST (NULL, str_check (g_build_path (":", "x", "y:", NULL), "x:y:")); TEST (NULL, str_check (g_build_path (":", ":x:", ":y:", NULL), ":x:y:")); TEST (NULL, str_check (g_build_path (":", ":x::", "::y:", NULL), ":x:y:")); + TEST (NULL, str_check (g_build_path (":", "x", "","y", NULL), "x:y")); + TEST (NULL, str_check (g_build_path (":", "x", ":", "y", NULL), "x:y")); + TEST (NULL, str_check (g_build_path (":", "x", "::", "y", NULL), "x:y")); TEST (NULL, str_check (g_build_path (":", "x", "y", "z", NULL), "x:y:z")); TEST (NULL, str_check (g_build_path (":", ":x:", ":y:", ":z:", NULL), ":x:y:z:")); TEST (NULL, str_check (g_build_path (":", "::x::", "::y::", "::z::", NULL), "::x:y:z::")); TEST (NULL, str_check (g_build_path ("::", NULL), "")); TEST (NULL, str_check (g_build_path ("::", "::", NULL), "::")); + TEST (NULL, str_check (g_build_path ("::", ":::", NULL), ":::")); TEST (NULL, str_check (g_build_path ("::", "::x", NULL), "::x")); TEST (NULL, str_check (g_build_path ("::", "x::", NULL), "x::")); + TEST (NULL, str_check (g_build_path ("::", "", "x", NULL), "x")); + TEST (NULL, str_check (g_build_path ("::", "", "::x", NULL), "::x")); + TEST (NULL, str_check (g_build_path ("::", "::", "x", NULL), "::x")); + TEST (NULL, str_check (g_build_path ("::", "::::", "x", NULL), "::::x")); + TEST (NULL, str_check (g_build_path ("::", "x", "", NULL), "x")); + TEST (NULL, str_check (g_build_path ("::", "x::", "", NULL), "x::")); + TEST (NULL, str_check (g_build_path ("::", "x", "::", NULL), "x::")); + /* This following is weird, but keeps the definition simple */ + TEST (NULL, str_check (g_build_path ("::", "x", ":::", NULL), "x:::::")); + TEST (NULL, str_check (g_build_path ("::", "x", "::::", NULL), "x::::")); TEST (NULL, str_check (g_build_path ("::", "x", "y", NULL), "x::y")); TEST (NULL, str_check (g_build_path ("::", "::x", "y", NULL), "::x::y")); TEST (NULL, str_check (g_build_path ("::", "x", "y::", NULL), "x::y::")); TEST (NULL, str_check (g_build_path ("::", "::x::", "::y::", NULL), "::x::y::")); TEST (NULL, str_check (g_build_path ("::", "::x:::", ":::y::", NULL), "::x::::y::")); TEST (NULL, str_check (g_build_path ("::", "::x::::", "::::y::", NULL), "::x::y::")); + TEST (NULL, str_check (g_build_path ("::", "x", "", "y", NULL), "x::y")); + TEST (NULL, str_check (g_build_path ("::", "x", "::", "y", NULL), "x::y")); + TEST (NULL, str_check (g_build_path ("::", "x", "::::", "y", NULL), "x::y")); TEST (NULL, str_check (g_build_path ("::", "x", "y", "z", NULL), "x::y::z")); TEST (NULL, str_check (g_build_path ("::", "::x::", "::y::", "::z::", NULL), "::x::y::z::")); TEST (NULL, str_check (g_build_path ("::", ":::x:::", ":::y:::", ":::z:::", NULL), ":::x::::y::::z:::")); @@ -373,11 +398,22 @@ main (int argc, TEST (NULL, str_check (g_build_filename (S, NULL), S)); TEST (NULL, str_check (g_build_filename (S"x", NULL), S"x")); TEST (NULL, str_check (g_build_filename ("x"S, NULL), "x"S)); + TEST (NULL, str_check (g_build_filename ("", "x", NULL), "x")); + TEST (NULL, str_check (g_build_filename ("", S"x", NULL), S"x")); + TEST (NULL, str_check (g_build_filename (S, "x", NULL), S"x")); + TEST (NULL, str_check (g_build_filename (S S, "x", NULL), S S"x")); + TEST (NULL, str_check (g_build_filename ("x", "", NULL), "x")); + TEST (NULL, str_check (g_build_filename ("x"S, "", NULL), "x"S)); + TEST (NULL, str_check (g_build_filename ("x", S, NULL), "x"S)); + TEST (NULL, str_check (g_build_filename ("x", S S, NULL), "x"S S)); TEST (NULL, str_check (g_build_filename ("x", "y", NULL), "x"S"y")); TEST (NULL, str_check (g_build_filename (S"x", "y", NULL), S"x"S"y")); TEST (NULL, str_check (g_build_filename ("x", "y"S, NULL), "x"S"y"S)); TEST (NULL, str_check (g_build_filename (S"x"S, S"y"S, NULL), S"x"S"y"S)); TEST (NULL, str_check (g_build_filename (S"x"S S, S S"y"S, NULL), S"x"S"y"S)); + TEST (NULL, str_check (g_build_filename ("x", "", "y", NULL), "x"S"y")); + TEST (NULL, str_check (g_build_filename ("x", S, "y", NULL), "x"S"y")); + TEST (NULL, str_check (g_build_filename ("x", S S, "y", NULL), "x"S"y")); TEST (NULL, str_check (g_build_filename ("x", "y", "z", NULL), "x"S"y"S"z")); TEST (NULL, str_check (g_build_filename (S"x"S, S"y"S, S"z"S, NULL), S"x"S"y"S"z"S)); TEST (NULL, str_check (g_build_filename (S S"x"S S, S S"y"S S, S S"z"S S, NULL), S S"x"S"y"S"z"S S)); |