From f1a0b842dc2469643c5e07903f52b5105130c959 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 26 Jun 2010 19:52:30 -0400 Subject: [PATCH] Nibbler upgrade - Merged Nibbler code from Tegelsten. - Updated unit tests. --- src/Nibbler.cpp | 272 ++++++++++++++++++++++------- src/Nibbler.h | 30 +++- src/tests/nibbler.t.cpp | 367 +++++++++++++++++++++++++--------------- 3 files changed, 469 insertions(+), 200 deletions(-) diff --git a/src/Nibbler.cpp b/src/Nibbler.cpp index 10b452ce8..09ead442a 100644 --- a/src/Nibbler.cpp +++ b/src/Nibbler.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // task - a command line task list manager. // -// Copyright 2006 - 2010, Paul Beckingham. +// Copyright 2006 - 2010, Paul Beckingham, Federico Hernandez. // All rights reserved. // // This program is free software; you can redistribute it and/or modify it under @@ -29,6 +29,7 @@ #include #include #include "Nibbler.h" +#include "rx.h" const char* c_digits = "0123456789"; @@ -37,6 +38,7 @@ Nibbler::Nibbler () : mInput ("") , mLength (0) , mCursor (0) +, mSaved (0) { } @@ -83,7 +85,7 @@ Nibbler::~Nibbler () } //////////////////////////////////////////////////////////////////////////////// -// Extract up until the next c, or EOS. +// Extract up until the next c (but not including) or EOS. bool Nibbler::getUntil (char c, std::string& result) { if (mCursor < mLength) @@ -129,6 +131,36 @@ bool Nibbler::getUntil (const std::string& terminator, std::string& result) return false; } +//////////////////////////////////////////////////////////////////////////////// +bool Nibbler::getUntilRx (const std::string& regex, std::string& result) +{ + if (mCursor < mLength) + { + std::string modified_regex; + if (regex[0] != '(') + modified_regex = "(" + regex + ")"; + else + modified_regex = regex; + + std::vector start; + std::vector end; + if (regexMatch (start, end, mInput.substr (mCursor), modified_regex, true)) + { + result = mInput.substr (mCursor, start[0]); + mCursor += start[0]; + } + else + { + result = mInput.substr (mCursor); + mCursor = mLength; + } + + return true; + } + + return false; +} + //////////////////////////////////////////////////////////////////////////////// bool Nibbler::getUntilOneOf (const std::string& chars, std::string& result) { @@ -153,67 +185,24 @@ bool Nibbler::getUntilOneOf (const std::string& chars, std::string& result) } //////////////////////////////////////////////////////////////////////////////// -bool Nibbler::skipN (const int quantity /* = 1 */) +bool Nibbler::getUntilWS (std::string& result) { - if (mCursor >= mLength) - return false; - - if (mCursor <= mLength - quantity) - { - mCursor += quantity; - return true; - } - - return false; + return this->getUntilOneOf (" \t\r\n\f", result); } //////////////////////////////////////////////////////////////////////////////// -bool Nibbler::skip (char c) +bool Nibbler::getUntilEOL (std::string& result) { - if (mCursor < mLength && - mInput[mCursor] == c) - { - ++mCursor; - return true; - } - - return false; + return getUntil ('\n', result); } //////////////////////////////////////////////////////////////////////////////// -bool Nibbler::skipAll (char c) +bool Nibbler::getUntilEOS (std::string& result) { if (mCursor < mLength) { - std::string::size_type i = mInput.find_first_not_of (c, mCursor); - if (i == mCursor) - return false; - - if (i == std::string::npos) - mCursor = mLength; // Yes, off the end. - else - mCursor = i; - - return true; - } - - return false; -} - -//////////////////////////////////////////////////////////////////////////////// -bool Nibbler::skipAllOneOf (const std::string& chars) -{ - if (mCursor < mLength) - { - std::string::size_type i = mInput.find_first_not_of (chars, mCursor); - if (i == mCursor) - return false; - - if (i == std::string::npos) - mCursor = mLength; // Yes, off the end. - else - mCursor = i; - + result = mInput.substr (mCursor); + mCursor = mLength; return true; } @@ -288,24 +277,175 @@ bool Nibbler::getUnsignedInt (int& result) } //////////////////////////////////////////////////////////////////////////////// -bool Nibbler::getUntilEOL (std::string& result) +bool Nibbler::getLiteral (const std::string& literal) { - return getUntil ('\n', result); -} - -//////////////////////////////////////////////////////////////////////////////// -bool Nibbler::getUntilEOS (std::string& result) -{ - if (mCursor < mLength) + if (mCursor < mLength && + mInput.find (literal, mCursor) == mCursor) { - result = mInput.substr (mCursor); - mCursor = mLength; + mCursor += literal.length (); return true; } return false; } +//////////////////////////////////////////////////////////////////////////////// +bool Nibbler::getRx (const std::string& regex, std::string& result) +{ + if (mCursor < mLength) + { + // Regex may be anchored to the beginning and include capturing parentheses, + // otherwise they are added. + std::string modified_regex; + if (regex.substr (0, 2) != "^(") + modified_regex = "^(" + regex + ")"; + else + modified_regex = regex; + + std::vector results; + if (regexMatch (results, mInput.substr (mCursor), modified_regex, true)) + { + result = results[0]; + mCursor += result.length (); + return true; + } + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Nibbler::skipN (const int quantity /* = 1 */) +{ + if (mCursor >= mLength) + return false; + + if (mCursor <= mLength - quantity) + { + mCursor += quantity; + return true; + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Nibbler::skip (char c) +{ + if (mCursor < mLength && + mInput[mCursor] == c) + { + ++mCursor; + return true; + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Nibbler::skipAll (char c) +{ + if (mCursor < mLength) + { + std::string::size_type i = mInput.find_first_not_of (c, mCursor); + if (i == mCursor) + return false; + + if (i == std::string::npos) + mCursor = mLength; // Yes, off the end. + else + mCursor = i; + + return true; + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Nibbler::skipWS () +{ + return this->skipAllOneOf (" \t\n\r\f"); +} + +//////////////////////////////////////////////////////////////////////////////// +bool Nibbler::skipRx (const std::string& regex) +{ + if (mCursor < mLength) + { + // Regex may be anchored to the beginning and include capturing parentheses, + // otherwise they are added. + std::string modified_regex; + if (regex.substr (0, 2) != "^(") + modified_regex = "^(" + regex + ")"; + else + modified_regex = regex; + + std::vector results; + if (regexMatch (results, mInput.substr (mCursor), modified_regex, true)) + { + mCursor += results[0].length (); + return true; + } + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Nibbler::skipAllOneOf (const std::string& chars) +{ + if (mCursor < mLength) + { + std::string::size_type i = mInput.find_first_not_of (chars, mCursor); + if (i == mCursor) + return false; + + if (i == std::string::npos) + mCursor = mLength; // Yes, off the end. + else + mCursor = i; + + return true; + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +// Peeks ahead - does not move cursor. +char Nibbler::next () +{ + if (mCursor < mLength) + return mInput[mCursor]; + + return '\0'; +} + +//////////////////////////////////////////////////////////////////////////////// +// Peeks ahead - does not move cursor. +std::string Nibbler::next (const int quantity) +{ + if ( mCursor < mLength && + (unsigned) quantity <= mLength && + mCursor <= mLength - quantity) + return mInput.substr (mCursor, quantity); + + return ""; +} + +//////////////////////////////////////////////////////////////////////////////// +void Nibbler::save () +{ + mSaved = mCursor; +} + +//////////////////////////////////////////////////////////////////////////////// +void Nibbler::restore () +{ + mCursor = mSaved; +} + //////////////////////////////////////////////////////////////////////////////// bool Nibbler::depleted () { @@ -316,3 +456,11 @@ bool Nibbler::depleted () } //////////////////////////////////////////////////////////////////////////////// +std::string Nibbler::dump () +{ + return std::string ("Nibbler ‹") + + mInput.substr (mCursor) + + "›"; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/Nibbler.h b/src/Nibbler.h index 40fa78c3e..8a5fddf23 100644 --- a/src/Nibbler.h +++ b/src/Nibbler.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // task - a command line task list manager. // -// Copyright 2006 - 2010, Paul Beckingham. +// Copyright 2006 - 2010, Paul Beckingham, Federico Hernandez. // All rights reserved. // // This program is free software; you can redistribute it and/or modify it under @@ -41,22 +41,40 @@ public: bool getUntil (char, std::string&); bool getUntil (const std::string&, std::string&); + bool getUntilRx (const std::string&, std::string&); bool getUntilOneOf (const std::string&, std::string&); + bool getUntilWS (std::string&); + bool getUntilEOL (std::string&); + bool getUntilEOS (std::string&); + + bool getQuoted (char, std::string&); + bool getInt (int&); + bool getUnsignedInt (int&); + bool getLiteral (const std::string&); + bool getRx (const std::string&, std::string&); + bool skipN (const int quantity = 1); bool skip (char); bool skipAll (char); bool skipAllOneOf (const std::string&); - bool getQuoted (char, std::string&); - bool getInt (int&); - bool getUnsignedInt (int&i); - bool getUntilEOL (std::string&); - bool getUntilEOS (std::string&); + bool skipWS (); + bool skipRx (const std::string&); + + char next (); + std::string next (const int quantity); + + void save (); + void restore (); + bool depleted (); + std::string dump (); + private: std::string mInput; std::string::size_type mLength; std::string::size_type mCursor; + std::string::size_type mSaved; }; #endif diff --git a/src/tests/nibbler.t.cpp b/src/tests/nibbler.t.cpp index d0a1042ed..3a1f754cd 100644 --- a/src/tests/nibbler.t.cpp +++ b/src/tests/nibbler.t.cpp @@ -33,157 +33,260 @@ Context context; //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - UnitTest t (81); + UnitTest t (140); - Nibbler n; - std::string s; - int i; + try + { + Nibbler n; + std::string s; + int i; - // Make sure the nibbler behaves itself with trivial input. - t.diag ("Test all nibbler calls given empty input"); - n = Nibbler (""); - t.notok (n.getUntil (' ', s), "trivial: getUntil"); - t.notok (n.getUntil ("hi", s), "trivial: getUntil"); - t.notok (n.getUntilOneOf ("ab", s), "trivial: getUntilOneOf"); - t.notok (n.skipN (123), "trivial: skipN"); - t.notok (n.skip ('x'), "trivial: skip"); - t.notok (n.skipAll ('x'), "trivial: skipAll"); - t.notok (n.skipAllOneOf ("abc"), "trivial: skipAllOneOf"); - t.notok (n.getQuoted ('"', s), "trivial: getQuoted"); - t.notok (n.getInt (i), "trivial: getInt"); - t.notok (n.getUnsignedInt (i), "trivial: getUnsignedInt"); - t.notok (n.getUntilEOL (s), "trivial: getUntilEOL"); -// - t.notok (n.getUntilEOS (s), "trivial: getUntilEOS"); - t.ok (n.depleted (), "trivial: depleted"); + // Make sure the nibbler behaves itself with trivial input. + t.diag ("Test all nibbler calls given empty input"); + n = Nibbler (""); + t.notok (n.getUntil (' ', s), "trivial: getUntil"); + t.notok (n.getUntil ("hi", s), "trivial: getUntil"); + t.notok (n.getUntilOneOf ("ab", s), "trivial: getUntilOneOf"); + t.notok (n.skipN (123), "trivial: skipN"); + t.notok (n.skip ('x'), "trivial: skip"); + t.notok (n.skipAll ('x'), "trivial: skipAll"); + t.notok (n.skipAllOneOf ("abc"), "trivial: skipAllOneOf"); + t.notok (n.getQuoted ('"', s), "trivial: getQuoted"); + t.notok (n.getInt (i), "trivial: getInt"); + t.notok (n.getUnsignedInt (i), "trivial: getUnsignedInt"); + t.notok (n.getUntilEOL (s), "trivial: getUntilEOL"); + t.notok (n.getUntilEOS (s), "trivial: getUntilEOS"); + t.ok (n.depleted (), "trivial: depleted"); - // bool getUntil (char, std::string&); - t.diag ("Nibbler::getUntil"); - n = Nibbler ("one two"); - t.ok (n.getUntil (' ', s), " 'one two' : getUntil (' ') -> true"); - t.is (s, "one", " 'one two' : getUntil (' ') -> 'one'"); - t.ok (n.getUntil (' ', s), " ' two' : getUntil (' ') -> true"); - t.is (s, "", " ' two' : getUntil (' ') -> ''"); - t.ok (n.skip (' '), " ' two' : skip (' ') -> true"); - t.ok (n.getUntil (' ', s), " 'two' : getUntil (' ') -> 'two'"); - t.notok (n.getUntil (' ', s), " '' : getUntil (' ') -> false"); - t.ok (n.depleted (), " '' : depleted () -> true"); + // bool getUntil (char, std::string&); + t.diag ("Nibbler::getUntil"); + n = Nibbler ("one two"); + t.ok (n.getUntil (' ', s), " 'one two' : getUntil (' ') -> true"); + t.is (s, "one", " 'one two' : getUntil (' ') -> 'one'"); + t.ok (n.getUntil (' ', s), " ' two' : getUntil (' ') -> true"); + t.is (s, "", " ' two' : getUntil (' ') -> ''"); + t.ok (n.skip (' '), " ' two' : skip (' ') -> true"); + t.ok (n.getUntil (' ', s), " 'two' : getUntil (' ') -> 'two'"); + t.notok (n.getUntil (' ', s), " '' : getUntil (' ') -> false"); + t.ok (n.depleted (), " '' : depleted () -> true"); - // bool getUntilOneOf (const std::string&, std::string&); - t.diag ("Nibbler::getUntilOneOf"); - n = Nibbler ("ab.cd"); - t.ok (n.getUntilOneOf (".:", s), " 'ab.cd' : getUntilOneOf ('.:') -> true"); - t.is (s, "ab", " 'ab.cd' : getUntilOneOf ('.:') -> 'ab'"); - t.ok (n.skipN (), " '.cd' : skipN () -> true"); - t.ok (n.getUntilOneOf (".:", s), " 'cd' : getUntilOneOf ('.:') -> true"); - t.notok (n.getUntilOneOf (".:", s), " '' : getUntilOneOf ('.:') -> false"); - t.ok (n.depleted (), " '' : depleted () -> true"); + // bool getUntilRx (const std::string&, std::string&); + t.diag ("Nibbler::getUntilRx"); + n = Nibbler ("one two"); + t.ok (n.getUntilRx ("th", s), " 'one two' : getUntilRx ('th') -> true"); + t.is (s, "one two", " 'one two' : getUntilRx ('th') -> 'one two'"); - // bool getUntil (const std::string&, std::string&); - t.diag ("Nibbler::getUntil"); - n = Nibbler ("ab\r\ncd"); - t.ok (n.getUntil ("\r\n", s), "'ab\\r\\ncd' : getUntil ('\\r\\n') -> true"); - t.ok (n.skipN (2), " '\\r\\ncd' : skipN (2) -> true"); - t.ok (n.getUntil ("\r\n", s), " 'cd' : getUntil ('\\r\\n') -> true"); - t.ok (n.depleted (), " '' : depleted () -> true"); + n = Nibbler ("one two"); + t.ok (n.getUntilRx ("e", s), " 'one two' : getUntilRx ('e') -> true"); + t.is (s, "on", " 'one two' : getUntilRx ('e') -> 'on'"); + t.ok (n.getUntilRx ("tw", s), " 'e two' : getUntilRx ('tw') -> true"); + t.is (s, "e ", " 'e two' : getUntilRx ('tw') -> 'e '"); + t.ok (n.getUntilRx ("$", s), " 'two' : getUntilRx ('$') -> true"); + t.is (s, "two", " 'two' : getUntilRx ('$') -> 'two'"); + t.ok (n.depleted (), " '' : depleted () -> true"); - // bool skipN (const int quantity = 1); - t.diag ("Nibbler::skipN"); - n = Nibbler ("abcde"); - t.ok (n.skipN (), " 'abcde' : skipN () -> true"); - t.ok (n.skipN (2), " 'bcde' : skipN (2 -> true"); - t.notok (n.skipN (3), " 'de' : skipN (3 -> false"); - t.notok (n.depleted (), " 'de' : depleted () -> true"); + // bool getUntilOneOf (const std::string&, std::string&); + t.diag ("Nibbler::getUntilOneOf"); + n = Nibbler ("ab.cd"); + t.ok (n.getUntilOneOf (".:", s), " 'ab.cd' : getUntilOneOf ('.:') -> true"); + t.is (s, "ab", " 'ab.cd' : getUntilOneOf ('.:') -> 'ab'"); + t.ok (n.skipN (), " '.cd' : skipN () -> true"); + t.ok (n.getUntilOneOf (".:", s), " 'cd' : getUntilOneOf ('.:') -> true"); + t.notok (n.getUntilOneOf (".:", s), " '' : getUntilOneOf ('.:') -> false"); + t.ok (n.depleted (), " '' : depleted () -> true"); - // bool skip (char); - t.diag ("Nibbler::skip"); - n = Nibbler (" a"); - t.ok (n.skip (' '), " ' a' : skip (' ') -> true"); - t.ok (n.skip (' '), " ' a' : skip (' ') -> true"); - t.notok (n.skip (' '), " 'a' : skip (' ') -> false"); - t.notok (n.depleted (), " 'a' : depleted () -> false"); - t.ok (n.skip ('a'), " 'a' : skip ('a') -> true"); - t.ok (n.depleted (), " '' : depleted () -> true"); + // bool getUntil (const std::string&, std::string&); + t.diag ("Nibbler::getUntil"); + n = Nibbler ("ab\r\ncd"); + t.ok (n.getUntil ("\r\n", s), "'ab\\r\\ncd' : getUntil ('\\r\\n') -> true"); + t.ok (n.skipN (2), " '\\r\\ncd' : skipN (2) -> true"); + t.ok (n.getUntil ("\r\n", s), " 'cd' : getUntil ('\\r\\n') -> true"); + t.ok (n.depleted (), " '' : depleted () -> true"); - // bool skipAll (char); - t.diag ("Nibbler::skipAll"); - n = Nibbler ("aaaabb"); - t.ok (n.skipAll ('a'), " 'aaaabb' : skipAll ('a') -> true"); - t.notok (n.skipAll ('a'), " 'bb' : skipAll ('a') -> false"); - t.ok (n.skipAll ('b'), " 'bb' : skipAll ('b') -> true"); - t.notok (n.skipAll ('b'), " '' : skipAll ('b') -> false"); - t.ok (n.depleted (), " '' : depleted () -> true"); + // bool getUntilWS (std::string&); + t.diag ("Nibbler::getUntilWS"); + n = Nibbler ("ab \t\ncd"); + t.ok (n.getUntilWS (s), " 'ab \\t\\ncd' : getUntilWS () -> true"); + t.is (s, "ab", " 'ab \\t\\ncd' : getUntilWS () -> 'ab'"); + t.ok (n.getUntilWS (s), " ' \\t\\ncd' : getUntilWS () -> true"); + t.is (s, "", " ' \\t\\ncd' : getUntilWS () -> ''"); + t.ok (n.skipWS (), " 'cd' : skipWS () -> true"); + t.ok (n.getUntilWS (s), " '' : getUntilWS () -> true"); + t.is (s, "cd", " 'cd' : getUntilWS () -> 'cd'"); + t.ok (n.depleted (), " '' : depleted () -> true"); - // bool skipAllOneOf (const std::string&); - t.diag ("Nibbler::skipAllOneOf"); - n = Nibbler ("abababcc"); - t.ok (n.skipAllOneOf ("ab"), "'abababcc' : skipAllOneOf ('ab') -> true"); - t.notok (n.skipAllOneOf ("ab"), " 'cc' : skipAllOneOf ('ab') -> false"); - t.ok (n.skipAllOneOf ("c"), " 'cc' : skipAllOneOf ('ab') -> false"); - t.ok (n.depleted (), " '' : depleted () -> true"); + // bool skipN (const int quantity = 1); + t.diag ("Nibbler::skipN"); + n = Nibbler ("abcde"); + t.ok (n.skipN (), " 'abcde' : skipN () -> true"); + t.ok (n.skipN (2), " 'bcde' : skipN (2 -> true"); + t.notok (n.skipN (3), " 'de' : skipN (3 -> false"); + t.notok (n.depleted (), " 'de' : depleted () -> true"); - // bool getQuoted (char, std::string&); - t.diag ("Nibbler::getQuoted"); - n = Nibbler ("''"); - t.ok (n.getQuoted ('\'', s), " '''' : getQuoted (''') -> true"); - t.is (s, "", " '''' : getQuoted (''') -> ''"); + // bool skip (char); + t.diag ("Nibbler::skip"); + n = Nibbler (" a"); + t.ok (n.skip (' '), " ' a' : skip (' ') -> true"); + t.ok (n.skip (' '), " ' a' : skip (' ') -> true"); + t.notok (n.skip (' '), " 'a' : skip (' ') -> false"); + t.notok (n.depleted (), " 'a' : depleted () -> false"); + t.ok (n.skip ('a'), " 'a' : skip ('a') -> true"); + t.ok (n.depleted (), " '' : depleted () -> true"); - n = Nibbler ("'\"'"); - t.ok (n.getQuoted ('\'', s), " ''\"'' : getQuoted (''') -> true"); - t.is (s, "\"", " ''\"'' : getQuoted (''') -> '\"'"); + // bool skipAll (char); + t.diag ("Nibbler::skipAll"); + n = Nibbler ("aaaabb"); + t.ok (n.skipAll ('a'), " 'aaaabb' : skipAll ('a') -> true"); + t.notok (n.skipAll ('a'), " 'bb' : skipAll ('a') -> false"); + t.ok (n.skipAll ('b'), " 'bb' : skipAll ('b') -> true"); + t.notok (n.skipAll ('b'), " '' : skipAll ('b') -> false"); + t.ok (n.depleted (), " '' : depleted () -> true"); - n = Nibbler ("'x'"); - t.ok (n.getQuoted ('\'', s), " ''x'' : getQuoted (''') -> true"); - t.is (s, "x", " ''x'' : getQuoted (''') -> ''"); + // bool skipAllOneOf (const std::string&); + t.diag ("Nibbler::skipAllOneOf"); + n = Nibbler ("abababcc"); + t.ok (n.skipAllOneOf ("ab"), "'abababcc' : skipAllOneOf ('ab') -> true"); + t.notok (n.skipAllOneOf ("ab"), " 'cc' : skipAllOneOf ('ab') -> false"); + t.ok (n.skipAllOneOf ("c"), " 'cc' : skipAllOneOf ('ab') -> false"); + t.ok (n.depleted (), " '' : depleted () -> true"); - n = Nibbler ("'x"); - t.notok (n.getQuoted ('\'', s), " ''x' : getQuoted (''') -> false"); + // bool skipWS (); + t.diag ("Nibbler::skipWS"); + n = Nibbler (" \tfoo"); + t.ok (n.skipWS (), " ' \\tfoo' : skipWS () -> true"); + t.notok (n.skipWS (), " 'foo' : skipWS () -> false"); + t.ok (n.getUntilEOS (s), " 'foo' : getUntilEOS () -> true"); + t.is (s, "foo", " 'foo' : getUntilEOS () -> 'foo'"); + t.ok (n.depleted (), " '' : depleted () -> true"); - n = Nibbler ("x"); - t.notok (n.getQuoted ('\'', s), " 'x' : getQuoted (''') -> false"); + // bool skipRx (const std::string&); + t.diag ("Nibbler::skipRx"); + n = Nibbler ("one two"); + t.ok (n.skipRx ("o."), " 'one two' : skipRx ('o.') -> true"); + t.notok (n.skipRx ("A+"), " 'e two' : skipRx ('A+') -> false"); + t.ok (n.skipRx ("e+"), " 'e two' : skipRx ('e+') -> true"); + t.ok (n.skipRx ("...."), " ' two' : skipRx ('....') -> true"); + t.ok (n.depleted (), " '' : depleted () -> true"); - // bool getInt (int&); - t.diag ("Nibbler::getInt"); - n = Nibbler ("123 -4"); - t.ok (n.getInt (i), " '123 -4' : getInt () -> true"); - t.is (i, 123, " '123 -4' : getInt () -> '123'"); - t.ok (n.skip (' '), " ' -4' : skip (' ') -> true"); - t.ok (n.getInt (i), " '-4' : getInt () -> true"); - t.is (i, -4, " '-4' : getInt () -> '-4'"); - t.ok (n.depleted (), " '' : depleted () -> true"); + // bool getQuoted (char, std::string&); + t.diag ("Nibbler::getQuoted"); + n = Nibbler ("''"); + t.ok (n.getQuoted ('\'', s), " '''' : getQuoted (''') -> true"); + t.is (s, "", " '''' : getQuoted (''') -> ''"); - // bool getUnsignedInt (int&i); - t.diag ("Nibbler::getUnsignedInt"); - n = Nibbler ("123 4"); - t.ok (n.getUnsignedInt (i), " '123 4' : getUnsignedInt () -> true"); - t.is (i, 123, " '123 4' : getUnsignedInt () -> '123'"); - t.ok (n.skip (' '), " ' 4' : skip (' ') -> true"); - t.ok (n.getUnsignedInt (i), " '4' : getUnsignedInt () -> true"); - t.is (i, 4, " '4' : getUnsignedInt () -> '4'"); - t.ok (n.depleted (), " '' : depleted () -> true"); + n = Nibbler ("'\"'"); + t.ok (n.getQuoted ('\'', s), " ''\"'' : getQuoted (''') -> true"); + t.is (s, "\"", " ''\"'' : getQuoted (''') -> '\"'"); - // bool getUntilEOL (std::string&); - t.diag ("Nibbler::getUntilEOL"); - n = Nibbler ("one\ntwo"); - t.ok (n.getUntilEOL (s), "'one\\ntwo' : getUntilEOL () -> true"); - t.is (s, "one", "'one\\ntwo' : getUntilEOL () -> 'one'"); - t.ok (n.skip ('\n'), " '\\ntwo' : skip ('\\n') -> true"); - t.ok (n.getUntilEOL (s), " 'two' : getUntilEOL () -> true"); - t.is (s, "two", " 'two' : getUntilEOL () -> 'two'"); - t.ok (n.depleted (), " '' : depleted () -> true"); + n = Nibbler ("'x'"); + t.ok (n.getQuoted ('\'', s), " ''x'' : getQuoted (''') -> true"); + t.is (s, "x", " ''x'' : getQuoted (''') -> ''"); - // bool getUntilEOS (std::string&); - t.diag ("Nibbler::getUntilEOS"); - n = Nibbler ("one two"); - t.ok (n.getUntilEOS (s), " 'one two' : getUntilEOS () -> 'one two'"); - t.ok (n.depleted (), " '' : depleted () -> true"); + n = Nibbler ("'x"); + t.notok (n.getQuoted ('\'', s), " ''x' : getQuoted (''') -> false"); - // bool depleted (); - t.diag ("Nibbler::depleted"); - n = Nibbler (" "); - t.notok (n.depleted (), " ' ' : depleted () -> false"); - t.ok (n.skipN (), " '' : skip () -> true"); - t.ok (n.depleted (), " '' : depleted () -> true"); + n = Nibbler ("x"); + t.notok (n.getQuoted ('\'', s), " 'x' : getQuoted (''') -> false"); + + // bool getInt (int&); + t.diag ("Nibbler::getInt"); + n = Nibbler ("123 -4"); + t.ok (n.getInt (i), " '123 -4' : getInt () -> true"); + t.is (i, 123, " '123 -4' : getInt () -> '123'"); + t.ok (n.skip (' '), " ' -4' : skip (' ') -> true"); + t.ok (n.getInt (i), " '-4' : getInt () -> true"); + t.is (i, -4, " '-4' : getInt () -> '-4'"); + t.ok (n.depleted (), " '' : depleted () -> true"); + + // bool getUnsignedInt (int&i); + t.diag ("Nibbler::getUnsignedInt"); + n = Nibbler ("123 4"); + t.ok (n.getUnsignedInt (i), " '123 4' : getUnsignedInt () -> true"); + t.is (i, 123, " '123 4' : getUnsignedInt () -> '123'"); + t.ok (n.skip (' '), " ' 4' : skip (' ') -> true"); + t.ok (n.getUnsignedInt (i), " '4' : getUnsignedInt () -> true"); + t.is (i, 4, " '4' : getUnsignedInt () -> '4'"); + t.ok (n.depleted (), " '' : depleted () -> true"); + + // bool getLiteral (const std::string&); + t.diag ("Nibbler::getLiteral"); + n = Nibbler ("foobar"); + t.ok (n.getLiteral ("foo"), " 'foobar' : getLiteral ('foo') -> true"); + t.notok (n.getLiteral ("foo"), " 'bar' : getLiteral ('foo') -> false"); + t.ok (n.getLiteral ("bar"), " 'bar' : getLiteral ('bar') -> true"); + t.ok (n.depleted (), " '' : depleted () -> true"); + + // bool getRx (const std::string&, std::string&); + t.diag ("Nibbler::getRx"); + n = Nibbler ("one two three"); + t.ok (n.getRx ("^(o..)", s), "'one two three' : getRx ('^(o..)') -> true"); + t.is (s, "one", "'one two three' : getRx ('^(o..)') -> 'one'"); + t.ok (n.skip (' '), " ' two three' : skip (' ') -> true"); + t.ok (n.getRx ("t..", s), " 'two three' : getRx ('t..') -> true"); + t.is (s, "two", " 'two three' : getRx ('t..') -> 'two'"); + t.notok (n.getRx ("th...", s), " ' three' : getRx ('th...') -> false"); + t.ok (n.skip (' '), " ' three' : skip (' ') -> true"); + t.ok (n.getRx ("th...", s), " 'three' : getRx ('th...') -> true"); + t.is (s, "three", " 'three' : getRx ('th...') -> 'three'"); + t.ok (n.depleted (), " '' : depleted () -> true"); + + // bool getUntilEOL (std::string&); + t.diag ("Nibbler::getUntilEOL"); + n = Nibbler ("one\ntwo"); + t.ok (n.getUntilEOL (s), "'one\\ntwo' : getUntilEOL () -> true"); + t.is (s, "one", "'one\\ntwo' : getUntilEOL () -> 'one'"); + t.ok (n.skip ('\n'), " '\\ntwo' : skip ('\\n') -> true"); + t.ok (n.getUntilEOL (s), " 'two' : getUntilEOL () -> true"); + t.is (s, "two", " 'two' : getUntilEOL () -> 'two'"); + t.ok (n.depleted (), " '' : depleted () -> true"); + + // bool getUntilEOS (std::string&); + t.diag ("Nibbler::getUntilEOS"); + n = Nibbler ("one two"); + t.ok (n.getUntilEOS (s), " 'one two' : getUntilEOS () -> 'one two'"); + t.ok (n.depleted (), " '' : depleted () -> true"); + + // char next (); + t.diag ("Nibbler::next"); + n = Nibbler ("hello"); + t.is (n.next (), 'h', " 'hello' : next () -> 'h'"); + t.is (n.next (), 'h', " 'hello' : next () -> 'h'"); + t.ok (n.skipN (4), " 'hello' : skipN (4) -> true"); + t.is (n.next (), 'o', " 'o' : next () -> 'o'"); + t.ok (n.skipN (1), " 'o' : skipN () -> true"); + t.ok (n.depleted (), " '' : depleted () -> true"); + + // std::string next (const int quantity); + t.diag ("Nibbler::next"); + n = Nibbler ("hello"); + t.is (n.next (1), "h", " 'hello' : next (1) -> 'h'"); + t.is (n.next (1), "h", " 'hello' : next (1) -> 'h'"); + t.is (n.next (2), "he", " 'hello' : next (2) -> 'he'"); + t.is (n.next (3), "hel", " 'hello' : next (3) -> 'hel'"); + t.is (n.next (4), "hell", " 'hello' : next (4) -> 'hell'"); + t.is (n.next (5), "hello", " 'hello' : next (5) -> 'hello'"); + t.is (n.next (6), "", " 'hello' : next (6) -> ''"); + + // bool depleted (); + t.diag ("Nibbler::depleted"); + n = Nibbler (" "); + t.notok (n.depleted (), " ' ' : depleted () -> false"); + t.ok (n.skipN (), " '' : skip () -> true"); + t.ok (n.depleted (), " '' : depleted () -> true"); + + // void save (); + // void restore (); + n = Nibbler ("abcde"); + t.ok (n.skipN (), " 'abcde' : skip () -> true"); + n.save (); + t.ok (n.skipN (), " 'bcde' : skip () -> true"); + t.ok (n.skipN (), " 'cde' : skip () -> true"); + t.ok (n.skipN (), " 'de' : skip () -> true"); + n.restore (); + t.is (n.next (1), "b", " 'bcde' : skip () -> 'b'"); + } + + catch (std::string& e) {t.diag (e);} return 0; }