SquidString.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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 
16 #include <ostream>
17 
18 /* squid string placeholder (for printf) */
19 #ifndef SQUIDSTRINGPH
20 #define SQUIDSTRINGPH "%.*s"
21 #define SQUIDSTRINGPRINT(s) (s).psize(),(s).rawBuf()
22 #endif /* SQUIDSTRINGPH */
23 
24 class String
25 {
26 
27 public:
28  String();
29  String(char const *);
30  String(String const &);
31  String(String && S) : size_(S.size_), len_(S.len_), buf_(S.buf_) {
32  S.buf_ = nullptr; // S is about to be destructed
33  S.size_ = S.len_ = 0;
34  }
35  ~String();
36 
37  typedef size_t size_type; //storage size intentionally unspecified
38  const static size_type npos = static_cast<size_type>(-1);
39 
40  String &operator =(char const *);
41  String &operator =(String const &);
43  if (this != &S) {
44  size_ = S.size_;
45  len_ = S.len_;
46  buf_ = S.buf_;
47  S.size_ = 0;
48  S.len_ = 0;
49  S.buf_ = nullptr; // S is about to be destructed
50  }
51  return *this;
52  }
53 
54  bool operator ==(String const &) const;
55  bool operator !=(String const &) const;
56 
61  char operator [](unsigned int aPos) const {
62  assert(aPos < size_);
63  return buf_[aPos];
64  }
65 
69  static size_type SizeMaxXXX() { return SizeMax_; }
70 
71  size_type size() const { return len_; }
72 
75  int psize() const {
76  Must(size() < INT_MAX);
77  return size();
78  }
79 
84  char const * rawBuf() const { return buf_; }
85 
90  char const * termedBuf() const { return buf_; }
91 
92  void limitInit(const char *str, int len); // TODO: rename to assign()
93  void clean();
94  void reset(char const *str);
95  void append(char const *buf, int len);
96  void append(char const *buf);
97  void append(char const);
98  void append(String const &);
99  void absorb(String &old);
100  const char * pos(char const *aString) const;
101  const char * pos(char const ch) const;
104  size_type find(char const ch) const;
105  size_type find(char const *aString) const;
106  const char * rpos(char const ch) const;
107  size_type rfind(char const ch) const;
108  int cmp(char const *) const;
109  int cmp(char const *, size_type count) const;
110  int cmp(String const &) const;
111  int caseCmp(char const *) const;
112  int caseCmp(char const *, size_type count) const;
113  int caseCmp(String const &str) const {
114  return caseCmp(str.rawBuf(),str.size());
115  }
116 
120  static bool CanGrowTo(size_type totalLen, const size_type extras = 0) { return SafeAdd(totalLen, extras) && SafeAdd(totalLen, 1); }
122  bool canGrowBy(const size_type growthLen) const { return CanGrowTo(size(), growthLen); }
123 
124  String substr(size_type from, size_type to) const;
125 
126  void cut(size_type newLength);
127 
128 private:
129  void allocAndFill(const char *str, int len);
130  void allocBuffer(size_type sz);
131  void setBuffer(char *buf, size_type sz);
132 
133  bool defined() const {return buf_!=NULL;}
134  bool undefined() const {return !defined();}
135 
136  /* never reference these directly! */
137  size_type size_ = 0; /* buffer size; limited by SizeMax_ */
138 
139  size_type len_ = 0; /* current length */
140 
141  static const size_type SizeMax_ = 65535;
142  static bool SafeAdd(size_type &base, size_type extra) { if (extra <= SizeMax_ && base <= SizeMax_ - extra) { base += extra; return true; } return false; }
144 
145  char *buf_ = nullptr;
146 
147  void set(char const *loc, char const ch) {
148  if (loc < buf_ || loc > (buf_ + size_))
149  return;
150  buf_[loc-buf_] = ch;
151  }
152 
153  void cutPointer(char const *loc) {
154  if (loc < buf_ || loc > (buf_ + size_))
155  return;
156  len_ = loc-buf_;
157  buf_[len_] = '\0';
158  }
159 };
160 
161 inline std::ostream & operator<<(std::ostream &os, String const &aString)
162 {
163  os.write(aString.rawBuf(),aString.size());
164  return os;
165 }
166 
167 inline bool operator<(const String &a, const String &b)
168 {
169  return a.cmp(b) < 0;
170 }
171 
172 const char *checkNullString(const char *p);
173 int stringHasWhitespace(const char *);
174 int stringHasCntl(const char *);
175 char *strwordtok(char *buf, char **t);
176 
177 #endif /* SQUID_STRING_H */
178 
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:113
static const size_type npos
Definition: SquidString.h:38
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:139
static size_type SizeMaxXXX()
Definition: SquidString.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:143
char * buf_
Definition: SquidString.h:145
char * p
Definition: membanger.c:43
const char * pos(char const *aString) const
Definition: String.cc:498
bool defined() const
Definition: SquidString.h:133
static bool CanGrowTo(size_type totalLen, const size_type extras=0)
Definition: SquidString.h:120
size_type size() const
Definition: SquidString.h:71
void append(char const *buf, int len)
Definition: String.cc:161
int psize() const
Definition: SquidString.h:75
bool canGrowBy(const size_type growthLen) const
whether appending growthLen characters is safe (i.e., unlikely to assert)
Definition: SquidString.h:122
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:137
bool operator<(const String &a, const String &b)
Definition: SquidString.h:167
char const * rawBuf() const
Definition: SquidString.h:84
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:90
size_type rfind(char const ch) const
Definition: String.cc:542
void set(char const *loc, char const ch)
Definition: SquidString.h:147
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:37
#define Must(cond)
Definition: TextException.h:89
void cutPointer(char const *loc)
Definition: SquidString.h:153
static const size_type SizeMax_
Definition: SquidString.h:141
String(String &&S)
Definition: SquidString.h:31
std::ostream & operator<<(std::ostream &os, String const &aString)
Definition: SquidString.h:161
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:134
char operator[](unsigned int aPos) const
Definition: SquidString.h:61

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors