Merge lp:~squid/squid/sbuflist-merge into lp:~squid/squid/trunk

Proposed by Francesco Chemolli
Status: Merged
Merged at revision: 13182
Proposed branch: lp:~squid/squid/sbuflist-merge
Merge into: lp:~squid/squid/trunk
Diff against target: 358 lines (+250/-2)
9 files modified
src/Makefile.am (+48/-0)
src/SBuf.h (+1/-0)
src/SBufAlgos.h (+76/-0)
src/SBufList.cc (+9/-0)
src/SBufList.h (+17/-0)
src/tests/testSBufList.cc (+37/-0)
src/tests/testSBufList.h (+19/-0)
src/wordlist.cc (+11/-0)
src/wordlist.h (+32/-2)
To merge this branch: bzr merge lp:~squid/squid/sbuflist-merge
Reviewer Review Type Date Requested Status
squid Pending
Review via email: mp+199024@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Francesco Chemolli (kinkie) wrote :

Last reviewing activity on mailing-list Dec 5th 2013.

lp:~squid/squid/sbuflist-merge updated
13172. By Francesco Chemolli

Clarified comment in SBufAlgos.h, improved coding standards adherence

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/Makefile.am'
2--- src/Makefile.am 2013-11-29 19:47:54 +0000
3+++ src/Makefile.am 2013-12-14 15:58:20 +0000
4@@ -1073,6 +1073,7 @@
5 tests/testString \
6 tests/testURL \
7 tests/testSBuf \
8+ tests/testSBufList \
9 tests/testConfigParser \
10 tests/testStatHist \
11 tests/testVector
12@@ -2539,6 +2540,8 @@
13 String.cc \
14 cache_cf.h \
15 YesNoNone.h \
16+ $(SBUF_SOURCE) \
17+ tests/stub_SBufDetailedStats.cc \
18 tests/stub_cache_cf.cc \
19 tests/stub_cache_manager.cc \
20 tests/stub_debug.cc \
21@@ -2988,6 +2991,9 @@
22 tests/stub_mem.cc \
23 MemBuf.cc \
24 String.cc \
25+ $(SBUF_SOURCE) \
26+ SBufDetailedStats.h \
27+ tests/stub_SBufDetailedStats.cc \
28 tests/testMain.cc \
29 tests/testString.cc \
30 tests/testString.h \
31@@ -3668,12 +3674,54 @@
32 $(COMMON_LIBS)
33 tests_testSBuf_DEPENDENCIES= $(SQUID_CPPUNIT_LA)
34
35+tests_testSBufList_SOURCES= \
36+ tests/testSBufList.h \
37+ tests/testSBufList.cc \
38+ tests/testMain.cc \
39+ $(SBUF_SOURCE) \
40+ SBufList.h \
41+ SBufList.cc \
42+ SBufAlgos.h \
43+ SBufDetailedStats.h \
44+ tests/stub_SBufDetailedStats.cc \
45+ SBufStream.h \
46+ tests/stub_time.cc \
47+ mem.cc \
48+ tests/stub_debug.cc \
49+ tests/stub_event.cc \
50+ tests/stub_fatal.cc \
51+ tests/stub_HelperChildConfig.cc \
52+ tests/stub_cache_cf.cc \
53+ tests/stub_cache_manager.cc \
54+ tests/stub_store_stats.cc \
55+ tests/stub_tools.cc \
56+ SquidString.h \
57+ String.cc \
58+ tests/stub_wordlist.cc \
59+ tests/stub_MemBuf.cc
60+nodist_tests_testSBufList_SOURCES=$(TESTSOURCES)
61+tests_testSBufList_LDFLAGS = $(LIBADD_DL)
62+tests_testSBufList_LDADD=\
63+ $(SQUID_CPPUNIT_LIBS) \
64+ $(SQUID_CPPUNIT_LA) \
65+ $(COMPAT_LIB) \
66+ libsquid.la \
67+ ip/libip.la \
68+ mgr/libmgr.la \
69+ base/libbase.la \
70+ $(top_builddir)/lib/libmiscutil.la \
71+ $(COMMON_LIBS)
72+tests_testSBufList_DEPENDENCIES= $(SQUID_CPPUNIT_LA)
73+
74 tests_testConfigParser_SOURCES = \
75 ClientInfo.h \
76 Mem.h \
77 tests/stub_mem.cc \
78 tests/stub_MemBuf.cc \
79 tests/stub_time.cc \
80+ $(SBUF_SOURCE) \
81+ SBufDetailedStats.h \
82+ tests/stub_SBufDetailedStats.cc \
83 String.cc \
84 ConfigParser.cc \
85 fatal.h \
86
87=== modified file 'src/SBuf.h'
88--- src/SBuf.h 2013-10-08 04:17:17 +0000
89+++ src/SBuf.h 2013-12-14 15:58:20 +0000
90@@ -55,6 +55,7 @@
91 #define SQUIDSBUFPRINT(s) (s).plength(),(s).rawContent()
92 #endif /* SQUIDSBUFPH */
93
94+// TODO: move within SBuf and rename
95 typedef enum {
96 caseSensitive,
97 caseInsensitive
98
99=== added file 'src/SBufAlgos.h'
100--- src/SBufAlgos.h 1970-01-01 00:00:00 +0000
101+++ src/SBufAlgos.h 2013-12-14 15:58:20 +0000
102@@ -0,0 +1,76 @@
103+#ifndef SQUID_SBUFALGOS_H_
104+#define SQUID_SBUFALGOS_H_
105+
106+#include "SBuf.h"
107+
108+#include <algorithm>
109+#include <numeric>
110+
111+/// SBuf equality predicate for STL algorithms etc
112+class SBufEqual
113+{
114+public:
115+ explicit SBufEqual(const SBuf &reference, SBufCaseSensitive sensitivity = caseSensitive) :
116+ reference_(reference), sensitivity_(sensitivity) {}
117+ bool operator() (const SBuf & checking) { return checking.compare(reference_,sensitivity_) == 0; }
118+private:
119+ SBuf reference_;
120+ SBufCaseSensitive sensitivity_;
121+};
122+
123+/// SBuf "starts with" predicate for STL algorithms etc
124+class SBufStartsWith
125+{
126+public:
127+ explicit SBufStartsWith(const SBuf &prefix, SBufCaseSensitive sensitivity = caseSensitive) :
128+ prefix_(prefix), sensitivity_(sensitivity) {}
129+ bool operator() (const SBuf & checking) { return checking.startsWith(prefix_,sensitivity_); }
130+private:
131+ SBuf prefix_;
132+ SBufCaseSensitive sensitivity_;
133+};
134+
135+/** SBuf size addition accumulator for STL contaniners
136+ *
137+ * Equivalent to prefix_length + SBuf.length() + separator.length()
138+ */
139+class SBufAddLength
140+{
141+public:
142+ explicit SBufAddLength(const SBuf &separator) :
143+ separatorLen_(separator.length()) {}
144+ SBuf::size_type operator()(const SBuf::size_type sz, const SBuf & item) {
145+ return sz + item.length() + separatorLen_;
146+ }
147+private:
148+ SBuf::size_type separatorLen_;
149+};
150+
151+/// join all the SBuf in a container of SBuf into a single SBuf, separating with separator
152+template <class Container>
153+SBuf
154+SBufContainerJoin(const Container &items, const SBuf& separator)
155+{
156+ // optimization: pre-calculate needed storage
157+ const SBuf::size_type sz = std::accumulate(items.begin(), items.end(), 0, SBufAddLength(separator));
158+
159+ // sz can be zero in two cases: either items is empty, or all items
160+ // are zero-length. In the former case, we must protect against
161+ // dereferencing the iterator later on, and checking sz is more efficient
162+ // than checking items.size(). This check also provides an optimization
163+ // for the latter case without adding complexity.
164+ if (sz == 0)
165+ return SBuf();
166+
167+ SBuf rv;
168+ rv.reserveSpace(sz);
169+
170+ typename Container::const_iterator i(items.begin());
171+ rv.append(*i);
172+ ++i;
173+ for (;i != items.end(); ++i)
174+ rv.append(separator).append(*i);
175+ return rv;
176+}
177+
178+#endif /* SQUID_SBUFALGOS_H_ */
179
180=== added file 'src/SBufList.cc'
181--- src/SBufList.cc 1970-01-01 00:00:00 +0000
182+++ src/SBufList.cc 2013-12-14 15:58:20 +0000
183@@ -0,0 +1,9 @@
184+#include "squid.h"
185+#include "SBufAlgos.h"
186+#include "SBufList.h"
187+
188+bool
189+IsMember(const SBufList & sl, const SBuf &S, const SBufCaseSensitive case_sensitive)
190+{
191+ return std::find_if(sl.begin(), sl.end(), SBufEqual(S,case_sensitive)) != sl.end();
192+}
193
194=== added file 'src/SBufList.h'
195--- src/SBufList.h 1970-01-01 00:00:00 +0000
196+++ src/SBufList.h 2013-12-14 15:58:20 +0000
197@@ -0,0 +1,17 @@
198+#ifndef SQUID_SBUFLIST_H
199+#define SQUID_SBUFLIST_H
200+
201+#include "SBuf.h"
202+
203+#include <list>
204+
205+typedef std::list<SBuf> SBufList;
206+
207+/** check for membership
208+ *
209+ * \return true if the supplied SBuf is a member of the list
210+ * \param case_sensitive one of caseSensitive or caseInsensitive
211+ */
212+bool IsMember(const SBufList &, const SBuf &, const SBufCaseSensitive isCaseSensitive = caseSensitive);
213+
214+#endif /* SQUID_SBUFLIST_H */
215
216=== added file 'src/tests/testSBufList.cc'
217--- src/tests/testSBufList.cc 1970-01-01 00:00:00 +0000
218+++ src/tests/testSBufList.cc 2013-12-14 15:58:20 +0000
219@@ -0,0 +1,37 @@
220+#include "squid.h"
221+#include "SBufAlgos.h"
222+#include "SBufList.h"
223+#include "testSBufList.h"
224+
225+CPPUNIT_TEST_SUITE_REGISTRATION( testSBufList );
226+
227+SBuf literal("The quick brown fox jumped over the lazy dog");
228+static int sbuf_tokens_number=9;
229+static SBuf tokens[]={
230+ SBuf("The",3), SBuf("quick",5), SBuf("brown",5), SBuf("fox",3),
231+ SBuf("jumped",6), SBuf("over",4), SBuf("the",3), SBuf("lazy",4),
232+ SBuf("dog",3)
233+};
234+
235+void
236+testSBufList::testSBufListMembership()
237+{
238+ SBufList foo;
239+ for (int j=0; j<sbuf_tokens_number; ++j)
240+ foo.push_back(tokens[j]);
241+ CPPUNIT_ASSERT_EQUAL(true,IsMember(foo,SBuf("fox")));
242+ CPPUNIT_ASSERT_EQUAL(true,IsMember(foo,SBuf("Fox"),caseInsensitive));
243+ CPPUNIT_ASSERT_EQUAL(false,IsMember(foo,SBuf("garble")));
244+}
245+
246+void
247+testSBufList::testSBufListJoin()
248+{
249+ SBufList foo;
250+ CPPUNIT_ASSERT_EQUAL(SBuf(""),SBufContainerJoin(foo,SBuf()));
251+ CPPUNIT_ASSERT_EQUAL(SBuf(""),SBufContainerJoin(foo,SBuf()));
252+ for (int j = 0; j < sbuf_tokens_number; ++j)
253+ foo.push_back(tokens[j]);
254+ SBuf joined=SBufContainerJoin(foo,SBuf(" "));
255+ CPPUNIT_ASSERT_EQUAL(literal,joined);
256+}
257
258=== added file 'src/tests/testSBufList.h'
259--- src/tests/testSBufList.h 1970-01-01 00:00:00 +0000
260+++ src/tests/testSBufList.h 2013-12-14 15:58:20 +0000
261@@ -0,0 +1,19 @@
262+#ifndef SQUID_SRC_TEST_TESTSBUF_H
263+#define SQUID_SRC_TEST_TESTSBUF_H
264+
265+#include <cppunit/extensions/HelperMacros.h>
266+
267+#include "OutOfBoundsException.h"
268+
269+class testSBufList : public CPPUNIT_NS::TestFixture
270+{
271+ CPPUNIT_TEST_SUITE( testSBufList );
272+ CPPUNIT_TEST( testSBufListMembership );
273+ CPPUNIT_TEST( testSBufListJoin );
274+ CPPUNIT_TEST_SUITE_END();
275+protected:
276+ void testSBufListMembership();
277+ void testSBufListJoin();
278+};
279+
280+#endif
281
282=== modified file 'src/wordlist.cc'
283--- src/wordlist.cc 2013-10-25 00:13:46 +0000
284+++ src/wordlist.cc 2013-12-14 15:58:20 +0000
285@@ -109,3 +109,14 @@
286
287 return D;
288 }
289+
290+SBufList
291+ToSBufList(wordlist *wl)
292+{
293+ SBufList rv;
294+ while (wl != NULL) {
295+ rv.push_back(SBuf(wl->key));
296+ wl = wl->next;
297+ }
298+ return rv;
299+}
300
301=== modified file 'src/wordlist.h'
302--- src/wordlist.h 2012-09-21 14:57:30 +0000
303+++ src/wordlist.h 2013-12-14 15:58:20 +0000
304@@ -33,10 +33,14 @@
305 #include "globals.h"
306 #include "MemPool.h"
307 #include "profiler/Profiler.h"
308+#include "SBufList.h"
309
310+/** A list of C-strings
311+ *
312+ * \deprecated use SBufList instead
313+ */
314 class wordlist
315 {
316-
317 public:
318 MEMPROXY_CLASS(wordlist);
319 char *key;
320@@ -47,11 +51,37 @@
321
322 class MemBuf;
323
324+/** Add a null-terminated c-string to a wordlist
325+ *
326+ * \deprecated use SBufList.push_back(SBuf(word)) instead
327+ */
328 const char *wordlistAdd(wordlist **, const char *);
329-void wordlistCat(const wordlist *, MemBuf * mb);
330+
331+/** Concatenate a wordlist
332+ *
333+ * \deprecated use SBufListContainerJoin(SBuf()) from SBufAlgos.h instead
334+ */
335+void wordlistCat(const wordlist *, MemBuf *);
336+
337+/** append a wordlist to another
338+ *
339+ * \deprecated use SBufList.merge(otherwordlist) instead
340+ */
341 void wordlistAddWl(wordlist **, wordlist *);
342+
343+/** Concatenate the words in a wordlist
344+ *
345+ * \deprecated use SBufListContainerJoin(SBuf()) from SBufAlgos.h instead
346+ */
347 void wordlistJoin(wordlist **, wordlist **);
348+
349+/// duplicate a wordlist
350 wordlist *wordlistDup(const wordlist *);
351+
352+/// destroy a wordlist
353 void wordlistDestroy(wordlist **);
354
355+/// convert a wordlist to a SBufList
356+SBufList ToSBufList(wordlist *);
357+
358 #endif /* SQUID_WORDLIST_H */

Subscribers

People subscribed via source and target branches

to all changes: