From 8838c19c39e6fd5a65964c26adc55d3a5ccad946 Mon Sep 17 00:00:00 2001 From: Bruno Herbelin Date: Fri, 31 Dec 2021 13:16:00 +0100 Subject: [PATCH] String functions to wrap test or join lists --- BaseToolkit.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++++-- BaseToolkit.h | 6 ++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/BaseToolkit.cpp b/BaseToolkit.cpp index 0d231bd..d215f26 100644 --- a/BaseToolkit.cpp +++ b/BaseToolkit.cpp @@ -114,6 +114,44 @@ std::string BaseToolkit::unspace(const std::string &input) return output; } +std::string BaseToolkit::wrapped(const std::string &input, unsigned per_line) +{ + std::string text(input); + unsigned line_begin = 0; + + while (line_begin < text.size()) + { + const unsigned ideal_end = line_begin + per_line ; + unsigned line_end = ideal_end <= text.size() ? ideal_end : text.size()-1; + + if (line_end == text.size() - 1) + ++line_end; + else if (std::isspace(text[line_end])) + { + text[line_end] = '\n'; + ++line_end; + } + else // backtrack + { + unsigned end = line_end; + while ( end > line_begin && !std::isspace(text[end])) + --end; + + if (end != line_begin) + { + line_end = end; + text[line_end++] = '\n'; + } + else + text.insert(line_end++, 1, '\n'); + } + + line_begin = line_end; + } + + return text; +} + std::string BaseToolkit::byte_to_string(long b) { double numbytes = static_cast(b); @@ -161,17 +199,29 @@ std::string BaseToolkit::truncated(const std::string& str, int N) return trunc; } -std::list BaseToolkit::splitted(const std::string& str, char delim) { +std::list BaseToolkit::splitted(const std::string& str, char delim) +{ std::list strings; size_t start = 0; size_t end = 0; while ((start = str.find_first_not_of(delim, end)) != std::string::npos) { end = str.find(delim, start); - strings.push_back(str.substr(start - 1, end - start + 1)); + size_t delta = start > 0 ? 1 : 0; + strings.push_back(str.substr( start -delta, end - start + delta)); } return strings; } + +std::string BaseToolkit::joinned(std::list strlist, char separator) +{ + std::string str; + for (auto it = strlist.cbegin(); it != strlist.cend(); ++it) + str += (*it) + separator; + + return str; +} + bool BaseToolkit::is_a_number(const std::string& str, int *val) { bool isanumber = false; diff --git a/BaseToolkit.h b/BaseToolkit.h index dd9fc3f..cea23e7 100644 --- a/BaseToolkit.h +++ b/BaseToolkit.h @@ -19,6 +19,9 @@ std::string transliterate(const std::string &input); // replaces spaces by underscores in a string std::string unspace(const std::string &input); +// get a wrapped version of the input +std::string wrapped(const std::string &input, unsigned per_line); + // get a string to display memory size with unit KB, MB, GB, TB std::string byte_to_string(long b); @@ -31,6 +34,9 @@ std::string truncated(const std::string& str, int N); // split a string into list of strings separated by delimitor (e.g. /home/me/toto.mpg -> {home, me, toto.mpg} ) std::list splitted(const std::string& str, char delim); +// rebuilds a splitted string +std::string joinned(std::list strlist, char separator = ' '); + // returns true if the string bool is_a_number(const std::string& str, int *val = nullptr);