// examples/cgi translated by mycpp

// BEGIN mycpp output

#include "mycpp/runtime.h"

GLOBAL_STR(str0, "\\");
GLOBAL_STR(str1, "\\d+");
GLOBAL_STR(str2, "&");
GLOBAL_STR(str3, "&");
GLOBAL_STR(str4, "<");
GLOBAL_STR(str5, "&lt;");
GLOBAL_STR(str6, ">");
GLOBAL_STR(str7, "&gt;");
GLOBAL_STR(str8, "\"");
GLOBAL_STR(str9, "&quot;");
GLOBAL_STR(str10, "xo--xo");
GLOBAL_STR(str11, "s: %s");
GLOBAL_STR(str12, "escaped: %s");
GLOBAL_STR(str13, "<html>");
GLOBAL_STR(str14, "%s\n");
GLOBAL_STR(str15, "x");
GLOBAL_STR(str16, "X");
GLOBAL_STR(str17, "<html>");

namespace cgi {  // forward declare


}  // forward declare namespace cgi

namespace cgi {  // declare

extern BigStr* BACKSLASH;
extern BigStr* RAW_BACKSLASH;
BigStr* escape(BigStr* s, bool quote = false);
void run_tests();
void run_benchmarks();

}  // declare namespace cgi

namespace cgi {  // define

BigStr* BACKSLASH = str0;
BigStr* RAW_BACKSLASH = str1;

BigStr* escape(BigStr* s, bool quote) {
  s = s->replace(str2, str3);
  s = s->replace(str4, str5);
  s = s->replace(str6, str7);
  if (quote) {
    s = s->replace(str8, str9);
  }
  return s;
}

void run_tests() {
  BigStr* mystr = nullptr;
  mystr = str10;
  mylib::print_stderr(StrFormat("s: %s", mystr));
  mylib::print_stderr(StrFormat("escaped: %s", escape(str13, true)));
  mylib::print_stderr(StrFormat("%s\n", mystr->replace(str15, str16)));
}

void run_benchmarks() {
  int i;
  int n;
  i = 0;
  n = 1000000;
  while (i < n) {
    escape(str17, true);
    i = (i + 1);
    mylib::MaybeCollect();
  }
}

}  // define namespace cgi

int main(int argc, char **argv) {
  gHeap.Init();

  char* b = getenv("BENCHMARK");
  if (b && strlen(b)) {  // match Python's logic
    fprintf(stderr, "Benchmarking...\n");
    cgi::run_benchmarks();
  } else {
    cgi::run_tests();
  }

  gHeap.CleanProcessExit();
}