From 844c980bced9b76ef2764c8ae24b95e8720cfa27 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Mon, 5 Jul 2010 15:55:50 -0400 Subject: [PATCH] Enhancement - strippedLength - Added a text method that calculates a string length but does not include color control codes. --- src/tests/text.t.cpp | 10 +++++++++- src/text.cpp | 27 +++++++++++++++++++++++++++ src/text.h | 1 + 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/tests/text.t.cpp b/src/tests/text.t.cpp index 463be543c..a42896cf1 100644 --- a/src/tests/text.t.cpp +++ b/src/tests/text.t.cpp @@ -34,7 +34,7 @@ Context context; //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - UnitTest t (180); + UnitTest t (185); // void wrapText (std::vector & lines, const std::string& text, const int width) std::string text = "This is a test of the line wrapping code."; @@ -361,6 +361,14 @@ int main (int argc, char** argv) // Test start offset. t.is ((int) find ("one two three", "e", 3, true), (int) 11, "offset obeyed"); t.is ((int) find ("one two three", "e", 11, true), (int) 11, "offset obeyed"); + + // int strippedLength (const std::string&); + t.is (strippedLength (std::string ("")), 0, "strippedLength -> 0"); + t.is (strippedLength (std::string ("abc")), 3, "strippedLength abc -> 3"); + t.is (strippedLength (std::string ("one\033[5;38;255mtwo\033[0mthree")), 11, "strippedLength one^[[5;38;255mtwo^[[0mthree -> 11"); + t.is (strippedLength (std::string ("\033[0m")), 0, "strippedLength ^[[0m -> 0"); + t.is (strippedLength (std::string ("\033[1m\033[0m")), 0, "strippedLength ^[[1m^[[0m -> 0"); + return 0; } diff --git a/src/text.cpp b/src/text.cpp index 9116a5a95..2b7b0f9f9 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -568,3 +568,30 @@ std::string::size_type find ( } //////////////////////////////////////////////////////////////////////////////// +// Return the length, in characters, of the input, subtracting color control +// codes. +int strippedLength (const std::string& input) +{ + int length = input.length (); + bool inside = false; + int count = 0; + for (int i = 0; i < length; ++i) + { + if (inside) + { + if (input[i] == 'm') + inside = false; + } + else + { + if (input[i] == 033) + inside = true; + else + ++count; + } + } + + return count; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/text.h b/src/text.h index 4b488cda9..886c13ea6 100644 --- a/src/text.h +++ b/src/text.h @@ -57,6 +57,7 @@ bool isWordEnd (const std::string&, std::string::size_type); bool compare (const std::string&, const std::string&, bool sensitive = true); std::string::size_type find (const std::string&, const std::string&, bool sensitive = true); std::string::size_type find (const std::string&, const std::string&, std::string::size_type, bool sensitive = true); +int strippedLength (const std::string&); #endif ////////////////////////////////////////////////////////////////////////////////