Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CStr.h
Go to the documentation of this file.
1 /* Copyright (C) 2011 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 
75  /**
76  * Repeat: Named constructor, to avoid overload overload.
77  *
78  * @param const CStr & String reference to another CStr object to be repeated for initialization
79  * @param size_t Reps number of times to repeat the initialization
80  * @return CStr new CStr object
81  **/
82  static CStr Repeat(const CStr& String, size_t Reps);
83 
84  /**
85  * Construction from utf16strings.
86  *
87  * @param utf16string String utf16string to be used for initialization.
88  **/
89  explicit CStr(const utf16string& String) : std::tstring(String.begin(), String.end()) {}
90 
91  // Conversion to/from UTF-8, encoded in a CStr8.
92  // Invalid bytes/characters (e.g. broken UTF-8, and Unicode characters
93  // above U+FFFF) are silently replaced with U+FFFD.
94  #ifdef _UNICODE
95  CStr8 ToUTF8() const;
96  #else
97  CStrW FromUTF8() const;
98  #endif
99 
100  // Conversions:
101 
102  static CStr FromInt(int n);
103  static CStr FromUInt(unsigned int n);
104  static CStr FromInt64(i64 n);
105  static CStr FromDouble(double n);
106 
107  /**
108  * Return CStr as Integer.
109  * Conversion is from the beginning of CStr.
110  *
111  * @return int CStr represented as an integer.
112  **/
113  int ToInt() const;
114  /**
115  * Return CStr as Unsigned Integer.
116  * Conversion is from the beginning of CStr.
117  *
118  * @return unsigned int CStr represented as an unsigned integer.
119  **/
120  unsigned int ToUInt() const;
121  /**
122  * Return CStr as Long.
123  * Conversion is from the beginning of CStr.
124  *
125  * @return long CStr represented as a long.
126  **/
127  long ToLong() const;
128  /**
129  * Return CStr as Unsigned Long.
130  * Conversion is from the beginning of CStr.
131  *
132  * @return unsigned long CStr represented as an unsigned long.
133  **/
134  unsigned long ToULong() const;
135  /**
136  * Return CStr as Float.
137  * Conversion is from the beginning of CStr.
138  *
139  * @return float CStr represented as a float.
140  **/
141  float ToFloat() const;
142  /**
143  * Return CStr as Double.
144  * Conversion is from the beginning of CStr.
145  *
146  * @return double CStr represented as a double.
147  **/
148  double ToDouble() const;
149 
150  /**
151  * Search the CStr for another string.
152  * The search is case-sensitive.
153  *
154  * @param const CStr & Str reference to the search string
155  * @return long offset into the CStr of the first occurrence of the search string
156  * -1 if the search string is not found
157  **/
158  long Find(const CStr& Str) const;
159  /**
160  * Search the CStr for another string.
161  * The search is case-sensitive.
162  *
163  * @param const {t|w}char_t & chr reference to the search string
164  * @return long offset into the CStr of the first occurrence of the search string
165  * -1 if the search string is not found
166  **/
167  long Find(const tchar chr) const;
168  /**
169  * Search the CStr for another string with starting offset.
170  * The search is case-sensitive.
171  *
172  * @param const int & start character offset into CStr to begin search
173  * @param const {t|w}char_t & chr reference to the search string
174  * @return long offset into the CStr of the first occurrence of the search string
175  * -1 if the search string is not found
176  **/
177  long Find(const int start, const tchar chr) const;
178 
179  /**
180  * Search the CStr for another string.
181  * The search is case-insensitive.
182  *
183  * @param const CStr & Str reference to the search string
184  * @return long offset into the CStr of the first occurrence of the search string
185  * -1 if the search string is not found
186  **/
187  long FindInsensitive(const CStr& Str) const;
188  /**
189  * Search the CStr for another string.
190  * The search is case-insensitive.
191  *
192  * @param const {t|w}char_t & chr reference to the search string
193  * @return long offset into the CStr of the first occurrence of the search string
194  * -1 if the search string is not found
195  **/
196  long FindInsensitive(const tchar chr) const;
197  /**
198  * Search the CStr for another string with starting offset.
199  * The search is case-insensitive.
200  *
201  * @param const int & start character offset into CStr to begin search
202  * @param const {t|w}char_t & chr reference to the search string
203  * @return long offset into the CStr of the first occurrence of the search string
204  * -1 if the search string is not found
205  **/
206  long FindInsensitive(const int start, const tchar chr) const;
207 
208  /**
209  * Search the CStr for another string.
210  * The search is case-sensitive.
211  *
212  * @param const CStr & Str reference to the search string
213  * @return long offset into the CStr of the last occurrence of the search string
214  * -1 if the search string is not found
215  **/
216  long ReverseFind(const CStr& Str) const;
217 
218  /**
219  * Make a copy of the CStr in lower-case.
220  *
221  * @return CStr converted copy of CStr.
222  **/
223  CStr LowerCase() const;
224  /**
225  * Make a copy of the CStr in upper-case.
226  *
227  * @return CStr converted copy of CStr.
228  **/
229  CStr UpperCase() const;
230 
231  /**
232  * Retrieve first n characters of the CStr.
233  *
234  * @param size_t len the number of characters to retrieve.
235  * @return CStr retrieved substring.
236  **/
237  CStr Left(size_t len) const;
238 
239  /**
240  * Retrieve last n characters of the CStr.
241  *
242  * @param size_t len the number of characters to retrieve.
243  * @return CStr retrieved substring.
244  **/
245  CStr Right(size_t len) const;
246 
247  /**
248  * Retrieve substring of the CStr after last occurrence of a string.
249  * Return substring of the CStr after the last occurrence of the search string.
250  *
251  * @param const CStr & Str reference to search string
252  * @param size_t startPos character position to start searching from
253  * @return CStr substring remaining after match
254  * the CStr if no match is found
255  **/
256  CStr AfterLast(const CStr& Str, size_t startPos = npos) const;
257 
258  /**
259  * Retrieve substring of the CStr preceding last occurrence of a string.
260  * Return substring of the CStr preceding the last occurrence of the search string.
261  *
262  * @param const CStr & Str reference to search string
263  * @param size_t startPos character position to start searching from
264  * @return CStr substring preceding before match
265  * the CStr if no match is found
266  **/
267  CStr BeforeLast(const CStr& Str, size_t startPos = npos) const;
268 
269  /**
270  * Retrieve substring of the CStr after first occurrence of a string.
271  * Return substring of the CStr after the first occurrence of the search string.
272  *
273  * @param const CStr & Str reference to search string
274  * @param size_t startPos character position to start searching from
275  * @return CStr substring remaining after match
276  * the CStr if no match is found
277  **/
278  CStr AfterFirst(const CStr& Str, size_t startPos = 0) const;
279 
280  /**
281  * Retrieve substring of the CStr preceding first occurrence of a string.
282  * Return substring of the CStr preceding the first occurrence of the search string.
283  *
284  * @param const CStr & Str reference to search string
285  * @param size_t startPos character position to start searching from
286  * @return CStr substring preceding before match
287  * the CStr if no match is found
288  **/
289  CStr BeforeFirst(const CStr& Str, size_t startPos = 0) const;
290 
291  /**
292  * Remove all occurrences of a string from the CStr.
293  *
294  * @param const CStr & Str reference to search string to remove.
295  **/
296  void Remove(const CStr& Str);
297 
298  /**
299  * Replace all occurrences of one string by another string in the CStr.
300  *
301  * @param const CStr & StrToReplace reference to search string.
302  * @param const CStr & ReplaceWith reference to replace string.
303  **/
304  void Replace(const CStr& StrToReplace, const CStr& ReplaceWith);
305 
306  /**
307  * Convert strings like \\\n into <backslash><newline>
308  *
309  * @return CStr converted copy of CStr.
310  **/
311  CStr UnescapeBackslashes() const;
312 
313  /**
314  * Convert strings to printable ASCII characters with JSON-style escapes.
315  */
316  std::string EscapeToPrintableASCII() const;
317 
318  /**
319  * Return a trimmed copy of the CStr.
320  *
321  * @param PS_TRIM_MODE Mode value from trim mode enumeration.
322  * @return CStr copy of trimmed CStr.
323  **/
324  CStr Trim(PS_TRIM_MODE Mode) const;
325 
326  /**
327  * Return a space padded copy of the CStr.
328  *
329  * @param PS_TRIM_MODE Mode value from trim mode enumeration.
330  * @param size_t Length number of pad spaces to add
331  * @return CStr copy of padded CStr.
332  **/
333  CStr Pad(PS_TRIM_MODE Mode, size_t Length) const;
334 
335  // Conversion to utf16string
336  utf16string utf16() const { return utf16string(begin(), end()); }
337 
338  // Calculates a hash of the string's contents
339  size_t GetHashCode() const;
340 
341  // Serialization functions
342  // (These are not virtual or inherited from ISerializable, to avoid
343  // adding a vtable and making the strings larger than std::string)
344  size_t GetSerializedLength() const;
345  u8* Serialize(u8* buffer) const;
346  const u8* Deserialize(const u8* buffer, const u8* bufferend);
347 };
348 
349 static inline size_t hash_value(const CStr& s)
350 {
351  return s.GetHashCode();
352 }
353 
354 #endif
#define u8
Definition: types.h:39
Trim all white space from the beginning of the string.
Definition: CStr.h:34
size_t hash_value(const CStrIntern &v)
Trim all white space from the end of the string.
Definition: CStr.h:35
#define tchar
Definition: secure_crt.cpp:69
std::basic_string< utf16_t, utf16_traits > utf16string
Definition: utf16string.h:109
#define i64
Definition: types.h:37
static float Length(const SVec3 v)
Definition: mikktspace.cpp:112
PS_TRIM_MODE
File : CStr.h Project : engine Description : Contains CStr class which is a versatile class for makin...
Definition: CStr.h:31