/* 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 #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, on first occurrence.\n" "Unlike `uniq`, it doesn't require pre-sorted lines to work correctly, and\n" "keeps lines in their original order." ""; void handle_input_faster(istream& in, string& line, set& seen) { while (!cout.eof() && getline(in, line)) { if (seen.find(line) == seen.end()) { cout << line << '\n'; seen.insert(line); } } } void handle_input(istream& in, string& line, set& seen, bool live) { if (!live) { handle_input_faster(in, line, seen); return; } while (!cout.eof() && getline(in, line)) { if (seen.find(line) == seen.end()) { cout << line << endl; seen.insert(line); } } } bool handle_file(const char* path, string& line, set& seen, bool live) { ifstream f(path); if (f.is_open()) { handle_input(f, line, seen, live); return true; } cerr << ERROR_STYLE "can't open file named '" << path << "'" RESET_STYLE << endl; return false; } // run returns the number of errors int run(size_t argc, char** argv, bool live_lines) { string line; set seen; 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 0; } if (s == "--") { start_args++; } } if (argc > start_args) { const string& s = argv[start_args]; if (s == "--") { start_args++; } } cin.tie(NULL); ios_base::sync_with_stdio(false); size_t dashes = 0; size_t errors = 0; for (size_t i = start_args; i < argc && !cout.eof(); i++) { if (argv[i][0] == '-' && argv[i][1] == 0) { dashes++; if (dashes < 2) { handle_input(cin, line, seen, live_lines); } continue; } if (!handle_file(argv[i], line, seen, live_lines)) { errors++; } } if (argc - start_args == 0) { handle_input(cin, line, seen, live_lines); } if (!live_lines) { cout.flush(); } return errors; } int main(int argc, char** argv) { const bool live_lines = lseek(fileno(stdout), 0, SEEK_CUR) != 0; if (!live_lines) { setvbuf(stdout, NULL, _IOFBF, 0); } try { return run(argc, argv, live_lines) == 0 ? 0 : 1; } catch (const bad_alloc& e) { cerr << ERROR_STYLE "out of memory" RESET_STYLE << endl; return 1; } }