From ad05411350f1152cc3c86cebb5b8492d34507b33 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Mon, 9 Dec 2024 11:06:46 +0500 Subject: feat: an util for separating string vectors by text length --- bot/src/utils/string.cpp | 22 ++++++++++++++++++++++ bot/src/utils/string.hpp | 3 +++ 2 files changed, 25 insertions(+) diff --git a/bot/src/utils/string.cpp b/bot/src/utils/string.cpp index 71c06bf..0fd01ec 100644 --- a/bot/src/utils/string.cpp +++ b/bot/src/utils/string.cpp @@ -61,6 +61,28 @@ namespace bot { return false; } + + std::vector> separate_by_length( + const std::vector &vector, const int &max_length) { + std::vector> output; + std::vector active; + int length = 0; + + for (const std::string &str : vector) { + length += str.length(); + + if (length >= max_length) { + output.push_back(active); + active = {str}; + } else { + active.push_back(str); + } + } + + if (!active.empty()) output.push_back(active); + + return output; + } } } } diff --git a/bot/src/utils/string.hpp b/bot/src/utils/string.hpp index c8385ad..c1f0121 100644 --- a/bot/src/utils/string.hpp +++ b/bot/src/utils/string.hpp @@ -27,6 +27,9 @@ namespace bot { } bool string_contains_sql_injection(const std::string &input); + + std::vector> separate_by_length( + const std::vector &vector, const int &max_length); } } } -- cgit v1.2.3