From 0f2ac0c4311e4429bfa4ede1d96ce467b5dceb5b Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Thu, 5 Jan 2023 10:37:41 +0100 Subject: Added tests --- stringhelper.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 stringhelper.cpp (limited to 'stringhelper.cpp') diff --git a/stringhelper.cpp b/stringhelper.cpp new file mode 100644 index 0000000..af37499 --- /dev/null +++ b/stringhelper.cpp @@ -0,0 +1,66 @@ +#include "stringhelper.h" + +#include +#include + +#include + +std::string Reichwein::Stringhelper::strfmt(const char* fmt, ...) +{ + va_list args; + + va_start(args, fmt); + int size = std::vsnprintf(nullptr, 0, fmt, args); + va_end(args); + + std::string result(size, ' '); + + va_start(args, fmt); + std::vsnprintf(result.data(), size + 1, fmt, args); + va_end(args); + + return result; +} + +std::vector Reichwein::Stringhelper::split(std::string value, const std::string separators) +{ + std::vector result; + + size_t pos0 = 0; + size_t pos1 = 0; + while (pos0 < value.size()) { + pos1 = value.find_first_of(separators, pos0); + if (pos1 == std::string::npos) + pos1 = value.size(); + std::string part = value.substr(pos0, pos1 - pos0); + //std::cout << "DEBUG: " << part << std::endl << std::flush; + if (part != "") + result.push_back(part); + pos0 = value.find_first_not_of(separators, pos1); + if (pos0 == std::string::npos) + pos0 = value.size(); + } + + return result; +} + +std::string Reichwein::Stringhelper::join(std::vector vs, std::string separator) +{ + std::string s; + for (const auto& line : vs) { + if (s.size() > 0) + s += separator; + s += line; + } + + return s; +} + +bool Reichwein::Stringhelper::startsWithAnyOfLower(const std::string &s, const std::vector &list) { + for (const std::string& element : list) { + if (boost::algorithm::starts_with(boost::algorithm::to_lower_copy(s), boost::algorithm::to_lower_copy(element))) + return true; + } + return false; +} + -- cgit v1.2.3