From 88d1943221df8da8beaa72e5129d5aa8a64a7c67 Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Fri, 8 Aug 2025 12:39:32 +0200 Subject: [PATCH] Change to use sets instead of vectors This simplifies the editing handlers. --- src/main.cpp | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index effbdb0..2e723c3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -28,34 +28,32 @@ using namespace std; set additions; string change; map changes; -vector deletions; +set deletions; bool should_list = false; -static void handle_additions(vector& cur_tags) { - set tags {cur_tags.begin(), cur_tags.end()}; +static void handle_additions(set& tags) { tags.insert(additions.begin(), additions.end()); - cur_tags.clear(); - copy(tags.begin(), tags.end(), back_inserter(cur_tags)); } -static void handle_changes(vector& tags) { +static void handle_changes(set& tags) { for (const auto& elem : changes) { - auto it = find(tags.begin(), tags.end(), elem.first); + auto it = tags.find(elem.first); if (it != tags.end()) { - *it = elem.second; + tags.erase(*it); + tags.insert(elem.second); } } } -static void handle_deletions(vector& tags) { - for (const string& tag : deletions) { - tags.erase(remove(tags.begin(), tags.end(), tag), tags.end()); - } +static void handle_deletions(set& tags) { + set_difference(tags.begin(), tags.end(), + deletions.begin(), deletions.end(), + inserter(tags, tags.begin())); } static void process(const char* path) { const char* tagname {"user.xdg.tags"}; - vector tags; + set tags; vector buf(1024); ssize_t sz = 0; do { @@ -72,24 +70,31 @@ static void process(const char* path) { } while (sz == -1 && errno == ERANGE); if (sz > 0) { string val(buf.data(), sz); - tags = sc::split(val, ","); + vector cur_tags = sc::split(val, ","); + tags.insert(cur_tags.begin(), cur_tags.end()); } bool edits = additions.size() > 0 || changes.size() > 0 || deletions.size() > 0; if (edits) { if (should_list) { cout << path << '\n'; - cout << " before: " << sc::io::greenf << sc::join(tags, ", ") << sc::io::reset << '\n'; + cout << " before: " << sc::io::greenf + << sc::join(tags.begin(), tags.end(), ", ") + << sc::io::reset << '\n'; } handle_deletions(tags); handle_changes(tags); handle_additions(tags); if (should_list) { - cout << " after: " << sc::io::yellowf << sc::join(tags, ", ") << sc::io::reset << '\n'; + cout << " after: " << sc::io::yellowf + << sc::join(tags.begin(), tags.end(), ", ") + << sc::io::reset << '\n'; } - string joined {sc::join(tags, ",")}; + string joined {sc::join(tags.begin(), tags.end(), ",")}; throw_if_min1(setxattr(path, tagname, joined.c_str(), joined.size(), 0)); } else { - cout << path << ": " << sc::io::yellowf << sc::join(tags, ", ") << sc::io::reset << '\n'; + cout << path << ": " << sc::io::yellowf + << sc::join(tags.begin(), tags.end(), ", ") + << sc::io::reset << '\n'; } } @@ -131,7 +136,7 @@ int main(int argc, char* argv[]) { } case 'a': additions.insert(arg); break; case 'c': change = arg; break; - case 'd': deletions.push_back(arg); break; + case 'd': deletions.insert(arg); break; case 'h': print_help(); return EXIT_SUCCESS; case 'i': changes.emplace(change, arg); break; case 'l': should_list = true; break;