From 57dc36fb988dba4166f06582f136f583dc916a2e Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 13 Jun 2023 14:36:32 +1000 Subject: [PATCH] BLI_path: add BLI_path_slash_skip utility function Avoids having to add inline loops that step over slashes. --- source/blender/blenlib/BLI_path_util.h | 4 ++++ source/blender/blenlib/intern/path_util.c | 13 ++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/source/blender/blenlib/BLI_path_util.h b/source/blender/blenlib/BLI_path_util.h index cf0a34a6199..f17ab3a3c2d 100644 --- a/source/blender/blenlib/BLI_path_util.h +++ b/source/blender/blenlib/BLI_path_util.h @@ -265,6 +265,10 @@ int BLI_path_slash_ensure(char *path, size_t path_maxncpy) ATTR_NONNULL(1); * Removes the last slash and everything after it to the end of path, if there is one. */ void BLI_path_slash_rstrip(char *path) ATTR_NONNULL(1); +/** + * \return the next non-slash character or the null byte (when `path` only contains slashes). + */ +const char *BLI_path_slash_skip(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT; /** * Changes to the path separators to the native ones for this OS. */ diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c index 13574d2bae0..cd0c5f23bc2 100644 --- a/source/blender/blenlib/intern/path_util.c +++ b/source/blender/blenlib/intern/path_util.c @@ -1118,9 +1118,7 @@ bool BLI_path_abs(char path[FILE_MAX], const char *basepath) BLI_assert(strlen(tmp) == root_dir_len); /* Step over the slashes at the beginning of the path. */ - while (BLI_path_slash_is_native_compat(*p)) { - p++; - } + p = (char *)BLI_path_slash_skip(p); BLI_strncpy(tmp + root_dir_len, p, sizeof(tmp) - root_dir_len); } else { @@ -1958,6 +1956,15 @@ void BLI_path_slash_rstrip(char *path) } } +const char *BLI_path_slash_skip(const char *path) +{ + /* This accounts for a null byte too. */ + while (BLI_path_slash_is_native_compat(*path)) { + path++; + } + return path; +} + void BLI_path_slash_native(char *path) { #ifdef WIN32