Squid Web Cache master
Loading...
Searching...
No Matches
SquidString.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 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
74 size_type size() const { return len_; }
75
78 int psize() const {
79 Must(size() < INT_MAX);
80 return size();
81 }
82
87 char const * rawBuf() const { return buf_; }
88
93 char const * termedBuf() const { return buf_; }
94
95 void assign(const char *str, int len);
96 void clean();
97 void reset(char const *str);
98 void append(char const *buf, int len);
99 void append(char const *buf);
100 void append(char const);
101 void append(String const &);
102 void append(const SBuf &);
103 void absorb(String &old);
104 const char * pos(char const *aString) const;
105 const char * pos(char const ch) const;
108 size_type find(char const ch) const;
109 size_type find(char const *aString) const;
110 const char * rpos(char const ch) const;
111 size_type rfind(char const ch) const;
112 int cmp(char const *) const;
113 int cmp(char const *, size_type count) const;
114 int cmp(String const &) const;
115 int caseCmp(char const *) const;
116 int caseCmp(char const *, size_type count) const;
117 int caseCmp(String const &str) const {
118 return caseCmp(str.rawBuf(),str.size());
119 }
120
124 static bool CanGrowTo(size_type totalLen, const size_type extras = 0) { return SafeAdd(totalLen, extras) && SafeAdd(totalLen, 1); }
126 bool canGrowBy(const size_type growthLen) const { return CanGrowTo(size(), growthLen); }
127
128 String substr(size_type from, size_type to) const;
129
130 void cut(size_type newLength);
131
132private:
133 void allocAndFill(const char *str, int len);
134 void allocBuffer(size_type sz);
135 void setBuffer(char *buf, size_type sz);
136
137 bool defined() const {return buf_!=nullptr;}
138 bool undefined() const {return !defined();}
139
140 /* never reference these directly! */
141 size_type size_ = 0; /* buffer size; limited by SizeMax_ */
142
143 size_type len_ = 0; /* current length */
144
153 static const size_type SizeMax_ = 3*64*1024 - 1;
154
156 static bool SafeAdd(size_type &base, size_type extra) { if (extra <= SizeMax_ && base <= SizeMax_ - extra) { base += extra; return true; } return false; }
157
158 char *buf_ = nullptr;
159
160 void set(char const *loc, char const ch) {
161 if (loc < buf_ || loc > (buf_ + size_))
162 return;
163 buf_[loc-buf_] = ch;
164 }
165
166 void cutPointer(char const *loc) {
167 if (loc < buf_ || loc > (buf_ + size_))
168 return;
169 len_ = loc-buf_;
170 buf_[len_] = '\0';
171 }
172};
173
174inline std::ostream & operator<<(std::ostream &os, String const &aString)
175{
176 os.write(aString.rawBuf(),aString.size());
177 return os;
178}
179
180inline bool operator<(const String &a, const String &b)
181{
182 return a.cmp(b) < 0;
183}
184
185const char *checkNullString(const char *p);
186int stringHasWhitespace(const char *);
187int stringHasCntl(const char *);
188char *strwordtok(char *buf, char **t);
189
190#endif /* SQUID_SRC_SQUIDSTRING_H */
191
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:78
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:87
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
int caseCmp(String const &str) const
String()=default
char const * termedBuf() const
Definition SquidString.h:93
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:74
#define INT_MAX
Definition types.h:70