From f0f3e55cc66ec5a4e673372067819982c0d195e5 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Mon, 23 May 2011 20:08:33 -0400 Subject: [PATCH] Text Handling - Implemented nontrivial (), which detemines whether a string contains any non-space characters. Used to detect strings with no content. --- src/text.cpp | 10 ++++++++++ src/text.h | 1 + test/text.t.cpp | 16 +++++++++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/text.cpp b/src/text.cpp index 3ec018641..c491badb7 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -481,6 +481,16 @@ void guess ( } } +//////////////////////////////////////////////////////////////////////////////// +bool nontrivial (const std::string& input) +{ + for (size_t i = 0; i < input.length (); ++i) + if (!isspace (input[i])) + return true; + + return false; +} + //////////////////////////////////////////////////////////////////////////////// bool digitsOnly (const std::string& input) { diff --git a/src/text.h b/src/text.h index d226f9403..b82695ec4 100644 --- a/src/text.h +++ b/src/text.h @@ -52,6 +52,7 @@ std::string upperCase (const std::string&); std::string ucFirst (const std::string&); const char* optionalBlankLine (); void guess (const std::string&, std::vector&, std::string&); +bool nontrivial (const std::string&); bool digitsOnly (const std::string&); bool noSpaces (const std::string&); bool noVerticalSpace (const std::string&); diff --git a/test/text.t.cpp b/test/text.t.cpp index 20cf347b4..fe9941bda 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 (243); + UnitTest t (255); // void wrapText (std::vector & lines, const std::string& text, const int width) std::string text = "This is a test of the line wrapping code."; @@ -206,6 +206,7 @@ int main (int argc, char** argv) // std::string trimLeft (const std::string& in, const std::string& t /*= " "*/) t.is (trimLeft (""), "", "trimLeft '' -> ''"); + t.is (trimLeft (" "), "", "trimLeft ' ' -> ''"); t.is (trimLeft ("", " \t"), "", "trimLeft '' -> ''"); t.is (trimLeft ("xxx"), "xxx", "trimLeft 'xxx' -> 'xxx'"); t.is (trimLeft ("xxx", " \t"), "xxx", "trimLeft 'xxx' -> 'xxx'"); @@ -214,6 +215,7 @@ int main (int argc, char** argv) // std::string trimRight (const std::string& in, const std::string& t /*= " "*/) t.is (trimRight (""), "", "trimRight '' -> ''"); + t.is (trimRight (" "), "", "trimRight ' ' -> ''"); t.is (trimRight ("", " \t"), "", "trimRight '' -> ''"); t.is (trimRight ("xxx"), "xxx", "trimRight 'xxx' -> 'xxx'"); t.is (trimRight ("xxx", " \t"), "xxx", "trimRight 'xxx' -> 'xxx'"); @@ -222,6 +224,7 @@ int main (int argc, char** argv) // std::string trim (const std::string& in, const std::string& t /*= " "*/) t.is (trim (""), "", "trim '' -> ''"); + t.is (trim (" "), "", "trim ' ' -> ''"); t.is (trim ("", " \t"), "", "trim '' -> ''"); t.is (trim ("xxx"), "xxx", "trim 'xxx' -> 'xxx'"); t.is (trim ("xxx", " \t"), "xxx", "trim 'xxx' -> 'xxx'"); @@ -278,6 +281,17 @@ int main (int argc, char** argv) t.is (upperCase (""), "", "upperCase '' -> ''"); t.is (upperCase ("pre01_:POST"), "PRE01_:POST", "upperCase 'pre01_:POST' -> 'PRE01_:POST'"); + // bool nontrivial (const std::string&); + t.notok (nontrivial (""), "nontrivial '' -> false"); + t.notok (nontrivial (" "), "nontrivial ' ' -> false"); + t.notok (nontrivial ("\t\t"), "nontrivial '\\t\\t' -> false"); + t.notok (nontrivial (" \t \t"), "nontrivial ' \\t \\t' -> false"); + t.ok (nontrivial ("a"), "nontrivial 'a' -> true"); + t.ok (nontrivial (" a"), "nontrivial ' a' -> true"); + t.ok (nontrivial ("a "), "nontrivial 'a ' -> true"); + t.ok (nontrivial (" \t\ta"), "nontrivial ' \\t\\ta' -> true"); + t.ok (nontrivial ("a\t\t "), "nontrivial 'a\\t\\t ' -> true"); + // bool digitsOnly (const std::string&); t.ok (digitsOnly (""), "digitsOnly '' -> true"); t.ok (digitsOnly ("0"), "digitsOnly '0' -> true");