mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-06-26 10:54:26 +02:00
Parsing
- Nibbler now understands parsing from a list of tokens, with getOneOf.
This commit is contained in:
parent
a5feb6ef83
commit
8f20efc739
3 changed files with 37 additions and 1 deletions
|
@ -587,6 +587,24 @@ bool Nibbler::getDate (const std::string& format, time_t& t)
|
|||
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 */)
|
||||
{
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
#define L10N // Localization complete.
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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>&, std::string&);
|
||||
|
||||
bool skipN (const int quantity = 1);
|
||||
bool skip (char);
|
||||
|
|
|
@ -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 <std::string> 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>&, 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");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue