summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon McVittie <simon.mcvittie@collabora.co.uk>2023-02-10 15:41:31 +0000
committerSimon McVittie <simon.mcvittie@collabora.co.uk>2023-02-10 15:41:31 +0000
commit702e606f7397d046f588d20894456b4c2cfbd1dd (patch)
tree38d00fa90eefe952e16186c718e60e6aa9dfc92c
parent9ab080c6ce70c542690a29cc7a31aaabd58b5e6d (diff)
parentdef7c0ac10b456f98b822f06f618bc52f3cfaf4a (diff)
downloadglib-702e606f7397d046f588d20894456b4c2cfbd1dd.tar.gz
Merge branch 'wip/smcv/pathbuf-windows' into 'main'
pathbuf: Treat forward slashes and backslashes as equivalent on Windows Closes #2914 See merge request GNOME/glib!3264
-rw-r--r--glib/gpathbuf.c13
-rw-r--r--glib/tests/pathbuf.c7
2 files changed, 17 insertions, 3 deletions
diff --git a/glib/gpathbuf.c b/glib/gpathbuf.c
index ac359c33d..c7cf04849 100644
--- a/glib/gpathbuf.c
+++ b/glib/gpathbuf.c
@@ -288,9 +288,13 @@ g_path_buf_copy (GPathBuf *buf)
*
* If @path is absolute, it replaces the current path.
*
- * If @path contains `G_DIR_SEPARATOR_S`, the buffer is extended by
+ * If @path contains a directory separator, the buffer is extended by
* as many elements the path provides.
*
+ * On Windows, both forward slashes and backslashes are treated as
+ * directory separators. On other platforms, %G_DIR_SEPARATOR_S is the
+ * only directory separator.
+ *
* |[<!-- language="C" -->
* GPathBuf buf, cmp;
*
@@ -323,7 +327,11 @@ g_path_buf_push (GPathBuf *buf,
if (g_path_is_absolute (path))
{
+#ifdef G_OS_WIN32
+ char **elements = g_strsplit_set (path, "\\/", -1);
+#else
char **elements = g_strsplit (path, G_DIR_SEPARATOR_S, -1);
+#endif
#ifdef G_OS_UNIX
/* strsplit() will add an empty element for the leading root,
@@ -515,6 +523,9 @@ g_path_buf_set_extension (GPathBuf *buf,
*
* Retrieves the built path from the path buffer.
*
+ * On Windows, the result contains backslashes as directory separators,
+ * even if forward slashes were used in input.
+ *
* If the path buffer is empty, this function returns `NULL`.
*
* Returns: (transfer full) (type filename) (nullable): the path
diff --git a/glib/tests/pathbuf.c b/glib/tests/pathbuf.c
index e9104f15d..1f93ff5e8 100644
--- a/glib/tests/pathbuf.c
+++ b/glib/tests/pathbuf.c
@@ -88,9 +88,12 @@ test_pathbuf_init (void)
GPathBuf buf;
char *path;
- g_path_buf_init_from_path (&buf, "C:\\windows\\system32.dll");
+ /* Forward slashes and backslashes are treated as interchangeable
+ * on input... */
+ g_path_buf_init_from_path (&buf, "C:\\windows/system32.dll");
path = g_path_buf_clear_to_path (&buf);
g_assert_nonnull (path);
+ /* ... and normalized to backslashes on output */
g_assert_cmpstr (path, ==, "C:\\windows\\system32.dll");
g_free (path);
@@ -164,7 +167,7 @@ test_pathbuf_push_pop (void)
g_path_buf_push (&buf, "system32.dll");
g_test_message ("Popping a path component");
- g_path_buf_init_from_path (&cmp, "C:\\windows\\system32.dll");
+ g_path_buf_init_from_path (&cmp, "C:\\windows/system32.dll");
g_assert_path_buf_equal (&buf, &cmp);
g_path_buf_clear (&cmp);