]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Prep for SBufList merge from stringng
authorFrancesco Chemolli <kinkie@squid-cache.org>
Wed, 4 Dec 2013 18:37:08 +0000 (19:37 +0100)
committerFrancesco Chemolli <kinkie@squid-cache.org>
Wed, 4 Dec 2013 18:37:08 +0000 (19:37 +0100)
src/Makefile.am
src/SBuf.h
src/SBufAlgos.h [new file with mode: 0644]
src/SBufList.cc [new file with mode: 0644]
src/SBufList.h [new file with mode: 0644]
src/tests/testSBufList.cc [new file with mode: 0644]
src/tests/testSBufList.h [new file with mode: 0644]
src/wordlist.h

index 9595c5c7ac0d3d08106f05e774ea6cc923320c90..9e6e9d280c675bb8f8be4c7e0cbd7cef426f1c64 100644 (file)
@@ -1073,6 +1073,7 @@ check_PROGRAMS+=\
        tests/testString \
        tests/testURL \
        tests/testSBuf \
+       tests/testSBufList \
        tests/testConfigParser \
        tests/testStatHist \
        tests/testVector
@@ -3668,6 +3669,45 @@ tests_testSBuf_LDADD=\
        $(COMMON_LIBS)
 tests_testSBuf_DEPENDENCIES= $(SQUID_CPPUNIT_LA)
 
+tests_testSBufList_SOURCES= \
+       tests/testSBufList.h \
+       tests/testSBufList.cc \
+       tests/testMain.cc \
+       $(SBUF_SOURCE) \
+  SBufList.h \
+  SBufList.cc \
+  SBufAlgos.h \
+       SBufDetailedStats.h \
+       tests/stub_SBufDetailedStats.cc \
+       SBufStream.h \
+       tests/stub_time.cc \
+       mem.cc \
+       tests/stub_debug.cc \
+       tests/stub_event.cc \
+       tests/stub_fatal.cc \
+       tests/stub_HelperChildConfig.cc \
+       tests/stub_cache_cf.cc \
+       tests/stub_cache_manager.cc \
+       tests/stub_store_stats.cc \
+       tests/stub_tools.cc \
+       SquidString.h \
+       String.cc \
+       tests/stub_wordlist.cc \
+       tests/stub_MemBuf.cc
+nodist_tests_testSBufList_SOURCES=$(TESTSOURCES)
+tests_testSBufList_LDFLAGS = $(LIBADD_DL)
+tests_testSBufList_LDADD=\
+       $(SQUID_CPPUNIT_LIBS) \
+       $(SQUID_CPPUNIT_LA) \
+       $(COMPAT_LIB) \
+       libsquid.la \
+       ip/libip.la \
+       mgr/libmgr.la \
+       base/libbase.la \
+       $(top_builddir)/lib/libmiscutil.la \
+       $(COMMON_LIBS)
+tests_testSBufList_DEPENDENCIES= $(SQUID_CPPUNIT_LA)
+
 tests_testConfigParser_SOURCES = \
        ClientInfo.h \
        Mem.h \
index 5f914605a816b1c2b0e8a859f875f9c9efabe191..d374b11a0057b1dbe9d311a1c3ba9552cf85083c 100644 (file)
@@ -55,6 +55,7 @@
 #define SQUIDSBUFPRINT(s) (s).plength(),(s).rawContent()
 #endif /* SQUIDSBUFPH */
 
