From 3fd83ca4008c1f0278e5ebf339d24b1930e4b2e7 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 24 Jul 2011 01:00:24 -0400 Subject: [PATCH] Nibbler - Implemented Nibbler::getName to parse an alpha-numeric name. - Added appropriate unit tests. --- src/Nibbler.cpp | 30 ++++++++++++++++++++++++++++++ src/Nibbler.h | 1 + test/nibbler.t.cpp | 25 ++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/Nibbler.cpp b/src/Nibbler.cpp index c681cf254..117d5b232 100644 --- a/src/Nibbler.cpp +++ b/src/Nibbler.cpp @@ -987,6 +987,36 @@ bool Nibbler::getDOM (std::string& result) return false; } +//////////////////////////////////////////////////////////////////////////////// +// A name is a string of alpha-numeric characters. +bool Nibbler::getName (std::string& result) +{ + std::string::size_type i = mCursor; + + if (i < mLength) + { + if (isalpha (mInput[i])) + { + ++i; + while (i < mLength && + (isalpha (mInput[i]) || + isdigit (mInput[i]))) + { + ++i; + } + } + + if (i > mCursor) + { + result = mInput.substr (mCursor, i - mCursor); + mCursor = i; + return true; + } + } + + return false; +} + //////////////////////////////////////////////////////////////////////////////// // A word is a contiguous string of non-space, non-digit, non-punct characters. bool Nibbler::getWord (std::string& result) diff --git a/src/Nibbler.h b/src/Nibbler.h index 08bf014c8..381eba912 100644 --- a/src/Nibbler.h +++ b/src/Nibbler.h @@ -66,6 +66,7 @@ public: bool getDate (const std::string&, time_t&); bool getOneOf (const std::vector &, std::string&); bool getDOM (std::string&); + bool getName (std::string&); bool getWord (std::string&); bool skipN (const int quantity = 1); diff --git a/test/nibbler.t.cpp b/test/nibbler.t.cpp index 6fbff61ad..991eb14c0 100644 --- a/test/nibbler.t.cpp +++ b/test/nibbler.t.cpp @@ -34,7 +34,7 @@ Context context; //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - UnitTest t (277); + UnitTest t (292); try { @@ -439,6 +439,29 @@ int main (int argc, char** argv) n = Nibbler ("..foo "); t.notok (n.getDOM (s), "'..foo' getDOM -> notok"); + // bool getName (std::string&); + t.diag ("Nibbler::getName"); + n = Nibbler ("a1 one one.two 9"); + t.ok (n.getName (s), "'a1 one one.two 9' getName -> ok"); + t.is (s, "a1", " ' one one.two 9' getName -> 'a1'"); + t.ok (n.skipWS (), " 'one one.two 9' skipWS -> ok"); + + t.ok (n.getName (s), " 'one one.two 9' getName -> ok"); + t.is (s, "one", " ' one.two 9' getName -> 'one'"); + t.ok (n.skipWS (), " 'one.two 9' skipWS -> ok"); + + t.ok (n.getName (s), " 'one.two 9' getName -> ok"); + t.is (s, "one", " '.two 9' getName -> 'one'"); + t.ok (n.skip ('.'), " 'two 9' skip . -> ok"); + + t.ok (n.getName (s), " 'two 9' getName -> ok"); + t.is (s, "two", " ' 9' getName -> 'two'"); + t.ok (n.skipWS (), " '9' skipWS -> ok"); + + t.notok (n.getName (s), " '9' getName -> not ok"); + t.ok (n.skip ('9'), " '' skip 9 -> ok"); + t.ok (n.depleted (), "depleted"); + // bool getWord (std::string&); t.diag ("Nibbler::getWord"); n = Nibbler ("one two th3ee");