]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Broken: try and figure out CharacterSet::operator +=; more Tokenizer::prefix unit...
authorFrancesco Chemolli <kinkie@squid-cache.org>
Fri, 13 Dec 2013 05:47:07 +0000 (06:47 +0100)
committerFrancesco Chemolli <kinkie@squid-cache.org>
Fri, 13 Dec 2013 05:47:07 +0000 (06:47 +0100)
src/parser/CharacterSet.h
src/parser/testTokenizer.cc

index ef99075da13a14082c2d2cac70f0412ef8a55f37..6b7f17ab70b8ce9602ce6f1ec2633a409c47dc2d 100644 (file)
@@ -3,7 +3,6 @@
 
 #include <vector>
 
-//#include <iostream>
 namespace Parser {
 
 class CharacterSet
@@ -21,15 +20,29 @@ public:
     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;
     }
 
index 011dcec620cdc6a931d73014fcc8e619e70fd068..e3e43abf92c4ad91f36eecdd3dd992b3e5ffecc1 100644 (file)
@@ -10,11 +10,22 @@ SBuf text("GET http://resource.com/path HTTP/1.1\r\n"
     "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()
 {
@@ -40,11 +51,18 @@ 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