]> git.ipfire.org Git - thirdparty/squid.git/blame - src/SquidString.h
Bug 5428: Warn if pkg-config is not found (#1902)
[thirdparty/squid.git] / src / SquidString.h
CommitLineData
0f15e632 1/*
b8ae064d 2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
0f15e632 3 *
bbc27441
AJ
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.
0f15e632 7 */
8
bbc27441
AJ
9/* DEBUG: section 67 String */
10
ff9d9458
FC
11#ifndef SQUID_SRC_SQUIDSTRING_H
12#define SQUID_SRC_SQUIDSTRING_H
0f15e632 13
f63c4b05 14#include "base/TextException.h"
675b8408 15#include "debug/Stream.h"
f63c4b05 16
e1f7507e
AJ
17#include <ostream>
18
bb790702
FC
19/* squid string placeholder (for printf) */
20#ifndef SQUIDSTRINGPH
21#define SQUIDSTRINGPH "%.*s"
b716b3d4 22#define SQUIDSTRINGPRINT(s) (s).psize(),(s).rawBuf()
bb790702
FC
23#endif /* SQUIDSTRINGPH */
24
30abd221 25class String
26{
0353e724 27
30abd221 28public:
8ee00d73 29 String() = default;
9e9ef416
RP
30 String(char const *);
31 String(String const &);
20a04c12
AJ
32 String(String && S) : size_(S.size_), len_(S.len_), buf_(S.buf_) {
33 S.buf_ = nullptr; // S is about to be destructed
34 S.size_ = S.len_ = 0;
35 }
30abd221 36 ~String();
37
b4197865 38 typedef size_t size_type; //storage size intentionally unspecified
556bfec0 39 const static size_type npos = static_cast<size_type>(-1);
a7a42b14 40
30abd221 41 String &operator =(char const *);
42 String &operator =(String const &);
20a04c12
AJ
43 String &operator =(String && S) {
44 if (this != &S) {
9c62c979 45 clean();
20a04c12
AJ
46 size_ = S.size_;
47 len_ = S.len_;
48 buf_ = S.buf_;
49 S.size_ = 0;
50 S.len_ = 0;
51 S.buf_ = nullptr; // S is about to be destructed
52 }
53 return *this;
54 }
55
30abd221 56 bool operator ==(String const &) const;
57 bool operator !=(String const &) const;
58
3db44fdf 59 /**
60 * Retrieve a single character in the string.
f63c4b05 61 \param aPos Position of character to retrieve.
3db44fdf 62 */
f63c4b05
AJ
63 char operator [](unsigned int aPos) const {
64 assert(aPos < size_);
65 return buf_[aPos];
66 }
67
90be6ff5
EB
68 /// The absolute size limit on data held in a String.
69 /// Since Strings can be nil-terminated implicitly it is best to ensure
70 /// the useful content length is strictly less than this limit.
98d0df03 71 static size_type SizeMaxXXX() { return SizeMax_; }
90be6ff5 72
f63c4b05 73 size_type size() const { return len_; }
3db44fdf 74
9b558d8a 75 /// variant of size() suited to be used for printf-alikes.
f63c4b05
AJ
76 /// throws when size() >= INT_MAX
77 int psize() const {
78 Must(size() < INT_MAX);
79 return size();
80 }
99524de7 81
99524de7
FC
82 /**
83 * Returns a raw pointer to the underlying backing store. The caller has been
84 * verified not to make any assumptions about null-termination
85 */
f63c4b05
AJ
86 char const * rawBuf() const { return buf_; }
87
99524de7
FC
88 /**
89 * Returns a raw pointer to the underlying backing store.
90 * The caller requires it to be null-terminated.
91 */
f63c4b05
AJ
92 char const * termedBuf() const { return buf_; }
93
2fe0439c 94 void assign(const char *str, int len);
30abd221 95 void clean();
96 void reset(char const *str);
97 void append(char const *buf, int len);
98 void append(char const *buf);
99 void append(char const);
9e9ef416 100 void append(String const &);
30abd221 101 void absorb(String &old);
826a1fed
FC
102 const char * pos(char const *aString) const;
103 const char * pos(char const ch) const;
b4f2886c 104 ///offset from string start of the first occurrence of ch
2c1fd837 105 /// returns String::npos if ch is not found
826a1fed
FC
106 size_type find(char const ch) const;
107 size_type find(char const *aString) const;
108 const char * rpos(char const ch) const;
109 size_type rfind(char const ch) const;
f63c4b05
AJ
110 int cmp(char const *) const;
111 int cmp(char const *, size_type count) const;
112 int cmp(String const &) const;
113 int caseCmp(char const *) const;
114 int caseCmp(char const *, size_type count) const;
115 int caseCmp(String const &str) const {
116 return caseCmp(str.rawBuf(),str.size());
117 }
30abd221 118
70df76e3
AR
119 /// Whether creating a totalLen-character string is safe (i.e., unlikely to assert).
120 /// Optional extras can be used for overflow-safe length addition.
121 /// Implementation has to add 1 because many String allocation methods do.
122 static bool CanGrowTo(size_type totalLen, const size_type extras = 0) { return SafeAdd(totalLen, extras) && SafeAdd(totalLen, 1); }
123 /// whether appending growthLen characters is safe (i.e., unlikely to assert)
124 bool canGrowBy(const size_type growthLen) const { return CanGrowTo(size(), growthLen); }
125
a7a42b14 126 String substr(size_type from, size_type to) const;
9b558d8a 127
f63c4b05 128 void cut(size_type newLength);
30abd221 129
30abd221 130private:
e7e1bf60 131 void allocAndFill(const char *str, int len);
a7a42b14
FC
132 void allocBuffer(size_type sz);
133 void setBuffer(char *buf, size_type sz);
e7e1bf60 134
aee3523a 135 bool defined() const {return buf_!=nullptr;}
b38b26cb
AJ
136 bool undefined() const {return !defined();}
137
30abd221 138 /* never reference these directly! */
f63c4b05 139 size_type size_ = 0; /* buffer size; limited by SizeMax_ */
30abd221 140
f63c4b05 141 size_type len_ = 0; /* current length */
30abd221 142
801593a9
AR
143 /// An earlier 64KB limit was meant to protect some fixed-size buffers, but
144 /// (a) we do not know where those buffers are (or whether they still exist)
145 /// (b) too many String users unknowingly exceeded that limit and asserted.
146 /// We are now using a larger limit to reduce the number of (b) cases,
147 /// especially cases where "compact" lists of items grow 50% in size when we
148 /// convert them to canonical form. The new limit is selected to withstand
149 /// concatenation and ~50% expansion of two HTTP headers limited by default
150 /// request_header_max_size and reply_header_max_size settings.
151 static const size_type SizeMax_ = 3*64*1024 - 1;
152
70df76e3
AR
153 /// returns true after increasing the first argument by extra if the sum does not exceed SizeMax_
154 static bool SafeAdd(size_type &base, size_type extra) { if (extra <= SizeMax_ && base <= SizeMax_ - extra) { base += extra; return true; } return false; }
155
f63c4b05 156 char *buf_ = nullptr;
9b558d8a 157
f63c4b05
AJ
158 void set(char const *loc, char const ch) {
159 if (loc < buf_ || loc > (buf_ + size_))
160 return;
161 buf_[loc-buf_] = ch;
162 }
9b558d8a 163
f63c4b05
AJ
164 void cutPointer(char const *loc) {
165 if (loc < buf_ || loc > (buf_ + size_))
166 return;
167 len_ = loc-buf_;
168 buf_[len_] = '\0';
169 }
30abd221 170};
171
f63c4b05
AJ
172inline std::ostream & operator<<(std::ostream &os, String const &aString)
173{
174 os.write(aString.rawBuf(),aString.size());
175 return os;
176}
634592a6 177
f63c4b05
AJ
178inline bool operator<(const String &a, const String &b)
179{
180 return a.cmp(b) < 0;
181}
0f15e632 182
8a648e8d
FC
183const char *checkNullString(const char *p);
184int stringHasWhitespace(const char *);
185int stringHasCntl(const char *);
186char *strwordtok(char *buf, char **t);
0875c529 187
ff9d9458 188#endif /* SQUID_SRC_SQUIDSTRING_H */
f53969cc 189