Pyrogenesis  trunk
CStr.h
Go to the documentation of this file.
1 /* Copyright (C) 2014 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /**
19  * File : CStr.h
20  * Project : engine
21  * Description : Contains CStr class which is a versatile class for making string use easy.
22  * : The class implements a series of string manipulation/formatting functions.
23  **/
24 
25 #ifndef INCLUDED_CSTR
26 #define INCLUDED_CSTR
27 
28 /**
29  * Whitespace trim identifier for Trim and Pad functions
30  **/
32 {
33  PS_TRIM_LEFT, /// Trim all white space from the beginning of the string
34  PS_TRIM_RIGHT, /// Trim all white space from the end of the string
35  PS_TRIM_BOTH /// Trim all white space from the beginning and end of the string
36 };
37 
38 #ifndef IN_UNIDOUBLER
39  #define UNIDOUBLER_HEADER "CStr.h"
40  #include "UniDoubler.h"
41 #endif
42 
43 #endif
44 
45 // Include this section when in unidoubler mode, and when this unicode/ascii
46 // version has not already been included.
47 #if defined(IN_UNIDOUBLER) && ( (defined(_UNICODE) && !defined(CSTR_H_U)) || (!defined(_UNICODE) && !defined(CSTR_H_A)) )
48 
49 #ifdef _UNICODE
50 #define CSTR_H_U
51 #else
52 #define CSTR_H_A
53 #endif
54 
55 #include <string>
56 #include "ps/utf16string.h"
57 
58 class CStr8;
59 class CStrW;
60 
61 /**
62  * The base class of all strings
63  **/
64 class CStr: public std::tstring
65 {
66 public:
67 
68  // CONSTRUCTORS
69 
70  CStr() {}
71  CStr(const tchar* String) : std::tstring(String) {}
72  CStr(const tchar* String, size_t Length) : std::tstring(String, Length) {}
73  CStr(const std::tstring& String) : std::tstring(String) {}
74  template <class InputIterator>
75  CStr (InputIterator first, InputIterator last) : std::tstring(first, last) {}
76 
77  /**
78  * Repeat: Named constructor, to avoid overload overload.
79  *
80  * @param const CStr & String reference to another CStr object to be repeated for initialization
81  * @param size_t Reps number of times to repeat the initialization
82  * @return CStr new CStr object
83  **/
84  static CStr Repeat(const CStr& String, size_t Reps);
85 
86  /**
87  * Construction from utf16strings.
88  *
89  * @param utf16string String utf16string to be used for initialization.
90  **/
91  explicit CStr(const utf16string& String) : std::tstring(String.begin(), String.end()) {}
92 
93  // Conversion to/from UTF-8, encoded in a CStr8.
94  // Invalid bytes/characters (e.g. broken UTF-8, and Unicode characters
95  // above U+FFFF) are silently replaced with U+FFFD.
96  #ifdef _UNICODE
97  CStr8 ToUTF8() const;
98  #else
99  CStrW FromUTF8() const;
100  #endif
101 
102  // Conversions:
103 
104  static CStr FromInt(int n);
105  static CStr FromUInt(unsigned int n);
106  static CStr FromInt64(i64 n);
107  static CStr FromDouble(double n);
108 
109  /**
110  * Return CStr as Integer.
111  * Conversion is from the beginning of CStr.
112  *
113  * @return int CStr represented as an integer.
114  **/
115  int ToInt() const;
116  /**
117  * Return CStr as Unsigned Integer.
118  * Conversion is from the beginning of CStr.
119  *
120  * @return unsigned int CStr represented as an unsigned integer.
121  **/
122  unsigned int ToUInt() const;
123  /**
124  * Return CStr as Long.
125  * Conversion is from the beginning of CStr.
126  *
127  * @return long CStr represented as a long.
128  **/
129  long ToLong() const;
130  /**
131  * Return CStr as Unsigned Long.
132  * Conversion is from the beginning of CStr.
133  *
134  * @return unsigned long CStr represented as an unsigned long.
135  **/
136  unsigned long ToULong() const;
137  /**
138  * Return CStr as Float.
139  * Conversion is from the beginning of CStr.
140  *
141  * @return float CStr represented as a float.
142  **/
143  float ToFloat() const;
144  /**
145  * Return CStr as Double.
146  * Conversion is from the beginning of CStr.
147  *
148  * @return double CStr represented as a double.
149  **/
150  double ToDouble() const;
151 
152  /**
153  * Search the CStr for another string.
154  * The search is case-sensitive.
155  *
156  * @param const CStr & Str reference to the search string
157  * @return long offset into the CStr of the first occurrence of the search string
158  * -1 if the search string is not found
159  **/
160  long Find(const CStr& Str) const;
161  /**
162  * Search the CStr for another string.
163  * The search is case-sensitive.
164  *
165  * @param const {t|w}char_t & chr reference to the search string
166  * @return long offset into the CStr of the first occurrence of the search string
167  * -1 if the search string is not found
168  **/
169  long Find(const tchar chr) const;
170  /**
171  * Search the CStr for another string with starting offset.
172  * The search is case-sensitive.
173  *
174  * @param const int & start character offset into CStr to begin search
175  * @param const {t|w}char_t & chr reference to the search string
176  * @return long offset into the CStr of the first occurrence of the search string
177  * -1 if the search string is not found
178  **/
179  long Find(const int start, const tchar chr) const;
180 
181  /**
182  * Search the CStr for another string.
183  * The search is case-insensitive.
184  *
185  * @param const CStr & Str reference to the search string
186  * @return long offset into the CStr of the first occurrence of the search string
187  * -1 if the search string is not found
188  **/
189  long FindInsensitive(const CStr& Str) const;
190  /**
191  * Search the CStr for another string.
192  * The search is case-insensitive.
193  *
194  * @param const {t|w}char_t & chr reference to the search string
195  * @return long offset into the CStr of the first occurrence of the search string
196  * -1 if the search string is not found
197  **/
198  long FindInsensitive(const tchar chr) const;
199  /**
200  * Search the CStr for another string with starting offset.
201  * The search is case-insensitive.
202  *
203  * @param const int & start character offset into CStr to begin search
204  * @param const {t|w}char_t & chr reference to the search string
205  * @return long offset into the CStr of the first occurrence of the search string
206  * -1 if the search string is not found
207  **/
208  long FindInsensitive(const int start, const tchar chr) const;
209 
210  /**
211  * Search the CStr for another string.
212  * The search is case-sensitive.
213  *
214  * @param const CStr & Str reference to the search string
215  * @return long offset into the CStr of the last occurrence of the search string
216  * -1 if the search string is not found
217  **/
218  long ReverseFind(const CStr& Str) const;
219 
220  /**
221  * Make a copy of the CStr in lower-case.
222  *
223  * @return CStr converted copy of CStr.
224  **/
225  CStr LowerCase() const;
226  /**
227  * Make a copy of the CStr in upper-case.
228  *
229  * @return CStr converted copy of CStr.
230  **/
231  CStr UpperCase() const;
232 
233  /**
234  * Retrieve first n characters of the CStr.
235  *
236  * @param size_t len the number of characters to retrieve.
237  * @return CStr retrieved substring.
238  **/
239  CStr Left(size_t len) const;
240 
241  /**
242  * Retrieve last n characters of the CStr.
243  *
244  * @param size_t len the number of characters to retrieve.
245  * @return CStr retrieved substring.
246  **/
247  CStr Right(size_t len) const;
248 
249  /**
250  * Retrieve substring of the CStr after last occurrence of a string.
251  * Return substring of the CStr after the last occurrence of the search string.
252  *
253  * @param const CStr & Str reference to search string
254  * @param size_t startPos character position to start searching from
255  * @return CStr substring remaining after match
256  * the CStr if no match is found
257  **/
258  CStr AfterLast(const CStr& Str, size_t startPos = npos) const;
259 
260  /**
261  * Retrieve substring of the CStr preceding last occurrence of a string.
262  * Return substring of the CStr preceding the last occurrence of the search string.
263  *
264  * @param const CStr & Str reference to search string
265  * @param size_t startPos character position to start searching from
266  * @return CStr substring preceding before match
267  * the CStr if no match is found
268  **/
269  CStr BeforeLast(const CStr& Str, size_t startPos = npos) const;
270 
271  /**
272  * Retrieve substring of the CStr after first occurrence of a string.
273  * Return substring of the CStr after the first occurrence of the search string.
274  *
275  * @param const CStr & Str reference to search string
276  * @param size_t startPos character position to start searching from
277  * @return CStr substring remaining after match
278  * the CStr if no match is found
279  **/
280  CStr AfterFirst(const CStr& Str, size_t startPos = 0) const;
281 
282  /**
283  * Retrieve substring of the CStr preceding first occurrence of a string.
284  * Return substring of the CStr preceding the first occurrence of the search string.
285  *
286  * @param const CStr & Str reference to search string
287  * @param size_t startPos character position to start searching from
288  * @return CStr substring preceding before match
289  * the CStr if no match is found
290  **/
291  CStr BeforeFirst(const CStr& Str, size_t startPos = 0) const;
292 
293  /**
294  * Remove all occurrences of a string from the CStr.
295  *
296  * @param const CStr & Str reference to search string to remove.
297  **/
298  void Remove(const CStr& Str);
299 
300  /**
301  * Replace all occurrences of one string by another string in the CStr.
302  *
303  * @param const CStr & StrToReplace reference to search string.
304  * @param const CStr & ReplaceWith reference to replace string.
305  **/
306  void Replace(const CStr& StrToReplace, const CStr& ReplaceWith);
307 
308  /**
309  * Convert strings to printable ASCII characters with JSON-style escapes.
310  */
311  std::string EscapeToPrintableASCII() const;
312 
313  /**
314  * Return a trimmed copy of the CStr.
315  *
316  * @param PS_TRIM_MODE Mode value from trim mode enumeration.
317  * @return CStr copy of trimmed CStr.
318  **/
319  CStr Trim(PS_TRIM_MODE Mode) const;
320 
321  /**
322  * Return a space padded copy of the CStr.
323  *
324  * @param PS_TRIM_MODE Mode value from trim mode enumeration.
325  * @param size_t Length number of pad spaces to add
326  * @return CStr copy of padded CStr.
327  **/
328  CStr Pad(PS_TRIM_MODE Mode, size_t Length) const;
329 
330  // Conversion to utf16string
331  utf16string utf16() const { return utf16string(begin(), end()); }
332 
333  // Calculates a hash of the string's contents
334  size_t GetHashCode() const;
335 
336  // Serialization functions
337  // (These are not virtual or inherited from ISerializable, to avoid
338  // adding a vtable and making the strings larger than std::string)
339  size_t GetSerializedLength() const;
340  u8* Serialize(u8* buffer) const;
341  const u8* Deserialize(const u8* buffer, const u8* bufferend);
342 };
343 
344 static inline size_t hash_value(const CStr& s)
345 {
346  return s.GetHashCode();
347 }
348 
349 #endif
int64_t i64
Definition: types.h:35
Trim all white space from the beginning of the string.
Definition: CStr.h:34
#define tstring
Definition: secure_crt.cpp:75
Definition: unique_range.h:196
Trim all white space from the end of the string.
Definition: CStr.h:35
uint8_t u8
Definition: types.h:37
#define tchar
Definition: secure_crt.cpp:74
std::basic_string< utf16_t, utf16_traits > utf16string
Definition: utf16string.h:109
Definition: CStr.h:33
static float Length(const SVec3 v)
Definition: mikktspace.cpp:108
size_t hash_value(const CVector4D &v)
Definition: ShaderDefines.cpp:28
PS_TRIM_MODE
File : CStr.h Project : engine Description : Contains CStr class which is a versatile class for makin...
Definition: CStr.h:31