From ddb3a4f079e154f637a986415c5af84eb11b2f93 Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Mon, 29 Apr 2024 18:25:50 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20utils:=20refactor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils.cpp | 29 ++++++++++++--------- src/utils.hpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 85 insertions(+), 16 deletions(-) diff --git a/src/utils.cpp b/src/utils.cpp index c5b4749..9cf546c 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -44,6 +44,7 @@ #include #include #include +#include #include #include @@ -329,25 +330,25 @@ bool prompt_char(const char* prompt, const char* color, char* read) noexcept { return false; } -auto make_multiline(const std::string_view& str, bool reverse, char delim) noexcept -> std::vector { - static constexpr auto functor = [](auto&& rng) { - return std::string_view(&*rng.begin(), static_cast(ranges::distance(rng))); - }; - static constexpr auto second = [](auto&& rng) { return rng != ""; }; - - auto&& view_res = str - | ranges::views::split(delim) - | ranges::views::transform(functor); - +auto make_multiline(std::string_view str, bool reverse, char delim) noexcept -> std::vector { std::vector lines{}; - ranges::for_each(view_res | ranges::views::filter(second), [&](auto&& rng) { lines.emplace_back(rng); }); + ranges::for_each(utils::make_split_view(str, delim), [&](auto&& rng) { lines.emplace_back(rng); }); if (reverse) { ranges::reverse(lines); } return lines; } -auto make_multiline(const std::vector& multiline, bool reverse, const std::string_view&& delim) noexcept -> std::string { +auto make_multiline_view(std::string_view str, bool reverse, char delim) noexcept -> std::vector { + std::vector lines{}; + ranges::for_each(utils::make_split_view(str, delim), [&](auto&& rng) { lines.emplace_back(rng); }); + if (reverse) { + ranges::reverse(lines); + } + return lines; +} + +auto make_multiline(const std::vector& multiline, bool reverse, std::string_view delim) noexcept -> std::string { std::string res{}; for (const auto& line : multiline) { res += line; @@ -361,6 +362,10 @@ auto make_multiline(const std::vector& multiline, bool reverse, con return res; } +auto join(const std::vector& lines, std::string_view delim) noexcept -> std::string { + return lines | ranges::views::join(delim) | ranges::to(); +} + // install a pkg in the live session if not installed void inst_needed(const std::string_view& pkg) noexcept { if (utils::exec(fmt::format(FMT_COMPILE("pacman -Qq {} &>/dev/null"), pkg), true) != "0") { diff --git a/src/utils.hpp b/src/utils.hpp index 9585404..b5a5c37 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -11,6 +11,29 @@ #include // for string_view #include // for vector +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wold-style-cast" +#pragma clang diagnostic ignored "-Wsign-conversion" +#elif defined(__GNUC__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wnull-dereference" +#pragma GCC diagnostic ignored "-Wuseless-cast" +#pragma GCC diagnostic ignored "-Wold-style-cast" +#endif + +#include +#include +#include +#include +#include + +#if defined(__clang__) +#pragma clang diagnostic pop +#elif defined(__GNUC__) +#pragma GCC diagnostic pop +#endif + #include namespace utils { @@ -18,8 +41,6 @@ namespace utils { [[nodiscard]] bool is_connected() noexcept; bool prompt_char(const char* prompt, const char* color = RESET, char* read = nullptr) noexcept; void clear_screen() noexcept; -[[nodiscard]] auto make_multiline(const std::string_view& str, bool reverse = false, char delim = '\n') noexcept -> std::vector; -[[nodiscard]] auto make_multiline(const std::vector& multiline, bool reverse = false, const std::string_view&& delim = "\n") noexcept -> std::string; void inst_needed(const std::string_view& pkg) noexcept; void secure_wipe() noexcept; void auto_partition() noexcept; @@ -84,6 +105,49 @@ void grub_mkconfig() noexcept; void enable_services() noexcept; void final_check() noexcept; +/// @brief Split a string into multiple lines based on a delimiter. +/// @param str The string to split. +/// @param reverse Flag indicating whether to reverse the order of the resulting lines. +/// @param delim The delimiter to split the string. +/// @return A vector of strings representing the split lines. +auto make_multiline(std::string_view str, bool reverse = false, char delim = '\n') noexcept -> std::vector; + +/// @brief Split a string into views of multiple lines based on a delimiter. +/// @param str The string to split. +/// @param reverse Flag indicating whether to reverse the order of the resulting lines. +/// @param delim The delimiter to split the string. +/// @return A vector of string views representing the split lines. +auto make_multiline_view(std::string_view str, bool reverse = false, char delim = '\n') noexcept -> std::vector; + +/// @brief Combine multiple lines into a single string using a delimiter. +/// @param multiline The lines to combine. +/// @param reverse Flag indicating whether to reverse the order of the lines before combining. +/// @param delim The delimiter to join the lines. +/// @return The combined lines as a single string. +auto make_multiline(const std::vector& multiline, bool reverse = false, std::string_view delim = "\n") noexcept -> std::string; + +/// @brief Join a vector of strings into a single string using a delimiter. +/// @param lines The lines to join. +/// @param delim The delimiter to join the lines. +/// @return The joined lines as a single string. +auto join(const std::vector& lines, std::string_view delim = "\n") noexcept -> std::string; + +/// @brief Make a split view from a string into multiple lines based on a delimiter. +/// @param str The string to split. +/// @param delim The delimiter to split the string. +/// @return A range view representing the split lines. +constexpr auto make_split_view(std::string_view str, char delim = '\n') noexcept { + constexpr auto functor = [](auto&& rng) { + return std::string_view(&*rng.begin(), static_cast(ranges::distance(rng))); + }; + constexpr auto second = [](auto&& rng) { return rng != ""; }; + + return str + | ranges::views::split(delim) + | ranges::views::transform(functor) + | ranges::views::filter(second); +} + template ::is_integer>> inline T to_int(const std::string_view& str) { @@ -118,7 +182,7 @@ constexpr inline double convert_unit(const double number, const std::string_view /// @param what The substring to replace. /// @param with The replacement string. /// @return The number of replacements made. -inline std::size_t replace_all(std::string& inout, const std::string_view& what, const std::string_view& with) noexcept { +inline std::size_t replace_all(std::string& inout, std::string_view what, std::string_view with) noexcept { std::size_t count{}; std::size_t pos{}; while (std::string::npos != (pos = inout.find(what.data(), pos, what.length()))) { @@ -132,7 +196,7 @@ inline std::size_t replace_all(std::string& inout, const std::string_view& what, /// @param inout The string to modify. /// @param what The substring to remove. /// @return The number of removals made. -inline std::size_t remove_all(std::string& inout, const std::string_view& what) noexcept { +inline std::size_t remove_all(std::string& inout, std::string_view what) noexcept { return replace_all(inout, what, ""); }