+// TODO: move within SBuf and rename
 typedef enum {
     caseSensitive,
     caseInsensitive
diff --git a/src/SBufAlgos.h b/src/SBufAlgos.h
new file mode 100644 (file)
index 0000000..67f14ee
--- /dev/null
@@ -0,0 +1,68 @@
+#ifndef SQUID_SBUFALGOS_H_
+#define SQUID_SBUFALGOS_H_
+
+#include "SBuf.h"
+#include <algorithm>
+
+/// SBuf equality predicate for STL algorithms etc
+class SBufEqual {
+public:
+    explicit SBufEqual(const SBuf &reference, SBufCaseSensitive caseSensitivity_ = caseSensitive) :
+        reference_(reference), sensitivity_(caseSensitivity_) {}
+    inline bool operator() (const SBuf & checking) { return 0 == checking.compare(reference_,sensitivity_); }
+private:
+    SBuf reference_;
+    SBufCaseSensitive sensitivity_;
+};
+
+/// SBuf "starts with" predicate for STL algorithms etc
+class SBufStartsWith {
+public:
+    explicit SBufStartsWith(const SBuf &prefix, SBufCaseSensitive caseSensitive = caseSensitive) :
+        prefix_(prefix), sensitive(caseSensitive) {}
+    inline bool operator() (const SBuf & checking) { return checking.startsWith(prefix_,sensitive); }
+private:
+    SBuf prefix_;
+    SBufCaseSensitive sensitive;
+};
+
+/** SBuf size addition accumulator for STL contaniners
+ *
+ * Equivalent to prefix_length +  SBuf.length() +  separator.length()
+ */
+class SBufAddLength {
+public:
+    explicit SBufAddLength(const SBuf &separator) :
+        separator_len(separator.length()) {}
+    SBuf::size_type operator()(const SBuf::size_type sz, const SBuf & item) {
+        return sz + item.length() + separator_len;
+    }
+private:
+    SBuf::size_type separator_len;
+};
+
+/// join all the SBuf in a container of SBuf into a single SBuf, separating with separator
+template <class Container>
+SBuf
+SBufContainerJoin(const Container &items, const SBuf& separator)
+{
+    // optimization: pre-calculate needed storage
+    SBuf::size_type sz;
+    sz = std::accumulate(items.begin(), items.end(), 0, SBufAddLength(separator));
+
+    // protect against blindly dereferencing items.begin() if items.size()==0
+    if (sz == 0)
+        return SBuf();
+
+    SBuf rv;
+    rv.reserveSpace(sz);
+
+    typename Container::const_iterator i(items.begin());
+    rv.append(*i);
+    ++i;
+    for (;i != items.end(); ++i)
+        rv.append(separator).append(*i);
+    return rv;
+}
+
+#endif /* SQUID_SBUFALGOS_H_ */
diff --git a/src/SBufList.cc b/src/SBufList.cc
new file mode 100644 (file)
index 0000000..adbf576
--- /dev/null
@@ -0,0 +1,21 @@
+#include "squid.h"
+#include "SBufList.h"
+#include "SBufAlgos.h"
+#include "wordlist.h"
+
+bool
+IsMember(const SBufList & sl, const SBuf &S, const SBufCaseSensitive case_sensitive)
+{
+    return std::find_if(sl.begin(), sl.end(), SBufEqual(S,case_sensitive)) != sl.end();
+}
+
+SBufList
+ToSBufList(wordlist *wl)
+{
+    SBufList rv;
+    while (wl != NULL) {
+        rv.push_back(SBuf(wl->key));
+        wl = wl->next;
+    }
+    return rv;
+}
diff --git a/src/SBufList.h b/src/SBufList.h
new file mode 100644 (file)
index 0000000..d95815b
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef SQUID_SBUFLIST_H
+#define SQUID_SBUFLIST_H
+
+#include "SBuf.h"
+
+#include <list>
+
+typedef std::list<SBuf> SBufList;
+
+/** check for membership
+ *
+ * \return true if the supplied SBuf is a member of the list
+ * \param case_sensitive one of caseSensitive or caseInsensitive
+ */
+bool IsMember(const SBufList &, const SBuf &, const SBufCaseSensitive isCaseSensitive = caseSensitive);
+
+class wordlist;
+
+/// convert a wordlist to a SBufList
+SBufList ToSBufList(wordlist *);
+
+#endif /* SQUID_SBUFLIST_H */
diff --git a/src/tests/testSBufList.cc b/src/tests/testSBufList.cc
new file mode 100644 (file)
index 0000000..51c5ef4
--- /dev/null
@@ -0,0 +1,37 @@
+#include "squid.h"
+#include "SBufAlgos.h"
+#include "SBufList.h"
+#include "testSBufList.h"
+
+CPPUNIT_TEST_SUITE_REGISTRATION( testSBufList );
+
+SBuf literal("The quick brown fox jumped over the lazy dog");
+static int sbuf_tokens_number=9;
+static SBuf tokens[]={
+    SBuf("The",3), SBuf("quick",5), SBuf("brown",5), SBuf("fox",3),
+    SBuf("jumped",6), SBuf("over",4), SBuf("the",3), SBuf("lazy",4),
+    SBuf("dog",3)
+};
+
+void
+testSBufList::testSBufListMembership()
+{
+    SBufList foo;
+    for (int j=0; j<sbuf_tokens_number; ++j)
+        foo.push_back(tokens[j]);
+    CPPUNIT_ASSERT_EQUAL(true,IsMember(foo,SBuf("fox")));
+    CPPUNIT_ASSERT_EQUAL(true,IsMember(foo,SBuf("Fox"),caseInsensitive));
+    CPPUNIT_ASSERT_EQUAL(false,IsMember(foo,SBuf("garble")));
+}
+
+void
+testSBufList::testSBufListJoin()
+{
+    SBufList foo;
+    CPPUNIT_ASSERT_EQUAL(SBuf(""),SBufContainerJoin(foo,SBuf()));
+    CPPUNIT_ASSERT_EQUAL(SBuf(""),SBufContainerJoin(foo,SBuf()));
+    for (int j = 0; j < sbuf_tokens_number; ++j)
+        foo.push_back(tokens[j]);
+    SBuf joined=SBufContainerJoin(foo,SBuf(" "));
+    CPPUNIT_ASSERT_EQUAL(literal,joined);
+}
diff --git a/src/tests/testSBufList.h b/src/tests/testSBufList.h
new file mode 100644 (file)
index 0000000..c956ce0
--- /dev/null
@@ -0,0 +1,19 @@
+#ifndef SQUID_SRC_TEST_TESTSBUF_H
+#define SQUID_SRC_TEST_TESTSBUF_H
+
+#include <cppunit/extensions/HelperMacros.h>
+
+#include "OutOfBoundsException.h"
+
+class testSBufList : public CPPUNIT_NS::TestFixture
+{
+    CPPUNIT_TEST_SUITE( testSBufList );
+    CPPUNIT_TEST( testSBufListMembership );
+    CPPUNIT_TEST( testSBufListJoin );
+    CPPUNIT_TEST_SUITE_END();
+protected:
+    void testSBufListMembership();
+    void testSBufListJoin();
+};
+
+#endif
index 13b3c09fcd6ae143769738807638e361a1fbfb5a..497a2ab6d5fff7a846b9aa00b4cae6d6427d2b9a 100644 (file)
 #include "MemPool.h"
 #include "profiler/Profiler.h"
 
+/** A list of C-strings
+ *
+ * \deprecated use SBufList instead
+ */
 class wordlist
 {
-
 public:
     MEMPROXY_CLASS(wordlist);
     char *key;
@@ -46,12 +49,29 @@ public:
 MEMPROXY_CLASS_INLINE(wordlist);
 
 class MemBuf;
-
+/** Add a null-terminated c-string to a wordlist
+ *
+ * \deprecated use SBufList.push_back(SBuf(word)) instead
+ */
 const char *wordlistAdd(wordlist **, const char *);
-void wordlistCat(const wordlist *, MemBuf * mb);
+/** Concatenate a wordlist
+ *
+ * \deprecated use SBufListContainerJoin(SBuf()) from SBufAlgos.h instead
+ */
+void wordlistCat(const wordlist *, MemBuf *);
+/** append a wordlist to another
+ *
+ * \deprecated use SBufList.merge(otherwordlist) instead
+ */
 void wordlistAddWl(wordlist **, wordlist *);
+/** Concatenate the words in a wordlist
+ *
+ * \deprecated use SBufListContainerJoin(SBuf()) from SBufAlgos.h instead
+ */
 void wordlistJoin(wordlist **, wordlist **);
+/// duplicate a wordlist
 wordlist *wordlistDup(const wordlist *);
+/// destroy a wordlist
 void wordlistDestroy(wordlist **);
 
 #endif /* SQUID_WORDLIST_H */