From b4e4727887dfd91a23efac10cd56ea4c00bd00a9 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 11 Dec 2016 17:39:11 -0500 Subject: [PATCH] util: Migrated strippedLength from text --- src/text.cpp | 27 --------------------------- src/util.cpp | 27 +++++++++++++++++++++++++++ src/util.h | 1 + test/text.t.cpp | 9 +-------- test/util.t.cpp | 9 ++++++++- 5 files changed, 37 insertions(+), 36 deletions(-) diff --git a/src/text.cpp b/src/text.cpp index 3b64eea1c..214412026 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -53,30 +53,3 @@ const char* optionalBlankLine () } //////////////////////////////////////////////////////////////////////////////// -// 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/util.cpp b/src/util.cpp index ee6703cb7..66dfefe5c 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -328,3 +328,30 @@ bool nontrivial (const std::string& input) } //////////////////////////////////////////////////////////////////////////////// +// 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/util.h b/src/util.h index bce568846..845e0668d 100644 --- a/src/util.h +++ b/src/util.h @@ -63,6 +63,7 @@ const std::vector extractParents ( std::string osName (); const std::string obfuscateText (const std::string&); bool nontrivial (const std::string&); +int strippedLength (const std::string&); #endif //////////////////////////////////////////////////////////////////////////////// diff --git a/test/text.t.cpp b/test/text.t.cpp index dd4d980ec..b188d4bc3 100644 --- a/test/text.t.cpp +++ b/test/text.t.cpp @@ -37,14 +37,7 @@ Context context; //////////////////////////////////////////////////////////////////////////////// int main (int, char**) { - UnitTest t (5); - - // 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"); + UnitTest t (0); return 0; } diff --git a/test/util.t.cpp b/test/util.t.cpp index 03050d360..5c8104a71 100644 --- a/test/util.t.cpp +++ b/test/util.t.cpp @@ -36,7 +36,7 @@ Context context; //////////////////////////////////////////////////////////////////////////////// int main (int, char**) { - UnitTest t (19); + UnitTest t (24); // Ensure environment has no influence. unsetenv ("TASKDATA"); @@ -86,6 +86,13 @@ int main (int, char**) t.ok (nontrivial (" \t\ta"), "nontrivial ' \\t\\ta' -> true"); t.ok (nontrivial ("a\t\t "), "nontrivial 'a\\t\\t ' -> true"); + // 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; }