util: Migrated strippedLength from text

This commit is contained in:
Paul Beckingham 2016-12-11 17:39:11 -05:00
parent 0027c9face
commit a0d88aaef8
5 changed files with 37 additions and 36 deletions

View file

@ -328,3 +328,30 @@ bool nontrivial (const std::string& input)
}
////////////////////////////////////////////////////////////////////////////////
// Return the length, in characters, of the input, subtracting color control
// codes.
int strippedLength (const std::string& input)
{
int length = input.length ();
bool inside = false;
int count = 0;
for (int i = 0; i < length; ++i)
{
if (inside)
{
if (input[i] == 'm')
inside = false;
}
else
{
if (input[i] == 033)
inside = true;
else
++count;
}
}
return count;
}
////////////////////////////////////////////////////////////////////////////////