Str.pkg 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. $#include "Str.h"
  2. /// %String class.
  3. class String
  4. {
  5. public:
  6. /// Construct empty.
  7. String();
  8. /// Construct from another string.
  9. String(const String& str);
  10. /// Construct from a C string.
  11. String(const char* str);
  12. /// Construct from a char array and length.
  13. String(const char* str, unsigned length);
  14. /// Construct from an integer.
  15. explicit String(int value);
  16. /// Construct from a short integer.
  17. explicit String(short value);
  18. /// Construct from a long integer.
  19. explicit String(long value);
  20. /// Construct from a long long integer.
  21. explicit String(long long value);
  22. /// Construct from an unsigned integer.
  23. explicit String(unsigned value);
  24. /// Construct from an unsigned short integer.
  25. explicit String(unsigned short value);
  26. /// Construct from an unsigned long integer.
  27. explicit String(unsigned long value);
  28. /// Construct from an unsigned long long integer.
  29. explicit String(unsigned long long value);
  30. /// Construct from a float.
  31. explicit String(float value);
  32. /// Construct from a double.
  33. explicit String(double value);
  34. /// Construct from a bool.
  35. explicit String(bool value);
  36. /// Construct from a character.
  37. explicit String(char value);
  38. /// Construct from a character and fill length.
  39. explicit String(char value, unsigned length);
  40. /// Destruct.
  41. ~String();
  42. /// Add a string.
  43. String operator + (const String& rhs) const;
  44. /// Add a C string.
  45. String operator + (const char* rhs) const;
  46. /// Add a character.
  47. String operator + (char rhs) const;
  48. /// Test for equality with another string.
  49. bool operator == (const String& rhs) const;
  50. /// Test if string is less than another string.
  51. bool operator < (const String& rhs) const;
  52. /// Test for equality with a C string.
  53. bool operator == (const char* rhs) const;
  54. /// Test if string is less than a C string.
  55. bool operator < (const char* rhs) const;
  56. /// Return char at index.
  57. char& At(unsigned index);
  58. /// Return const char at index.
  59. const char& At(unsigned index) const;
  60. /// Replace all occurrences of a character.
  61. void Replace(char replaceThis, char replaceWith);
  62. /// Replace all occurrences of a string.
  63. void Replace(const String& replaceThis, const String& replaceWith);
  64. /// Replace a substring.
  65. void Replace(unsigned pos, unsigned length, const String& replaceWith);
  66. /// Return a string with all occurrences of a character replaced.
  67. String Replaced(char replaceThis, char replaceWith) const;
  68. /// Return a string with all occurrences of a string replaced.
  69. String Replaced(const String& replaceThis, const String& replaceWith) const;
  70. /// Append a string.
  71. String& Append(const String& str);
  72. /// Append a C string.
  73. String& Append(const char* str);
  74. /// Append a character.
  75. String& Append(char c);
  76. /// Append characters.
  77. String& Append(const char* str, unsigned length);
  78. /// Insert a string.
  79. void Insert(unsigned pos, const String& str);
  80. /// Insert a character.
  81. void Insert(unsigned pos, char c);
  82. /// Erase a substring.
  83. void Erase(unsigned pos, unsigned length = 1);
  84. /// Resize the string.
  85. void Resize(unsigned newLength);
  86. /// Set new capacity.
  87. void Reserve(unsigned newCapacity);
  88. /// Reallocate so that no extra memory is used.
  89. void Compact();
  90. /// Clear the string.
  91. void Clear();
  92. /// Swap with another string.
  93. void Swap(String& str);
  94. /// Return first char, or 0 if empty.
  95. char Front() const;
  96. /// Return last char, or 0 if empty.
  97. char Back() const;
  98. /// Return a substring from position to end.
  99. String Substring(unsigned pos) const;
  100. /// Return a substring with length from position.
  101. String Substring(unsigned pos, unsigned length) const;
  102. /// Return string with whitespace trimmed from the beginning and the end.
  103. String Trimmed() const;
  104. /// Return string in uppercase.
  105. String ToUpper() const;
  106. /// Return string in lowercase.
  107. String ToLower() const;
  108. /// Return index to the first occurrence of a string, or NPOS if not found.
  109. unsigned Find(const String& str, unsigned startPos = 0) const;
  110. /// Return index to the first occurrence of a character, or NPOS if not found.
  111. unsigned Find(char c, unsigned startPos = 0) const;
  112. /// Return index to the last occurrence of a string, or NPOS if not found.
  113. unsigned FindLast(const String& str, unsigned startPos = String::NPOS) const;
  114. /// Return index to the last occurrence of a character, or NPOS if not found.
  115. unsigned FindLast(char c, unsigned startPos = String::NPOS) const;
  116. /// Return whether starts with a string.
  117. bool StartsWith(const String& str) const;
  118. /// Return whether ends with a string.
  119. bool EndsWith(const String& str) const;
  120. /// Return the C string.
  121. const char* CString() const;
  122. /// Return length.
  123. unsigned Length() const;
  124. /// Return buffer capacity.
  125. unsigned Capacity() const;
  126. /// Return whether the string is empty.
  127. bool Empty() const;
  128. /// Return comparision result with a string.
  129. int Compare(const String& str, bool caseSensitive = true) const;
  130. /// Return comparision result with a C string.
  131. int Compare(const char* str, bool caseSensitive = true) const;
  132. /// Return whether contains a specific occurences of string.
  133. bool Contains(const String& str) const;
  134. /// Return whether contains a specific character.
  135. bool Contains(char c) const;
  136. /// Return hash value for HashSet & HashMap.
  137. unsigned ToHash() const;
  138. /// Return length of a C string.
  139. static unsigned CStringLength(const char* str);
  140. /// Compare two C strings.
  141. static int Compare(const char* str1, const char* str2, bool caseSensitive);
  142. /// Empty string.
  143. static const String EMPTY;
  144. };