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 
26 class String
27 {
28 
29 public:
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 
132 private:
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 
174 inline std::ostream & operator<<(std::ostream &os, String const &aString)
175 {
176  os.write(aString.rawBuf(),aString.size());
177  return os;
178 }
179 
180 inline bool operator<(const String &a, const String &b)
181 {
182  return a.cmp(b) < 0;
183 }
184 
185 const char *checkNullString(const char *p);
186 int stringHasWhitespace(const char *);
187 int stringHasCntl(const char *);
188 char *strwordtok(char *buf, char **t);
189 
190 #endif /* SQUID_SRC_SQUIDSTRING_H */
191 
bool undefined() const
Definition: SquidString.h:138
int caseCmp(char const *) const
Definition: String.cc:273
char operator[](unsigned int aPos) const
Definition: SquidString.h:64
size_type len_
Definition: SquidString.h:143
bool operator!=(String const &) const
Definition: String.cc:69
const char * rawBuf() const
Definition: SquidString.h:87
int stringHasCntl(const char *)
Definition: String.cc:301
int psize() const
Definition: SquidString.h:78
size_type rfind(char const ch) const
Definition: String.cc:456
String & operator=(char const *)
Definition: String.cc:44
Definition: SBuf.h:93
size_type size_
Definition: SquidString.h:141
void setBuffer(char *buf, size_type sz)
Definition: String.cc:29
int cmp(char const *) const
Definition: String.cc:243
String()=default
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_
Definition: SquidString.h:156
const static size_type npos
Definition: SquidString.h:40
bool operator==(String const &) const
Definition: String.cc:60
bool canGrowBy(const size_type growthLen) const
whether appending growthLen characters is safe (i.e., unlikely to assert)
Definition: SquidString.h:126
static size_type SizeMaxXXX()
Definition: SquidString.h:72
char * buf_
Definition: SquidString.h:158
static bool CanGrowTo(size_type totalLen, const size_type extras=0)
Definition: SquidString.h:124
void append(char const *buf, int len)
Definition: String.cc:131
const char * rpos(char const ch) const
Definition: String.cc:428
char * strwordtok(char *buf, char **t)
Definition: String.cc:321
#define INT_MAX
Definition: types.h:70
void cutPointer(char const *loc)
Definition: SquidString.h:166
std::ostream & operator<<(std::ostream &os, String const &aString)
Definition: SquidString.h:174
bool operator<(const String &a, const String &b)
Definition: SquidString.h:180
bool defined() const
Definition: SquidString.h:137
void allocAndFill(const char *str, int len)
Definition: String.cc:88
#define assert(EX)
Definition: assert.h:17
void absorb(String &old)
Definition: String.cc:186
const char * pos(char const *aString) const
Definition: String.cc:412
~String()
Definition: String.cc:117
int caseCmp(String const &str) const
Definition: SquidString.h:117
size_t size_type
Definition: SquidString.h:39
void set(char const *loc, char const ch)
Definition: SquidString.h:160
static const size_type SizeMax_
Definition: SquidString.h:153
const char * termedBuf() const
Definition: SquidString.h:93
void cut(size_type newLength)
Definition: String.cc:210
size_type size() const
Definition: SquidString.h:74
void allocBuffer(size_type sz)
Definition: String.cc:19
#define Must(condition)
Definition: TextException.h:75
void reset(char const *str)
Definition: String.cc:123
void assign(const char *str, int len)
Definition: String.cc:79
String(String &&S)
Definition: SquidString.h:33
size_type find(char const ch) const
Definition: String.cc:436
String substr(size_type from, size_type to) const
Definition: String.cc:197
int stringHasWhitespace(const char *)
Definition: String.cc:294
const char * checkNullString(const char *p)
Definition: String.cc:406
void clean()
Definition: String.cc:104

 

Introduction

Documentation

Support

Miscellaneous