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 
30 namespace Common {
31 template<class T>
32 class BaseString {
33 public:
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 
41 protected:
47  static const uint32 _builtinCapacity = 32 - (sizeof(uint32) + sizeof(char *)) / sizeof(value_type);
48 
54  uint32 _size;
55 
60  value_type *_str;
61 
62 
63  union {
72  struct {
73  mutable int *_refCount;
74  uint32 _capacity;
75  } _extern;
76  };
77 
78  inline bool isStorageIntern() const {
79  return _str == _storage;
80  }
81 
82 public:
84  constexpr BaseString() : _size(0), _str(_storage), _storage{0} {}
85 
87  BaseString(const BaseString &str);
88 
90  BaseString(BaseString &&str);
91 
93  explicit BaseString(const value_type *str);
94 
96  BaseString(const value_type *str, uint32 len);
97 
99  BaseString(const value_type *beginP, const value_type *endP);
100 
101  bool operator==(const BaseString &x) const;
102  bool operator==(const value_type *x) const;
103  bool operator!=(const BaseString &x) const;
104  bool operator!=(const value_type *x) const;
105 
106  bool operator<(const BaseString &x) const;
107  bool operator<(const value_type *x) const;
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  bool operator>=(const BaseString &x) const;
113  bool operator>=(const value_type *x) const;
114 
119  bool equals(const BaseString &x) const;
120  bool equals(const value_type *x) const;
121  bool equalsC(const char *x) const;
122  int compareTo(const BaseString &x) const; // strcmp clone
123  int compareTo(const value_type *x) const; // strcmp clone
124  int compareToC(const char *x) const; // strcmp clone
125 
127  void setChar(value_type c, uint32 p);
128 
134  void deleteChar(uint32 p);
135 
137  inline void deleteLastChar() { chop(1); }
138 
140  void erase(uint32 p, uint32 len = npos);
141 
143  iterator erase(iterator it);
144 
146  void chop(uint32 len = 1);
147 
149  void clear();
150 
151  iterator begin() {
152  // Since the user could potentially
153  // change the string via the returned
154  // iterator we have to assure we are
155  // pointing to a unique storage.
156  makeUnique();
157 
158  return _str;
159  }
160 
161  iterator end() {
162  return begin() + size();
163  }
164 
165  const_iterator begin() const {
166  return _str;
167  }
168 
169  const_iterator end() const {
170  return begin() + size();
171  }
172 
173  inline const value_type *c_str() const { return _str; }
174  inline uint size() const { return _size; }
175 
176  inline bool empty() const { return (_size == 0); }
177  value_type firstChar() const { return (_size > 0) ? _str[0] : 0; }
178  value_type lastChar() const { return (_size > 0) ? _str[_size - 1] : 0; }
179 
180  value_type operator[](int idx) const {
181  assert(_str);
182  assert(idx >= 0);
183  assert(idx < (int)_size);
184  return _str[idx];
185  }
186 
190  bool contains(const BaseString &otherString) const;
191  bool contains(value_type x) const;
192 
194  void insertChar(value_type c, uint32 p);
195  void insertString(const value_type *s, uint32 p);
196  void insertString(const BaseString &s, uint32 p);
197 
199  uint32 find(value_type x, uint32 pos = 0) const;
201  size_t find(const value_type *s, uint32 pos = 0) const;
202  uint32 find(const BaseString &str, uint32 pos = 0) const;
203 
205  size_t rfind(const value_type *s) const;
206  size_t rfind(const BaseString &s) const {
207  return rfind(s.c_str());
208  }
209 
211  size_t rfind(value_type c, size_t pos = npos) const;
212 
214  size_t findFirstOf(value_type c, size_t pos = 0) const;
215 
217  size_t findFirstOf(const value_type *chars, size_t pos = 0) const;
218  size_t findFirstOf(const BaseString &chars, size_t pos = 0) const {
219  return findFirstOf(chars.c_str(), pos);
220  }
221 
223  size_t findLastOf(value_type c, size_t pos = npos) const;
224 
226  size_t findLastOf(const value_type *chars, size_t pos = npos) const;
227  size_t findLastOf(const BaseString &chars, size_t pos = npos) const {
228  return findLastOf(chars.c_str(), pos);
229  }
230 
232  size_t findFirstNotOf(value_type c, size_t pos = 0) const;
233 
235  size_t findFirstNotOf(const value_type *chars, size_t pos = 0) const;
236  size_t findFirstNotOf(const BaseString &chars, size_t pos = 0) const {
237  return findFirstNotOf(chars.c_str(), pos);
238  }
239 
241  size_t findLastNotOf(value_type c) const;
242 
244  size_t findLastNotOf(const value_type *chars) const;
245  size_t findLastNotOf(const BaseString &chars) const {
246  return findLastNotOf(chars.c_str());
247  }
248 
263  // Replace 'count' bytes, starting from 'pos' with str.
264  void replace(uint32 pos, uint32 count, const BaseString &str);
265  // The same as above, but accepts a C-like array of characters.
266  void replace(uint32 pos, uint32 count, const value_type *str);
267  // Replace the characters in [begin, end) with str._str.
268  void replace(iterator begin, iterator end, const BaseString &str);
269  // Replace the characters in [begin, end) with str.
270  void replace(iterator begin, iterator end, const value_type *str);
271  // Replace _str[posOri, posOri + countOri) with
272  // str._str[posDest, posDest + countDest)
273  void replace(uint32 posOri, uint32 countOri, const BaseString &str,
274  uint32 posDest, uint32 countDest);
275  // Replace _str[posOri, posOri + countOri) with
276  // str[posDest, posDest + countDest)
277  void replace(uint32 posOri, uint32 countOri, const value_type *str,
278  uint32 posDest, uint32 countDest);
286  void replace(value_type from, value_type to);
287 
289  void append(const value_type *begin, const value_type *end);
290 
300  void wordWrap(const uint32 maxLength);
301 
303  uint64 asUint64() const;
304 
306  uint64 asUint64Ext() const;
307 
314  void toLowercase();
315 
322  void toUppercase();
323 
328  void trim();
329 
330  uint hash() const;
331 
332 protected:
333  ~BaseString();
334 
335  void makeUnique() {
336  ensureCapacity(_size, true);
337  }
338 
339  void ensureCapacity(uint32 new_size, bool keep_old);
340  void incRefCount() const;
341  void decRefCount(int *oldRefCount);
342  void initWithValueTypeStr(const value_type *str, uint32 len);
343 
344  void assignInsert(const value_type *str, uint32 p);
345  void assignInsert(value_type c, uint32 p);
346  void assignInsert(const BaseString &str, uint32 p);
347  void assignAppend(const value_type *str);
348  void assignAppend(value_type c);
349  void assignAppend(const BaseString &str);
350  void assign(const BaseString &str);
351  void assign(BaseString &&str);
352  void assign(value_type c);
353  void assign(const value_type *str);
354 
355  bool pointerInOwnBuffer(const value_type *str) const;
356 
357  uint getUnsignedValue(uint pos) const;
358 
359  void toCase(int (*caseChangeFunc)(int));
360 
361  static uint32 cStrLen(const value_type *str);
362  static const value_type *cMemChr(const value_type *ptr, value_type c, size_t count);
363  static value_type *cMemChr(value_type *ptr, value_type c, size_t count);
364  static int cMemCmp(const value_type* ptr1, const value_type* ptr2, size_t count);
365 };
366 }
367 #endif
size_t findFirstNotOf(value_type c, size_t pos=0) const
bool equals(const BaseString &x) const
uint64 asUint64() const
void insertChar(value_type c, uint32 p)
uint64 asUint64Ext() const
void deleteChar(uint32 p)
value_type _storage[_builtinCapacity]
Definition: str-base.h:67
size_t findLastOf(value_type c, size_t pos=npos) const
size_t findLastNotOf(value_type c) const
uint32 find(value_type x, uint32 pos=0) const
size_t findFirstOf(value_type c, size_t pos=0) const
Definition: str-base.h:32
void chop(uint32 len=1)
struct Common::BaseString::@23::@25 _extern
uint32 _size
Definition: str-base.h:54
constexpr BaseString()
Definition: str-base.h:84
static const uint32 _builtinCapacity
Definition: str-base.h:47
Definition: algorithm.h:29
void deleteLastChar()
Definition: str-base.h:137
void erase(uint32 p, uint32 len=npos)
size_t rfind(const value_type *s) const
void replace(uint32 pos, uint32 count, const BaseString &str)
void append(const value_type *begin, const value_type *end)
void setChar(value_type c, uint32 p)
value_type * _str
Definition: str-base.h:60
void wordWrap(const uint32 maxLength)
bool contains(const BaseString &otherString) const