From 56ccd16730b38365434ea3da896588fa5dbdf10e Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Mon, 30 May 2011 21:22:26 -0400 Subject: [PATCH] Helpers - Implemented longestLine, which finds the longest line in a string. --- src/text.cpp | 27 +++++++++++++++++++++++++++ src/text.h | 1 + test/text.t.cpp | 15 ++++++++++----- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/text.cpp b/src/text.cpp index 8367ab49e..df2d56ae4 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -283,6 +283,33 @@ int longestWord (const std::string& input) 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) { diff --git a/src/text.h b/src/text.h index ec0ce1d03..bd38556f2 100644 --- a/src/text.h +++ b/src/text.h @@ -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 unquoteText (const std::string&); int longestWord (const std::string&); +int longestLine (const std::string&); void extractLine (std::string&, std::string&, int); void splitq (std::vector&, const std::string&, const char); void split (std::vector&, const std::string&, const char); diff --git a/test/text.t.cpp b/test/text.t.cpp index a5ec1e7ea..f904638a6 100644 --- a/test/text.t.cpp +++ b/test/text.t.cpp @@ -35,7 +35,7 @@ Context context; //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - UnitTest t (255); + UnitTest t (258); // void wrapText (std::vector & lines, const std::string& text, const int width) std::string text = "This is a test of the line wrapping code."; @@ -250,10 +250,15 @@ int main (int argc, char** argv) 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"); + 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"); + + // 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) t.is (commify (""), "", "commify '' -> ''");