=== modified file 'src/SBuf.h' --- src/SBuf.h 2013-10-08 04:17:17 +0000 +++ src/SBuf.h 2013-12-02 18:00:42 +0000 @@ -55,6 +55,7 @@ #define SQUIDSBUFPRINT(s) (s).plength(),(s).rawContent() #endif /* SQUIDSBUFPH */ +// TODO: move within SBuf and rename typedef enum { caseSensitive, caseInsensitive === added file 'src/SBufAlgos.h' --- src/SBufAlgos.h 1970-01-01 00:00:00 +0000 +++ src/SBufAlgos.h 2013-12-02 18:00:42 +0000 @@ -0,0 +1,80 @@ +#ifndef SQUID_SBUFALGOS_H_ +#define SQUID_SBUFALGOS_H_ + +#include "SBuf.h" +#include + +/// 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; +}; + +/// SBuf contents concatenation accumulator for STL containers +class SBufAddContent { +public: + explicit SBufAddContent(const SBuf& separator) : + separator_(separator), first(true) {} + SBuf operator() (SBuf accumulated, const SBuf &item) { + if (first) { + first = false; + accumulated.append(item); + return accumulated; + } + accumulated.append(separator_).append(item); + return accumulated; + } +private: + const SBuf separator_; + bool first; +}; + +/// join all the SBuf in a container of SBuf into a single SBuf, separating with separator +template +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)); + + SBuf rv; + rv.reserveSpace(sz); + rv = std::accumulate(items.begin(), items.end(), rv, SBufAddContent(separator)); + return rv; +} + + + +#endif /* SQUID_SBUFALGOS_H_ */ === added file 'src/SBufList.cc' --- src/SBufList.cc 1970-01-01 00:00:00 +0000 +++ src/SBufList.cc 2013-12-02 18:00:42 +0000 @@ -0,0 +1,27 @@ +#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(); +} + +SBuf +SBufListJoin(const SBufList &list, const SBuf &separator) +{ + return SBufContainerJoin(list,separator); +} + +SBufList +wordlistToSBufList(wordlist *wl) +{ + SBufList rv; + while (wl != NULL) { + rv.push_back(SBuf(wl->key)); + wl = wl->next; + } + return rv; +} === added file 'src/SBufList.h' --- src/SBufList.h 1970-01-01 00:00:00 +0000 +++ src/SBufList.h 2013-12-02 18:00:42 +0000 @@ -0,0 +1,26 @@ +#ifndef SQUID_SBUFLIST_H +#define SQUID_SBUFLIST_H + +#include "SBuf.h" + +#include + +typedef std::list 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); + +/** join a SBufList into a SBuf using the supplied separator. + */ +SBuf SBufListJoin(const SBufList &list, const SBuf &separator = SBuf()); + +class wordlist; +/** convert a wordlist to a SBufList + */ +SBufList wordlistToSbufList(wordlist *); + +#endif /* SQUID_SBUFLIST_H */ === added file 'src/tests/testSBufList.cc' --- src/tests/testSBufList.cc 1970-01-01 00:00:00 +0000 +++ src/tests/testSBufList.cc 2013-12-01 21:37:19 +0000 @@ -0,0 +1,36 @@ +#include "squid.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 + +#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