TDME2  1.9.200
MutableString.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 
6 #include <tdme/tdme.h>
7 #include <tdme/math/Math.h>
10 
11 using std::string;
12 using std::to_string;
13 using std::vector;
14 
15 using tdme::math::Math;
17 
18 /**
19  * Mutable utf8 aware string class
20  * @author Andreas Drewke
21  */
23 {
24 public:
25  /**
26  * Public default constructor
27  */
28  inline MutableString() {}
29 
30  /**
31  * Public constructor
32  * @param s string
33  */
34  inline MutableString(const string& s) {
35  data = s;
36  }
37 
38  /**
39  * Public constructor
40  * @param i integer
41  */
42  inline MutableString(int i) {
43  set(i);
44  }
45 
46  /**
47  * Public constructor
48  * @param f f
49  * @param decimals decimals
50  */
51  inline MutableString(float f, int32_t decimals = 3) {
52  set(f, decimals);
53  }
54 
55  /**
56  * @return binary size
57  */
58  inline int size() const {
59  return data.size();
60  }
61 
62  /**
63  * @return character count
64  */
65  inline int length() const {
66  if (utf8Length != -1) return utf8Length;
67  auto u8It = getUTF8CharacterIterator();
68  while (u8It.hasNext() == true) u8It.next();
69  utf8Length = u8It.getCharacterPosition();
70  return utf8Length;
71  }
72 
73  /**
74  * Get char at given binary index
75  * @param idx idx
76  * @return char
77  */
78  inline char getCharAt(int32_t idx) const {
79  if (idx < 0 || idx >= data.size()) return 0;
80  return data[idx];
81  }
82 
83  /**
84  * @return utf 8 character at given character index
85  */
86  inline int getUTF8CharAt(int32_t idx) const {
87  auto u8It = getUTF8CharacterIterator();
88  u8It.seekBinaryPosition(getUtf8BinaryIndex(idx));
89  return u8It.hasNext() == true?u8It.next():-1;
90  }
91 
92  /**
93  * Reset
94  */
95  inline MutableString& reset() {
96  data.clear();
98  utf8Length = -1;
99  return *this;
100  }
101 
102  /**
103  * Set character
104  * @param c char
105  * @return this mutable string
106  */
107  inline MutableString& set(char c) {
108  reset();
109  append(c);
110  return *this;
111  }
112 
113  /**
114  * Append character
115  * @param c char
116  * @return this mutable string
117  */
118  inline MutableString& append(char c) {
119  data.push_back(c);
120  return *this;
121  }
122 
123  /**
124  * Insert character c at idx
125  * @param idx index
126  * @param c char
127  * @return this mutable string
128  */
129  inline MutableString& insert(int32_t idx, char c) {
130  auto binaryIdx = getUtf8BinaryIndex(idx);
131  data.insert(binaryIdx, 1, c);
132  removeCache(binaryIdx, idx);
133  return *this;
134  }
135 
136  /**
137  * Set string
138  * @param s s
139  * @return this mutable string
140  */
141  inline MutableString& set(const string& s) {
142  reset();
143  append(s);
144  return *this;
145  }
146 
147  /**
148  * Append string
149  * @param s s
150  * @return this mutable string
151  */
152  inline MutableString& append(const string& s) {
153  data+= s;
154  return *this;
155  }
156 
157  /**
158  * Insert string at idx
159  * @param idx index
160  * @param s string
161  * @return this mutable string
162  */
163  inline MutableString& insert(int32_t idx, const string& s) {
164  auto binaryIdx = getUtf8BinaryIndex(idx);
165  data.insert(binaryIdx, s);
166  removeCache(binaryIdx, idx);
167  return *this;
168  }
169 
170  /**
171  * Set mutable string
172  * @param s s
173  * @return this mutable string
174  */
175  inline MutableString& set(const MutableString& s) {
176  reset();
177  append(s);
178  return *this;
179  }
180 
181  /**
182  * Append mutable string
183  * @param s s
184  * @return this mutable string
185  */
186  inline MutableString& append(const MutableString& s) {
187  data+= s.data;
188  return *this;
189  }
190 
191  /**
192  * Insert mutable string at idx
193  * @param idx index
194  * @param s string
195  * @return this mutable string
196  */
197  inline MutableString& insert(int32_t idx, const MutableString& s) {
198  auto binaryIdx = getUtf8BinaryIndex(idx);
199  insert(binaryIdx, s.data);
200  removeCache(binaryIdx, idx);
201  return *this;
202  }
203 
204  /**
205  * Set integer
206  * @param i i
207  * @return this mutable string
208  */
209  inline MutableString& set(int32_t i) {
210  reset();
211  append(i);
212  return *this;
213  }
214 
215  /**
216  * Append integer
217  * @param i i
218  * @return this mutable string
219  */
220  inline MutableString& append(int32_t i) {
221  data+= to_string(i);
222  return *this;
223  }
224 
225  /**
226  * Insert integer at idx
227  * @param idx index
228  * @param i i
229  * @return this mutable string
230  */
231  inline MutableString& insert(int32_t idx, int32_t i) {
232  // see: http://stackoverflow.com/questions/7123490/how-compiler-is-converting-integer-to-string-and-vice-versa
233  auto negative = false;
234  if (i < 0) {
235  negative = true;
236  i = -i;
237  }
238  while (true == true) {
239  auto remainder = i % 10;
240  i = i / 10;
241  insert(idx, static_cast<char>(('0' + remainder)));
242  if (i == 0) {
243  break;
244  }
245  }
246  if (negative == true) {
247  insert(idx, '-');
248  }
249  return *this;
250  }
251 
252  /**
253  * Set float
254  * @param f f
255  * @param decimals decimals
256  * @return this mutable string
257  */
258  inline MutableString& set(float f, int32_t decimals = 3) {
259  reset();
260  append(f, decimals);
261  return *this;
262  }
263 
264  /**
265  * Append float with given decimals
266  * @param f f
267  * @param decimals decimals
268  * @return this mutable string
269  */
270  inline MutableString& append(float f, int32_t decimals = 3) {
271  insert(data.size(), f, decimals);
272  return *this;
273  }
274 
275  /**
276  * Insert float at idx
277  * @param idx index
278  * @param f float
279  * @param decimals decimals
280  * @return this mutable string
281  */
282  inline MutableString& insert(int32_t idx, float f, int32_t decimals = 3) {
283  // see: http://stackoverflow.com/questions/7123490/how-compiler-is-converting-integer-to-string-and-vice-versa
284  auto integer = static_cast<int>(f);
285  for (auto i = 0; i < decimals; i++) {
286  auto integerDecimal = static_cast<int>(((f - integer) * Math::pow(10.0f, static_cast<float>(i) + 1.0f))) - (10 * static_cast<int>(((f - integer) * Math::pow(10.0f, static_cast<float>(i)))));
287  insert(idx + i, Math::abs(integerDecimal));
288  }
289  insert(idx, '.');
290  insert(idx, Math::abs(integer));
291  if (f < 0.0) insert(idx, '-');
292  return *this;
293  }
294 
295  /**
296  * Remove characters at idx with given length
297  * @param idx idx
298  * @param count length
299  * @param binaryCount count of binary bytes that have been removed
300  * @return this mutable string
301  */
302  inline MutableString& remove(int32_t idx, int32_t count, int* binaryCount = nullptr) {
303 
304  auto u8It = getUTF8CharacterIterator();
305  u8It.seekCharacterPosition(idx);
306  auto binaryStartIdx = u8It.getBinaryPosition();
307  for (auto i = 0; u8It.hasNext() == true &&i < count; i++) {
308  u8It.next();
309  }
310  auto binaryEndIdx = u8It.getBinaryPosition();
311  data.erase(binaryStartIdx, binaryEndIdx - binaryStartIdx);
312  removeCache(binaryStartIdx, idx);
313  if (binaryCount != nullptr) *binaryCount = binaryEndIdx - binaryStartIdx;
314  return *this;
315  }
316 
317  /**
318  * Returns the character index where string s have been found or -1 if not found
319  * @param s string
320  * @param idx index
321  * @return index where string has been found or -1
322  */
323  inline int32_t indexOf(const MutableString& s, int32_t idx) const {
324  return data.find(s.data, getUtf8BinaryIndex(idx));
325  }
326 
327  /**
328  * Returns the character index where string s have been found or -1 if not found
329  * @param s string
330  * @return index where string has been found or -1
331  */
332  inline int32_t indexOf(const MutableString& s) const {
333  return indexOf(s, 0);
334  }
335 
336  /**
337  * Replace string with another string
338  * @param what what to replace
339  * @param by to replace by
340  * @param beginIndex index to begin with
341  */
342  inline void replace(const string& what, const string& by, int beginIndex = 0) {
343  beginIndex = getUtf8BinaryIndex(beginIndex);
344  string result = data;
345  if (what.empty()) return;
346  while ((beginIndex = result.find(what, beginIndex)) != std::string::npos) {
347  result.replace(beginIndex, what.length(), by);
348  beginIndex += by.length();
349  }
350  // TODO: could be improved
351  cache.removeCache();
352  utf8Length = -1;
353  }
354 
355  /**
356  * @return if mutable string is empty
357  */
358  inline bool empty() const {
359  return data.empty();
360  }
361 
362  /**
363  * Equals
364  * @param s2 string 2
365  * @return string 2 equals this string
366  */
367  inline bool equals(const string& s2) const {
368  return data == s2;
369  }
370 
371  /**
372  * Equals
373  * @param s2 string 2
374  * @return string 2 equals this string
375  */
376  inline bool equals(const MutableString& s2) const {
377  return data == s2.data;
378  }
379 
380  /**
381  * @return string
382  */
383  inline const string& getString() const {
384  return data;
385  }
386 
387  /**
388  * @return UTF8 character iterator
389  */
391  return UTF8CharacterIterator(data, &cache);
392  }
393 
394  /**
395  * @return Get utf8 binary index
396  * @param idx character index
397  * @return utf8 binary index
398  */
399  int getUtf8BinaryIndex(int idx) const {
400  auto u8It = getUTF8CharacterIterator();
401  u8It.seekCharacterPosition(idx);
402  return u8It.getBinaryPosition();
403  }
404 
405  /**
406  * @return Get utf8 character index
407  * @param idx binary index
408  * @return utf8 character index
409  */
410  int getUtf8CharacterIndex(int idx) const {
411  auto u8It = getUTF8CharacterIterator();
412  u8It.seekBinaryPosition(idx);
413  return u8It.getCharacterPosition();
414  }
415 
416  /**
417  * Clone
418  */
419  inline MutableString clone() {
420  return MutableString(data);
421  }
422 
423 private:
424  string data;
426  mutable int utf8Length { -1 };
427 
428  /**
429  * Remove from cache by binary index
430  * @param idx binary index
431  */
432  inline void removeCache(int binaryIdx, int characterIdx) {
433  //
434  cache.removeCache(binaryIdx, characterIdx);
435  // we also cache the length
436  utf8Length = -1;
437  }
438 
439 };
Standard math functions.
Definition: Math.h:19
Mutable utf8 aware string class.
Definition: MutableString.h:23
MutableString & append(char c)
Append character.
MutableString & insert(int32_t idx, float f, int32_t decimals=3)
Insert float at idx.
MutableString(const string &s)
Public constructor.
Definition: MutableString.h:34
MutableString & insert(int32_t idx, const string &s)
Insert string at idx.
bool equals(const string &s2) const
Equals.
char getCharAt(int32_t idx) const
Get char at given binary index.
Definition: MutableString.h:78
MutableString & insert(int32_t idx, int32_t i)
Insert integer at idx.
MutableString & append(const string &s)
Append string.
int getUtf8CharacterIndex(int idx) const
MutableString & append(const MutableString &s)
Append mutable string.
const UTF8CharacterIterator getUTF8CharacterIterator() const
MutableString & insert(int32_t idx, const MutableString &s)
Insert mutable string at idx.
MutableString & set(int32_t i)
Set integer.
int getUtf8BinaryIndex(int idx) const
MutableString & remove(int32_t idx, int32_t count, int *binaryCount=nullptr)
Remove characters at idx with given length.
MutableString & set(const MutableString &s)
Set mutable string.
MutableString & set(const string &s)
Set string.
MutableString & set(float f, int32_t decimals=3)
Set float.
MutableString & append(int32_t i)
Append integer.
MutableString()
Public default constructor.
Definition: MutableString.h:28
MutableString clone()
Clone.
UTF8CharacterIterator::UTF8PositionCache cache
MutableString & append(float f, int32_t decimals=3)
Append float with given decimals.
const string & getString() const
MutableString & set(char c)
Set character.
MutableString & reset()
Reset.
Definition: MutableString.h:95
MutableString(int i)
Public constructor.
Definition: MutableString.h:42
MutableString & insert(int32_t idx, char c)
Insert character c at idx.
int32_t indexOf(const MutableString &s, int32_t idx) const
Returns the character index where string s have been found or -1 if not found.
void removeCache(int binaryIdx, int characterIdx)
Remove from cache by binary index.
void replace(const string &what, const string &by, int beginIndex=0)
Replace string with another string.
int32_t indexOf(const MutableString &s) const
Returns the character index where string s have been found or -1 if not found.
MutableString(float f, int32_t decimals=3)
Public constructor.
Definition: MutableString.h:51
bool equals(const MutableString &s2) const
Equals.
int getUTF8CharAt(int32_t idx) const
Definition: MutableString.h:86
UTF8 string character iterator.