/* The MIT License (MIT) Copyright © 2020-2025 pacman64 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* You can build this command-line app by running c++ -Wall -s -O3 -march=native -mtune=native -flto -o ./dedup ./dedup.cpp */ #include #include #include #include #include #include #ifdef RED_ERRORS #define ERROR_STYLE "\x1b[38;2;204;0;0m" #ifdef __APPLE__ #define ERROR_STYLE "\x1b[31m" #endif #define RESET_STYLE "\x1b[0m" #else #define ERROR_STYLE "" #define RESET_STYLE "" #endif using namespace std; const string info = "" "dedup [files...]\n" "\n" "DEDUPlicate lines, emitting each distinct line once, when it first appears.\n" "Unlike `uniq`, this app doesn't require pre-sorted lines to work correctly,\n" "and keeps lines in their original order.\n" ""; typedef unordered_set string_set; void dedup(istream& in, string& line, string_set& seen, bool live) { if (live) { while (!cout.eof() && getline(in, line)) { if (seen.find(line) == seen.end()) { cout << line << endl; seen.insert(line); } } return; } while (!cout.eof() && getline(in, line)) { if (seen.find(line) == seen.end()) { cout << line << '\n'; seen.insert(line); } } } bool dedup(const char* path, string& line, string_set& seen, bool live) { ifstream f(path); if (!f.is_open()) { cerr << ERROR_STYLE "can't open file named '" << path << "'" RESET_STYLE << endl; return false; } dedup(f, line, seen, live); return true; } bool run(size_t argc, char** argv) { size_t start_args = 1; if (argc > start_args) { const string& s = argv[start_args]; if (s == "-h" || s == "-help" || s == "--h" || s == "--help") { cout << info; return true; } if (s == "--") { start_args++; } } string line; string_set seen; string_set files; size_t errors = 0; const bool live_lines = lseek(STDOUT_FILENO, 0, SEEK_CUR) != 0; // speed up IO: the stdio API isn't used directly, so there's no need to // synchronize with it ios_base::sync_with_stdio(false); for (size_t i = start_args; i < argc && !cout.eof(); i++) { string arg(argv[i]); // avoid handling any file more than once if (files.find(arg) != files.end()) { continue; } files.insert(arg); if (arg == "-") { dedup(cin, line, seen, live_lines); continue; } if (!dedup(argv[i], line, seen, live_lines)) { errors++; } } if (argc - start_args == 0) { dedup(cin, line, seen, live_lines); } if (!live_lines) { cout.flush(); } return errors == 0; } int main(int argc, char** argv) { try { return run(argc, argv) ? 0 : 1; } catch (const bad_alloc& e) { cerr << ERROR_STYLE "out of memory" RESET_STYLE << endl; return 2; } }