diff options
Diffstat (limited to 'bot/src/utils')
| -rw-r--r-- | bot/src/utils/string.cpp | 30 | ||||
| -rw-r--r-- | bot/src/utils/string.hpp | 5 |
2 files changed, 35 insertions, 0 deletions
diff --git a/bot/src/utils/string.cpp b/bot/src/utils/string.cpp index 0fd01ec..38087dc 100644 --- a/bot/src/utils/string.cpp +++ b/bot/src/utils/string.cpp @@ -1,5 +1,6 @@ #include "string.hpp" +#include <algorithm> #include <sstream> #include <string> #include <vector> @@ -83,6 +84,35 @@ namespace bot { return output; } + + std::vector<std::string> separate_by_length( + const std::string &base, const std::vector<std::string> &values, + const std::string &prefix, const std::string &separator, + const long long &max_length) { + std::vector<std::string> lines = {""}; + int index = 0; + + std::for_each(values.begin(), values.end(), + [&lines, &prefix, &separator, &base, &index, + &max_length](const std::string &v) { + const std::string &m = lines.at(index); + std::string x = prefix + v; + + if (base.length() + m.length() + x.length() + + separator.length() >= + max_length) { + index += 1; + } + + if (index > lines.size() - 1) { + lines.push_back(x); + } else { + lines[index] = m + separator + x; + } + }); + + return lines; + } } } } diff --git a/bot/src/utils/string.hpp b/bot/src/utils/string.hpp index c1f0121..18d4f9a 100644 --- a/bot/src/utils/string.hpp +++ b/bot/src/utils/string.hpp @@ -30,6 +30,11 @@ namespace bot { std::vector<std::vector<std::string>> separate_by_length( const std::vector<std::string> &vector, const int &max_length); + + std::vector<std::string> separate_by_length( + const std::string &base, const std::vector<std::string> &values, + const std::string &prefix, const std::string &separator, + const long long &max_length); } } } |
