sources/mystring.h

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

mercurial