<commit_before>import Sys;
import sys.io.Process;
import haxe.io.Input;
import haxe.io.Output;

class Bux
{

  private static var VERSION :String = "0.0.1";

  static public function main() {
    var bux = new Bux();
    bux.interpret(Sys.args());
    bux.begin();
  }

  private static var REGEX_COMMAND_LINE_ARGUMENT    :EReg = ~/^-(\w)|^--(\w+)/;
  private static var REGEX_REGULAR_EXPRESSION_INPUT :EReg = ~/\/(.+?)\/(\w+?)?/;
  private static var REGEX_COMMAND_REPLACE          :EReg = ~/\{(\d)\}/g;

  private var FLAG_DRY_RUN    :Bool = false;
  private var FLAG_LINE_INPUT :Bool = false;

  private var stdin  :Input;
  private var stdout :Output;

  /**
   * User inserted regular expression (required). Will be compared to
   * STDIN and injected in the user command.
   */
  private var userRegEx :EReg;

  /**
   * User defined command exec. This is the piece which will be injected with
   * matches and groups from the user regular expression.
   */
  private var commandString :String;

  /**
   * @constructor
   * bux instance
   */
  public function new(){
    stdout = Sys.stdout();
    stdin  = Sys.stdin();
  }

  public function interpret(args :Array<String>):Void {
    for (arg in args) {
      if (REGEX_COMMAND_LINE_ARGUMENT.match(arg)) {
        var flag :String = REGEX_COMMAND_LINE_ARGUMENT.matched(1);
        var long :String = REGEX_COMMAND_LINE_ARGUMENT.matched(2);

        var spec_arg :String = null;
        if (flag != null) {
          spec_arg = flag;
        } else {
          spec_arg = long;
        }
        switch (spec_arg) {
          case "v" | "version": printVersion();
          case "h" | "help":    printHelp();
          case "d" | "dry-run": FLAG_DRY_RUN    = true;
          case "l" | "lines":   FLAG_LINE_INPUT = true;
        }
      } else if (REGEX_REGULAR_EXPRESSION_INPUT.match(arg)) {
        var pattern :String = REGEX_REGULAR_EXPRESSION_INPUT.matched(1);
        var options :String = "g";
        try {
          options = REGEX_REGULAR_EXPRESSION_INPUT.matched(2);
        } catch (e :String) { }
        if (options == null) {
          options = "g";
        }
        userRegEx = new EReg(pattern, options);
      } else {
        commandString = REGEX_COMMAND_REPLACE.replace(arg, "$$$1");
      }
    }
    if (userRegEx == null) {
      printHelp(1);
    }
  }

  public function begin():Void {
    var regex   :EReg   = userRegEx;
    var command :String = commandString;
    var input   :String = stdin.readAll().toString();

    if (FLAG_LINE_INPUT == false) {
      exec(merge(input, regex, command));
    } else {
      var lines :Array<String> = input.split("\n");
      for (line in lines) {
        exec(merge(line, regex, command));
      }
    }
  }

  private function merge(input :String, regex :EReg, command :String):String {
    var output     :String = command;
    var foundMatch :Bool   = false;
    var indexRegex :EReg   = ~/\$(\d)/;
    regex.match(input);

    while(indexRegex.match(output)) {
      var groupIndex  :Int    = Std.parseInt(indexRegex.matched(1));
      var replacement :String = "";<commit_msg>Shorthand flags may now be defined together, i.e; -d -l == -dl<commit_after>import Sys;
import sys.io.Process;
import haxe.io.Input;
import haxe.io.Output;

class Bux
{

  private static var VERSION :String = "0.0.1";

  static public function main() {
    var bux = new Bux();
    bux.interpret(Sys.args());
    bux.begin();
  }

  private static var REGEX_COMMAND_LINE_ARGUMENT    :EReg = ~/^-(\w+)|^--(\w+)/;
  private static var REGEX_REGULAR_EXPRESSION_INPUT :EReg = ~/\/(.+?)\/(\w+?)?/;
  private static var REGEX_COMMAND_REPLACE          :EReg = ~/\{(\d)\}/g;

  private var FLAG_DRY_RUN    :Bool = false;
  private var FLAG_LINE_INPUT :Bool = false;

  private var stdin  :Input;
  private var stdout :Output;

  /**
   * User inserted regular expression (required). Will be compared to
   * STDIN and injected in the user command.
   */
  private var userRegEx :EReg;

  /**
   * User defined command exec. This is the piece which will be injected with
   * matches and groups from the user regular expression.
   */
  private var commandString :String;

  /**
   * @constructor
   * bux instance
   */
  public function new(){
    stdout = Sys.stdout();
    stdin  = Sys.stdin();
  }

  public function interpret(args :Array<String>):Void {
    for (arg in args) {
      if (REGEX_COMMAND_LINE_ARGUMENT.match(arg)) {
        var flag :String = REGEX_COMMAND_LINE_ARGUMENT.matched(1);
        var long :String = REGEX_COMMAND_LINE_ARGUMENT.matched(2);
        if (flag != null) {
          if (flag.length > 1) {
            for (subFlag in flag.split("")) {
              registerFlag(subFlag);
            }
          } else {
            registerFlag(flag);
          }
        } else {
          registerFlag(long);
        }
      } else if (REGEX_REGULAR_EXPRESSION_INPUT.match(arg)) {
        var pattern :String = REGEX_REGULAR_EXPRESSION_INPUT.matched(1);
        var options :String = "g";
        try {
          options = REGEX_REGULAR_EXPRESSION_INPUT.matched(2);
        } catch (e :String) { }
        if (options == null) {
          options = "g";
        }
        userRegEx = new EReg(pattern, options);
      } else {
        commandString = REGEX_COMMAND_REPLACE.replace(arg, "$$$1");
      }
    }
    if (userRegEx == null) {
      printHelp(1);
    }
  }

  private function registerFlag(flag :String):Void {
    switch (flag) {
      case "v" | "version": printVersion();
      case "h" | "help":    printHelp();
      case "d" | "dry-run": FLAG_DRY_RUN    = true;
      case "l" | "lines":   FLAG_LINE_INPUT = true;
    }
  }

  public function begin():Void {
    var regex   :EReg   = userRegEx;
    var command :String = commandString;
    var input   :String = stdin.readAll().toString();

    if (FLAG_LINE_INPUT == false) {
      exec(merge(input, regex, command));
    } else {
      var lines :Array<String> = input.split("\n");
      for (line in lines) {
        exec(merge(line, regex, command));
      }
    }
  }

  private function merge(input :String, regex :EReg, command :String):String {
    var output     :String = command;
    var foundMatch :Bool   = false;
    var indexRegex :EReg   = ~/\$(\d)/;
    regex.match(input);

    while(indexRegex.match(output)) {
      var groupIndex  :Int    = Std.parseInt(indexRegex.matched(1));
      var replacement :String = "";
{'fraction_non_alphanumeric': 0.1321722904680454, 'fraction_numerical': 0.0032654330586222983, 'mean_word_length': 2.4122015915119364, 'pattern_counts': {'":': 8, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6209306', 'n_tokens_mistral': 2120, 'n_tokens_neox': 2081, 'n_words': 543}