| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 | #include "utils.h"#include <regex>#include <string>#include <vector>bool replace(std::string &str, const std::string &from, const std::string &to) {  size_t start_pos = str.find(from);  if (start_pos == std::string::npos) return false;  do {    str.replace(start_pos, from.length(), to);  } while ((start_pos = str.find(from)) != std::string::npos);  return true;}bool replace(std::string &str, const char *from, const char *to) {  size_t start_pos = str.find(from);  if (start_pos == std::string::npos) return false;  do {    str.replace(start_pos, strlen(from), to);  } while ((start_pos = str.find(from)) != std::string::npos);  return true;}void ansi_clean(std::string &str) {  static std::regex ansi_cleaner("\x1b\[[0-9;]*[A-Zmh]",                                 std::regex_constants::ECMAScript);  str = std::regex_replace(str, ansi_cleaner, "");}void high_ascii(std::string &str) {  // the + replaces all of them into one.  I want each high ascii replaced with  // #.  static std::regex high_cleaner("[\x80-\xff]",                                 std::regex_constants::ECMAScript);  str = std::regex_replace(str, high_cleaner, "#");}std::smatch ansi_newline(const std::string &str) {  static std::regex ansi_nl("\x1b\[[0-9;]*[JK]",                            std::regex_constants::ECMAScript);  std::smatch m;  std::regex_search(str, m, ansi_nl);  return m;}std::string clean_string(const std::string &source) {  std::string clean = source;  replace(clean, "\n", "\\n");  replace(clean, "\r", "\\r");  replace(clean, "\b", "\\b");  // ANSI too  ansi_clean(clean);  // BUGZ_LOG(error) << "cleaned: " << clean;  high_ascii(clean);  replace(clean, "\x1b", "^");  return clean;}std::vector<std::string> split(const std::string &line) {  static std::regex rx_split("[^\\s]+");  std::vector<std::string> results;  for (auto it = std::sregex_iterator(line.begin(), line.end(), rx_split);       it != std::sregex_iterator(); ++it) {    results.push_back(it->str());  }  return results;}
 |