]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Implemented base/StringArea
authorFrancesco Chemolli <kinkie@squid-cache.org>
Tue, 27 Sep 2011 05:16:58 +0000 (07:16 +0200)
committerFrancesco Chemolli <kinkie@squid-cache.org>
Tue, 27 Sep 2011 05:16:58 +0000 (07:16 +0200)
src/HttpHdrCc.cc
src/base/Makefile.am
src/base/StringArea.h [new file with mode: 0644]

index 443b4d6a3907fc8ba8b86aa18449d90c21d8a732..97bcff1a3e0d56b96b4e9b3b321d0163d397b9c1 100644 (file)
@@ -31,6 +31,7 @@
  */
 
 #include "squid.h"
+#include "base/StringArea.h"
 #include "Store.h"
 #include "HttpHeader.h"
 #include "HttpHdrCc.h"
 #include <map>
 #endif
 
-/** dumb char* /length combo for quick lookups.
- *
- * Data is not copied.
- * Validity of the pointed-to storage is responsibility of the caller.
- * */
-class strblob {
-    public:
-    strblob(const char * ptr, size_t len): thePtr(ptr), theLen(len) {}
-    bool operator==(strblob &s) const { return theLen==s.theLen && 0==strncmp(thePtr,s.thePtr,theLen); }
-    bool operator< ( const strblob &s2) const { return strncmp(thePtr,s2.thePtr,theLen) < 0; }
-
-    private:
-    const char *thePtr;
-    size_t theLen;
-};
-
 /* a row in the table used for parsing cache control header and statistics */
 typedef struct {
     const char *name;
@@ -81,7 +66,7 @@ static HttpHeaderCcFields CcAttrs[CC_ENUM_END] = {
 };
 
 /// Map an header name to its type, to expedite parsing
-typedef std::map<const strblob,http_hdr_cc_type> CcNameToIdMap_t;
+typedef std::map<const StringArea,http_hdr_cc_type> CcNameToIdMap_t;
 static CcNameToIdMap_t CcNameToIdMap;
 
 /// iterate over a table of http_header_cc_type structs
@@ -99,10 +84,10 @@ httpHdrCcInitModule(void)
 {
     /* build lookup and accounting structures */
     for (int32_t i = 0;i < CC_ENUM_END; ++i) {
-        const HttpHeaderCcFields *f=&CcAttrs[i];
-        assert(i == f->id); /* verify assumption: the id is the key into the array */
-        const strblob k(f->name,strlen(f->name));
-        CcNameToIdMap[k]=f->id;
+        const HttpHeaderCcFields &f=CcAttrs[i];
+        assert(i == f.id); /* verify assumption: the id is the key into the array */
+        const StringArea k(f.name,strlen(f.name));
+        CcNameToIdMap[k]=f.id;
     }
 }
 
@@ -141,8 +126,8 @@ HttpHdrCc::parse(const String & str)
             nlen = ilen;
 
         /* find type */
-        const strblob tmpstr(item,nlen);
-        const CcNameToIdMap_t::iterator i=CcNameToIdMap.find(tmpstr);
+        const StringArea tmpstr(item,nlen);
+        const CcNameToIdMap_t::const_iterator i=CcNameToIdMap.find(tmpstr);
         if (i==CcNameToIdMap.end())
             type=CC_OTHER;
         else
index cd38376cebb3ce50effa1cedaa11bce6d560ff69..786671eae165d052b51cb901a973a549e36b20cf 100644 (file)
@@ -19,4 +19,5 @@ libbase_la_SOURCES = \
        RunnersRegistry.h \
        Subscription.h \
        TextException.cc \
-       TextException.h
+       TextException.h \
+       StringArea.h
diff --git a/src/base/StringArea.h b/src/base/StringArea.h
new file mode 100644 (file)
index 0000000..1ffda12
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * StringArea.h
+ *
+ *
+ * SQUID Web Proxy Cache          http://www.squid-cache.org/
+ * ----------------------------------------------------------
+ *
+ *  Squid is the result of efforts by numerous individuals from
+ *  the Internet community; see the CONTRIBUTORS file for full
+ *  details.   Many organizations have provided support for Squid's
+ *  development; see the SPONSORS file for full details.  Squid is
+ *  Copyrighted (C) 2001 by the Regents of the University of
+ *  California; see the COPYRIGHT file for full details.  Squid
+ *  incorporates software developed and/or copyrighted by other
+ *  sources; see the CREDITS file for full details.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
+ */
+
+#ifndef SQUID_STRINGAREA_H
+#define SQUID_STRINGAREA_H
+
+#if HAVE_CSTRING
+#include <cstring>
+#endif
+
+/** A char* plus length combination. Useful for temporary storing
+ * and quickly looking up strings.
+ *
+ * The pointed-to string may not be null-terminated.
+ * The pointed-to string is not copied.
+ *
+ * Not meant for stand-alone storage. Validity of the
+ * pointed-to string is responsibility of the caller.
+ */
+class StringArea {
+    public:
+    StringArea(const char * ptr, size_t len): theStart(ptr), theLen(len) {}
+    bool operator==(const StringArea &s) const { return theLen==s.theLen && strncmp(theStart,s.theStart,theLen)==0; }
+    bool operator!=(const StringArea &s) const { return !operator==(s); }
+    bool operator< ( const StringArea &s) const { return theLen < s.theLen || strncmp(theStart,s.theStart,theLen) < 0; }
+
+    private:
+    const char *theStart;
+    size_t theLen;
+};
+
+#endif /* SQUID_STRINGAREA_H */