From 93bbdeb4696ce47db7f12334656a3f393b32dfc2 Mon Sep 17 00:00:00 2001 From: Ricardo Abreu Date: Fri, 27 Oct 2023 23:53:08 +0100 Subject: [PATCH] [utils] Separate template declaration/definition For better readability. --- include/multipass/utils.h | 91 ++++++++++++++++++++++----------------- 1 file changed, 51 insertions(+), 40 deletions(-) diff --git a/include/multipass/utils.h b/include/multipass/utils.h index 533df41d32c..56e0a7337f7 100644 --- a/include/multipass/utils.h +++ b/include/multipass/utils.h @@ -101,52 +101,18 @@ bool valid_mac_address(const std::string& mac); // string helpers bool has_only_digits(const std::string& value); - -namespace detail -{ -auto is_space = static_cast(std::isspace); -} - template -Str&& trim_begin(Str&& s, Filter&& filter) -{ - const auto it = std::find_if_not(s.begin(), s.end(), std::forward(filter)); - s.erase(s.begin(), it); - return std::forward(s); -} - +Str&& trim_begin(Str&& s, Filter&& filter); template -Str&& trim_begin(Str&& s) -{ - return trim_begin(std::forward(s), detail::is_space); -} - +Str&& trim_begin(Str&& s); template -Str&& trim_end(Str&& s, Filter&& filter) -{ - auto rev_it = std::find_if_not(s.rbegin(), s.rend(), std::forward(filter)); - s.erase(rev_it.base(), s.end()); - return std::forward(s); -} - +Str&& trim_end(Str&& s, Filter&& filter); template -Str&& trim_end(Str&& s) -{ - return trim_end(std::forward(s), detail::is_space); -} - +Str&& trim_end(Str&& s); template -Str&& trim(Str&& s, Filter&& filter) -{ - return trim_begin(trim_end(std::forward(s), std::forward(filter))); -} - +Str&& trim(Str&& s, Filter&& filter); template -Str&& trim(Str&& s) -{ - return trim(std::forward(s), detail::is_space); -} - +Str&& trim(Str&& s); std::string& trim_newline(std::string& s); std::string escape_char(const std::string& s, char c); std::string escape_for_shell(const std::string& s); @@ -274,6 +240,51 @@ class Utils : public Singleton }; } // namespace multipass +namespace multipass::utils::detail +{ +inline constexpr auto is_space = static_cast(std::isspace); +} + +template +Str&& multipass::utils::trim_begin(Str&& s, Filter&& filter) +{ + const auto it = std::find_if_not(s.begin(), s.end(), std::forward(filter)); + s.erase(s.begin(), it); + return std::forward(s); +} + +template +Str&& multipass::utils::trim_begin(Str&& s) +{ + return trim_begin(std::forward(s), detail::is_space); +} + +template +Str&& multipass::utils::trim_end(Str&& s, Filter&& filter) +{ + auto rev_it = std::find_if_not(s.rbegin(), s.rend(), std::forward(filter)); + s.erase(rev_it.base(), s.end()); + return std::forward(s); +} + +template +Str&& multipass::utils::trim_end(Str&& s) +{ + return trim_end(std::forward(s), detail::is_space); +} + +template +Str&& multipass::utils::trim(Str&& s, Filter&& filter) +{ + return trim_begin(trim_end(std::forward(s), std::forward(filter))); +} + +template +Str&& multipass::utils::trim(Str&& s) +{ + return trim(std::forward(s), detail::is_space); +} + template void multipass::utils::try_action_for(OnTimeoutCallable&& on_timeout, std::chrono::milliseconds timeout, TryAction&& try_action, Args&&... args)