From 1e6bc8bab22097db4e541553c0f584d0c4168e76 Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Wed, 3 Jul 2024 01:41:24 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20gucc:=20refactor=20initcpio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit use string_view literals instead of raw string literals use make_split_view instead of allocating memory --- gucc/src/initcpio.cpp | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/gucc/src/initcpio.cpp b/gucc/src/initcpio.cpp index e727199..d4509cb 100644 --- a/gucc/src/initcpio.cpp +++ b/gucc/src/initcpio.cpp @@ -29,6 +29,8 @@ #pragma GCC diagnostic pop #endif +using namespace std::string_view_literals; + namespace gucc::detail { bool Initcpio::write() const noexcept { @@ -41,15 +43,15 @@ bool Initcpio::write() const noexcept { | ranges::views::transform([&](auto&& rng) { /* clang-format off */ auto&& line = std::string_view(&*rng.begin(), static_cast(ranges::distance(rng))); - if (line.starts_with("MODULES")) { + if (line.starts_with("MODULES"sv)) { auto&& formatted_modules = modules | ranges::views::join(' ') | ranges::to(); return fmt::format(FMT_COMPILE("MODULES=({})"), std::move(formatted_modules)); - } else if (line.starts_with("FILES")) { + } else if (line.starts_with("FILES"sv)) { auto&& formatted_files = files | ranges::views::join(' ') | ranges::to(); return fmt::format(FMT_COMPILE("FILES=({})"), std::move(formatted_files)); - } else if (line.starts_with("HOOKS")) { + } else if (line.starts_with("HOOKS"sv)) { auto&& formatted_hooks = hooks | ranges::views::join(' ') | ranges::to(); return fmt::format(FMT_COMPILE("HOOKS=({})"), std::move(formatted_hooks)); @@ -77,10 +79,10 @@ bool Initcpio::parse_file() noexcept { return false; } - const auto& parse_line = [](auto&& line) -> std::vector { + const auto& parse_line = [](std::string_view line) -> std::vector { auto&& open_bracket_pos = line.find('('); auto&& close_bracket = ranges::find(line, ')'); - if (open_bracket_pos != std::string::npos && close_bracket != line.end()) { + if (open_bracket_pos != std::string_view::npos && close_bracket != line.end()) { const auto length = ranges::distance(line.begin() + static_cast(open_bracket_pos), close_bracket - 1); auto&& input_data = line.substr(open_bracket_pos + 1, static_cast(length)); @@ -89,14 +91,14 @@ bool Initcpio::parse_file() noexcept { return {}; }; - auto&& file_content_lines = utils::make_multiline(file_content); + auto&& file_content_lines = utils::make_split_view(file_content); for (auto&& line : file_content_lines) { - if (line.starts_with("MODULES")) { - modules = parse_line(line); - } else if (line.starts_with("FILES")) { - files = parse_line(line); - } else if (line.starts_with("HOOKS")) { - hooks = parse_line(line); + if (line.starts_with("MODULES"sv)) { + modules = parse_line(std::move(line)); + } else if (line.starts_with("FILES"sv)) { + files = parse_line(std::move(line)); + } else if (line.starts_with("HOOKS"sv)) { + hooks = parse_line(std::move(line)); } }