SquidString.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2021 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_STRING_H
12 #define SQUID_STRING_H
13 
14 #include "base/TextException.h"
15 #include "Debug.h"
16 
17 #include <ostream>
18 
19 /* squid string placeholder (for printf) */
20 #ifndef SQUIDSTRINGPH
21 #define SQUIDSTRINGPH "%.*s"
22 #define SQUIDSTRINGPRINT(s) (s).psize(),(s).rawBuf()
23 #endif /* SQUIDSTRINGPH */
24 
25 class String
26 {
27 
28 public:
29  String();
30  String(char const *);
31  String(String const &);
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  }
36  ~String();
37 
38  typedef size_t size_type; //storage size intentionally unspecified
39  const static size_type npos = static_cast<size_type>(-1);
40 
41  String &operator =(char const *);
42  String &operator =(String const &);
44  if (this != &S) {
45  clean();
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 
56  bool operator ==(String const &) const;
57  bool operator !=(String const &) const;
58 
63  char operator [](unsigned int aPos) const {
64  assert(aPos < size_);
65  return buf_[aPos];
66  }
67 
71  static size_type SizeMaxXXX() { return SizeMax_; }
72 
73  size_type size() const { return len_; }
74 
77  int psize() const {
78  Must(size() < INT_MAX);
79  return size();
80  }
81 
86  char const * rawBuf() const { return buf_; }
87 
92  char const * termedBuf() const { return buf_; }
93 
94  void assign(const char *str, int len);
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);
100  void append(String const &);
101  void absorb(String &old);
102  const char * pos(char const *aString) const;
103  const char * pos(char const ch) const;
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;
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  }
118 
122  static bool CanGrowTo(size_type totalLen, const size_type extras = 0) { return SafeAdd(totalLen, extras) && SafeAdd(totalLen, 1); }
124  bool canGrowBy(const size_type growthLen) const { return CanGrowTo(size(), growthLen); }
125 
126  String substr(size_type from, size_type to) const;
127 
128  void cut(size_type newLength);
129 
130 private:
131  void allocAndFill(const char *str, int len);
132  void allocBuffer(size_type sz);
133  void setBuffer(char *buf, size_type sz);
134 
135  bool defined() const {return buf_!=NULL;}
136  bool undefined() const {return !defined();}
137 
138  /* never reference these directly! */
139  size_type size_ = 0; /* buffer size; limited by SizeMax_ */
140 
141  size_type len_ = 0; /* current length */
142 
143  static const size_type SizeMax_ = 65535;
144  static bool SafeAdd(size_type &base, size_type extra) { if (extra <= SizeMax_ && base <= SizeMax_ - extra) { base += extra; return true; } return false; }
146 
147  char *buf_ = nullptr;
148 
149  void set(char const *loc, char const ch) {
150  if (loc < buf_ || loc > (buf_ + size_))
151  return;
152  buf_[loc-buf_] = ch;
153  }
154 
155  void cutPointer(char const *loc) {
156  if (loc < buf_ || loc > (buf_ + size_))
157  return;
158  len_ = loc-buf_;
159  buf_[len_] = '\0';
160  }
161 };
162 
163 inline std::ostream & operator<<(std::ostream &os, String const &aString)
164 {
165  os.write(aString.rawBuf(),aString.size());
166  return os;
167 }
168 
169 inline bool operator<(const String &a, const String &b)
170 {
171  return a.cmp(b) < 0;
172 }
173 
174 const char *checkNullString(const char *p);
175 int stringHasWhitespace(const char *);
176 int stringHasCntl(const char *);
177 char *strwordtok(char *buf, char **t);
178 
179 #endif /* SQUID_STRING_H */
180 
bool undefined() const
Definition: SquidString.h:136
int caseCmp(char const *) const
Definition: String.cc:287
char operator[](unsigned int aPos) const
Definition: SquidString.h:63
size_type len_
Definition: SquidString.h:141
bool operator!=(String const &) const
Definition: String.cc:81
int stringHasCntl(const char *)
Definition: String.cc:375
int psize() const
Definition: SquidString.h:77
size_type rfind(char const ch) const
Definition: String.cc:530
String()
Definition: String.cc:39
String & operator=(char const *)
Definition: String.cc:56
size_type size_
Definition: SquidString.h:139
void setBuffer(char *buf, size_type sz)
Definition: String.cc:31
int cmp(char const *) const
Definition: String.cc:257
static const size_type npos
Definition: SquidString.h:39
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:145
bool operator==(String const &) const
Definition: String.cc:72
bool canGrowBy(const size_type growthLen) const
whether appending growthLen characters is safe (i.e., unlikely to assert)
Definition: SquidString.h:124
static size_type SizeMaxXXX()
Definition: SquidString.h:71
char * buf_
Definition: SquidString.h:147
#define NULL
Definition: types.h:166
static bool CanGrowTo(size_type totalLen, const size_type extras=0)
Definition: SquidString.h:122
void append(char const *buf, int len)
Definition: String.cc:151
const char * rpos(char const ch) const
Definition: String.cc:502
char * strwordtok(char *buf, char **t)
Definition: String.cc:395
#define INT_MAX
Definition: types.h:76
void cutPointer(char const *loc)
Definition: SquidString.h:155
std::ostream & operator<<(std::ostream &os, String const &aString)
Definition: SquidString.h:163
bool operator<(const String &a, const String &b)
Definition: SquidString.h:169
bool defined() const
Definition: SquidString.h:135
void allocAndFill(const char *str, int len)
Definition: String.cc:100
#define assert(EX)
Definition: assert.h:19
char const * termedBuf() const
Definition: SquidString.h:92
void absorb(String &old)
Definition: String.cc:200
void const char int sz
Definition: stub_cbdata.cc:16
const char * pos(char const *aString) const
Definition: String.cc:486
~String()
Definition: String.cc:133
int caseCmp(String const &str) const
Definition: SquidString.h:115
size_t size_type
Definition: SquidString.h:38
void set(char const *loc, char const ch)
Definition: SquidString.h:149
static const size_type SizeMax_
Definition: SquidString.h:143
void cut(size_type newLength)
Definition: String.cc:224
size_type size() const
Definition: SquidString.h:73
void allocBuffer(size_type sz)
Definition: String.cc:21
#define Must(condition)
Like assert() but throws an exception instead of aborting the process.
Definition: TextException.h:73
void reset(char const *str)
Definition: String.cc:143
void assign(const char *str, int len)
Definition: String.cc:91
String(String &&S)
Definition: SquidString.h:32
size_type find(char const ch) const
Definition: String.cc:510
String substr(size_type from, size_type to) const
Definition: String.cc:211
int stringHasWhitespace(const char *)
Definition: String.cc:368
const char * checkNullString(const char *p)
Definition: String.cc:480
char const * rawBuf() const
Definition: SquidString.h:86
void clean()
Definition: String.cc:120

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors