From edda82fafec00e6f21f8a13897cfaedbc4c49f7c Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Wed, 30 Dec 2015 10:59:14 -0500 Subject: [PATCH] Pig: Added ::getOneOf --- src/Pig.cpp | 20 ++++++++++++++++++++ src/Pig.h | 2 ++ 2 files changed, 22 insertions(+) diff --git a/src/Pig.cpp b/src/Pig.cpp index ad32fce1..507fafed 100644 --- a/src/Pig.cpp +++ b/src/Pig.cpp @@ -263,6 +263,26 @@ bool Pig::getNumber (double& result) return false; } +//////////////////////////////////////////////////////////////////////////////// +// Assumes that the options are sorted by decreasing length, so that if the +// options contain 'fourteen' and 'four', the stream is first matched against +// the longer entry. +bool Pig::getOneOf ( + const std::vector & options, + std::string& found) +{ + for (auto& option : options) + { + if (skipLiteral (option)) + { + found = option; + return true; + } + } + + return false; +} + //////////////////////////////////////////////////////////////////////////////// bool Pig::getRemainder (std::string& result) { diff --git a/src/Pig.h b/src/Pig.h index 529b8f16..46f5533c 100644 --- a/src/Pig.h +++ b/src/Pig.h @@ -28,6 +28,7 @@ #define INCLUDED_PIG #include +#include class Pig { @@ -44,6 +45,7 @@ public: bool getDigits (int&); bool getNumber (std::string&); bool getNumber (double&); + bool getOneOf (const std::vector &, std::string&); bool getRemainder (std::string&); bool eos () const;