------------------------------------------------------------ revno: 13182 [merge] revision-id: kinkie@squid-cache.org-20131215092818-d82fkhluvh268d6r parent: squid3@treenet.co.nz-20131211111243-9lfbb56nz8ud7kts parent: kinkie@squid-cache.org-20131214155804-8eirq381xpef850z committer: Francesco Chemolli branch nick: trunk timestamp: Sun 2013-12-15 10:28:18 +0100 message: Merge SBufList ------------------------------------------------------------ Use --include-merges or -n0 to see merged revisions. ------------------------------------------------------------ # Bazaar merge directive format 2 (Bazaar 0.90) # revision_id: kinkie@squid-cache.org-20131215092818-d82fkhluvh268d6r # target_branch: http://bzr.squid-cache.org/bzr/squid3/trunk/ # testament_sha1: a10916bcfd97eefc45e7cf23459078666c8ab4c9 # timestamp: 2013-12-15 09:53:41 +0000 # source_branch: http://bzr.squid-cache.org/bzr/squid3/trunk/ # base_revision_id: squid3@treenet.co.nz-20131211111243-\ # 9lfbb56nz8ud7kts # # Begin patch === modified file 'src/Makefile.am' --- src/Makefile.am 2013-11-29 19:47:54 +0000 +++ src/Makefile.am 2013-12-05 15:26:22 +0000 @@ -1073,6 +1073,7 @@ tests/testString \ tests/testURL \ tests/testSBuf \ + tests/testSBufList \ tests/testConfigParser \ tests/testStatHist \ tests/testVector @@ -2539,6 +2540,8 @@ String.cc \ cache_cf.h \ YesNoNone.h \ + $(SBUF_SOURCE) \ + tests/stub_SBufDetailedStats.cc \ tests/stub_cache_cf.cc \ tests/stub_cache_manager.cc \ tests/stub_debug.cc \ @@ -2988,6 +2991,9 @@ tests/stub_mem.cc \ MemBuf.cc \ String.cc \ + $(SBUF_SOURCE) \ + SBufDetailedStats.h \ + tests/stub_SBufDetailedStats.cc \ tests/testMain.cc \ tests/testString.cc \ tests/testString.h \ @@ -3668,12 +3674,54 @@ $(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 \ tests/stub_mem.cc \ tests/stub_MemBuf.cc \ tests/stub_time.cc \ + $(SBUF_SOURCE) \ + SBufDetailedStats.h \ + tests/stub_SBufDetailedStats.cc \ String.cc \ ConfigParser.cc \ fatal.h \ === modified file 'src/SBuf.h' --- src/SBuf.h 2013-10-08 04:17:17 +0000 +++ src/SBuf.h 2013-12-04 18:37:08 +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-14 15:58:04 +0000 @@ -0,0 +1,76 @@ +#ifndef SQUID_SBUFALGOS_H_ +#define SQUID_SBUFALGOS_H_ + +#include "SBuf.h" + +#include +#include + +/// SBuf equality predicate for STL algorithms etc +class SBufEqual +{ +public: + explicit SBufEqual(const SBuf &reference, SBufCaseSensitive sensitivity = caseSensitive) : + reference_(reference), sensitivity_(sensitivity) {} + bool operator() (const SBuf & checking) { return checking.compare(reference_,sensitivity_) == 0; } +private: + SBuf reference_; + SBufCaseSensitive sensitivity_; +}; + +/// SBuf "starts with" predicate for STL algorithms etc +class SBufStartsWith +{ +public: + explicit SBufStartsWith(const SBuf &prefix, SBufCaseSensitive sensitivity = caseSensitive) : + prefix_(prefix), sensitivity_(sensitivity) {} + bool operator() (const SBuf & checking) { return checking.startsWith(prefix_,sensitivity_); } +private: + SBuf prefix_; + SBufCaseSensitive sensitivity_; +}; + +/** SBuf size addition accumulator for STL contaniners + * + * Equivalent to prefix_length + SBuf.length() + separator.length() + */ +class SBufAddLength +{ +public: + explicit SBufAddLength(const SBuf &separator) : + separatorLen_(separator.length()) {} + SBuf::size_type operator()(const SBuf::size_type sz, const SBuf & item) { + return sz + item.length() + separatorLen_; + } +private: + SBuf::size_type separatorLen_; +}; + +/// 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 + const SBuf::size_type sz = std::accumulate(items.begin(), items.end(), 0, SBufAddLength(separator)); + + // sz can be zero in two cases: either items is empty, or all items + // are zero-length. In the former case, we must protect against + // dereferencing the iterator later on, and checking sz is more efficient + // than checking items.size(). This check also provides an optimization + // for the latter case without adding complexity. + 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_ */ === added file 'src/SBufList.cc' --- src/SBufList.cc 1970-01-01 00:00:00 +0000 +++ src/SBufList.cc 2013-12-14 15:58:04 +0000 @@ -0,0 +1,9 @@ +#include "squid.h" +#include "SBufAlgos.h" +#include "SBufList.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(); +} === added file 'src/SBufList.h' --- src/SBufList.h 1970-01-01 00:00:00 +0000 +++ src/SBufList.h 2013-12-05 15:26:22 +0000 @@ -0,0 +1,17 @@ +#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); + +#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-04 18:37:08 +0000 @@ -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 + +#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 === modified file 'src/wordlist.cc' --- src/wordlist.cc 2013-10-25 00:13:46 +0000 +++ src/wordlist.cc 2013-12-05 15:26:22 +0000 @@ -109,3 +109,14 @@ return D; } + +SBufList +ToSBufList(wordlist *wl) +{ + SBufList rv; + while (wl != NULL) { + rv.push_back(SBuf(wl->key)); + wl = wl->next; + } + return rv; +} === modified file 'src/wordlist.h' --- src/wordlist.h 2012-09-21 14:57:30 +0000 +++ src/wordlist.h 2013-12-05 15:26:22 +0000 @@ -33,10 +33,14 @@ #include "globals.h" #include "MemPool.h" #include "profiler/Profiler.h" +#include "SBufList.h" +/** A list of C-strings + * + * \deprecated use SBufList instead + */ class wordlist { - public: MEMPROXY_CLASS(wordlist); char *key; @@ -47,11 +51,37 @@ 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 **); +/// convert a wordlist to a SBufList +SBufList ToSBufList(wordlist *); + #endif /* SQUID_WORDLIST_H */