From 28f9ed1d8d96c4c390ae68913320b339be213fbb Mon Sep 17 00:00:00 2001 From: Bruno Herbelin Date: Sat, 1 Jan 2022 10:12:50 +0100 Subject: [PATCH] Cleanup & new unwrapped function --- BaseToolkit.cpp | 22 +++++++++++++--------- BaseToolkit.h | 3 ++- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/BaseToolkit.cpp b/BaseToolkit.cpp index d215f26..70d2bb0 100644 --- a/BaseToolkit.cpp +++ b/BaseToolkit.cpp @@ -107,22 +107,26 @@ std::string BaseToolkit::transliterate(const std::string &input) std::string BaseToolkit::unspace(const std::string &input) { std::string output = input; - for(std::size_t i = 0; i < output.length(); ++i) { - if( isspace(output[i]) ) - output[i] = '_'; - } + std::replace( output.begin(), output.end(), ' ', '_'); return output; } -std::string BaseToolkit::wrapped(const std::string &input, unsigned per_line) +std::string BaseToolkit::unwrapped(const std::string &input) +{ + std::string output = input; + std::replace( output.begin(), output.end(), '\n', ' '); + return output; +} + +std::string BaseToolkit::wrapped(const std::string &input, size_t per_line) { std::string text(input); - unsigned line_begin = 0; + size_t 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; + const size_t ideal_end = line_begin + per_line ; + size_t line_end = ideal_end <= text.size() ? ideal_end : text.size()-1; if (line_end == text.size() - 1) ++line_end; @@ -133,7 +137,7 @@ std::string BaseToolkit::wrapped(const std::string &input, unsigned per_line) } else // backtrack { - unsigned end = line_end; + size_t end = line_end; while ( end > line_begin && !std::isspace(text[end])) --end; diff --git a/BaseToolkit.h b/BaseToolkit.h index cea23e7..06629ad 100644 --- a/BaseToolkit.h +++ b/BaseToolkit.h @@ -20,7 +20,8 @@ std::string transliterate(const std::string &input); std::string unspace(const std::string &input); // get a wrapped version of the input -std::string wrapped(const std::string &input, unsigned per_line); +std::string wrapped(const std::string &input, size_t per_line); +std::string unwrapped(const std::string &input); // get a string to display memory size with unit KB, MB, GB, TB std::string byte_to_string(long b);