Squid Web Cache master
Loading...
Searching...
No Matches
SquidString.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2026 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/* DEBUG: section 67 String */
10
11#ifndef SQUID_SRC_SQUIDSTRING_H
12#define SQUID_SRC_SQUIDSTRING_H
13
14#include "base/TextException.h"
15#include "debug/Stream.h"
16#include "sbuf/forward.h"
17
18#include <ostream>
19
20/* squid string placeholder (for printf) */
21#ifndef SQUIDSTRINGPH
22#define SQUIDSTRINGPH "%.*s"
23#define SQUIDSTRINGPRINT(s) (s).psize(),(s).rawBuf()
24#endif /* SQUIDSTRINGPH */
25
26class String
27{
28
29public:
30 String() = default;
31 String(char const *);
32 String(String const &);
33 String(String && S) : size_(S.size_), len_(S.len_), buf_(S.buf_) {
34 S.buf_ = nullptr; // S is about to be destructed
35 S.size_ = S.len_ = 0;
36 }
37 ~String();
38
39 typedef size_t size_type; //storage size intentionally unspecified
40 const static size_type npos = static_cast<size_type>(-1);
41
42 String &operator =(char const *);
43 String &operator =(String const &);
45 if (this != &S) {
46 clean();
47 size_ = S.size_;
48 len_ = S.len_;
49 buf_ = S.buf_;
50 S.size_ = 0;
51 S.len_ = 0;
52 S.buf_ = nullptr; // S is about to be destructed
53 }
54 return *this;
55 }
56
57 bool operator ==(String const &) const;
58 bool operator !=(String const &) const;
59
64 char operator [](unsigned int aPos) const {
65 assert(aPos < size_);
66 return buf_[aPos];
67 }
68
72 static size_type SizeMaxXXX() { return SizeMax_; }
73
76 static size_type RawSizeMaxXXX() { return (SizeMaxXXX()+1)/3; }
77
78 size_type size() const { return len_; }
79
82 int psize() const {
83 Must(size() < INT_MAX);
84 return size();
85 }
86
91 char const * rawBuf() const { return buf_; }
92
97 char const * termedBuf() const { return buf_; }
98
99 void assign(const char *str, int len);
100 void clean();
101 void reset(char const *str);
102 void append(char const *buf, int len);
103 void append(char const *buf);
104 void append(char const);
105 void append(String const &);
106 void append(const SBuf &);
107 void absorb(String &old);
108 const char * pos(char const *aString) const;
109 const char * pos(char const ch) const;
112 size_type find(char const ch) const;
113 size_type find(char const *aString) const;
114 const char * rpos(char const ch) const;
115 size_type rfind(char const ch) const;
116 int cmp(char const *) const;
117 int cmp(char const *, size_type count) const;
118 int cmp(String const &) const;
119 int caseCmp(char const *) const;
120 int caseCmp(char const *, size_type count) const;
121 int caseCmp(String const &str) const {
122 return caseCmp(str.rawBuf(),str.size());
123 }
124
128 static bool CanGrowTo(size_type totalLen, const size_type extras = 0) { return SafeAdd(totalLen, extras) && SafeAdd(totalLen, 1); }
130 bool canGrowBy(const size_type growthLen) const { return CanGrowTo(size(), growthLen); }
131
132 String substr(size_type from, size_type to) const;
133
134 void cut(size_type newLength);
135
136private:
137 void allocAndFill(const char *str, int len);
138 void allocBuffer(size_type sz);
139 void setBuffer(char *buf, size_type sz);
140
141 bool defined() const {return buf_!=nullptr;}
142 bool undefined() const {return !defined();}
143
144 /* never reference these directly! */
145 size_type size_ = 0; /* buffer size; limited by SizeMax_ */
146
147 size_type len_ = 0; /* current length */
148
157 static const size_type SizeMax_ = 3*64*1024 - 1;
158
160 static bool SafeAdd(size_type &base, size_type extra) { if (extra <= SizeMax_ && base <= SizeMax_ - extra) { base += extra; return true; } return false; }
161
162 char *buf_ = nullptr;
163
164 void set(char const *loc, char const ch) {
165 if (loc < buf_ || loc > (buf_ + size_))
166 return;
167 buf_[loc-buf_] = ch;
168 }
169
170 void cutPointer(char const *loc) {
171 if (loc < buf_ || loc > (buf_ + size_))
172 return;
173 len_ = loc-buf_;
174 buf_[len_] = '\0';
175 }
176};
177
178inline std::ostream & operator<<(std::ostream &os, String const &aString)
179{
180 os.write(aString.rawBuf(),aString.size());
181 return os;
182}
183
184inline bool operator<(const String &a, const String &b)
185{
186 return a.cmp(b) < 0;
187}
188
189const char *checkNullString(const char *p);
190int stringHasWhitespace(const char *);
191int stringHasCntl(const char *);
192char *strwordtok(char *buf, char **t);
193
194#endif /* SQUID_SRC_SQUIDSTRING_H */
195
int stringHasWhitespace(const char *)
Definition String.cc:294
std::ostream & operator<<(std::ostream &os, String const &aString)
bool operator<(const String &a, const String &b)
int stringHasCntl(const char *)
Definition String.cc:301
char * strwordtok(char *buf, char **t)
Definition String.cc:321
const char * checkNullString(const char *p)
Definition String.cc:406
#define Must(condition)
#define assert(EX)
Definition assert.h:17
Definition SBuf.h:94
bool canGrowBy(const size_type growthLen) const
whether appending growthLen characters is safe (i.e., unlikely to assert)
int psize() const
Definition SquidString.h:82
int cmp(char const *) const
Definition String.cc:243
bool undefined() const
size_type size_
String(String &&S)
Definition SquidString.h:33
static size_type SizeMaxXXX()
Definition SquidString.h:72
String substr(size_type from, size_type to) const
Definition String.cc:197
void absorb(String &old)
Definition String.cc:186
size_t size_type
Definition SquidString.h:39
size_type len_
void setBuffer(char *buf, size_type sz)
Definition String.cc:29
size_type find(char const ch) const
Definition String.cc:436
static const size_type npos
Definition SquidString.h:40
void clean()
Definition String.cc:104
void assign(const char *str, int len)
Definition String.cc:79
const char * pos(char const *aString) const
Definition String.cc:412
bool operator==(String const &) const
Definition String.cc:60
size_type rfind(char const ch) const
Definition String.cc:456
char const * rawBuf() const
Definition SquidString.h:91
void allocAndFill(const char *str, int len)
Definition String.cc:88
char * buf_
void allocBuffer(size_type sz)
Definition String.cc:19
void cut(size_type newLength)
Definition String.cc:210
static size_type RawSizeMaxXXX()
Definition SquidString.h:76
int caseCmp(String const &str) const
String()=default
char const * termedBuf() const
Definition SquidString.h:97
void append(char const *buf, int len)
Definition String.cc:131
char operator[](unsigned int aPos) const
Definition SquidString.h:64
~String()
Definition String.cc:117
static bool SafeAdd(size_type &base, size_type extra)
returns true after increasing the first argument by extra if the sum does not exceed SizeMax_
bool defined() const
int caseCmp(char const *) const
Definition String.cc:273
static bool CanGrowTo(size_type totalLen, const size_type extras=0)
void cutPointer(char const *loc)
void reset(char const *str)
Definition String.cc:123
static const size_type SizeMax_
String & operator=(char const *)
Definition String.cc:44
const char * rpos(char const ch) const
Definition String.cc:428
void set(char const *loc, char const ch)
bool operator!=(String const &) const
Definition String.cc:69
size_type size() const
Definition SquidString.h:78
#define INT_MAX
Definition types.h:70