Text Handling
- Implemented longestWord, a UTF8-aware helper for word-wrapping.
This commit is contained in:
24
src/text.cpp
24
src/text.cpp
@@ -215,6 +215,30 @@ std::string unquoteText (const std::string& input)
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
int longestWord (const std::string& input)
|
||||||
|
{
|
||||||
|
int longest = 0;
|
||||||
|
int length = 0;
|
||||||
|
std::string::size_type i;
|
||||||
|
int character;
|
||||||
|
|
||||||
|
while (character = utf8_next_char (input, i))
|
||||||
|
{
|
||||||
|
if (character == ' ')
|
||||||
|
{
|
||||||
|
if (length > longest)
|
||||||
|
longest = length;
|
||||||
|
|
||||||
|
length = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
++length;
|
||||||
|
}
|
||||||
|
|
||||||
|
return longest;
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
void extractLine (std::string& text, std::string& line, int length)
|
void extractLine (std::string& text, std::string& line, int length)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ std::string trimLeft (const std::string& in, const std::string& t = " ");
|
|||||||
std::string trimRight (const std::string& in, const std::string& t = " ");
|
std::string trimRight (const std::string& in, const std::string& t = " ");
|
||||||
std::string trim (const std::string& in, const std::string& t = " ");
|
std::string trim (const std::string& in, const std::string& t = " ");
|
||||||
std::string unquoteText (const std::string&);
|
std::string unquoteText (const std::string&);
|
||||||
|
int longestWord (const std::string&);
|
||||||
void extractLine (std::string&, std::string&, int);
|
void extractLine (std::string&, std::string&, int);
|
||||||
void split (std::vector<std::string>&, const std::string&, const char);
|
void split (std::vector<std::string>&, const std::string&, const char);
|
||||||
void split (std::vector<std::string>&, const std::string&, const std::string&);
|
void split (std::vector<std::string>&, const std::string&, const std::string&);
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ Context context;
|
|||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
int main (int argc, char** argv)
|
int main (int argc, char** argv)
|
||||||
{
|
{
|
||||||
UnitTest t (223);
|
UnitTest t (227);
|
||||||
|
|
||||||
// void wrapText (std::vector <std::string>& lines, const std::string& text, const int width)
|
// void wrapText (std::vector <std::string>& lines, const std::string& text, const int width)
|
||||||
std::string text = "This is a test of the line wrapping code.";
|
std::string text = "This is a test of the line wrapping code.";
|
||||||
@@ -239,6 +239,12 @@ int main (int argc, char** argv)
|
|||||||
t.is (unquoteText ("'x'"), "x", "unquoteText ''x'' -> 'x'");
|
t.is (unquoteText ("'x'"), "x", "unquoteText ''x'' -> 'x'");
|
||||||
t.is (unquoteText ("\"x\""), "x", "unquoteText '\"x\"' -> 'x'");
|
t.is (unquoteText ("\"x\""), "x", "unquoteText '\"x\"' -> 'x'");
|
||||||
|
|
||||||
|
// int longestWord (const std::string&)
|
||||||
|
t.is (longestWord (" "), 0, "longestWord ( ) --> 0");
|
||||||
|
t.is (longestWord ("this is a test"), 4, "longestWord (this is a test) --> 4");
|
||||||
|
t.is (longestWord ("this is a better test"), 6, "longestWord (this is a better test) --> 6");
|
||||||
|
t.is (longestWord ("house Çirçös clown"), 6, "longestWord (Çirçös) --> 6");
|
||||||
|
|
||||||
// std::string commify (const std::string& data)
|
// std::string commify (const std::string& data)
|
||||||
t.is (commify (""), "", "commify '' -> ''");
|
t.is (commify (""), "", "commify '' -> ''");
|
||||||
t.is (commify ("1"), "1", "commify '1' -> '1'");
|
t.is (commify ("1"), "1", "commify '1' -> '1'");
|
||||||
|
|||||||
Reference in New Issue
Block a user