SquidString.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2018 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  size_ = S.size_;
46  len_ = S.len_;
47  buf_ = S.buf_;
48  S.size_ = 0;
49  S.len_ = 0;
50  S.buf_ = nullptr; // S is about to be destructed
51  }
52  return *this;
53  }
54 
55  bool operator ==(String const &) const;
56  bool operator !=(String const &) const;
57 
62  char operator [](unsigned int aPos) const {
63  assert(aPos < size_);
64  return buf_[aPos];
65  }
66 
70  static size_type SizeMaxXXX() { return SizeMax_; }
71 
72  size_type size() const { return len_; }
73 
76  int psize() const {
77  Must(size() < INT_MAX);
78  return size();
79  }
80 
85  char const * rawBuf() const { return buf_; }
86 
91  char const * termedBuf() const { return buf_; }
92 
93  void limitInit(const char *str, int len); // TODO: rename to assign()
94  void clean();
95  void reset(char const *str);
96  void append(char const *buf, int len);
97  void append(char const *buf);
98  void append(char const);
99  void append(String const &);
100  void absorb(String &old);
101  const char * pos(char const *aString) const;
102  const char * pos(char const ch) const;
105  size_type find(char const ch) const;
106  size_type find(char const *aString) const;
107  const char * rpos(char const ch) const;
108  size_type rfind(char const ch) const;
109  int cmp(char const *) const;
110  int cmp(char const *, size_type count) const;
111  int cmp(String const &) const;
112  int caseCmp(char const *) const;
113  int caseCmp(char const *, size_type count) const;
114  int caseCmp(String const &str) const {
115  return caseCmp(str.rawBuf(),str.size());
116  }
117 
121  static bool CanGrowTo(size_type totalLen, const size_type extras = 0) { return SafeAdd(totalLen, extras) && SafeAdd(totalLen, 1); }
123  bool canGrowBy(const size_type growthLen) const { return CanGrowTo(size(), growthLen); }
124 
125  String substr(size_type from, size_type to) const;
126 
127  void cut(size_type newLength);
128 
129 private:
130  void allocAndFill(const char *str, int len);
131  void allocBuffer(size_type sz);
132  void setBuffer(char *buf, size_type sz);
133 
134  bool defined() const {return buf_!=NULL;}
135  bool undefined() const {return !defined();}
136 
137  /* never reference these directly! */
138  size_type size_ = 0; /* buffer size; limited by SizeMax_ */
139 
140  size_type len_ = 0; /* current length */
141 
142  static const size_type SizeMax_ = 65535;
143  static bool SafeAdd(size_type &base, size_type extra) { if (extra <= SizeMax_ && base <= SizeMax_ - extra) { base += extra; return true; } return false; }
145 
146  char *buf_ = nullptr;
147 
148  void set(char const *loc, char const ch) {
149  if (loc < buf_ || loc > (buf_ + size_))
150  return;
151  buf_[loc-buf_] = ch;
152  }
153 
154  void cutPointer(char const *loc) {
155  if (loc < buf_ || loc > (buf_ + size_))
156  return;
157  len_ = loc-buf_;
158  buf_[len_] = '\0';
159  }
160 };
161 
162 inline std::ostream & operator<<(std::ostream &os, String const &aString)
163 {
164  os.write(aString.rawBuf(),aString.size());
165  return os;
166 }
167 
168 inline bool operator<(const String &a, const String &b)
169 {
170  return a.cmp(b) < 0;
171 }
172 
173 const char *checkNullString(const char *p);
174 int stringHasWhitespace(const char *);
175 int stringHasCntl(const char *);
176 char *strwordtok(char *buf, char **t);
177 
178 #endif /* SQUID_STRING_H */
179 
String & operator=(char const *)
Definition: String.cc:59
int cmp(char const *) const
Definition: String.cc:269
void limitInit(const char *str, int len)
Definition: String.cc:94
const char * checkNullString(const char *p)
Definition: String.cc:492
#define assert(EX)
Definition: assert.h:17
int caseCmp(char const *) const
Definition: String.cc:299
void setBuffer(char *buf, size_type sz)
Definition: String.cc:34
int caseCmp(String const &str) const
Definition: SquidString.h:114
static const size_type npos
Definition: SquidString.h:39
int stringHasWhitespace(const char *)
Definition: String.cc:380
size_type find(char const ch) const
Definition: String.cc:522
String()
Definition: String.cc:42
size_type len_
Definition: SquidString.h:140
static size_type SizeMaxXXX()
Definition: SquidString.h:70
#define Must(condition)
Like assert() but throws an exception instead of aborting the process.
Definition: TextException.h:69
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:144
char * buf_
Definition: SquidString.h:146
char * p
Definition: membanger.c:43
const char * pos(char const *aString) const
Definition: String.cc:498
bool defined() const
Definition: SquidString.h:134
static bool CanGrowTo(size_type totalLen, const size_type extras=0)
Definition: SquidString.h:121
size_type size() const
Definition: SquidString.h:72
void append(char const *buf, int len)
Definition: String.cc:161
int psize() const
Definition: SquidString.h:76
bool canGrowBy(const size_type growthLen) const
whether appending growthLen characters is safe (i.e., unlikely to assert)
Definition: SquidString.h:123
char * strwordtok(char *buf, char **t)
Definition: String.cc:407
void allocAndFill(const char *str, int len)
Definition: String.cc:103
size_type size_
Definition: SquidString.h:138
bool operator<(const String &a, const String &b)
Definition: SquidString.h:168
char const * rawBuf() const
Definition: SquidString.h:85
const char * rpos(char const ch) const
Definition: String.cc:514
String substr(size_type from, size_type to) const
Definition: String.cc:223
char const * termedBuf() const
Definition: SquidString.h:91
size_type rfind(char const ch) const
Definition: String.cc:542
void set(char const *loc, char const ch)
Definition: SquidString.h:148
void absorb(String &old)
Definition: String.cc:212
void cut(size_type newLength)
Definition: String.cc:236
int unsigned int const char *desc STUB void int len
Definition: stub_fd.cc:20
void const char * buf
Definition: stub_helper.cc:16
size_t size_type
Definition: SquidString.h:38
void cutPointer(char const *loc)
Definition: SquidString.h:154
static const size_type SizeMax_
Definition: SquidString.h:142
String(String &&S)
Definition: SquidString.h:32
std::ostream & operator<<(std::ostream &os, String const &aString)
Definition: SquidString.h:162
void allocBuffer(size_type sz)
Definition: String.cc:22
int a
Definition: membanger.c:50
#define INT_MAX
Definition: types.h:76
~String()
Definition: String.cc:141
void clean()
Definition: String.cc:125
void reset(char const *str)
Definition: String.cc:151
bool operator==(String const &) const
Definition: String.cc:75
bool operator!=(String const &) const
Definition: String.cc:84
#define NULL
Definition: types.h:166
int stringHasCntl(const char *)
Definition: String.cc:387
bool undefined() const
Definition: SquidString.h:135
char operator[](unsigned int aPos) const
Definition: SquidString.h:62

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors