#include <vector>
-//#include <iostream>
namespace Parser {
class CharacterSet
bool operator[](char c) const {return chars_[static_cast<uint8_t>(c)];}
/// add a given char to the character set
- void add(const char c) {chars_[static_cast<uint8_t>(c)] = true;}
+ CharacterSet & add(const char c) {chars_[static_cast<uint8_t>(c)] = true; return *this; }
/// add all characters from the given CharacterSet to this one
const CharacterSet &operator +=(const CharacterSet &src) {
- // TODO: iterate src.chars_ vector instead of walking the entire 8-bit space
- for (size_t i = 0; i < 256; ++i) {
- if (src.chars_[static_cast<uint8_t>(i)])
- chars_[static_cast<uint8_t>(i)] = true;
+#if 1
+ if (src.chars_.size() > chars_.size())
+ chars_.reserve(src.chars_.size());
+ //notworking
+ std::vector<bool>::const_iterator s = src.chars_.begin();
+ const std::vector<bool>::const_iterator e = src.chars_.end();
+ std::vector<bool>::iterator d = chars_.begin();
+ while (s != e) {
+ if (*s)
+ *d = true;
+ ++s;
+ ++d;
}
+#else
+ for (int i = 0; i < 256; ++i) {
+ if (src[i])
+ add(i);
+ }
+#endif
return *this;
}
"Host: resource.com\r\n"
"Cookie: laijkpk3422r j1noin \r\n"
"\r\n");
-const Parser::CharacterSet alpha("alpha","abcdefghijklmnopqrstuvwxzABCDEFGHIJKLMNOPQRSTUVWXYZ");
+const Parser::CharacterSet alpha("alpha","abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
const Parser::CharacterSet whitespace("whitespace"," ");
const Parser::CharacterSet crlf("crlf","\r\n");
const Parser::CharacterSet tab("tab","\t");
+#include <iostream>
+std::ostream &dumpCharSet(std::ostream &os, const Parser::CharacterSet &cs) {
+ for (int i = 0; i < 256; ++i) {
+ if (cs[i])
+ os << static_cast<char>(i);
+ else
+ os << '.';
+ }
+ os << std::endl;
+ return os;
+}
void
testTokenizer::testTokenizerPrefix()
{
CPPUNIT_ASSERT_EQUAL(SBuf("http"),s); //output SBuf left untouched
// match until the end of the sample
- Parser::CharacterSet all(alpha);
- // TODO: finish from here. But += is buggy
-// all += whitespace;
-// all += crlf;
-
+ dumpCharSet(std::cout,alpha);
+ dumpCharSet(std::cout,whitespace);
+ Parser::CharacterSet all("all"," ");
+ dumpCharSet(std::cout,all);
+ all += alpha;
+ dumpCharSet(std::cout,all);
+ all += crlf;
+ dumpCharSet(std::cout,all);
+ all.add(':').add('.').add('/');
+ dumpCharSet(std::cout,all);
+ CPPUNIT_ASSERT(t.prefix(s,all));
+ CPPUNIT_ASSERT_EQUAL(SBuf(),t.remaining());
}
void