mirror of
https://github.com/GothenburgBitFactory/timewarrior.git
synced 2025-07-07 20:06:39 +02:00
Common: Added longestLine and longestWord
This commit is contained in:
parent
90ccdc872f
commit
0e859d668c
2 changed files with 57 additions and 0 deletions
|
@ -26,6 +26,7 @@
|
||||||
|
|
||||||
#include <cmake.h>
|
#include <cmake.h>
|
||||||
#include <text.h>
|
#include <text.h>
|
||||||
|
#include <utf8.h>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
|
@ -47,6 +48,60 @@ std::vector <std::string> split (const std::string& input, const char delimiter)
|
||||||
return results;
|
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)
|
const std::string format (std::string& value)
|
||||||
{
|
{
|
||||||
|
|
|
@ -34,6 +34,8 @@
|
||||||
|
|
||||||
// text.cpp, Non-UTF-8 aware.
|
// text.cpp, Non-UTF-8 aware.
|
||||||
std::vector <std::string> split (const std::string&, const char);
|
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 (std::string&);
|
||||||
const std::string format (const char*);
|
const std::string format (const char*);
|
||||||
const std::string formatHex (int);
|
const std::string formatHex (int);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue