From fb38dca1db3dfc6ed5298b19f15af8648aa28b5c Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Thu, 5 Jan 2012 17:37:50 -0500 Subject: [PATCH] Parsing - Integrated modified Nibbler and test code from kronisk. These changes make both test and Nibbler standalone objects, with configurable features. --- src/Nibbler.cpp | 33 ++++++++++++++++++++++++++++----- src/Nibbler.h | 15 +++++++++++++++ test/nibbler.t.cpp | 29 ++++++++++++++++++++++++++++- test/test.cpp | 9 +++------ 4 files changed, 74 insertions(+), 12 deletions(-) diff --git a/src/Nibbler.cpp b/src/Nibbler.cpp index 565c1865a..332ee159c 100644 --- a/src/Nibbler.cpp +++ b/src/Nibbler.cpp @@ -25,20 +25,20 @@ // //////////////////////////////////////////////////////////////////////////////// - -#define L10N // Localization complete. - #include #include #include #include #include #include +#ifdef NIBBLER_FEATURE_DATE #include -#include +#endif +#ifdef NIBBLER_FEATURE_REGEX #include +#endif -const char* c_digits = "0123456789"; +const char* c_digits = "0123456789"; // TODO Not used? //////////////////////////////////////////////////////////////////////////////// Nibbler::Nibbler () @@ -139,6 +139,7 @@ bool Nibbler::getUntil (const std::string& terminator, std::string& result) } //////////////////////////////////////////////////////////////////////////////// +#ifdef NIBBLER_FEATURE_REGEX bool Nibbler::getUntilRx (const std::string& regex, std::string& result) { if (_cursor < _length) @@ -162,6 +163,7 @@ bool Nibbler::getUntilRx (const std::string& regex, std::string& result) return false; } +#endif //////////////////////////////////////////////////////////////////////////////// bool Nibbler::getUntilOneOf (const std::string& chars, std::string& result) @@ -529,6 +531,7 @@ bool Nibbler::getLiteral (const std::string& literal) } //////////////////////////////////////////////////////////////////////////////// +#ifdef NIBBLER_FEATURE_REGEX bool Nibbler::getRx (const std::string& regex, std::string& result) { if (_cursor < _length) @@ -553,6 +556,7 @@ bool Nibbler::getRx (const std::string& regex, std::string& result) return false; } +#endif //////////////////////////////////////////////////////////////////////////////// bool Nibbler::getUUID (std::string& result) @@ -685,6 +689,7 @@ bool Nibbler::getDateISO (time_t& t) } //////////////////////////////////////////////////////////////////////////////// +#ifdef NIBBLER_FEATURE_DATE bool Nibbler::getDate (const std::string& format, time_t& t) { std::string::size_type i = _cursor; @@ -989,6 +994,7 @@ bool Nibbler::getDate (const std::string& format, time_t& t) _cursor = i; return true; } +#endif //////////////////////////////////////////////////////////////////////////////// // Assumes that the options are sorted by decreasing length, so that if the @@ -1120,6 +1126,7 @@ bool Nibbler::skipWS () } //////////////////////////////////////////////////////////////////////////////// +#ifdef NIBBLER_FEATURE_REGEX bool Nibbler::skipRx (const std::string& regex) { if (_cursor < _length) @@ -1143,6 +1150,7 @@ bool Nibbler::skipRx (const std::string& regex) return false; } +#endif //////////////////////////////////////////////////////////////////////////////// bool Nibbler::skipAllOneOf (const std::string& chars) @@ -1219,6 +1227,21 @@ bool Nibbler::depleted () return false; } +//////////////////////////////////////////////////////////////////////////////// +// Override of ispunct, that considers #, $ and @ not to be punctuation. +// +// ispunct: ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~ +// Punctuation: ! " % & ' ( ) * + , - . / : ; < = > ? [ \ ] ^ _ ` { | } ~ +// delta: # $ @ +// +bool Nibbler::isPunctuation (char c) +{ + if (c == '@' || c == '#' || c == '$') + return false; + + return ispunct (c); +} + //////////////////////////////////////////////////////////////////////////////// std::string Nibbler::dump () { diff --git a/src/Nibbler.h b/src/Nibbler.h index a3c31b343..bed13ac45 100644 --- a/src/Nibbler.h +++ b/src/Nibbler.h @@ -29,6 +29,12 @@ #define INCLUDED_NIBBLER #define L10N // Localization complete. +#define NIBBLER_FEATURE_DATE +//#undef NIBBLER_FEATURE_DATE + +#define NIBBLER_FEATURE_REGEX +//#undef NIBBLER_FEATURE_REGEX + #include #include @@ -44,7 +50,9 @@ public: bool getUntil (char, std::string&); bool getUntil (const std::string&, std::string&); +#ifdef NIBBLER_FEATURE_REGEX bool getUntilRx (const std::string&, std::string&); +#endif bool getUntilOneOf (const std::string&, std::string&); bool getUntilWS (std::string&); bool getUntilEOL (std::string&); @@ -64,10 +72,14 @@ public: bool getNumber (double&); bool getUnsignedNumber (double&); bool getLiteral (const std::string&); +#ifdef NIBBLER_FEATURE_REGEX bool getRx (const std::string&, std::string&); +#endif bool getUUID (std::string&); bool getDateISO (time_t&); +#ifdef NIBBLER_FEATURE_DATE bool getDate (const std::string&, time_t&); +#endif bool getOneOf (const std::vector &, std::string&); bool getName (std::string&); bool getWord (std::string&); @@ -77,7 +89,9 @@ public: bool skipAll (char); bool skipAllOneOf (const std::string&); bool skipWS (); +#ifdef NIBBLER_FEATURE_REGEX bool skipRx (const std::string&); +#endif char next (); std::string next (const int quantity); @@ -89,6 +103,7 @@ public: bool depleted (); + static bool isPunctuation (char); std::string dump (); private: diff --git a/test/nibbler.t.cpp b/test/nibbler.t.cpp index 21fa00980..090ce02c3 100644 --- a/test/nibbler.t.cpp +++ b/test/nibbler.t.cpp @@ -26,8 +26,10 @@ //////////////////////////////////////////////////////////////////////////////// #include -#include #include +#ifdef NIBBLER_FEATURE_DATE +#include +#endif #include Context context; @@ -35,7 +37,19 @@ Context context; //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { +#ifdef NIBBLER_FEARURE_DATE +#ifdef NIBBLER_FEATURE_REGEX UnitTest t (292); +#else + UnitTest t (268); +#endif +#else +#ifdef NIBBLER_FEATURE_REGEX + UnitTest t (242); +#else + UnitTest t (218); +#endif +#endif try { @@ -44,7 +58,10 @@ int main (int argc, char** argv) int i; double d; time_t ti; + +#ifdef NIBBLER_FEATURE_DATE Date dt; +#endif std::vector options; // Make sure the nibbler behaves itself with trivial input. @@ -65,7 +82,9 @@ int main (int argc, char** argv) t.notok (n.getUntilEOL (s), "trivial: getUntilEOL"); t.notok (n.getUntilEOS (s), "trivial: getUntilEOS"); t.notok (n.getDateISO (ti), "trivial: getDateISO"); +#ifdef NIBBLER_FEATURE_DATE t.notok (n.getDate ("YYYYMMDD", ti), "trivial: getDate"); +#endif t.notok (n.getOneOf (options, s), "trivial: getOneOf"); t.ok (n.depleted (), "trivial: depleted"); @@ -81,6 +100,7 @@ int main (int argc, char** argv) t.notok (n.getUntil (' ', s), " '' : getUntil (' ') -> false"); t.ok (n.depleted (), " '' : depleted () -> true"); +#ifdef NIBBLER_FEATURE_REGEX // bool getUntilRx (const std::string&, std::string&); t.diag ("Nibbler::getUntilRx"); n = Nibbler ("one two"); @@ -95,6 +115,7 @@ int main (int argc, char** argv) t.ok (n.getUntilRx ("$", s), " 'two' : getUntilRx ('$') -> true"); t.is (s, "two", " 'two' : getUntilRx ('$') -> 'two'"); t.ok (n.depleted (), " '' : depleted () -> true"); +#endif // bool getUntilOneOf (const std::string&, std::string&); t.diag ("Nibbler::getUntilOneOf"); @@ -170,6 +191,7 @@ int main (int argc, char** argv) t.is (s, "foo", " 'foo' : getUntilEOS () -> 'foo'"); t.ok (n.depleted (), " '' : depleted () -> true"); +#ifdef NIBBLER_FEATURE_REGEX // bool skipRx (const std::string&); t.diag ("Nibbler::skipRx"); n = Nibbler ("one two"); @@ -178,6 +200,7 @@ int main (int argc, char** argv) t.ok (n.skipRx ("e+"), " 'e two' : skipRx ('e+') -> true"); t.ok (n.skipRx ("...."), " ' two' : skipRx ('....') -> true"); t.ok (n.depleted (), " '' : depleted () -> true"); +#endif // bool getQuoted (char, std::string&); t.diag ("Nibbler::getQuoted"); @@ -280,6 +303,7 @@ int main (int argc, char** argv) t.ok (n.getLiteral ("bar"), " 'bar' : getLiteral ('bar') -> true"); t.ok (n.depleted (), " '' : depleted () -> true"); +#ifdef NIBBLER_FEATURE_REGEX // bool getRx (const std::string&, std::string&); t.diag ("Nibbler::getRx"); n = Nibbler ("one two three"); @@ -293,6 +317,7 @@ int main (int argc, char** argv) t.ok (n.getRx ("th...", s), " 'three' : getRx ('th...') -> true"); t.is (s, "three", " 'three' : getRx ('th...') -> 'three'"); t.ok (n.depleted (), " '' : depleted () -> true"); +#endif // bool getUUID (std::string&); t.diag ("Nibbler::getUUID"); @@ -321,6 +346,7 @@ int main (int argc, char** argv) t.is (ti, 1234567890, "'20090213T233130Z': getDateISO () -> 1234567890"); t.ok (n.depleted (), "depleted"); +#ifdef NIBBLER_FEATURE_DATE // bool getDate (time_t&, const std::string&); t.diag ("Nibbler::getDate"); n = Nibbler ("1/1/2008"); @@ -401,6 +427,7 @@ int main (int argc, char** argv) t.is (dt.hour (), 12, "ctor (std::string) -> h"); t.is (dt.minute (), 34, "ctor (std::string) -> N"); t.is (dt.second (), 56, "ctor (std::string) -> S"); +#endif // bool getOneOf (const std::vector &, std::string&); t.diag ("Nibbler::getOneOf"); diff --git a/test/test.cpp b/test/test.cpp index 8ec6e4c12..eb3629e9f 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -29,9 +29,6 @@ #include #include #include -#include -#include -#include #include /////////////////////////////////////////////////////////////////////////////// @@ -394,9 +391,9 @@ void UnitTest::is ( /////////////////////////////////////////////////////////////////////////////// void UnitTest::diag (const std::string& text) { - std::string trimmed = trim (text, " \t\n\r\f"); - - std::cout << "# " << trimmed << "\n"; + std::string::size_type start = text.find_first_not_of (" \t\n\r\f"); + std::string::size_type end = text.find_last_not_of (" \t\n\r\f"); + std::cout << "# " << text.substr (start, end - start + 1); } ///////////////////////////////////////////////////////////////////////////////