]> git.ipfire.org Git - thirdparty/squid.git/blame - src/SBufAlgos.h
Fixed formatting
[thirdparty/squid.git] / src / SBufAlgos.h
CommitLineData
6a17a36d
FC
1#ifndef SQUID_SBUFALGOS_H_
2#define SQUID_SBUFALGOS_H_
3
4#include "SBuf.h"
5#include <algorithm>
67083a4d 6#include <numeric>
6a17a36d
FC
7
8/// SBuf equality predicate for STL algorithms etc
514fc315
FC
9class SBufEqual
10{
6a17a36d 11public:
514fc315
FC
12 explicit SBufEqual(const SBuf &reference, SBufCaseSensitive sensitivity = caseSensitive) :
13 reference_(reference), sensitivity_(sensitivity) {}
14 bool operator() (const SBuf & checking) { return checking.compare(reference_,sensitivity_) == 0; }
6a17a36d
FC
15private:
16 SBuf reference_;
17 SBufCaseSensitive sensitivity_;
18};
19
20/// SBuf "starts with" predicate for STL algorithms etc
514fc315
FC
21class SBufStartsWith
22{
6a17a36d 23public:
514fc315
FC
24 explicit SBufStartsWith(const SBuf &prefix, SBufCaseSensitive sensitivity = caseSensitive) :
25 prefix_(prefix), sensitivity_(sensitivity) {}
26 bool operator() (const SBuf & checking) { return checking.startsWith(prefix_,sensitivity_); }
6a17a36d
FC
27private:
28 SBuf prefix_;
514fc315 29 SBufCaseSensitive sensitivity_;
6a17a36d
FC
30};
31
32/** SBuf size addition accumulator for STL contaniners
33 *
34 * Equivalent to prefix_length + SBuf.length() + separator.length()
35 */
514fc315
FC
36class SBufAddLength
37{
6a17a36d
FC
38public:
39 explicit SBufAddLength(const SBuf &separator) :
40 separator_len(separator.length()) {}
41 SBuf::size_type operator()(const SBuf::size_type sz, const SBuf & item) {
42 return sz + item.length() + separator_len;
43 }
44private:
45 SBuf::size_type separator_len;
46};
47
48/// join all the SBuf in a container of SBuf into a single SBuf, separating with separator
49template <class Container>
50SBuf
51SBufContainerJoin(const Container &items, const SBuf& separator)
52{
53 // optimization: pre-calculate needed storage
514fc315 54 const SBuf::size_type sz = std::accumulate(items.begin(), items.end(), 0, SBufAddLength(separator));
6a17a36d
FC
55
56 // protect against blindly dereferencing items.begin() if items.size()==0
57 if (sz == 0)
58 return SBuf();
59
60 SBuf rv;
61 rv.reserveSpace(sz);
62
63 typename Container::const_iterator i(items.begin());
64 rv.append(*i);
65 ++i;
66 for (;i != items.end(); ++i)
67 rv.append(separator).append(*i);
68 return rv;
69}
70
71#endif /* SQUID_SBUFALGOS_H_ */