Common: Added longestLine and longestWord

This commit is contained in:
Paul Beckingham 2016-01-05 16:24:39 -05:00
parent 90ccdc872f
commit 0e859d668c
2 changed files with 57 additions and 0 deletions

View file

@ -26,6 +26,7 @@
#include <cmake.h>
#include <text.h>
#include <utf8.h>
#include <iomanip>
#include <math.h>
@ -47,6 +48,60 @@ std::vector <std::string> split (const std::string& input, const char delimiter)
return results;
}
////////////////////////////////////////////////////////////////////////////////
int longestWord (const std::string& input)
{
int longest = 0;
int length = 0;
std::string::size_type i = 0;
int character;
while ((character = utf8_next_char (input, i)))
{
if (character == ' ')
{
if (length > longest)
longest = length;
length = 0;
}
else
length += mk_wcwidth (character);
}
if (length > longest)
longest = length;
return longest;
}
////////////////////////////////////////////////////////////////////////////////
int longestLine (const std::string& input)
{
int longest = 0;
int length = 0;
std::string::size_type i = 0;
int character;
while ((character = utf8_next_char (input, i)))
{
if (character == '\n')
{
if (length > longest)
longest = length;
length = 0;
}
else
length += mk_wcwidth (character);
}
if (length > longest)
longest = length;
return longest;
}
////////////////////////////////////////////////////////////////////////////////
const std::string format (std::string& value)
{

View file

@ -34,6 +34,8 @@
// text.cpp, Non-UTF-8 aware.
std::vector <std::string> split (const std::string&, const char);
int longestWord (const std::string&);
int longestLine (const std::string&);
const std::string format (std::string&);
const std::string format (const char*);
const std::string formatHex (int);