]> git.ipfire.org Git - thirdparty/squid.git/blob - src/SquidString.h
Maintenance: Update astyle version to 3.1 (#841)
[thirdparty/squid.git] / src / SquidString.h
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 &);
43 String &operator =(String && S) {
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
58 /**
59 * Retrieve a single character in the string.
60 \param aPos Position of character to retrieve.
61 */
62 char operator [](unsigned int aPos) const {
63 assert(aPos < size_);
64 return buf_[aPos];
65 }
66
67 /// The absolute size limit on data held in a String.
68 /// Since Strings can be nil-terminated implicitly it is best to ensure
69 /// the useful content length is strictly less than this limit.
70 static size_type SizeMaxXXX() { return SizeMax_; }
71
72 size_type size() const { return len_; }
73
74 /// variant of size() suited to be used for printf-alikes.
75 /// throws when size() >= INT_MAX
76 int psize() const {
77 Must(size() < INT_MAX);
78 return size();
79 }
80
81 /**
82 * Returns a raw pointer to the underlying backing store. The caller has been
83 * verified not to make any assumptions about null-termination
84 */
85 char const * rawBuf() const { return buf_; }
86
87 /**
88 * Returns a raw pointer to the underlying backing store.
89 * The caller requires it to be null-terminated.
90 */
91 char const * termedBuf() const { return buf_; }
92
93 void assign(const char *str, int len);
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;
103 ///offset from string start of the first occurrence of ch
104 /// returns String::npos if ch is not found
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
118 /// Whether creating a totalLen-character string is safe (i.e., unlikely to assert).
119 /// Optional extras can be used for overflow-safe length addition.
120 /// Implementation has to add 1 because many String allocation methods do.
121 static bool CanGrowTo(size_type totalLen, const size_type extras = 0) { return SafeAdd(totalLen, extras) && SafeAdd(totalLen, 1); }
122 /// whether appending growthLen characters is safe (i.e., unlikely to assert)
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; ///< 64K limit protects some fixed-size buffers
143 /// returns true after increasing the first argument by extra if the sum does not exceed SizeMax_
144 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