Skip to content

Commit

Permalink
🧹 gucc: refactor initcpio
Browse files Browse the repository at this point in the history
use string_view literals instead of raw string literals
use make_split_view instead of allocating memory
  • Loading branch information
vnepogodin committed Jul 2, 2024
1 parent 7bcd6bb commit 1e6bc8b
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions gucc/src/initcpio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#pragma GCC diagnostic pop
#endif

using namespace std::string_view_literals;

namespace gucc::detail {

bool Initcpio::write() const noexcept {

Check failure on line 36 in gucc/src/initcpio.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/gucc/src/initcpio.cpp:36:16 [bugprone-exception-escape

an exception may be thrown in function 'write' which should not throw exceptions

Check failure on line 36 in gucc/src/initcpio.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/gucc/src/initcpio.cpp:36:16 [modernize-use-trailing-return-type

use a trailing return type for this function
Expand All @@ -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)) {

Check failure on line 50 in gucc/src/initcpio.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/gucc/src/initcpio.cpp:50:17 [readability-else-after-return

do not use 'else' after 'return'
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));
Expand Down Expand Up @@ -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);

Check failure on line 86 in gucc/src/initcpio.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/gucc/src/initcpio.cpp:86:124 [cppcoreguidelines-pro-bounds-pointer-arithmetic

do not use pointer arithmetic

auto&& input_data = line.substr(open_bracket_pos + 1, static_cast<std::size_t>(length));
Expand All @@ -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));

Check failure on line 97 in gucc/src/initcpio.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/gucc/src/initcpio.cpp:97:34 [performance-move-const-arg

std::move of the variable 'line' of the trivially-copyable type 'ranges::detail::iterator_associated_types_base_<ranges::adaptor_cursor<ranges::basic_iterator<ranges::adaptor_cursor<ranges::detail::split_outer_iterator<ranges::split_view<std::basic_string_view<char>, ranges::single_view<char>>, true>, ranges::iter_transform_view<ranges::split_view<std::basic_string_view<char>, ranges::single_view<char>>, ranges::indirected<(lambda at /home/runner/work/New-Cli-Installer/New-Cli-Installer/gucc/include/gucc/string_utils.hpp:67:30)>>::adaptor<false>>>, ranges::remove_if_view<ranges::transform_view<ranges::split_view<std::basic_string_view<char>, ranges::single_view<char>>, (lambda at /home/runner/work/New-Cli-Installer/New-Cli-Installer/gucc/include/gucc/string_utils.hpp:67:30)>, ranges::logical_negate<(lambda at /home/runner/work/New-Cli-Installer/New-Cli-Installer/gucc/include/gucc/string_utils.hpp:70:29)>>::adaptor>, true>::const_reference_t' (aka 'std::basic_string_view<char>') has no effect; remove std::move()
} else if (line.starts_with("FILES"sv)) {
files = parse_line(std::move(line));

Check failure on line 99 in gucc/src/initcpio.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/gucc/src/initcpio.cpp:99:32 [performance-move-const-arg

std::move of the variable 'line' of the trivially-copyable type 'ranges::detail::iterator_associated_types_base_<ranges::adaptor_cursor<ranges::basic_iterator<ranges::adaptor_cursor<ranges::detail::split_outer_iterator<ranges::split_view<std::basic_string_view<char>, ranges::single_view<char>>, true>, ranges::iter_transform_view<ranges::split_view<std::basic_string_view<char>, ranges::single_view<char>>, ranges::indirected<(lambda at /home/runner/work/New-Cli-Installer/New-Cli-Installer/gucc/include/gucc/string_utils.hpp:67:30)>>::adaptor<false>>>, ranges::remove_if_view<ranges::transform_view<ranges::split_view<std::basic_string_view<char>, ranges::single_view<char>>, (lambda at /home/runner/work/New-Cli-Installer/New-Cli-Installer/gucc/include/gucc/string_utils.hpp:67:30)>, ranges::logical_negate<(lambda at /home/runner/work/New-Cli-Installer/New-Cli-Installer/gucc/include/gucc/string_utils.hpp:70:29)>>::adaptor>, true>::const_reference_t' (aka 'std::basic_string_view<char>') has no effect; remove std::move()
} else if (line.starts_with("HOOKS"sv)) {
hooks = parse_line(std::move(line));

Check failure on line 101 in gucc/src/initcpio.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/gucc/src/initcpio.cpp:101:32 [performance-move-const-arg

std::move of the variable 'line' of the trivially-copyable type 'ranges::detail::iterator_associated_types_base_<ranges::adaptor_cursor<ranges::basic_iterator<ranges::adaptor_cursor<ranges::detail::split_outer_iterator<ranges::split_view<std::basic_string_view<char>, ranges::single_view<char>>, true>, ranges::iter_transform_view<ranges::split_view<std::basic_string_view<char>, ranges::single_view<char>>, ranges::indirected<(lambda at /home/runner/work/New-Cli-Installer/New-Cli-Installer/gucc/include/gucc/string_utils.hpp:67:30)>>::adaptor<false>>>, ranges::remove_if_view<ranges::transform_view<ranges::split_view<std::basic_string_view<char>, ranges::single_view<char>>, (lambda at /home/runner/work/New-Cli-Installer/New-Cli-Installer/gucc/include/gucc/string_utils.hpp:67:30)>, ranges::logical_negate<(lambda at /home/runner/work/New-Cli-Installer/New-Cli-Installer/gucc/include/gucc/string_utils.hpp:70:29)>>::adaptor>, true>::const_reference_t' (aka 'std::basic_string_view<char>') has no effect; remove std::move()
}
}

Expand Down

0 comments on commit 1e6bc8b

Please sign in to comment.