- Implemented longestLine, which finds the longest line in a string.
This commit is contained in:
Paul Beckingham 2011-05-30 21:22:26 -04:00
parent 6f17e80461
commit 56ccd16730
3 changed files with 38 additions and 5 deletions

View file

@ -283,6 +283,33 @@ int longestWord (const std::string& input)
return longest; 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;
}
if (length > longest)
longest = length;
return longest;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void extractLine (std::string& text, std::string& line, int length) void extractLine (std::string& text, std::string& line, int length)
{ {

View file

@ -39,6 +39,7 @@ 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&); int longestWord (const std::string&);
int longestLine (const std::string&);
void extractLine (std::string&, std::string&, int); void extractLine (std::string&, std::string&, int);
void splitq (std::vector<std::string>&, const std::string&, const char); void splitq (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 char);

View file

@ -35,7 +35,7 @@ Context context;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv) int main (int argc, char** argv)
{ {
UnitTest t (255); UnitTest t (258);
// 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.";
@ -255,6 +255,11 @@ int main (int argc, char** argv)
t.is (longestWord ("this is a better test"), 6, "longestWord (this is a better test) --> 6"); 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"); t.is (longestWord ("house Çirçös clown"), 6, "longestWord (Çirçös) --> 6");
// int longestLine (const std::string&)
t.is (longestLine ("one two three four"), 18, "longestLine (one two three four) --> 18");
t.is (longestLine ("one\ntwo three four"), 14, "longestLine (one\\ntwo three four) --> 14");
t.is (longestLine ("one\ntwo\nthree\nfour"), 5, "longestLine (one\\ntwo\\nthree\\nfour) --> 5");
// 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'");