From 29c26b5bc570884804b09d7da4bb63d0ba6cf82f Mon Sep 17 00:00:00 2001 From: Guido Cella Date: Fri, 27 Mar 2026 16:34:29 +0100 Subject: [PATCH] misc/path_utils: add mp_basename_bstr() This will be used by bstr_get_ext() and bstr_split_ext() without allocating memory. --- misc/path_utils.c | 29 ++++++++++++++++++----------- misc/path_utils.h | 1 + 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/misc/path_utils.c b/misc/path_utils.c index 39a6c67a48..9397735f6b 100644 --- a/misc/path_utils.c +++ b/misc/path_utils.c @@ -39,22 +39,29 @@ #include #endif -const char *mp_basename(const char *path) +bstr mp_basename_bstr(bstr path) { - const char *s; + int separator_pos; #if HAVE_DOS_PATHS - if (!mp_is_url(bstr0(path))) { - s = strrchr(path, '\\'); - if (s) - path = s + 1; - s = strrchr(path, ':'); - if (s) - path = s + 1; + if (!mp_is_url(path)) { + separator_pos = bstrrchr(path, '\\'); + if (separator_pos >= 0) + path = bstr_splice(path, separator_pos + 1, path.len); + separator_pos = bstrrchr(path, ':'); + if (separator_pos >= 0) + path = bstr_splice(path, separator_pos + 1, path.len); } #endif - s = strrchr(path, '/'); - return s ? s + 1 : path; + separator_pos = bstrrchr(path, '/'); + if (separator_pos < 0) + return path; + return bstr_splice(path, separator_pos + 1, path.len); +} + +const char *mp_basename(const char *path) +{ + return mp_basename_bstr(bstr0(path)).start; } struct bstr mp_dirname(const char *path) diff --git a/misc/path_utils.h b/misc/path_utils.h index 2732f4e996..625f23dc9c 100644 --- a/misc/path_utils.h +++ b/misc/path_utils.h @@ -25,6 +25,7 @@ // Return pointer to filename part of path const char *mp_basename(const char *path); +bstr mp_basename_bstr(bstr path); /* Return file extension, excluding the '.'. If root is not NULL, set it to the * part of the path without extension. So: path == root + "." + extension