From cbd5810df1f051a56a052fd4ad48c9629e884d26 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Thu, 17 Apr 2025 03:03:03 +0500 Subject: feat: separate strings by length (new function) --- bot/src/utils/string.cpp | 30 ++++++++++++++++++++++++++++++ bot/src/utils/string.hpp | 5 +++++ 2 files changed, 35 insertions(+) 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 #include #include #include @@ -83,6 +84,35 @@ namespace bot { return output; } + + std::vector separate_by_length( + const std::string &base, const std::vector &values, + const std::string &prefix, const std::string &separator, + const long long &max_length) { + std::vector 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> separate_by_length( const std::vector &vector, const int &max_length); + + std::vector separate_by_length( + const std::string &base, const std::vector &values, + const std::string &prefix, const std::string &separator, + const long long &max_length); } } } -- cgit v1.2.3