Str.pkg 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. $#include "Str.h"
  2. /// %String class.
  3. class String
  4. {
  5. public:
  6. /// Construct empty.
  7. String() :
  8. length_(0),
  9. capacity_(0),
  10. buffer_(&endZero)
  11. {
  12. }
  13. /// Construct from another string.
  14. String(const String& str) :
  15. length_(0),
  16. capacity_(0),
  17. buffer_(&endZero)
  18. {
  19. *this = str;
  20. }
  21. /// Construct from a C string.
  22. String(const char* str) :
  23. length_(0),
  24. capacity_(0),
  25. buffer_(&endZero)
  26. {
  27. *this = str;
  28. }
  29. /// Construct from a C string.
  30. String(char* str) :
  31. length_(0),
  32. capacity_(0),
  33. buffer_(&endZero)
  34. {
  35. *this = (const char*)str;
  36. }
  37. /// Construct from a char array and length.
  38. String(const char* str, unsigned length) :
  39. length_(0),
  40. capacity_(0),
  41. buffer_(&endZero)
  42. {
  43. Resize(length);
  44. CopyChars(buffer_, str, length);
  45. }
  46. /// Construct from an integer.
  47. explicit String(int value);
  48. /// Construct from a short integer.
  49. explicit String(short value);
  50. /// Construct from a long integer.
  51. explicit String(long value);
  52. /// Construct from a long long integer.
  53. explicit String(long long value);
  54. /// Construct from an unsigned integer.
  55. explicit String(unsigned value);
  56. /// Construct from an unsigned short integer.
  57. explicit String(unsigned short value);
  58. /// Construct from an unsigned long integer.
  59. explicit String(unsigned long value);
  60. /// Construct from an unsigned long long integer.
  61. explicit String(unsigned long long value);
  62. /// Construct from a float.
  63. explicit String(float value);
  64. /// Construct from a double.
  65. explicit String(double value);
  66. /// Construct from a bool.
  67. explicit String(bool value);
  68. /// Construct from a character.
  69. explicit String(char value);
  70. /// Construct from a character and fill length.
  71. explicit String(char value, unsigned length);
  72. /// Destruct.
  73. ~String()
  74. {
  75. if (capacity_)
  76. delete[] buffer_;
  77. }
  78. /// Add a string.
  79. String operator + (const String& rhs) const
  80. {
  81. String ret;
  82. ret.Resize(length_ + rhs.length_);
  83. CopyChars(ret.buffer_, buffer_, length_);
  84. CopyChars(ret.buffer_ + length_, rhs.buffer_, rhs.length_);
  85. return ret;
  86. }
  87. /// Add a C string.
  88. String operator + (const char* rhs) const
  89. {
  90. unsigned rhsLength = CStringLength(rhs);
  91. String ret;
  92. ret.Resize(length_ + rhsLength);
  93. CopyChars(ret.buffer_, buffer_, length_);
  94. CopyChars(ret.buffer_ + length_, rhs, rhsLength);
  95. return ret;
  96. }
  97. /// Add a character.
  98. String operator + (char rhs) const
  99. {
  100. String ret(*this);
  101. ret += rhs;
  102. return ret;
  103. }
  104. /// Test for equality with another string.
  105. bool operator == (const String& rhs) const { return strcmp(CString(), rhs.CString()) == 0; }
  106. /// Test if string is less than another string.
  107. bool operator < (const String& rhs) const { return strcmp(CString(), rhs.CString()) < 0; }
  108. bool operator == (const char* rhs) const { return strcmp(CString(), rhs) == 0; }
  109. /// Test if string is less than a C string.
  110. bool operator < (const char* rhs) const { return strcmp(CString(), rhs) < 0; }
  111. /// Return char at index.
  112. char& operator [] (unsigned index) { assert(index < length_); return buffer_[index]; }
  113. /// Return const char at index.
  114. const char& operator [] (unsigned index) const { assert(index < length_); return buffer_[index]; }
  115. /// Return char at index.
  116. char& At(unsigned index) { assert(index < length_); return buffer_[index]; }
  117. /// Return const char at index.
  118. const char& At(unsigned index) const { assert(index < length_); return buffer_[index]; }
  119. /// Replace all occurrences of a character.
  120. void Replace(char replaceThis, char replaceWith);
  121. /// Replace all occurrences of a string.
  122. void Replace(const String& replaceThis, const String& replaceWith);
  123. /// Replace a substring.
  124. void Replace(unsigned pos, unsigned length, const String& replaceWith);
  125. /// Return a string with all occurrences of a character replaced.
  126. String Replaced(char replaceThis, char replaceWith) const;
  127. /// Return a string with all occurrences of a string replaced.
  128. String Replaced(const String& replaceThis, const String& replaceWith) const;
  129. /// Append a string.
  130. String& Append(const String& str);
  131. /// Append a C string.
  132. String& Append(const char* str);
  133. /// Append a character.
  134. String& Append(char c);
  135. /// Append characters.
  136. String& Append(const char* str, unsigned length);
  137. /// Insert a string.
  138. void Insert(unsigned pos, const String& str);
  139. /// Insert a character.
  140. void Insert(unsigned pos, char c);
  141. /// Erase a substring.
  142. void Erase(unsigned pos, unsigned length = 1);
  143. /// Resize the string.
  144. void Resize(unsigned newLength);
  145. /// Set new capacity.
  146. void Reserve(unsigned newCapacity);
  147. /// Reallocate so that no extra memory is used.
  148. void Compact();
  149. /// Clear the string.
  150. void Clear();
  151. /// Swap with another string.
  152. void Swap(String& str);
  153. /// Return first char, or 0 if empty.
  154. char Front() const { return buffer_[0]; }
  155. /// Return last char, or 0 if empty.
  156. char Back() const { return length_ ? buffer_[length_ - 1] : buffer_[0]; }
  157. /// Return a substring from position to end.
  158. String Substring(unsigned pos) const;
  159. /// Return a substring with length from position.
  160. String Substring(unsigned pos, unsigned length) const;
  161. /// Return string with whitespace trimmed from the beginning and the end.
  162. String Trimmed() const;
  163. /// Return string in uppercase.
  164. String ToUpper() const;
  165. /// Return string in lowercase.
  166. String ToLower() const;
  167. /// Return substrings split by a separator char.
  168. Vector<String> Split(char separator) const;
  169. /// Join substrings with a 'glue' string.
  170. void Join(const Vector<String>& subStrings, String glue);
  171. /// Return index to the first occurrence of a string, or NPOS if not found.
  172. unsigned Find(const String& str, unsigned startPos = 0) const;
  173. /// Return index to the first occurrence of a character, or NPOS if not found.
  174. unsigned Find(char c, unsigned startPos = 0) const;
  175. /// Return index to the last occurrence of a string, or NPOS if not found.
  176. unsigned FindLast(const String& str, unsigned startPos = String::NPOS) const;
  177. /// Return index to the last occurrence of a character, or NPOS if not found.
  178. unsigned FindLast(char c, unsigned startPos = String::NPOS) const;
  179. /// Return whether starts with a string.
  180. bool StartsWith(const String& str) const;
  181. /// Return whether ends with a string.
  182. bool EndsWith(const String& str) const;
  183. /// Return the C string.
  184. const char* CString() const { return buffer_; }
  185. /// Return length.
  186. unsigned Length() const { return length_; }
  187. /// Return buffer capacity.
  188. unsigned Capacity() const { return capacity_; }
  189. /// Return whether the string is empty.
  190. bool Empty() const { return length_ == 0; }
  191. /// Return comparision result with a string.
  192. int Compare(const String& str, bool caseSensitive = true) const;
  193. /// Return comparision result with a C string.
  194. int Compare(const char* str, bool caseSensitive = true) const;
  195. /// Return whether contains a specific occurences of string.
  196. bool Contains(const String& str) const { return Find(str) != String::NPOS; }
  197. /// Return whether contains a specific character.
  198. bool Contains(char c) const { return Find(c) != String::NPOS; }
  199. /// Return hash value for HashSet & HashMap.
  200. unsigned ToHash() const
  201. {
  202. unsigned hash = 0;
  203. const char* ptr = buffer_;
  204. while (*ptr)
  205. {
  206. hash = *ptr + (hash << 6) + (hash << 16) - hash;
  207. ++ptr;
  208. }
  209. return hash;
  210. }
  211. /// Compare two C strings.
  212. static int Compare(const char* str1, const char* str2, bool caseSensitive);
  213. /// Position for "not found."
  214. static const unsigned NPOS;
  215. /// Initial dynamic allocation size.
  216. static const unsigned MIN_CAPACITY;
  217. /// Empty string.
  218. static const String EMPTY;
  219. };