UString.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /******************************************************************************
  19. *
  20. * FILE
  21. * $Archive: $
  22. *
  23. * DESCRIPTION
  24. * String management class (Unicode)
  25. *
  26. * PROGRAMMER
  27. * Denzil E. Long, Jr.
  28. * $Author: $
  29. *
  30. * VERSION INFO
  31. * $Modtime: $
  32. * $Revision: $
  33. *
  34. ******************************************************************************/
  35. #ifndef USTRING_H
  36. #define USTRING_H
  37. #include "UTypes.h"
  38. #include "RefCounted.h"
  39. class UString
  40. : public RefCounted
  41. {
  42. public:
  43. // Constructors
  44. UString();
  45. UString(UInt capacity);
  46. UString(const Char* s);
  47. UString(const WChar* ws);
  48. UString(const UString& s);
  49. virtual ~UString();
  50. //! Get the length of the string
  51. UInt Length(void) const;
  52. //! Copy string
  53. void Copy(const Char* s);
  54. void Copy(const WChar* ws);
  55. void Copy(const UString& s);
  56. //! Concatenate string
  57. void Concat(const Char* s);
  58. void Concat(const WChar* ws);
  59. void Concat(const UString& s);
  60. //! Compare strings
  61. Int Compare(const Char* s) const;
  62. Int Compare(const WChar* s) const;
  63. Int Compare(const UString& s) const;
  64. //! Compare strings not case-sensitive
  65. Int CompareNoCase(const Char* s) const;
  66. Int CompareNoCase(const WChar* ws) const;
  67. Int CompareNoCase(const UString& s) const;
  68. //! Find the first occurance of character
  69. Int Find(Char c) const;
  70. Int Find(WChar wc) const;
  71. //! Find the last occurance of a character
  72. Int FindLast(Char c) const;
  73. Int FindLast(WChar c) const;
  74. //! Find a substring
  75. UString SubString(const Char* s);
  76. UString SubString(const WChar* ws);
  77. UString SubString(const UString& s);
  78. //! Extract left part of the string
  79. UString Left(UInt count);
  80. //! Extract middle part of the string.
  81. UString Middle(UInt first, UInt count);
  82. //! Extract right part of the string.
  83. UString Right(UInt count);
  84. //! Convert string to uppercase
  85. void ToUpper(void);
  86. //! Convert string to lowercase
  87. void ToLower(void);
  88. //! Reverse characters of string
  89. void Reverse(void);
  90. //! Remove leading and trailing characters from string.
  91. // Returns true if any characters removed
  92. bool Trim(const Char* trimChars);
  93. bool Trim(const WChar* trimChars);
  94. bool Trim(const UString& trimChars);
  95. //! Remove characters from left side of string
  96. // Returns true if any characters removed
  97. bool TrimLeft(const Char* trimChars);
  98. bool TrimLeft(const WChar* trimChars);
  99. bool TrimLeft(const UString& trimChars);
  100. //! Remove characters from right side of string
  101. // Returns true if any characters removed
  102. bool TrimRight(const Char* trimChars);
  103. bool TrimRight(const WChar* trimChars);
  104. bool TrimRight(const UString& trimChars);
  105. // Convert string to ANSI
  106. void ConvertToANSI(Char* buffer, UInt bufferLength) const;
  107. //! Get the size (in bytes) of the string.
  108. UInt Size(void) const;
  109. //! Get the maximum number of characters this string can hold.
  110. UInt Capacity(void) const;
  111. //! Resize the string
  112. bool Resize(UInt size);
  113. const WChar* Get(void) const
  114. {return (mData != NULL) ? mData : L"";}
  115. //! Assignment operator
  116. UString operator=(const Char* s)
  117. {Copy(s); return *this;};
  118. UString operator=(const WChar* ws)
  119. {Copy(ws); return *this;};
  120. UString operator=(const UString& s)
  121. {Copy(s); return *this;};
  122. //! Addition operator (concatenate)
  123. UString operator+(const Char* s)
  124. {UString ns(*this); ns += s; return ns;}
  125. UString operator+(const WChar* ws)
  126. {UString ns(*this); ns += ws; return ns;}
  127. UString operator+(const UString& s)
  128. {UString ns(*this); ns += s; return ns;}
  129. UString operator+=(const Char* s)
  130. {Concat(s); return *this;}
  131. UString operator+=(const WChar* ws)
  132. {Concat(ws); return *this;}
  133. UString operator+=(const UString& s)
  134. {Concat(s); return *this;}
  135. //! Equal operator (case sensitive compare)
  136. bool operator==(const Char* s)
  137. {return (Compare(s) == 0);}
  138. bool operator==(const WChar* ws)
  139. {return (Compare(ws) == 0);}
  140. bool operator==(const UString& s)
  141. {return (Compare(s) == 0);}
  142. bool operator!=(const Char* s)
  143. {return (Compare(s) != 0);}
  144. bool operator!=(const WChar* ws)
  145. {return (Compare(ws) != 0);}
  146. bool operator!=(const UString& s)
  147. {return (Compare(s) != 0);}
  148. //! Less than operator (case sensitive compare)
  149. bool operator<(const Char* s)
  150. {return (Compare(s) == -1);}
  151. bool operator<(const WChar* ws)
  152. {return (Compare(ws) == -1);}
  153. bool operator<(const UString& s)
  154. {return (Compare(s) == -1);}
  155. bool operator<=(const Char* s)
  156. {return (Compare(s) <= 0);}
  157. bool operator<=(const WChar* ws)
  158. {return (Compare(ws) <= 0);}
  159. bool operator<=(const UString& s)
  160. {return (Compare(s) <= 0);}
  161. //! Greater than operator (case sensitive compare)
  162. bool operator>(const Char* s)
  163. {return (Compare(s) == 1);}
  164. bool operator>(const WChar* ws)
  165. {return (Compare(ws) == 1);}
  166. bool operator>(const UString& s)
  167. {return (Compare(s) == 1);}
  168. bool operator>=(const Char* s)
  169. {return (Compare(s) >= 0);}
  170. bool operator>=(const WChar* ws)
  171. {return (Compare(ws) >= 0);}
  172. bool operator>=(const UString& s)
  173. {return (Compare(s) >= 0);}
  174. // Conversion operator
  175. operator const WChar*() const
  176. {return Get();}
  177. private:
  178. bool AllocString(UInt size);
  179. WChar* mData;
  180. UInt mCapacity;
  181. };
  182. #endif // USTRING_H