utils: Add remove_ctrl

Add a helper function to remove control characters / multi-spaces, and a
test.
This commit is contained in:
Dirk-Jan C. Binnema
2021-03-16 16:51:01 +02:00
parent b7660ed33d
commit 725826231f
2 changed files with 32 additions and 0 deletions

View File

@ -147,6 +147,22 @@ Mu::utf8_clean (const std::string& dirty)
clean.erase (clean.find_last_not_of(" ") + 1); // remove trailing space
return clean;
std::string
Mu::remove_ctrl (const std::string& str)
{
char prev{'\0'};
std::string result;
result.reserve(str.length());
for (auto&& c: str) {
if (::iscntrl(c) || c == ' ') {
if (prev != ' ')
result += prev = ' ';
} else
result += prev = c;
}
return result;
}
std::vector<std::string>