File: erase.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 erase [regular expressions...]
  27 
  28 Ignore/remove all matches of all the regexes given.
  29 */
  30 
  31 // g++ -Wall -O2 -s -o erase erase.cpp
  32 
  33 #include <iostream>
  34 #include <regex>
  35 #include <vector>
  36 
  37 using namespace std;
  38 
  39 const auto regex_opts = regex_constants::ECMAScript;
  40 
  41 int compile(int argc, char** argv, vector<regex>& res) {
  42     size_t errors = 0;
  43 
  44     for (int i = 1; i < argc; i++) {
  45         try {
  46             regex expr(argv[i], regex_opts);
  47             res.push_back(expr);
  48         } catch (regex_error& e) {
  49             cerr << "\x1b[31m" << e.code() << ": " << e.what() << "\x1b[0m" << endl;
  50             errors++;
  51         }
  52     }
  53 
  54     return errors;
  55 }
  56 
  57 int main(int argc, char** argv) {
  58     cin.tie(NULL);
  59     ios_base::sync_with_stdio(false);
  60 
  61     vector<regex> expressions;
  62     size_t errors = compile(argc, argv, expressions);
  63     if (errors > 0) {
  64         return 1;
  65     }
  66 
  67     string line;
  68     while (getline(cin, line)) {
  69         for (regex e : expressions) {
  70             if (regex_search(line, e)) {
  71                 string s = regex_replace(line, e, "");
  72                 line.clear();
  73                 line.append(s);
  74             }
  75         }
  76 
  77         cout << line << endl;
  78         if (cout.eof()) {
  79             break;
  80         }
  81     }
  82 
  83     return 0;
  84 }