| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 | 
							- #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 repr(const std::string &source) {
 
-   std::string output = source;
 
-   replace(output, "\n", "\\n");
 
-   replace(output, "\r", "\\r");
 
-   replace(output, "\b", "\\b");
 
-   replace(output, "\x1b", "\\[");
 
-   high_ascii(output);
 
-   return output;
 
- }
 
- std::string clean_string(const std::string &source) {
 
-   std::string clean = source;
 
-   /*
 
-     replace(clean, "\n", "\\n");
 
-     replace(clean, "\r", "\\r");
 
-     replace(clean, "\b", "\\b");
 
-     replace(clean, "\x1b", "\\[");
 
-   */
 
-   replace(clean, "\n", "");
 
-   replace(clean, "\r", "");
 
-   // 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;
 
- }
 
- std::vector<std::string> split(const std::string &line, const std::string &by) {
 
-   std::string work = line;
 
-   std::vector<std::string> results;
 
-   size_t pos;
 
-   while ( (pos = work.find(by) ) != std::string::npos ) {
 
-     results.push_back(work.substr(0, pos));
 
-     work.erase(0, pos + by.length());
 
-   }
 
-   if (!work.empty())
 
-     results.push_back(work);
 
-   return results;
 
- }
 
- bool in(const std::string &line, const std::string &has) {
 
-   return (line.find(has) != std::string::npos);
 
- }
 
- bool startswith(const std::string &line, const std::string &has) {
 
-   return (line.substr(0, has.length()) == has);
 
- }
 
- bool endswith(const std::string &line, const std::string &has) {
 
-   if (line.length() < has.length()) return false;
 
-   return (line.substr(line.length() - has.length()) == has);
 
- }
 
 
  |