Advertisement
kambala

D2 string

Aug 9th, 2021
1,179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.25 KB | None | 0 0
  1. #include <algorithm>
  2. #include <functional>
  3. #include <iostream>
  4. #include <numeric>
  5. #include <queue>
  6. #include <stack>
  7. #include <string>
  8. #include <vector>
  9.  
  10. using namespace std;
  11.  
  12. string substring(const string& s, string::size_type begin, string::size_type end) {
  13.   return s.substr(begin, (end == string::npos ? s.size() : end) - begin);
  14. }
  15.  
  16. string joined(const vector<string>& stringList, const char* joiner = "\n")
  17. {
  18.     assert(!stringList.empty());
  19.     return accumulate(next(stringList.cbegin()), stringList.cend(), stringList.front(), [&](const string& s1, const string& s2) {
  20.         return s1 + joiner + s2;
  21.     });
  22. }
  23.  
  24. vector<string> split(const string& s, const string& separator = "\n", const function<string(string)>& lineTransform = [](string&& s){ return s; })
  25. {
  26.   vector<string> lines;
  27.   assert(!separator.empty());
  28.   string::size_type findPos = 0;
  29.   for (;;) {
  30.     auto separatorPos = s.find(separator, findPos);
  31.     lines.emplace_back(lineTransform(substring(s, findPos, separatorPos)));
  32.     if (separatorPos == string::npos)
  33.       break;
  34.     findPos = separatorPos + separator.size();
  35.   }
  36.   return lines;
  37. }
  38.  
  39. string buildColoredString(const string& s, const string& color)
  40. {
  41.   return "<span style='color: " + color + "'>" + s + "</span>";
  42. }
  43.  
  44. struct ColoredEntry {
  45.   string text;
  46.   string color;
  47.   bool addLineBreak;
  48. };
  49.  
  50. int main(int argc, char const *argv[])
  51. {
  52.   for (int i = 1; i < argc; ++i) {
  53.     string text = argv[i];
  54.     string::size_type findPos = 0;
  55.     string color = "white";
  56.     vector<pair<string, string>> coloredParts;
  57.     for (;;) {
  58.       auto colorStartPos = text.find('\\', findPos);
  59.       coloredParts.emplace_back(color, substring(text, findPos, colorStartPos));
  60.  
  61.       if (colorStartPos == string::npos) {
  62.         break;
  63.       }
  64.  
  65.       // TODO: handle npos
  66.       auto colorEndPos = text.find(';', ++colorStartPos);
  67.       // if (colorEndPos == string::npos) {
  68.       //   continue;
  69.       // }
  70.       findPos = colorEndPos + 1;
  71.  
  72.       color = substring(text, colorStartPos, colorEndPos);
  73.     }
  74.     assert(!coloredParts.empty());
  75.  
  76.     stack<queue<ColoredEntry>> entries;
  77.     decltype(entries)::value_type* lastEntry = nullptr;
  78.     for (const auto& it : coloredParts) {
  79.       auto color = it.first;
  80.       auto lines = split(it.second);
  81.       for (const auto& line : lines) {
  82.         ColoredEntry e;
  83.         e.text = line;
  84.         e.color = color;
  85.         auto moveToQueue = [&](decltype(entries)::value_type& queue) {
  86.           queue.emplace(std::move(e));
  87.         };
  88.      
  89.         if (lastEntry) {
  90.           assert(!lastEntry->empty());
  91.           // prevents adding useless items with empty text that won't have line break
  92.           if (lastEntry->front().text.empty())
  93.             lastEntry->pop();
  94.           moveToQueue(*lastEntry);
  95.           lastEntry = nullptr;
  96.         } else {
  97.           decltype(entries)::value_type q;
  98.           moveToQueue(q);
  99.           entries.emplace(std::move(q));
  100.         }
  101.       }
  102.       lastEntry = &entries.top();
  103.     }
  104.     assert(!entries.empty());
  105.  
  106.     vector<ColoredEntry> output;
  107.     do {
  108.       auto& q = entries.top();
  109.       do {
  110.         auto& e = q.front();
  111.         e.addLineBreak = false;
  112.         output.emplace_back(std::move(e));
  113.         q.pop();
  114.       } while (!q.empty());
  115.       output.back().addLineBreak = true;
  116.       entries.pop();
  117.     } while (!entries.empty());
  118.     assert(!output.empty());
  119.     output.back().addLineBreak = false;
  120.  
  121.     // JSON
  122.     cout << "[\n";
  123.     for (const auto& it : output) {
  124.       cout << "\t{\n";
  125.       cout << "\t\t\"text\": " << "\"" << it.text << "\",\n";
  126.       cout << "\t\t\"color\": " << "\"" << it.color << "\",\n";
  127.       cout << "\t\t\"addLineBreak\": " << (it.addLineBreak ? "true" : "false") << "\n";
  128.       cout << "\t},\n";
  129.     }
  130.     cout << "]\n\n";
  131.  
  132.     /*
  133.     // HTML
  134.     cout << "&lt;<br />\n";
  135.     for (const auto& it : output) {
  136.       if (!it.text.empty())
  137.         cout << buildColoredString(it.text, it.color);
  138.       if (it.addLineBreak)
  139.         cout << "<br />\n";
  140.     }
  141.     cout << "\n<br />&gt;<br /><br /><br />\n\n";
  142.  
  143.     // plain text
  144.     for (const auto& it : output) {
  145.       cout << it.text;
  146.       if (it.addLineBreak)
  147.         cout << "\n";
  148.     }
  149.     cout << "\n";
  150.     */
  151.   }
  152.   return 0;
  153. }
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement