]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Ready for testing
authorFrancesco Chemolli <kinkie@squid-cache.org>
Fri, 11 Nov 2016 22:43:43 +0000 (22:43 +0000)
committerFrancesco Chemolli <kinkie@squid-cache.org>
Fri, 11 Nov 2016 22:43:43 +0000 (22:43 +0000)
src/acl/UserData.cc
src/sbuf/Algorithms.h
src/tests/testSBufList.cc

index a139d3cde0a82007b50914a4fedf61444f439b14..77047249089d53cbe94fd7be3fa3e0f94ee38e3f 100644 (file)
@@ -50,7 +50,9 @@ ACLUserData::dump() const
 
     sl.insert(sl.end(), userDataNames.begin(), userDataNames.end());
 
-    debugs(28,5, "ACLUserData dump output: " << SBufContainerJoin(userDataNames,SBuf(" ")));
+    debugs(28,5, "ACLUserData dump output: " <<
+        JoinContainerToSBuf(userDataNames.begin(), userDataNames.end(),
+        SBuf(" ")));
     return sl;
 }
 
index e65284851aadedd9b0443c778a9c279317df7483..b24de5ea8c61f68b9322a7c6ff34eea2664dd9d7 100644 (file)
@@ -54,31 +54,53 @@ private:
     SBuf::size_type separatorLen_;
 };
 
-/// 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)
+/** Join container of SBufs and append to supplied target
+ *
+ * append to the target SBuf all elements in the [begin,end) range from
+ * an iterable container, prefixed by prefix, separated by separator and
+ * followed by suffix. Prefix and suffix are added also in case of empty
+ * iterable
+ *
+ * \return the modified dest
+ */
+template <class ContainerIterator>
+SBuf&
+JoinContainerIntoSBuf(SBuf &dest, const ContainerIterator &begin,
+               const ContainerIterator &end, const SBuf& separator,
+               const SBuf& prefix = SBuf(), const SBuf& suffix = SBuf())
 {
-    // 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();
+    if (begin == end) {
+       dest.append(prefix).append(suffix);
+        return dest;
+    }
 
-    SBuf rv;
-    rv.reserveSpace(sz);
+    // optimization: pre-calculate needed storage
+    const SBuf::size_type totalContainerSize =
+               std::accumulate(begin, end, 0, SBufAddLength(separator)) +
+                       dest.length() + prefix.length() + suffix.length();
+    SBufReservationRequirements req;
+    req.minSpace = totalContainerSize;
+    dest.reserve(req);
 
-    typename Container::const_iterator i(items.begin());
-    rv.append(*i);
+    auto i = begin;
+    dest.append(prefix);
+    dest.append(*i);
     ++i;
-    for (; i != items.end(); ++i)
-        rv.append(separator).append(*i);
-    return rv;
+    for (; i != end; ++i)
+        dest.append(separator).append(*i);
+    dest.append(suffix);
+    return dest;
+}
+
+/// convenience wrapper of JoinContainerIntoSBuf with no caller-supplied SBuf
+template <class ContainerIterator>
+SBuf
+JoinContainerToSBuf(const ContainerIterator &begin,
+               const ContainerIterator &end, const SBuf& separator,
+               const SBuf& prefix = SBuf(), const SBuf& suffix = SBuf())
+{
+       SBuf rv;
+       return JoinContainerIntoSBuf(rv, begin, end, separator, prefix, suffix);
 }
 
 namespace std {
index cc4ca0e16b675358772df5d67d3f18126ddf813c..01fdf828c734aace44d2a541b90ecdf63360d612 100644 (file)
@@ -37,11 +37,17 @@ void
 testSBufList::testSBufListJoin()
 {
     SBufList foo;
-    CPPUNIT_ASSERT_EQUAL(SBuf(""),SBufContainerJoin(foo,SBuf()));
-    CPPUNIT_ASSERT_EQUAL(SBuf(""),SBufContainerJoin(foo,SBuf()));
+    CPPUNIT_ASSERT_EQUAL(SBuf(""),JoinContainerToSBuf(foo.begin(), foo.end(),SBuf()));
     for (int j = 0; j < sbuf_tokens_number; ++j)
         foo.push_back(tokens[j]);
-    SBuf joined=SBufContainerJoin(foo,SBuf(" "));
+    SBuf joined=JoinContainerToSBuf(foo.begin(), foo.end(),SBuf(" "));
     CPPUNIT_ASSERT_EQUAL(literal,joined);
+    SBuf s1("1"), s2("2"), s3("3"), full("(1,2,3)");
+    SBufList sl{s1,s2,s3};
+    CPPUNIT_ASSERT_EQUAL(full, JoinContainerToSBuf(sl.begin(),
+               sl.end(), SBuf(","), SBuf("("), SBuf(")")));
+
+    CPPUNIT_ASSERT_EQUAL(SBuf(""),JoinContainerToSBuf(foo.begin(), foo.begin(),SBuf()));
+
 }