Hi,
v2 attached, fixing these issues.
On Tue, Nov 26, 2013 at 2:11 AM, Amos Jeffries <[email protected]> wrote:
> On 26/11/2013 11:40 a.m., Kinkie wrote:
>> Hi,
>> attached is v1 of SBufList. Its purpose is to be the eventual heir
>> to wordlist. For this reason I've implemented the bare minimum API to
>> cover wordlist's API, and kept code to a minimum by making it a
>> typedef of std::list.
>>
>> The patch is missing Makefile.am integration, it's a cherrypick of
>> lp:~squid/squid/stringng. I'll hand-merge Makefile.am if the code is
>> accepted.
>>
>> The only remaining bit of that branch is the tokenizer. As Alex
>> mentioned he's going to draft an API for that, it's quite likely that
>> if the two patches I'm proposing today and Amos' SBuf-based String
>> reimplementation are accepted, then this branch can (finally) be
>> closed, and the chase can begin for having clients use StringNG.
>>
>> Thanks,
>>
>
>
> in src/SBufList.cc:
> * copyright blurb
>
> * isMember() has a few issues:
> - please put return type on its own line consistent with the squid
> coding style guidelines
>
> in src/SBufList.h:
> * copyright blurb
>
> * please use a predefine "class wordlist;" instead of including wordlist.h
>
>
> Amos
>
--
/kinkie
=== added file 'src/SBufList.cc'
--- src/SBufList.cc 1970-01-01 00:00:00 +0000
+++ src/SBufList.cc 2013-11-26 09:16:32 +0000
@@ -0,0 +1,51 @@
+#include "squid.h"
+#include "SBufList.h"
+#include "wordlist.h"
+
+bool
+isMember(const SBufList & sl, const SBuf &S, SBufCaseSensitive case_sensitive)
+{
+ for (SBufList::const_iterator i = sl.begin(); i != sl.end(); ++i)
+ if (i->compare(S,case_sensitive) == 0)
+ return true;
+ return false;
+}
+
+SBuf
+SBufListJoin(const SBufList &list, const SBuf &separator)
+{
+ if (list.size() == 0)
+ return SBuf("");
+ if (list.size() == 1)
+ return list.front();
+
+ SBufList::const_iterator i;
+ // calculate the total size of the return value
+ SBuf::size_type sz = 0;
+ for (i = list.begin(); i != list.end(); ++i)
+ sz += i->length();
+ sz += separator.length()*list.size();
+
+ SBuf rv;
+ rv.rawSpace(sz);
+
+ i = list.begin();
+ rv.append(*i);
+ ++i;
+ for (; i!= list.end(); ++i) {
+ rv.append(separator);
+ rv.append(*i);
+ }
+ return rv;
+}
+
+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-11-26 09:16:32 +0000
@@ -0,0 +1,26 @@
+#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 &, SBufCaseSensitive isCaseSensitive = caseSensitive);
+
+/** join a SBufList into a SBuf using the supplied separator.
+ */
+SBuf SBufListJoin(const SBufList &list, const SBuf &separator);
+
+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-11-25 18:33:15 +0000
@@ -0,0 +1,34 @@
+#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<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;
+ for (int j = 0; j < sbuf_tokens_number; ++j)
+ foo.push_back(tokens[j]);
+ SBuf joined=SBufListJoin(foo,SBuf(" "));
+ CPPUNIT_ASSERT_EQUAL(literal,joined);
+}
=== added file 'src/tests/testSBufList.h'
--- src/tests/testSBufList.h 1970-01-01 00:00:00 +0000
+++ src/tests/testSBufList.h 2013-11-25 18:33:15 +0000
@@ -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