- Nibbler now understands parsing from a list of tokens, with getOneOf.
This commit is contained in:
Paul Beckingham 2011-06-11 11:13:56 -04:00
parent a5feb6ef83
commit 8f20efc739
3 changed files with 37 additions and 1 deletions

View file

@ -587,6 +587,24 @@ bool Nibbler::getDate (const std::string& format, time_t& t)
return false; return false;
} }
////////////////////////////////////////////////////////////////////////////////
bool Nibbler::getOneOf (
const std::vector <std::string>& options,
std::string& found)
{
std::vector <std::string>::const_iterator option;
for (option = options.begin (); option != options.end (); ++option)
{
if (getLiteral (*option))
{
found = *option;
return true;
}
}
return false;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
bool Nibbler::skipN (const int quantity /* = 1 */) bool Nibbler::skipN (const int quantity /* = 1 */)
{ {

View file

@ -29,6 +29,7 @@
#define L10N // Localization complete. #define L10N // Localization complete.
#include <string> #include <string>
#include <vector>
class Nibbler class Nibbler
{ {
@ -63,6 +64,7 @@ public:
bool getUUID (std::string&); bool getUUID (std::string&);
bool getDateISO (time_t&); bool getDateISO (time_t&);
bool getDate (const std::string&, time_t&); bool getDate (const std::string&, time_t&);
bool getOneOf (const std::vector <std::string>&, std::string&);
bool skipN (const int quantity = 1); bool skipN (const int quantity = 1);
bool skip (char); bool skip (char);

View file

@ -33,7 +33,7 @@ Context context;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv) int main (int argc, char** argv)
{ {
UnitTest t (185); UnitTest t (193);
try try
{ {
@ -42,6 +42,7 @@ int main (int argc, char** argv)
int i; int i;
double d; double d;
time_t ti; time_t ti;
std::vector <std::string> options;
// Make sure the nibbler behaves itself with trivial input. // Make sure the nibbler behaves itself with trivial input.
t.diag ("Test all nibbler calls given empty input"); t.diag ("Test all nibbler calls given empty input");
@ -62,6 +63,7 @@ int main (int argc, char** argv)
t.notok (n.getUntilEOS (s), "trivial: getUntilEOS"); t.notok (n.getUntilEOS (s), "trivial: getUntilEOS");
t.notok (n.getDateISO (ti), "trivial: getDateISO"); t.notok (n.getDateISO (ti), "trivial: getDateISO");
t.notok (n.getDate ("YYYYMMDD", ti), "trivial: getDate"); t.notok (n.getDate ("YYYYMMDD", ti), "trivial: getDate");
t.notok (n.getOneOf (options, s), "trivial: getOneOf");
t.ok (n.depleted (), "trivial: depleted"); t.ok (n.depleted (), "trivial: depleted");
// bool getUntil (char, std::string&); // bool getUntil (char, std::string&);
@ -311,6 +313,20 @@ int main (int argc, char** argv)
t.is (ti, 1234567890, "'20090213T233130Z': getDateISO () -> 1234567890"); t.is (ti, 1234567890, "'20090213T233130Z': getDateISO () -> 1234567890");
t.ok (n.depleted (), "depleted"); t.ok (n.depleted (), "depleted");
// bool getOneOf (const std::vector <std::string>&, std::string&);
t.diag ("Nibbler::getOneOf");
options.push_back ("one");
options.push_back ("two");
options.push_back ("three");
n = Nibbler ("onetwothreefour");
t.ok (n.getOneOf (options, s), "'onetwothreefour': getOneOf () -> true");
t.is (s, "one", "'onetwothreefour': getOneOf () -> one");
t.ok (n.getOneOf (options, s), " 'twothreefour': getOneOf () -> true");
t.is (s, "two", " 'twothreefour': getOneOf () -> two");
t.ok (n.getOneOf (options, s), " 'threefour': getOneOf () -> true");
t.is (s, "three", " 'threefour': getOneOf () -> three");
t.notok (n.getOneOf (options, s), " 'four': getOneOf () -> fasle");
// bool getUntilEOL (std::string&); // bool getUntilEOL (std::string&);
t.diag ("Nibbler::getUntilEOL"); t.diag ("Nibbler::getUntilEOL");
n = Nibbler ("one\ntwo"); n = Nibbler ("one\ntwo");