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