diff --git a/src/Nibbler.cpp b/src/Nibbler.cpp index 3630924e4..91a31e9e9 100644 --- a/src/Nibbler.cpp +++ b/src/Nibbler.cpp @@ -587,6 +587,24 @@ bool Nibbler::getDate (const std::string& format, time_t& t) return false; } +//////////////////////////////////////////////////////////////////////////////// +bool Nibbler::getOneOf ( + const std::vector & options, + std::string& found) +{ + std::vector ::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 */) { diff --git a/src/Nibbler.h b/src/Nibbler.h index 9dc92e9f3..3e21cbc87 100644 --- a/src/Nibbler.h +++ b/src/Nibbler.h @@ -29,6 +29,7 @@ #define L10N // Localization complete. #include +#include class Nibbler { @@ -63,6 +64,7 @@ public: bool getUUID (std::string&); bool getDateISO (time_t&); bool getDate (const std::string&, time_t&); + bool getOneOf (const std::vector &, std::string&); bool skipN (const int quantity = 1); bool skip (char); diff --git a/test/nibbler.t.cpp b/test/nibbler.t.cpp index a6ed28e33..e103e6883 100644 --- a/test/nibbler.t.cpp +++ b/test/nibbler.t.cpp @@ -33,7 +33,7 @@ Context context; //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - UnitTest t (185); + UnitTest t (193); try { @@ -42,6 +42,7 @@ int main (int argc, char** argv) int i; double d; time_t ti; + std::vector options; // Make sure the nibbler behaves itself with trivial 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.getDateISO (ti), "trivial: getDateISO"); t.notok (n.getDate ("YYYYMMDD", ti), "trivial: getDate"); + t.notok (n.getOneOf (options, s), "trivial: getOneOf"); t.ok (n.depleted (), "trivial: depleted"); // bool getUntil (char, std::string&); @@ -311,6 +313,20 @@ int main (int argc, char** argv) t.is (ti, 1234567890, "'20090213T233130Z': getDateISO () -> 1234567890"); t.ok (n.depleted (), "depleted"); + // bool getOneOf (const std::vector &, 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&); t.diag ("Nibbler::getUntilEOL"); n = Nibbler ("one\ntwo");