From d022155e776c543e0cb8702f02b2938d6ae0a8d3 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Wed, 14 Jul 2021 09:41:55 +0200 Subject: [PATCH] Split util headers into interface and implementation sections --- src/util/file_utils.hpp | 4 +++- src/util/path_utils.hpp | 22 ++++++++++++++-------- src/util/string_utils.hpp | 29 +++++++++++++++++++---------- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/src/util/file_utils.hpp b/src/util/file_utils.hpp index b5fddda30..e0af9dd58 100644 --- a/src/util/file_utils.hpp +++ b/src/util/file_utils.hpp @@ -22,6 +22,8 @@ namespace util { +// --- Interface --- + void create_cachedir_tag(const std::string& dir); -} +} // namespace util diff --git a/src/util/path_utils.hpp b/src/util/path_utils.hpp index 17d422c4c..3e2625122 100644 --- a/src/util/path_utils.hpp +++ b/src/util/path_utils.hpp @@ -25,12 +25,25 @@ namespace util { +// --- Interface --- + // Return whether `path` is absolute. bool is_absolute_path(nonstd::string_view path); // Return whether `path` includes at least one directory separator. +bool is_full_path(nonstd::string_view path); + +// Split a list of paths (such as the content of $PATH on Unix platforms or +// %PATH% on Windows platforms) into paths. +std::vector split_path_list(nonstd::string_view path_list); + +// Make `path` an absolute path. +std::string to_absolute_path(nonstd::string_view path); + +// --- Inline implementations --- + inline bool -is_full_path(nonstd::string_view path) +is_full_path(const nonstd::string_view path) { #ifdef _WIN32 if (path.find('\\') != nonstd::string_view::npos) { @@ -40,11 +53,4 @@ is_full_path(nonstd::string_view path) return path.find('/') != nonstd::string_view::npos; } -// Split a list of paths (such as the content of $PATH on Unix platforms or -// %PATH% on Windows platforms) into paths. -std::vector split_path_list(nonstd::string_view path_list); - -// Make `path` an absolute path. -std::string to_absolute_path(nonstd::string_view path); - } // namespace util diff --git a/src/util/string_utils.hpp b/src/util/string_utils.hpp index ac27704e1..956de8fe2 100644 --- a/src/util/string_utils.hpp +++ b/src/util/string_utils.hpp @@ -30,12 +30,10 @@ namespace util { +// --- Interface --- + // Return true if `suffix` is a suffix of `string`. -inline bool -ends_with(const nonstd::string_view string, const nonstd::string_view suffix) -{ - return string.ends_with(suffix); -} +bool ends_with(nonstd::string_view string, nonstd::string_view suffix); // Parse a string into a signed integer. // @@ -77,22 +75,33 @@ std::pair> split_once(nonstd::string_view string, char split_char); // Return true if `prefix` is a prefix of `string`. +bool starts_with(const char* string, nonstd::string_view prefix); + +// Return true if `prefix` is a prefix of `string`. +bool starts_with(nonstd::string_view string, nonstd::string_view prefix); + +// Strip whitespace from left and right side of a string. +[[nodiscard]] std::string strip_whitespace(nonstd::string_view string); + +// --- Inline implementations --- + inline bool -starts_with(const char* string, const nonstd::string_view prefix) +ends_with(const nonstd::string_view string, const nonstd::string_view suffix) +{ + return string.ends_with(suffix); +} +inline bool +starts_with(const char* const string, const nonstd::string_view prefix) { // Optimized version of starts_with(string_view, string_view): avoid computing // the length of the string argument. return std::strncmp(string, prefix.data(), prefix.length()) == 0; } -// Return true if `prefix` is a prefix of `string`. inline bool starts_with(const nonstd::string_view string, const nonstd::string_view prefix) { return string.starts_with(prefix); } -// Strip whitespace from left and right side of a string. -[[nodiscard]] std::string strip_whitespace(nonstd::string_view string); - } // namespace util -- 2.47.3