mirror of
https://github.com/CachyOS/New-Cli-Installer.git
synced 2025-01-23 14:32:22 +08:00
👷 gucc: rewrite pacmanconf to reuse file content
don't make extra memory allocations: - don't allocate vector of strings to find locations - don't convert that vector of strings into new string previously: ``` heaptrack stats: allocations: 69 leaked allocations: 0 temporary allocations: 4 ``` after: ``` heaptrack stats: allocations: 32 leaked allocations: 0 temporary allocations: 5 ```
This commit is contained in:
parent
1e6bc8bab2
commit
b6d5c2760e
@ -1,5 +1,6 @@
|
|||||||
#include "gucc/pacmanconf_repo.hpp"
|
#include "gucc/pacmanconf_repo.hpp"
|
||||||
#include "gucc/file_utils.hpp"
|
#include "gucc/file_utils.hpp"
|
||||||
|
#include "gucc/string_utils.hpp"
|
||||||
|
|
||||||
#include <fstream> // for ofstream
|
#include <fstream> // for ofstream
|
||||||
#include <string> // for string
|
#include <string> // for string
|
||||||
@ -17,9 +18,9 @@
|
|||||||
#pragma GCC diagnostic ignored "-Wold-style-cast"
|
#pragma GCC diagnostic ignored "-Wold-style-cast"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <range/v3/algorithm/find_if.hpp>
|
||||||
#include <range/v3/range/conversion.hpp>
|
#include <range/v3/range/conversion.hpp>
|
||||||
#include <range/v3/view/filter.hpp>
|
#include <range/v3/view/filter.hpp>
|
||||||
#include <range/v3/view/join.hpp>
|
|
||||||
#include <range/v3/view/split.hpp>
|
#include <range/v3/view/split.hpp>
|
||||||
|
|
||||||
#if defined(__clang__)
|
#if defined(__clang__)
|
||||||
@ -31,33 +32,38 @@
|
|||||||
namespace gucc::detail::pacmanconf {
|
namespace gucc::detail::pacmanconf {
|
||||||
|
|
||||||
bool push_repos_front(std::string_view file_path, std::string_view value) noexcept {
|
bool push_repos_front(std::string_view file_path, std::string_view value) noexcept {
|
||||||
using StringVec = std::vector<std::string>;
|
|
||||||
auto&& file_content = file_utils::read_whole_file(file_path);
|
auto&& file_content = file_utils::read_whole_file(file_path);
|
||||||
if (file_content.empty()) {
|
if (file_content.empty()) {
|
||||||
spdlog::error("[PACMANCONFREPO] '{}' error occurred!", file_path);
|
spdlog::error("[PACMANCONFREPO] '{}' error occurred!", file_path);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
auto&& content_lines = file_content | ranges::views::split('\n') | ranges::to<std::vector<std::string>>();
|
|
||||||
for (std::size_t i = 1; i < content_lines.size(); ++i) {
|
|
||||||
const std::string_view current_line{content_lines[i - 1]};
|
|
||||||
if (current_line.empty() || current_line.starts_with('#') || !current_line.starts_with('[') || current_line.starts_with("[options]")) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto&& to_be_inserted = value | ranges::views::split('\n') | ranges::to<std::vector<std::string>>();
|
auto file_split_view = utils::make_split_view(file_content);
|
||||||
to_be_inserted.emplace_back("\n");
|
// Find the insertion point (using ranges for iteration)
|
||||||
content_lines.insert(content_lines.begin() + static_cast<StringVec::iterator::difference_type>(i - 1),
|
auto insertion_point_it = ranges::find_if(
|
||||||
std::move_iterator(to_be_inserted.begin()), std::move_iterator(to_be_inserted.end()));
|
file_split_view,
|
||||||
break;
|
[](auto&& rng) {
|
||||||
|
auto&& line = std::string_view(&*rng.begin(), static_cast<size_t>(ranges::distance(rng)));
|
||||||
|
return !line.empty() && !line.starts_with('#') && line.starts_with('[') && !line.starts_with("[options]");
|
||||||
|
});
|
||||||
|
|
||||||
|
// Handle case where insertion point is not found
|
||||||
|
if (insertion_point_it == ranges::end(file_split_view)) {
|
||||||
|
// No suitable insertion point found
|
||||||
|
spdlog::error("[PACMANCONFREPO] Could not find a suitable insertion point in {}", file_path);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto line = *insertion_point_it;
|
||||||
|
auto pos = file_content.find(line);
|
||||||
|
file_content.insert(pos - 1, std::string{'\n'} + std::string{value} + std::string{'\n'});
|
||||||
|
|
||||||
std::ofstream pacmanconf_file{file_path.data()};
|
std::ofstream pacmanconf_file{file_path.data()};
|
||||||
if (!pacmanconf_file.is_open()) {
|
if (!pacmanconf_file.is_open()) {
|
||||||
spdlog::error("[PACMANCONFREPO] '{}' open failed: {}", file_path, std::strerror(errno));
|
spdlog::error("[PACMANCONFREPO] '{}' open failed: {}", file_path, std::strerror(errno));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
std::string&& result = content_lines | ranges::views::join('\n') | ranges::to<std::string>();
|
pacmanconf_file << file_content;
|
||||||
pacmanconf_file << result;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,7 +98,6 @@ ParallelDownloads = 10
|
|||||||
[cachyos]
|
[cachyos]
|
||||||
Include = /etc/pacman.d/cachyos-mirrorlist
|
Include = /etc/pacman.d/cachyos-mirrorlist
|
||||||
|
|
||||||
|
|
||||||
[core]
|
[core]
|
||||||
Include = /etc/pacman.d/mirrorlist
|
Include = /etc/pacman.d/mirrorlist
|
||||||
|
|
||||||
@ -115,7 +114,8 @@ Include = /etc/pacman.d/mirrorlist
|
|||||||
#Include = /etc/pacman.d/mirrorlist
|
#Include = /etc/pacman.d/mirrorlist
|
||||||
|
|
||||||
[multilib]
|
[multilib]
|
||||||
Include = /etc/pacman.d/mirrorlist)";
|
Include = /etc/pacman.d/mirrorlist
|
||||||
|
)";
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
using namespace gucc;
|
using namespace gucc;
|
||||||
|
Loading…
Reference in New Issue
Block a user