🧹 gucc: refactor initcpio

use string_view literals instead of raw string literals
use make_split_view instead of allocating memory
This commit is contained in:
Vladislav Nepogodin 2024-07-03 01:41:24 +04:00
parent 7bcd6bba4c
commit 1e6bc8bab2
No known key found for this signature in database
GPG Key ID: B62C3D10C54D5DA9

View File

@ -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<size_t>(ranges::distance(rng)));
if (line.starts_with("MODULES")) {
if (line.starts_with("MODULES"sv)) {
auto&& formatted_modules = modules | ranges::views::join(' ')
| ranges::to<std::string>();
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<std::string>();
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<std::string>();
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<std::string> {
const auto& parse_line = [](std::string_view line) -> std::vector<std::string> {
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<std::int64_t>(open_bracket_pos), close_bracket - 1);
auto&& input_data = line.substr(open_bracket_pos + 1, static_cast<std::size_t>(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));
}
}