String functions to wrap test or join lists

This commit is contained in:
Bruno Herbelin
2021-12-31 13:16:00 +01:00
parent f02a99a4e2
commit 8838c19c39
2 changed files with 58 additions and 2 deletions

View File

@@ -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<double>(b);
@@ -161,17 +199,29 @@ std::string BaseToolkit::truncated(const std::string& str, int N)
return trunc;
}
std::list<std::string> BaseToolkit::splitted(const std::string& str, char delim) {
std::list<std::string> BaseToolkit::splitted(const std::string& str, char delim)
{
std::list<std::string> 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<std::string> 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;

View File

@@ -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<std::string> splitted(const std::string& str, char delim);
// rebuilds a splitted string
std::string joinned(std::list<std::string> strlist, char separator = ' ');
// returns true if the string
bool is_a_number(const std::string& str, int *val = nullptr);