File: dedup.cpp
   1 /*
   2 The MIT License (MIT)
   3 
   4 Copyright © 2020-2025 pacman64
   5 
   6 Permission is hereby granted, free of charge, to any person obtaining a copy of
   7 this software and associated documentation files (the “Software”), to deal
   8 in the Software without restriction, including without limitation the rights to
   9 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  10 of the Software, and to permit persons to whom the Software is furnished to do
  11 so, subject to the following conditions:
  12 
  13 The above copyright notice and this permission notice shall be included in all
  14 copies or substantial portions of the Software.
  15 
  16 THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22 SOFTWARE.
  23 */
  24 
  25 /*
  26 You can build this command-line app by running
  27 
  28 c++ -Wall -s -O3 -march=native -mtune=native -flto -o dedup dedup.cpp
  29 */
  30 
  31 #include <fstream>
  32 #include <iostream>
  33 #include <new>
  34 #include <string>
  35 #include <unistd.h>
  36 
  37 #if defined FLAT_SET
  38 #include <flat_set>
  39 typedef std::flat_set<std::string> string_set;
  40 #elif defined UNORDERED_SET
  41 #include <unordered_set>
  42 typedef std::unordered_set<std::string> string_set;
  43 #else
  44 #include <set>
  45 typedef std::set<std::string> string_set;
  46 #endif
  47 
  48 #ifdef RED_ERRORS
  49 #define ERROR_STYLE "\x1b[38;2;204;0;0m"
  50 #ifdef __APPLE__
  51 #define ERROR_STYLE "\x1b[31m"
  52 #endif
  53 #define RESET_STYLE "\x1b[0m"
  54 #else
  55 #define ERROR_STYLE ""
  56 #define RESET_STYLE ""
  57 #endif
  58 
  59 using namespace std;
  60 
  61 const string info = ""
  62 "dedup [files...]\n"
  63 "\n"
  64 "DEDUPlicate lines, emitting each distinct line once, on first occurrence.\n"
  65 "Unlike `uniq`, it doesn't require pre-sorted lines to work correctly, and\n"
  66 "keeps lines in their original order."
  67 "";
  68 
  69 void handle_input_faster(istream& in, string& line, string_set& seen) {
  70     while (!cout.eof() && getline(in, line)) {
  71         if (seen.find(line) == seen.end()) {
  72             cout << line << '\n';
  73             seen.insert(line);
  74         }
  75     }
  76 
  77     // cout.flush();
  78 }
  79 
  80 void handle_input(istream& in, string& line, string_set& seen, bool live) {
  81     if (!live) {
  82         handle_input_faster(in, line, seen);
  83         return;
  84     }
  85 
  86     while (!cout.eof() && getline(in, line)) {
  87         if (seen.find(line) == seen.end()) {
  88             cout << line << endl;
  89             seen.insert(line);
  90         }
  91     }
  92 }
  93 
  94 bool handle_file(const char* path, string& line, string_set& seen, bool live) {
  95     ifstream f(path);
  96     if (f.is_open()) {
  97         handle_input(f, line, seen, live);
  98         return true;
  99     }
 100 
 101     cerr << ERROR_STYLE "can't open file named '" << path << "'" RESET_STYLE << endl;
 102     return false;
 103 }
 104 
 105 // run returns the number of errors
 106 int run(size_t argc, char** argv, bool live_lines) {
 107     string line;
 108     string_set seen;
 109 
 110     size_t start_args = 1;
 111     if (argc > start_args) {
 112         const string& s = argv[start_args];
 113         if (s == "-h" || s == "-help" || s == "--h" || s == "--help") {
 114             cout << info;
 115             return 0;
 116         }
 117         if (s == "--") {
 118             start_args++;
 119         }
 120     }
 121 
 122     if (argc > start_args) {
 123         const string& s = argv[start_args];
 124         if (s == "--") {
 125             start_args++;
 126         }
 127     }
 128 
 129     cin.tie(NULL);
 130     ios_base::sync_with_stdio(false);
 131 
 132     size_t dashes = 0;
 133     size_t errors = 0;
 134 
 135     for (size_t i = start_args; i < argc && !cout.eof(); i++) {
 136         if (argv[i][0] == '-' && argv[i][1] == 0) {
 137             dashes++;
 138             if (dashes < 2) {
 139                 handle_input(cin, line, seen, live_lines);
 140             }
 141             continue;
 142         }
 143 
 144         if (!handle_file(argv[i], line, seen, live_lines)) {
 145             errors++;
 146         }
 147     }
 148 
 149     if (argc < 2) {
 150         handle_input(cin, line, seen, live_lines);
 151     }
 152 
 153     if (!live_lines) {
 154         cout.flush();
 155     }
 156     return errors;
 157 }
 158 
 159 int main(int argc, char** argv) {
 160     const bool live_lines = lseek(fileno(stdout), 0, SEEK_CUR) != 0;
 161     if (!live_lines) {
 162         setvbuf(stdout, NULL, _IOFBF, 0);
 163     }
 164 
 165     try {
 166         return run(argc, argv, live_lines) == 0 ? 0 : 1;
 167     } catch (const bad_alloc& e) {
 168         cerr << ERROR_STYLE "out of memory" RESET_STYLE << endl;
 169         return 1;
 170     }
 171 }