ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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  explicit constexpr BaseString(value_type c) : _size((c == 0) ? 0 : 1), _str(_storage), _storage{c, 0} {}
88 
90  BaseString(const BaseString &str);
91 
93  BaseString(BaseString &&str);
94 
96  explicit BaseString(const value_type *str);
97 
99  BaseString(const value_type *str, uint32 len);
100 
102  BaseString(const value_type *beginP, const value_type *endP);
103 
104  bool operator==(const BaseString &x) const;
105  bool operator==(const value_type *x) const;
106  bool operator!=(const BaseString &x) const;
107  bool operator!=(const value_type *x) const;
108 
109  bool operator<(const BaseString &x) const;
110  bool operator<(const value_type *x) const;
111  bool operator<=(const BaseString &x) const;
112  bool operator<=(const value_type *x) const;
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 
122  bool equals(const BaseString &x) const;
123  bool equals(const value_type *x) const;
124  bool equalsC(const char *x) const;
125  int compareTo(const BaseString &x) const; // strcmp clone
126  int compareTo(const value_type *x) const; // strcmp clone
127  int compareToC(const char *x) const; // strcmp clone
128 
130  void setChar(value_type c, uint32 p);
131 
137  void deleteChar(uint32 p);
138 
140  inline void deleteLastChar() { chop(1); }
141 
143  void erase(uint32 p, uint32 len = npos);
144 
146  iterator erase(iterator it);
147 
149  void chop(uint32 len = 1);
150 
152  void clear();
153 
154  iterator begin() {
155  // Since the user could potentially
156  // change the string via the returned
157  // iterator we have to assure we are
158  // pointing to a unique storage.
159  makeUnique();
160 
161  return _str;
162  }
163 
164  iterator end() {
165  return begin() + size();
166  }
167 
168  const_iterator begin() const {
169  return _str;
170  }
171 
172  const_iterator end() const {
173  return begin() + size();
174  }
175 
176  inline const value_type *c_str() const { return _str; }
177  inline uint size() const { return _size; }
178 
179  inline bool empty() const { return (_size == 0); }
180  value_type firstChar() const { return (_size > 0) ? _str[0] : 0; }
181  value_type lastChar() const { return (_size > 0) ? _str[_size - 1] : 0; }
182 
183  value_type operator[](int idx) const {
184  assert(_str);
185  assert(idx >= 0);
186  assert(idx < (int)_size);
187  return _str[idx];
188  }
189 
193  bool contains(const BaseString &otherString) const;
194  bool contains(value_type x) const;
195 
197  void insertChar(value_type c, uint32 p);
198  void insertString(const value_type *s, uint32 p);
199  void insertString(const BaseString &s, uint32 p);
200 
202  uint32 find(value_type x, uint32 pos = 0) const;
204  size_t find(const value_type *s, uint32 pos = 0) const;
205  uint32 find(const BaseString &str, uint32 pos = 0) const;
206 
208  size_t rfind(const value_type *s) const;
209  size_t rfind(const BaseString &s) const {
210  return rfind(s.c_str());
211  }
212 
214  size_t rfind(value_type c, size_t pos = npos) const;
215 
217  size_t findFirstOf(value_type c, size_t pos = 0) const;
218 
220  size_t findFirstOf(const value_type *chars, size_t pos = 0) const;
221  size_t findFirstOf(const BaseString &chars, size_t pos = 0) const {
222  return findFirstOf(chars.c_str(), pos);
223  }
224 
226  size_t findLastOf(value_type c, size_t pos = npos) const;
227 
229  size_t findLastOf(const value_type *chars, size_t pos = npos) const;
230  size_t findLastOf(const BaseString &chars, size_t pos = npos) const {
231  return findLastOf(chars.c_str(), pos);
232  }
233 
235  size_t findFirstNotOf(value_type c, size_t pos = 0) const;
236 
238  size_t findFirstNotOf(const value_type *chars, size_t pos = 0) const;
239  size_t findFirstNotOf(const BaseString &chars, size_t pos = 0) const {
240  return findFirstNotOf(chars.c_str(), pos);
241  }
242 
244  size_t findLastNotOf(value_type c) const;
245 
247  size_t findLastNotOf(const value_type *chars) const;
248  size_t findLastNotOf(const BaseString &chars) const {
249  return findLastNotOf(chars.c_str());
250  }
251 
266  // Replace 'count' bytes, starting from 'pos' with str.
267  void replace(uint32 pos, uint32 count, const BaseString &str);
268  // The same as above, but accepts a C-like array of characters.
269  void replace(uint32 pos, uint32 count, const value_type *str);
270  // Replace the characters in [begin, end) with str._str.
271  void replace(iterator begin, iterator end, const BaseString &str);
272  // Replace the characters in [begin, end) with str.
273  void replace(iterator begin, iterator end, const value_type *str);
274  // Replace _str[posOri, posOri + countOri) with
275  // str._str[posDest, posDest + countDest)
276  void replace(uint32 posOri, uint32 countOri, const BaseString &str,
277  uint32 posDest, uint32 countDest);
278  // Replace _str[posOri, posOri + countOri) with
279  // str[posDest, posDest + countDest)
280  void replace(uint32 posOri, uint32 countOri, const value_type *str,
281  uint32 posDest, uint32 countDest);
289  void replace(value_type from, value_type to);
290 
292  void append(const value_type *begin, const value_type *end);
293 
303  void wordWrap(const uint32 maxLength);
304 
306  uint64 asUint64() const;
307 
309  uint64 asUint64Ext() const;
310 
317  void toLowercase();
318 
325  void toUppercase();
326 
331  void trim();
332 
333  uint hash() const;
334 
335 protected:
336  ~BaseString();
337 
338  void makeUnique() {
339  ensureCapacity(_size, true);
340  }
341 
342  void ensureCapacity(uint32 new_size, bool keep_old);
343  void incRefCount() const;
344  void decRefCount(int *oldRefCount);
345  void initWithValueTypeStr(const value_type *str, uint32 len);
346 
347  void assignInsert(const value_type *str, uint32 p);
348  void assignInsert(value_type c, uint32 p);
349  void assignInsert(const BaseString &str, uint32 p);
350  void assignAppend(const value_type *str);
351  void assignAppend(value_type c);
352  void assignAppend(const BaseString &str);
353  void assign(const BaseString &str);
354  void assign(BaseString &&str);
355  void assign(value_type c);
356  void assign(const value_type *str);
357 
358  bool pointerInOwnBuffer(const value_type *str) const;
359 
360  uint getUnsignedValue(uint pos) const;
361 
362  void toCase(int (*caseChangeFunc)(int));
363 
364  static uint32 cStrLen(const value_type *str);
365  static const value_type *cMemChr(const value_type *ptr, value_type c, size_t count);
366  static value_type *cMemChr(value_type *ptr, value_type c, size_t count);
367  static int cMemCmp(const value_type* ptr1, const value_type* ptr2, size_t count);
368 };
369 }
370 #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
constexpr BaseString(value_type c)
Definition: str-base.h:87
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:140
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