ScummVM API documentation
str-base.h
1/* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#ifndef COMMON_STR_BASE_H
23#define COMMON_STR_BASE_H
24
25#include "common/scummsys.h"
26#include "common/str-enc.h"
27
28#include <stdarg.h>
29
30namespace Common {
31template<class T>
33public:
34 static void releaseMemoryPoolMutex();
35
36 static const uint32 npos = 0xFFFFFFFF;
37 typedef T value_type;
38 typedef T * iterator;
39 typedef const T * const_iterator;
40 typedef size_t size_type;
41
42protected:
48 static const uint32 _builtinCapacity = 32 - (sizeof(uint32) + sizeof(char *)) / sizeof(value_type);
49
55 uint32 _size;
56
61 value_type *_str;
62
63
64 union {
73 struct {
74 mutable int *_refCount;
75 uint32 _capacity;
77 };
78
79 inline bool isStorageIntern() const {
80 return _str == _storage;
81 }
82
83public:
85 constexpr BaseString() : _size(0), _str(_storage), _storage{0} {}
86
88 explicit constexpr BaseString(value_type c) : _size((c == 0) ? 0 : 1), _str(_storage), _storage{c, 0} {}
89
91 BaseString(size_t n, value_type c);
92
95
98
100 explicit BaseString(const value_type *str);
101
103 BaseString(const value_type *str, uint32 len);
104
106 BaseString(const value_type *beginP, const value_type *endP);
107
108 bool operator==(const BaseString &x) const;
109 bool operator==(const value_type *x) const;
110 bool operator!=(const BaseString &x) const;
111 bool operator!=(const value_type *x) const;
112
113 bool operator<(const BaseString &x) const;
114 bool operator<(const value_type *x) const;
115 bool operator<=(const BaseString &x) const;
116 bool operator<=(const value_type *x) const;
117 bool operator>(const BaseString &x) const;
118 bool operator>(const value_type *x) const;
119 bool operator>=(const BaseString &x) const;
120 bool operator>=(const value_type *x) const;
121
126 bool equals(const BaseString &x) const;
127 bool equals(const value_type *x) const;
128 int compareTo(const BaseString &x) const; // strcmp clone
129 int compareTo(const value_type *x) const; // strcmp clone
130
132 void setChar(value_type c, uint32 p);
133
139 void deleteChar(uint32 p);
140
142 inline void deleteLastChar() { chop(1); }
143
145 void erase(uint32 p, uint32 len = npos);
146
148 iterator erase(iterator it);
149
151 void chop(uint32 len = 1);
152
154 void clear();
155
156 iterator begin() {
157 // Since the user could potentially
158 // change the string via the returned
159 // iterator we have to assure we are
160 // pointing to a unique storage.
161 makeUnique();
162
163 return _str;
164 }
165
166 iterator end() {
167 return begin() + size();
168 }
169
170 const_iterator begin() const {
171 return _str;
172 }
173
174 const_iterator end() const {
175 return begin() + size();
176 }
177
178 inline const value_type *c_str() const { return _str; }
179 inline uint size() const { return _size; }
180
181 inline bool empty() const { return (_size == 0); }
182 value_type firstChar() const { return (_size > 0) ? _str[0] : 0; }
183 value_type lastChar() const { return (_size > 0) ? _str[_size - 1] : 0; }
184
188 value_type &operator[](size_t idx) {
189 assert(_str);
190 assert(idx < _size);
191 return _str[idx];
192 }
193
197 value_type operator[](size_t idx) const {
198 assert(_str);
199 assert(idx < _size);
200 return _str[idx];
201 }
202
206 value_type at(size_t idx) const { return operator[](idx); }
207
211 bool contains(const BaseString &otherString) const;
212 bool contains(value_type x) const;
213
215 void insertChar(value_type c, uint32 p);
216 void insertString(const value_type *s, uint32 p);
217 void insertString(const BaseString &s, uint32 p);
218
220 uint32 find(value_type x, uint32 pos = 0) const;
222 size_t find(const value_type *s, uint32 pos = 0) const;
223 uint32 find(const BaseString &str, uint32 pos = 0) const;
224
226 size_t rfind(const value_type *s) const;
227 size_t rfind(const BaseString &s) const {
228 return rfind(s.c_str());
229 }
230
232 size_t rfind(value_type c, size_t pos = npos) const;
233
235 size_t findFirstOf(value_type c, size_t pos = 0) const;
236
238 size_t findFirstOf(const value_type *chars, size_t pos = 0) const;
239 size_t findFirstOf(const BaseString &chars, size_t pos = 0) const {
240 return findFirstOf(chars.c_str(), pos);
241 }
242
244 size_t findLastOf(value_type c, size_t pos = npos) const;
245
247 size_t findLastOf(const value_type *chars, size_t pos = npos) const;
248 size_t findLastOf(const BaseString &chars, size_t pos = npos) const {
249 return findLastOf(chars.c_str(), pos);
250 }
251
253 size_t findFirstNotOf(value_type c, size_t pos = 0) const;
254
256 size_t findFirstNotOf(const value_type *chars, size_t pos = 0) const;
257 size_t findFirstNotOf(const BaseString &chars, size_t pos = 0) const {
258 return findFirstNotOf(chars.c_str(), pos);
259 }
260
262 size_t findLastNotOf(value_type c) const;
263
265 size_t findLastNotOf(const value_type *chars) const;
266 size_t findLastNotOf(const BaseString &chars) const {
267 return findLastNotOf(chars.c_str());
268 }
269
284 // Replace 'count' bytes, starting from 'pos' with str.
285 void replace(uint32 pos, uint32 count, const BaseString &str);
286 // The same as above, but accepts a C-like array of characters.
287 void replace(uint32 pos, uint32 count, const value_type *str);
288 // Replace the characters in [begin, end) with str._str.
289 void replace(iterator begin, iterator end, const BaseString &str);
290 // Replace the characters in [begin, end) with str.
291 void replace(iterator begin, iterator end, const value_type *str);
292 // Replace _str[posOri, posOri + countOri) with
293 // str._str[posDest, posDest + countDest)
294 void replace(uint32 posOri, uint32 countOri, const BaseString &str,
295 uint32 posDest, uint32 countDest);
296 // Replace _str[posOri, posOri + countOri) with
297 // str[posDest, posDest + countDest)
298 void replace(uint32 posOri, uint32 countOri, const value_type *str,
299 uint32 posDest, uint32 countDest);
307 void replace(value_type from, value_type to);
308
312 void assign(const BaseString &str);
313 void assign(BaseString &&str);
314 void assign(size_t count, value_type c);
315 void assign(const value_type *str);
316 void assign(const value_type *str, size_t count);
317
321 void append(const value_type *str);
322 void append(size_t count, value_type c);
323 void append(const BaseString &str);
324
326 void append(const value_type *begin, const value_type *end);
327
331 inline void push_back(value_type c) {
332 append(1, c);
333 }
334
344 void wordWrap(const uint32 maxLength);
345
347 uint64 asUint64() const;
348
350 uint64 asUint64Ext() const;
351
359
367
372 void trim();
373
374 uint hash() const;
375
376protected:
377 ~BaseString();
378
379 void makeUnique() {
380 ensureCapacity(_size, true);
381 }
382
383 void ensureCapacity(uint32 new_size, bool keep_old);
384 void incRefCount() const;
385 void decRefCount(int *oldRefCount);
386 void initWithValueTypeChar(size_t count, value_type c);
387 void initWithValueTypeStr(const value_type *str, uint32 len);
388
389 void assignInsert(const value_type *str, uint32 p);
390 void assignInsert(value_type c, uint32 p);
391 void assignInsert(const BaseString &str, uint32 p);
392
393 bool pointerInOwnBuffer(const value_type *str) const;
394
395 uint getUnsignedValue(uint pos) const;
396
397 void toCase(int (*caseChangeFunc)(int));
398
399 static size_t cStrLen(const value_type *str);
400 static const value_type *cMemChr(const value_type *ptr, value_type c, size_t count);
401 static value_type *cMemChr(value_type *ptr, value_type c, size_t count);
402 static int cMemCmp(const value_type* ptr1, const value_type* ptr2, size_t count);
403 static void cMemSet(value_type *ptr, value_type c, size_t count);
404};
405}
406#endif
Definition: str-base.h:32
static const uint32 _builtinCapacity
Definition: str-base.h:48
size_t findFirstNotOf(const value_type *chars, size_t pos=0) const
size_t findLastOf(const value_type *chars, size_t pos=npos) const
size_t findLastNotOf(value_type c) const
value_type at(size_t idx) const
Definition: str-base.h:206
BaseString(size_t n, value_type c)
size_t findFirstOf(const value_type *chars, size_t pos=0) const
BaseString(BaseString &&str)
BaseString(const value_type *str)
void append(const value_type *begin, const value_type *end)
bool contains(const BaseString &otherString) const
value_type & operator[](size_t idx)
Definition: str-base.h:188
size_t find(const value_type *s, uint32 pos=0) const
void assign(const BaseString &str)
size_t findLastNotOf(const value_type *chars) const
BaseString(const value_type *beginP, const value_type *endP)
void setChar(value_type c, uint32 p)
size_t rfind(value_type c, size_t pos=npos) const
constexpr BaseString(value_type c)
Definition: str-base.h:88
BaseString(const BaseString &str)
size_t findFirstNotOf(value_type c, size_t pos=0) const
void erase(uint32 p, uint32 len=npos)
bool equals(const BaseString &x) const
size_t rfind(const value_type *s) const
void append(const value_type *str)
void replace(uint32 pos, uint32 count, const BaseString &str)
uint32 find(value_type x, uint32 pos=0) const
void replace(value_type from, value_type to)
void wordWrap(const uint32 maxLength)
void chop(uint32 len=1)
void deleteLastChar()
Definition: str-base.h:142
iterator erase(iterator it)
constexpr BaseString()
Definition: str-base.h:85
uint64 asUint64() const
void deleteChar(uint32 p)
value_type operator[](size_t idx) const
Definition: str-base.h:197
size_t findLastOf(value_type c, size_t pos=npos) const
uint32 _size
Definition: str-base.h:55
value_type * _str
Definition: str-base.h:61
BaseString(const value_type *str, uint32 len)
value_type _storage[_builtinCapacity]
Definition: str-base.h:68
void insertChar(value_type c, uint32 p)
uint64 asUint64Ext() const
struct Common::BaseString::@23::@25 _extern
size_t findFirstOf(value_type c, size_t pos=0) const
void push_back(value_type c)
Definition: str-base.h:331
Definition: algorithm.h:29