diff options
author | Joel Rosdahl <joel@rosdahl.net> | 2022-02-27 20:11:47 +0100 |
---|---|---|
committer | Joel Rosdahl <joel@rosdahl.net> | 2022-02-27 20:12:12 +0100 |
commit | 04870ac77e568cbd8609974a9914e0f6330cb008 (patch) | |
tree | 20d21a1c6077bed35c2bcefd1e01419215e3370b /src/Util.cpp | |
parent | 052277d8d4d24901892864679e2e1a55b0c5b47b (diff) | |
download | ccache-04870ac77e568cbd8609974a9914e0f6330cb008.tar.gz |
refactor: Make Util::is_absolute_path_with_prefix return optional
Diffstat (limited to 'src/Util.cpp')
-rw-r--r-- | src/Util.cpp | 50 |
1 files changed, 25 insertions, 25 deletions
diff --git a/src/Util.cpp b/src/Util.cpp index 007006cb..146ae7a9 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -233,31 +233,6 @@ base_name(string_view path) return n == std::string::npos ? path : path.substr(n + 1); } -bool -is_absolute_path_with_prefix(nonstd::string_view path, size_t& split_pos) -{ -#ifdef _WIN32 - const char delim[] = "/\\"; -#else - const char delim[] = "/"; -#endif - split_pos = path.find_first_of(delim); - if (split_pos != std::string::npos) { -#ifdef _WIN32 - // -I/C:/foo and -I/c/foo will already be handled by delim_pos - // correctly resulting in -I and /C:/foo or /c/foo respectively. - // -IC:/foo will not as we would get -IC: and /foo - if (split_pos > 0 && path[split_pos - 1] == ':') { - split_pos = split_pos - 2; - } -#endif - // this is not redundant on some platforms so nothing to simplify - // NOLINTNEXTLINE(readability-simplify-boolean-expr) - return true; - } - return false; -} - std::string change_extension(string_view path, string_view new_ext) { @@ -808,6 +783,31 @@ hard_link(const std::string& oldpath, const std::string& newpath) #endif } +nonstd::optional<size_t> +is_absolute_path_with_prefix(nonstd::string_view path) +{ +#ifdef _WIN32 + const char delim[] = "/\\"; +#else + const char delim[] = "/"; +#endif + auto split_pos = path.find_first_of(delim); + if (split_pos != std::string::npos) { +#ifdef _WIN32 + // -I/C:/foo and -I/c/foo will already be handled by delim_pos correctly + // resulting in -I and /C:/foo or /c/foo respectively. -IC:/foo will not as + // we would get -IC: and /foo. + if (split_pos > 0 && path[split_pos - 1] == ':') { + split_pos = split_pos - 2; + } +#endif + // This is not redundant on some platforms, so nothing to simplify. + // NOLINTNEXTLINE(readability-simplify-boolean-expr) + return split_pos; + } + return nonstd::nullopt; +} + #if defined(HAVE_LINUX_FS_H) || defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) int is_nfs_fd(int fd, bool* is_nfs) |