utils: Add remove_ctrl
Add a helper function to remove control characters / multi-spaces, and a test.
This commit is contained in:
@ -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>
|
||||
|
||||
@ -136,6 +136,22 @@ test_flatten ()
|
||||
test_cases (cases, [](auto s, auto f){ return utf8_flatten(s); });
|
||||
}
|
||||
|
||||
static void
|
||||
test_remove_ctrl ()
|
||||
{
|
||||
CaseVec cases = {
|
||||
{ "Foo\n\nbar", true, "Foo bar" },
|
||||
{ "", false, "" },
|
||||
{ " ", false, " " },
|
||||
{ "Hello World ", false, "Hello World " },
|
||||
{ "Ångström", false, "Ångström" },
|
||||
};
|
||||
|
||||
test_cases (cases, [](auto s, auto f){ return remove_ctrl(s); });
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void
|
||||
test_clean ()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user