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