1 #ifndef SQUID_SBUFALGOS_H_
2 #define SQUID_SBUFALGOS_H_
9 /// SBuf equality predicate for STL algorithms etc
13 explicit SBufEqual(const SBuf
&reference
, SBufCaseSensitive sensitivity
= caseSensitive
) :
14 reference_(reference
), sensitivity_(sensitivity
) {}
15 bool operator() (const SBuf
& checking
) { return checking
.compare(reference_
,sensitivity_
) == 0; }
18 SBufCaseSensitive sensitivity_
;
21 /// SBuf "starts with" predicate for STL algorithms etc
25 explicit SBufStartsWith(const SBuf
&prefix
, SBufCaseSensitive sensitivity
= caseSensitive
) :
26 prefix_(prefix
), sensitivity_(sensitivity
) {}
27 bool operator() (const SBuf
& checking
) { return checking
.startsWith(prefix_
,sensitivity_
); }
30 SBufCaseSensitive sensitivity_
;
33 /** SBuf size addition accumulator for STL contaniners
35 * Equivalent to prefix_length + SBuf.length() + separator.length()
40 explicit SBufAddLength(const SBuf
&separator
) :
41 separatorLen_(separator
.length()) {}
42 SBuf::size_type
operator()(const SBuf::size_type sz
, const SBuf
& item
) {
43 return sz
+ item
.length() + separatorLen_
;
46 SBuf::size_type separatorLen_
;
49 /// join all the SBuf in a container of SBuf into a single SBuf, separating with separator
50 template <class Container
>
52 SBufContainerJoin(const Container
&items
, const SBuf
& separator
)
54 // optimization: pre-calculate needed storage
55 const SBuf::size_type sz
= std::accumulate(items
.begin(), items
.end(), 0, SBufAddLength(separator
));
57 // sz can be zero in two cases: either items is empty, or all items
58 // are zero-length. In the former case, we must protect against
59 // dereferencing the iterator later on, and checking sz is more efficient
60 // than checking items.size(). This check also provides an optimization
61 // for the latter case without adding complexity.
68 typename
Container::const_iterator
i(items
.begin());
71 for (; i
!= items
.end(); ++i
)
72 rv
.append(separator
).append(*i
);
76 #endif /* SQUID_SBUFALGOS_H_ */