Algorithms.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9#ifndef SQUID_SBUFALGOS_H_
10#define SQUID_SBUFALGOS_H_
11
12#include "sbuf/SBuf.h"
13
14#include <algorithm>
15#include <numeric>
16
19{
20public:
21 explicit SBufEqual(const SBuf &reference, SBufCaseSensitive sensitivity = caseSensitive) :
22 reference_(reference), sensitivity_(sensitivity) {}
23 bool operator() (const SBuf & checking) { return checking.compare(reference_,sensitivity_) == 0; }
24private:
27};
28
31{
32public:
33 explicit SBufStartsWith(const SBuf &prefix, SBufCaseSensitive sensitivity = caseSensitive) :
34 prefix_(prefix), sensitivity_(sensitivity) {}
35 bool operator() (const SBuf & checking) { return checking.startsWith(prefix_,sensitivity_); }
36private:
39};
40
46{
47public:
48 explicit SBufAddLength(const SBuf &separator) :
49 separatorLen_(separator.length()) {}
51 return sz + item.length() + separatorLen_;
52 }
53private:
55};
56
66template <class ContainerIterator>
67SBuf&
68JoinContainerIntoSBuf(SBuf &dest, const ContainerIterator &begin,
69 const ContainerIterator &end, const SBuf& separator,
70 const SBuf& prefix = SBuf(), const SBuf& suffix = SBuf())
71{
72 if (begin == end) {
73 dest.append(prefix).append(suffix);
74 return dest;
75 }
76
77 // optimization: pre-calculate needed storage
78 const SBuf::size_type totalContainerSize =
79 std::accumulate(begin, end, 0, SBufAddLength(separator)) +
80 dest.length() + prefix.length() + suffix.length();
82 req.minSpace = totalContainerSize;
83 dest.reserve(req);
84
85 auto i = begin;
86 dest.append(prefix);
87 dest.append(*i);
88 ++i;
89 for (; i != end; ++i)
90 dest.append(separator).append(*i);
91 dest.append(suffix);
92 return dest;
93}
94
96template <class ContainerIterator>
97SBuf
98JoinContainerToSBuf(const ContainerIterator &begin,
99 const ContainerIterator &end, const SBuf& separator,
100 const SBuf& prefix = SBuf(), const SBuf& suffix = SBuf())
101{
102 SBuf rv;
103 return JoinContainerIntoSBuf(rv, begin, end, separator, prefix, suffix);
104}
105
106namespace std {
108template <>
109struct hash<SBuf>
110{
111 size_t operator()(const SBuf &) const noexcept;
112};
113}
114
118{
119public:
120 std::size_t operator()(const SBuf &) const noexcept;
121};
122
126{
127public:
128 bool operator()(const SBuf &lhs, const SBuf &rhs) const
129 {
130 // Optimization: Do not iterate strings of different lengths.
131 return lhs.length() == rhs.length() && (lhs.compare(rhs, caseInsensitive) == 0);
132 }
133};
134
135#endif /* SQUID_SBUFALGOS_H_ */
136
SBuf & JoinContainerIntoSBuf(SBuf &dest, const ContainerIterator &begin, const ContainerIterator &end, const SBuf &separator, const SBuf &prefix=SBuf(), const SBuf &suffix=SBuf())
Definition: Algorithms.h:68
SBuf JoinContainerToSBuf(const ContainerIterator &begin, const ContainerIterator &end, const SBuf &separator, const SBuf &prefix=SBuf(), const SBuf &suffix=SBuf())
convenience wrapper of JoinContainerIntoSBuf with no caller-supplied SBuf
Definition: Algorithms.h:98
SBufCaseSensitive
Definition: SBuf.h:36
@ caseInsensitive
Definition: SBuf.h:38
@ caseSensitive
Definition: SBuf.h:37
bool operator()(const SBuf &lhs, const SBuf &rhs) const
Definition: Algorithms.h:128
std::size_t operator()(const SBuf &) const noexcept
Definition: Algorithms.cc:35
SBuf::size_type operator()(const SBuf::size_type sz, const SBuf &item)
Definition: Algorithms.h:50
SBuf::size_type separatorLen_
Definition: Algorithms.h:54
SBufAddLength(const SBuf &separator)
Definition: Algorithms.h:48
SBuf equality predicate for STL algorithms etc.
Definition: Algorithms.h:19
SBufEqual(const SBuf &reference, SBufCaseSensitive sensitivity=caseSensitive)
Definition: Algorithms.h:21
SBuf reference_
Definition: Algorithms.h:25
bool operator()(const SBuf &checking)
Definition: Algorithms.h:23
SBufCaseSensitive sensitivity_
Definition: Algorithms.h:26
Named SBuf::reserve() parameters. Defaults ask for and restrict nothing.
Definition: SBuf.h:697
size_type minSpace
allocate [at least this much] if spaceSize() is smaller
Definition: SBuf.h:707
SBuf "starts with" predicate for STL algorithms etc.
Definition: Algorithms.h:31
bool operator()(const SBuf &checking)
Definition: Algorithms.h:35
SBufStartsWith(const SBuf &prefix, SBufCaseSensitive sensitivity=caseSensitive)
Definition: Algorithms.h:33
SBufCaseSensitive sensitivity_
Definition: Algorithms.h:38
Definition: SBuf.h:94
size_type length() const
Returns the number of bytes stored in SBuf.
Definition: SBuf.h:415
size_type reserve(const SBufReservationRequirements &requirements)
Definition: SBuf.cc:112
SBuf & append(const SBuf &S)
Definition: SBuf.cc:185
bool startsWith(const SBuf &S, const SBufCaseSensitive isCaseSensitive=caseSensitive) const
Definition: SBuf.cc:442
MemBlob::size_type size_type
Definition: SBuf.h:96
int compare(const SBuf &S, const SBufCaseSensitive isCaseSensitive, const size_type n) const
Definition: SBuf.cc:352
STL namespace.
int const char size_t
Definition: stub_liblog.cc:83
static hash_table * hash
Definition: text_backend.cc:41

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors