diff --git a/src/Nibbler.cpp b/src/Nibbler.cpp index 162c23fb7..9772c869c 100644 --- a/src/Nibbler.cpp +++ b/src/Nibbler.cpp @@ -446,6 +446,62 @@ bool Nibbler::getRx (const std::string& regex, std::string& result) return false; } +//////////////////////////////////////////////////////////////////////////////// +bool Nibbler::getUUID (std::string& result) +{ + std::string::size_type i = mCursor; + + if (i < mLength && + mLength - i >= 37) + { + // 8-4-4-4-6-6 + if (isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + mInput[i++] == '-' && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + mInput[i++] == '-' && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + mInput[i++] == '-' && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + mInput[i++] == '-' && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + mInput[i++] == '-' && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++]) && + isxdigit (mInput[i++])) + { + result = mInput.substr (mCursor, 37); + mCursor = i; + return true; + } + } + + return false; +} + //////////////////////////////////////////////////////////////////////////////// bool Nibbler::skipN (const int quantity /* = 1 */) { diff --git a/src/Nibbler.h b/src/Nibbler.h index 2a1439383..0e0853acb 100644 --- a/src/Nibbler.h +++ b/src/Nibbler.h @@ -59,6 +59,7 @@ public: bool getNumber (double&); bool getLiteral (const std::string&); bool getRx (const std::string&, std::string&); + bool getUUID (std::string&); bool skipN (const int quantity = 1); bool skip (char); diff --git a/test/nibbler.t.cpp b/test/nibbler.t.cpp index 85d5c2b83..cd55f1110 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 (162); + UnitTest t (168); try { @@ -209,7 +209,6 @@ int main (int argc, char** argv) t.ok (n.getQuoted ('"', s, true), "\"one\\\"two\" : getQuoted ('\"', true, true) -> true"); // 93 t.is (s, "\"one\"two\"", "getQuoted ('\"', true) -> \"one\"two\""); // 94 - // bool getInt (int&); t.diag ("Nibbler::getInt"); n = Nibbler ("123 -4"); @@ -272,6 +271,16 @@ int main (int argc, char** argv) t.is (s, "three", " 'three' : getRx ('th...') -> 'three'"); t.ok (n.depleted (), " '' : depleted () -> true"); + // bool getUUID (std::string&); + t.diag ("Nibbler::getUUID"); + n = Nibbler ("00000000-0000-0000-0000-000000-000000,a0b1c2d3-e4f5-A6B7-C8D9-E0F1a2-b3c4d5"); + t.ok (n.getUUID (s), "uuid 1 found"); + t.is (s, "00000000-0000-0000-0000-000000-000000", "uuid 1 -> correct"); + t.ok (n.skip (','), "comma -> skipped"); + t.ok (n.getUUID (s), "uuid 2 -> found"); + t.is (s, "a0b1c2d3-e4f5-A6B7-C8D9-E0F1a2-b3c4d5", "uuid 2 -> correct"); + t.ok (n.depleted (), "depleted"); + // bool getUntilEOL (std::string&); t.diag ("Nibbler::getUntilEOL"); n = Nibbler ("one\ntwo");