sources/mystring.h

changeset 182
20ca0a6be175
parent 179
7fc34735178e
child 183
9b6a0daedfc0
equal deleted inserted replaced
181:e254398fcc7c 182:20ca0a6be175
35 #include "basics.h" 35 #include "basics.h"
36 #include "list.h" 36 #include "list.h"
37 37
38 BEGIN_ZFC_NAMESPACE 38 BEGIN_ZFC_NAMESPACE
39 39
40 using StringList = std::vector<class String>; 40 using String = std::string;
41 using StringList = std::vector<std::string>;
42 using namespace std::string_literals;
41 43
42 class String 44 std::string to_lowercase(const std::string& string);
43 { 45 std::string join_string_list(const StringList& strings, const String& delim);
44 public: 46 std::string mid(const std::string& str, int rangeBegin, int rangeEnd);
45 typedef std::string::iterator Iterator; 47 std::string right(const std::string& str, int length);
46 typedef std::string::const_iterator ConstIterator; 48 std::string vsprintf(const char* formatString, va_list args);
47 49 std::string __cdecl sprintf(const char* formatString, ...);
48 String(); 50 std::string remove_range(const std::string& string, int start, int end);
49 String(char a); 51 void replace_all(std::string& str, const char* text, const char* replacement);
50 String(const char* data); 52 bool starts_with(const std::string& str, const String& other);
51 String(const std::string& data); 53 StringList split(const std::string& string, const String& delimeter);
52 String(const Vector<char>& data); 54 std::optional<long> to_int(const char* str, int base = 10);
53 55 void normalize(std::string& string, int (*filter)(int) = std::isspace);
54 void append(const char* text);
55 void append(char character);
56 void append(const String& text);
57 ConstIterator begin() const;
58 Iterator begin();
59 int compare(const String &other) const;
60 int count(char character) const;
61 const char* chars() const;
62 void clear();
63 ConstIterator end() const;
64 Iterator end();
65 bool endsWith(const String &other) const;
66 int find(const char* subString, int startingPosition = 0) const;
67 int find(char character, int startingPosition = 0) const;
68 int indexDifference(int a, int b);
69 void insert(int position, char character);
70 void insert(int position, const char* string);
71 bool isEmpty() const;
72 bool isNumeric() const;
73 void modifyIndex(int &a) const;
74 int findLast(const char* subString, int startingPosition = -1) const;
75 int length() const;
76 bool maskAgainst(const String &pattern) const;
77 String md5() const;
78 String mid(int rangeBegin, int rangeEnd) const;
79 void normalize(int(*filter)(int) = &isspace);
80 String normalized(int(*filter)(int) = &isspace) const;
81 void prepend(String text);
82 void remove(int position, int length);
83 void removeAt(int position);
84 void removeFromEnd(int length);
85 void removeFromStart(int length);
86 void replace(const char* text, const char* replacement);
87 void replace(int position, int amount, const String &text);
88 String right(int length) const;
89 void shrinkToFit();
90 StringList split(const String &delimeter) const;
91 StringList split(char delimeter) const;
92 void __cdecl sprintf(const char* fmtstr, ...);
93 bool startsWith(const String &other) const;
94 const std::string& stdString() const;
95 const unsigned char* toBytes() const;
96 double toDouble(bool* ok = nullptr) const;
97 float toFloat(bool* ok = nullptr) const;
98 long toInt(bool* ok = nullptr, int base = 10) const;
99 String toLowerCase() const;
100 String toUpperCase() const;
101 void vsprintf(const char* fmtstr, va_list args);
102
103 static String fromNumber(short int a);
104 static String fromNumber(int a);
105 static String fromNumber(long int a);
106 static String fromNumber(unsigned short int a);
107 static String fromNumber(unsigned int a);
108 static String fromNumber(unsigned long int a);
109 static String fromNumber(double a);
110 static String fromBytes(const ByteArray& bytes);
111
112 String operator+(const String& data) const;
113 String operator+(const char* data) const;
114 String operator+(int num) const;
115 String& operator+=(const String& data);
116 String& operator+=(const char* data);
117 String& operator+=(int num);
118 String& operator+=(char data);
119 char& operator[](int i);
120 char operator[](int i) const;
121 bool operator==(const String& other) const;
122 bool operator==(const char* other) const;
123 bool operator!=(const String& other) const;
124 bool operator!=(const char* other) const;
125 bool operator>(const String& other) const;
126 bool operator<(const String& other) const;
127 bool operator>=(const String& other) const;
128 bool operator<=(const String& other) const;
129 operator const char*() const;
130 operator const std::string&() const;
131
132 private:
133 std::string m_string;
134 };
135
136 String join_string_list(const StringList& strings, const String& delim);
137
138 inline bool operator==(const char* a, const String& b);
139 inline String operator+(const char* a, const String& b);
140
141 // --------------------------------------------------------------------------------------------------------------------
142
143 /*!
144 * \brief Constructs an empty string.
145 */
146 inline String::String() {}
147
148 /*!
149 * \brief Constructs a string from a single character.
150 * \param character Character to create a string out of.
151 */
152 inline String::String(char character)
153 {
154 char buffer[2] = { character, '\0' };
155 m_string = buffer;
156 }
157
158 /*!
159 * \brief Constructs a string from a char-array.
160 * \param string char-array to convert.
161 */
162 inline String::String(const char* string) :
163 m_string(string) {}
164
165 /*!
166 * \brief Constructs a string out of a \c std::string .
167 * \param string \c std::string to base the construction on.
168 */
169 inline String::String(const std::string& string) :
170 m_string(string) {}
171
172 /*!
173 * \brief Constructs a string out of a vector of characters. The vector does not have to be null-terminated.
174 * \param charVector Vector of characters to construct the string out of.
175 */
176 inline String::String(const Vector<char>& charVector) :
177 m_string(charVector.data(), charVector.size()) {}
178
179 /*!
180 * \returns a constant iterator to the beginning of the string.
181 */
182 inline String::ConstIterator String::begin() const
183 {
184 return m_string.cbegin();
185 }
186
187 /*!
188 * \returns the string's contents as a char-array.
189 */
190 inline const char* String::chars() const
191 {
192 return m_string.c_str();
193 }
194
195 /*!
196 * \returns the string's constant end-iterator.
197 */
198 inline String::ConstIterator String::end() const
199 {
200 return m_string.end();
201 }
202
203 /*!
204 * \returns whether or not the string is empty.
205 */
206 inline bool String::isEmpty() const
207 {
208 return m_string[0] == '\0';
209 }
210
211 /*!
212 * \returns the length of the string.
213 */
214 inline int String::length() const
215 {
216 return m_string.length();
217 }
218
219 /*!
220 * \returns the underlying \c std::string .
221 */
222 inline const std::string& String::stdString() const
223 {
224 return m_string;
225 }
226
227 /*!
228 * \brief Adds text from a char-array to the end of the string.
229 * \param text Text to append.
230 */
231 inline void String::append(const char* text)
232 {
233 m_string.append(text);
234 }
235
236 /*!
237 * \brief Adds text to the end of the string.
238 * \param text Text to append.
239 */
240 inline void String::append(char character)
241 {
242 m_string.push_back(character);
243 }
244
245 /*!
246 * \brief Adds text from another string to the end of this string.
247 * \param text Text to append.
248 */
249 inline void String::append(const String& text)
250 {
251 m_string.append(text.chars());
252 }
253
254 /*!
255 * \returns a mutable iterator to the beginning of the string.
256 */
257 inline String::Iterator String::begin()
258 {
259 return m_string.begin();
260 }
261
262 /*!
263 * \brief Clears the string.
264 */
265 inline void String::clear()
266 {
267 m_string.clear();
268 }
269
270 /*!
271 * \returns the string's mutable end-iterator.
272 */
273 inline String::Iterator String::end()
274 {
275 return m_string.end();
276 }
277
278 /*!
279 * \brief Compares two string indices, supporting negatives as offsets from the end of string.
280 * \param a First index to compare
281 * \param b Second index to compare
282 * \returns the difference of two indices.
283 */
284 inline int String::indexDifference(int a, int b)
285 {
286 modifyIndex(a);
287 modifyIndex(b);
288 return b - a;
289 }
290
291 /*!
292 * \brief Inserts a character into the string.
293 * \param position Position in the string where to insert the character into.
294 * \param character Character to insert into the string.
295 */
296 inline void String::insert(int position, char character)
297 {
298 m_string.insert(m_string.begin() + position, character);
299 }
300
301 /*!
302 * \brief Inserts a substring into the string.
303 * \param position Position in the string where to insert the substring.
304 * \param string Substring to insert.
305 */
306 inline void String::insert(int position, const char* string)
307 {
308 m_string.insert(position, string);
309 }
310
311 /*!
312 * \brief Modifies the given index so that if it is negative, it is translated into a positive index starting from the
313 * end of the string. For example, an index of -1 will be modified to point to the last character in the string,
314 * -2 to the second last, etc.
315 * \param index Index to translate.
316 */
317 inline void String::modifyIndex(int& index) const
318 {
319 if (index < 0)
320 index = length() - index;
321 }
322
323 /*!
324 * \brief Prepends the given text to the beginning of the string.
325 * \param text Text to prepend.
326 */
327 inline void String::prepend(String text)
328 {
329 m_string = (text + m_string).stdString();
330 }
331
332 /*!
333 * \brief Removes a range of text from the string.
334 * \param position Position where to start removing text.
335 * \param length Amount of characters to remove.
336 */
337 inline void String::remove(int position, int length)
338 {
339 m_string.replace(position, length, "");
340 }
341
342 /*!
343 * \brief Removes a single character from the string.
344 * \param position Position of the character to remove string from.
345 */
346 inline void String::removeAt(int position)
347 {
348 m_string.erase(m_string.begin() + position);
349 }
350
351 /*!
352 * \brief Removes a number of characters from the end of the string.
353 * \param length Amount of characters to remove.
354 */
355 inline void String::removeFromEnd(int length)
356 {
357 remove(this->length() - length, length);
358 }
359
360 /*!
361 * \brief Removes a number of characters from the beginning of the string.
362 * \param length Amount of characters to remove.
363 */
364 inline void String::removeFromStart(int length)
365 {
366 remove(0, length);
367 }
368
369 /*!
370 * \brief Replaces a range of text in the string with another.
371 * \param position Position where to start replacing text.
372 * \param amount Amount of characters to replace.
373 * \param text Replacement string.
374 */
375 inline void String::replace(int position, int amount, const String& text)
376 {
377 m_string.replace(position, amount, text.chars());
378 }
379
380 /*!
381 * \brief Shrinks the string so that it does not allocate more characters than necessary.
382 */
383 inline void String::shrinkToFit()
384 {
385 m_string.shrink_to_fit();
386 }
387
388 /*!
389 * \brief Converts a number into a string, and returns a new string with the number appended to the end of the string.
390 * \param number Number to convert and append.
391 * \returns the resulting string.
392 */
393 inline String String::operator+(int number) const
394 {
395 return *this + String::fromNumber(number);
396 }
397
398 /*!
399 * \brief Appends text into the string.
400 * \param text Text to append.
401 * \returns a reference to this string.
402 */
403 inline String& String::operator+=(const String& text)
404 {
405 append(text);
406 return *this;
407 }
408
409 /*!
410 * \brief Appends text into the string.
411 * \param text Text to append.
412 * \returns a reference to this string.
413 */
414 inline String& String::operator+=(const char* text)
415 {
416 append(text);
417 return *this;
418 }
419
420 /*!
421 * \brief Converts a number into a string, and appends it into this string.
422 * \param number The number to append.
423 * \returns a refence to this string.
424 */
425 inline String& String::operator+=(int number)
426 {
427 return operator+=(String::fromNumber(number));
428 }
429
430 /*!
431 * \brief Appends a character into this string.
432 * \param character The character to append.
433 * \return a reference to this string.
434 */
435 inline String& String::operator+=(char character)
436 {
437 append(character);
438 return *this;
439 }
440
441 /*!
442 * \param index Index referring to a character of this string.
443 * \returns an editable reference to the character pointed by the given index.
444 */
445 inline char& String::operator[](int index)
446 {
447 return m_string[index];
448 }
449
450 /*!
451 * \param index Index referring to a character of this string.
452 * \returns an const reference to the character pointed by the given index.
453 */
454 inline char String::operator[](int index) const
455 {
456 return m_string[index];
457 }
458
459 /*!
460 * \param other String to compare with.
461 * \returns whether or not this string is the same as the other string.
462 */
463 inline bool String::operator==(const String& other) const
464 {
465 return stdString() == other.stdString();
466 }
467
468 /*!
469 * \param other String to compare with.
470 * \returns whether or not this string is the same as the other string.
471 */
472 inline bool String::operator==(const char* other) const
473 {
474 return m_string == other;
475 }
476
477 /*!
478 * \param other String to compare with.
479 * \returns whether or not this string is different than the other string.
480 */
481 inline bool String::operator!=(const String& other) const
482 {
483 return stdString() != other.stdString();
484 }
485
486 /*!
487 * \param other String to compare with.
488 * \returns whether or not this string is different than the other string.
489 */
490 inline bool String::operator!=(const char* other) const
491 {
492 return m_string != other;
493 }
494
495 /*!
496 * \param other String to compare with.
497 * \return whether or not this string is lexicographically greater than the other string.
498 */
499 inline bool String::operator>(const String& other) const
500 {
501 return stdString() > other.stdString();
502 }
503
504 /*!
505 * \param other String to compare with.
506 * \return whether or not this string is lexicographically lesser than the other string.
507 */
508 inline bool String::operator<(const String& other) const
509 {
510 return stdString() < other.stdString();
511 }
512
513 /*!
514 * \param other String to compare with.
515 * \return whether or not this string is lexicographically at least as great as the other string.
516 */
517 inline bool String::operator>=(const String& other) const
518 {
519 return stdString() >= other.stdString();
520 }
521
522 /*!
523 * \param other String to compare with.
524 * \return whether or not this string is lexicographically at most as great as the other string.
525 */
526 inline bool String::operator<=(const String& other) const
527 {
528 return stdString() <= other.stdString();
529 }
530
531 /*!
532 * \returns a char-array representation of this string.
533 */
534 inline String::operator const char*() const
535 {
536 return chars();
537 }
538
539 /*!
540 * \returns the underlying \c std::string of this string.
541 */
542 inline String::operator const std::string&() const
543 {
544 return stdString();
545 }
546
547 /*!
548 * \returns the underlying char-array representation of this string, casted to unsigned chars.
549 */
550 inline const unsigned char* String::toBytes() const
551 {
552 return reinterpret_cast<const unsigned char*>(chars());
553 }
554
555 /*!
556 * \brief An \c operator== implementation that allows a char-array to be at the left side of a string comparison
557 * with a \c String.
558 * \param one A char-array representation of a string to compare.
559 * \param other A string to compare.
560 * \returns whether or not the two parameters are equal.
561 */
562 inline bool operator==(const char* one, const String& other)
563 {
564 return other == one;
565 }
566
567 /*!
568 * \brief An \c operator+ implementation that allows a char-array to be at the left side of a string catenation
569 * with a \c String.
570 * \param one A char-array representation of a string to catenate.
571 * \param other A string to catenate.
572 * \returns the catenated string.
573 */
574 inline String operator+(const char* one, const String& other)
575 {
576 return String(one) + other;
577 }
578
579 56
580 END_ZFC_NAMESPACE 57 END_ZFC_NAMESPACE

mercurial