]>
Commit | Line | Data |
---|---|---|
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 |
9 | class SBufEqual |
10 | { | |
6a17a36d | 11 | public: |
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 |
15 | private: |
16 | SBuf reference_; | |
17 | SBufCaseSensitive sensitivity_; | |
18 | }; | |
19 | ||
20 | /// SBuf "starts with" predicate for STL algorithms etc | |
514fc315 FC |
21 | class SBufStartsWith |
22 | { | |
6a17a36d | 23 | public: |
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 |
27 | private: |
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 |
36 | class SBufAddLength |
37 | { | |
6a17a36d FC |
38 | public: |
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 | } | |
44 | private: | |
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 | |
49 | template <class Container> | |
50 | SBuf | |
51 | SBufContainerJoin(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_ */ |