soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
sstringw.h
1#ifndef __TSTRINGW_H__
2#define __TSTRINGW_H__
3
4#include <utilities-def.h>
5#include <interface/sstring-i.h>
6#include <string/sstringdata.h>
7
8SNSBEGIN
9
10/// @brief Character traits for ASCII strings.
11struct UTILITIES_API wchar_traits
12{
13 /// @brief Computes the length of a null-terminated string.
14 /// @param psz The string to measure.
15 /// @return The length of the string.
16 static size_t StrLen(const wchar_t* psz);
17
18 /// @brief Compares two strings lexicographically.
19 /// @param psz1 The first string.
20 /// @param psz2 The second string.
21 /// @return An integer less than, equal to, or greater than zero if psz1 is found, respectively, to be less than, to match, or be greater than psz2.
22 static int Compare(const wchar_t* psz1, const wchar_t* psz2);
23
24 /// @brief Compares two strings lexicographically, ignoring case.
25 /// @param psz1 The first string.
26 /// @param psz2 The second string.
27 /// @return An integer less than, equal to, or greater than zero if psz1 is found, respectively, to be less than, to match, or be greater than psz2.
28 static int CompareNoCase(const wchar_t* psz1, const wchar_t* psz2);
29
30 /// @brief Finds the first occurrence of a character in a string.
31 /// @param psz The string to search.
32 /// @param ch The character to find.
33 /// @return A pointer to the first occurrence of ch in psz, or NULL if ch is not found.
34 static const wchar_t* StrChr(const wchar_t* psz, wchar_t ch);
35
36 /// @brief Finds the last occurrence of a character in a string.
37 /// @param psz The string to search.
38 /// @param ch The character to find.
39 /// @return A pointer to the last occurrence of ch in psz, or NULL if ch is not found.
40 static const wchar_t* StrRChr(const wchar_t* psz, wchar_t ch);
41
42 /// @brief Finds the first occurrence of a substring in a string.
43 /// @param psz The string to search.
44 /// @param psz2 The substring to find.
45 /// @return A pointer to the first occurrence of psz2 in psz, or NULL if psz2 is not found.
46 static const wchar_t* StrStr(const wchar_t* psz, const wchar_t* psz2);
47
48 /// @brief Converts a string to uppercase.
49 /// @param psz The string to convert.
50 /// @return A pointer to the converted string.
51 static wchar_t* StrUpper(wchar_t* psz);
52
53 /// @brief Converts a string to lowercase.
54 /// @param psz The string to convert.
55 /// @return A pointer to the converted string.
56 static wchar_t* StrLower(wchar_t* psz);
57
58 /// @brief Checks if a character is a whitespace character.
59 /// @param ch The character to check.
60 /// @return Non-zero if the character is a whitespace character, zero otherwise.
61 static int IsSpace(wchar_t ch);
62
63 /// @brief Converts a character to lowercase.
64 /// @param ch The character to convert.
65 /// @return The lowercase version of the character.
66 static wchar_t CharLower(wchar_t ch);
67
68 /// @brief Converts a character to uppercase.
69 /// @param ch The character to convert.
70 /// @return The uppercase version of the character.
71 static wchar_t CharUpper(wchar_t ch);
72
73 /// @brief Moves to the next character in a string.
74 /// @param psz The string to move through.
75 /// @return A pointer to the next character in the string.
76 static wchar_t* CharNext(wchar_t* psz);
77
78 /// @brief Formats a string using a format string and variable arguments.
79 /// @param ppszDst The destination string buffer.
80 /// @param pszFormat The format string.
81 /// @param args The variable arguments list.
82 /// @return The number of characters written to the buffer.
83 static int Format(wchar_t** ppszDst, const wchar_t* pszFormat, va_list & args);
84
85 /// @brief Loads a string resource from a module.
86 /// @param hInst The handle to the module.
87 /// @param uID The resource identifier.
88 /// @param lpBuffer The buffer to store the loaded string.
89 /// @param nBufferMax The maximum size of the buffer.
90 /// @return The number of characters copied to the buffer.
91 static int LoadString(HINSTANCE hInst, UINT uID, wchar_t* lpBuffer, int nBufferMax);
92};
93
94/// @brief A class representing an ASCII string.
95class UTILITIES_API SStringW : public IStringW
96{
97public:
98 typedef const wchar_t * pctstr;
99 typedef wchar_t _tchar;
100 typedef wchar_traits _tchar_traits;
101
102 /// @brief Default constructor.
103 SStringW();
104
105 /// @brief Copy constructor.
106 /// @param stringSrc The string to copy.
107 SStringW(const SStringW& stringSrc);
108
109 /// @brief Constructor from an IStringW object.
110 /// @param stringSrc The string to copy.
111 SStringW(const IStringW* stringSrc);
112
113 /// @brief Constructor from a single character.
114 /// @param ch The character to initialize the string with.
115 /// @param nLength The number of times to repeat the character.
116 SStringW(wchar_t ch, int nLength = 1);
117
118 /// @brief Constructor from a substring of a character array.
119 /// @param psz The character array.
120 /// @param nLength The length of the substring.
121 SStringW(const wchar_t* psz, int nLength);
122
123 /// @brief Constructor from a character array.
124 /// @param psz The character array.
125 SStringW(const wchar_t* psz);
126
127 /// @brief Destructor.
128 ~SStringW();
129
130public:
131 /// @brief Retrieves the length of the string.
132 /// @return The length of the string.
133 STDMETHOD_(int, GetLength)(THIS) SCONST;
134
135 /// @brief Checks if the string is empty.
136 /// @return TRUE if the string is empty, FALSE otherwise.
137 STDMETHOD_(BOOL, IsEmpty)(THIS) SCONST;
138
139 /// @brief Empties the string.
140 STDMETHOD_(void, Empty)(THIS);
141
142 /// @brief Retrieves the character at a specified index.
143 /// @param nIndex The index of the character.
144 /// @return The character at the specified index.
145 STDMETHOD_(wchar_t, GetAt)(THIS_ int nIndex) SCONST;
146
147 /// @brief Sets the character at a specified index.
148 /// @param nIndex The index of the character.
149 /// @param ch The new character value.
150 STDMETHOD_(void, SetAt)(THIS_ int nIndex, wchar_t ch);
151
152 /// @brief Retrieves a C-style string representation of the string.
153 /// @return A pointer to the C-style string.
154 STDMETHOD_(const wchar_t *, c_str)(THIS) SCONST;
155
156 /// @brief Compares the string with another string.
157 /// @param psz The string to compare with.
158 /// @return An integer less than, equal to, or greater than zero if the string is found, respectively, to be less than, to match, or be greater than psz.
159 STDMETHOD_(int, Compare)(THIS_ const wchar_t* psz) SCONST;
160
161 /// @brief Compares the string with another string, ignoring case.
162 /// @param psz The string to compare with.
163 /// @return An integer less than, equal to, or greater than zero if the string is found, respectively, to be less than, to match, or be greater than psz.
164 STDMETHOD_(int, CompareNoCase)(THIS_ const wchar_t* psz) SCONST;
165
166 /// @brief Trims leading and trailing whitespace characters from the string.
167 STDMETHOD_(void, TrimBlank)(THIS);
168
169 /// @brief Inserts a character at a specified index.
170 /// @param nIndex The index at which to insert the character.
171 /// @param ch The character to insert.
172 /// @return The new length of the string.
173 STDMETHOD_(int, InsertChar)(THIS_ int nIndex, wchar_t ch);
174
175 /// @brief Inserts a substring at a specified index.
176 /// @param nIndex The index at which to insert the substring.
177 /// @param psz The substring to insert.
178 /// @return The new length of the string.
179 STDMETHOD_(int, Insert)(THIS_ int nIndex, const wchar_t* psz);
180
181 /// @brief Deletes a substring from the string.
182 /// @param nIndex The index at which to start deletion.
183 /// @param nCount The number of characters to delete.
184 /// @return The new length of the string.
185 STDMETHOD_(int, Delete)(THIS_ int nIndex, int nCount = 1);
186
187 /// @brief Replaces all occurrences of a character with another character.
188 /// @param chOld The character to replace.
189 /// @param chNew The character to replace with.
190 /// @return The number of characters replaced.
191 STDMETHOD_(int, ReplaceChar)(THIS_ wchar_t chOld, wchar_t chNew);
192
193 /// @brief Replaces all occurrences of a substring with another substring.
194 /// @param pszOld The substring to replace.
195 /// @param pszNew The substring to replace with.
196 /// @return The number of substrings replaced.
197 STDMETHOD_(int, Replace)(THIS_ const wchar_t* pszOld, const wchar_t* pszNew);
198
199 /// @brief Removes all occurrences of a character from the string.
200 /// @param chRemove The character to remove.
201 /// @return The number of characters removed.
202 STDMETHOD_(int, Remove)(THIS_ wchar_t chRemove);
203
204 /// @brief Finds the first occurrence of a character in the string.
205 /// @param ch The character to find.
206 /// @param nStart The index at which to start the search.
207 /// @return The index of the first occurrence of the character, or -1 if not found.
208 STDMETHOD_(int, FindChar)(THIS_ wchar_t ch, int nStart DEF_VAL(0)) SCONST;
209
210 /// @brief Finds the last occurrence of a character in the string.
211 /// @param ch The character to find.
212 /// @return The index of the last occurrence of the character, or -1 if not found.
213 STDMETHOD_(int, ReverseFind)(THIS_ wchar_t ch) SCONST;
214
215 /// @brief Finds the first occurrence of a substring in the string.
216 /// @param pszSub The substring to find.
217 /// @param nStart The index at which to start the search.
218 /// @return The index of the first occurrence of the substring, or -1 if not found.
219 STDMETHOD_(int, Find)(THIS_ const wchar_t* pszSub, int nStart DEF_VAL(0)) SCONST;
220
221 /// @brief Retrieves a modifiable buffer for the string.
222 /// @param nMinBufLength The minimum buffer length.
223 /// @return A pointer to the buffer.
224 STDMETHOD_(wchar_t*, GetBuffer)(THIS_ int nMinBufLength DEF_VAL(-1));
225
226 /// @brief Releases the buffer and sets the new length of the string.
227 /// @param nNewLength The new length of the string.
228 STDMETHOD_(void, ReleaseBuffer)(THIS_ int nNewLength DEF_VAL(-1));
229
230 /// @brief Retrieves a modifiable buffer for the string and sets the new length.
231 /// @param nNewLength The new length of the string.
232 /// @return A pointer to the buffer.
233 STDMETHOD_(wchar_t*, GetBufferSetLength)(THIS_ int nNewLength);
234
235 /// @brief Sets the length of the string.
236 /// @param nLength The new length of the string.
237 STDMETHOD_(void, SetLength)(THIS_ int nLength);
238
239 /// @brief Copies the contents of another string into this string.
240 /// @param src The string to copy from.
241 STDMETHOD_(void, Copy)(THIS_ const IStringW* src);
242
243 /// @brief Assigns a character array to the string.
244 /// @param src The character array to assign.
245 STDMETHOD_(void, Assign)(THIS_ LPCWSTR src);
246
247 /// @brief Assigns a substring of a character array to the string.
248 /// @param src The character array to assign.
249 /// @param nLen The length of the substring.
250 STDMETHOD_(void, Assign2)(THIS_ LPCWSTR src, int nLen);
251
252 /// @brief Retrieves private data associated with the string.
253 /// @return A pointer to the private data.
254 STDMETHOD_(LPVOID, GetPrivData)(THIS) SCONST;
255
256 /// @brief Converts the string to uppercase.
257 STDMETHOD_(void, ToUpper)(THIS) OVERRIDE;
258
259 /// @brief Converts the string to lowercase.
260 STDMETHOD_(void, ToLower)(THIS) OVERRIDE;
261
262 /// @brief Trims trailing whitespace characters from the string.
263 /// @param chTarget The character to trim.
264 STDMETHOD_(void, TrimRight)(THIS_ wchar_t chTarget DEF_VAL(VK_SPACE)) OVERRIDE;
265
266 /// @brief Trims leading whitespace characters from the string.
267 /// @param chTarget The character to trim.
268 STDMETHOD_(void, TrimLeft)(THIS_ wchar_t chTarget DEF_VAL(VK_SPACE)) OVERRIDE;
269
270 /// @brief Trims leading and trailing whitespace characters from the string.
271 /// @param chTarget The character to trim.
272 STDMETHOD_(void, Trim)(THIS_ wchar_t chTarget DEF_VAL(VK_SPACE)) OVERRIDE;
273
274 /// @brief Appends a character to the string.
275 /// @param ch The character to append.
276 STDMETHOD_(void, AppendChar)(THIS_ wchar_t ch) OVERRIDE;
277
278 /// @brief Appends a substring to the string.
279 /// @param pszStr The substring to append.
280 /// @param nLen The length of the substring.
281 STDMETHOD_(void, AppendStr)(THIS_ const wchar_t* pszStr, int nLen DEF_VAL(-1)) OVERRIDE;
282
283 /// @brief Releases the string and its resources.
284 STDMETHOD_(void, Release)(THIS) OVERRIDE;
285
286 /// @brief Converts the string to an unsigned integer.
287 /// @return The unsigned integer value.
288 STDMETHOD_(UINT, ToUint)(CTHIS) SCONST OVERRIDE;
289
290 /// @brief Converts the string to a long integer.
291 /// @return The long integer value.
292 STDMETHOD_(long, ToLong)(CTHIS) SCONST OVERRIDE;
293
294 /// @brief Converts the string to an integer.
295 /// @return The integer value.
296 STDMETHOD_(int, ToInt)(CTHIS) SCONST OVERRIDE;
297
298 /// @brief Converts the string to a float.
299 /// @return The float value.
300 STDMETHOD_(float, ToFloat)(CTHIS) SCONST OVERRIDE;
301
302 /// @brief Converts the string to a double.
303 /// @return The double value.
304 STDMETHOD_(double, ToDouble)(CTHIS) SCONST OVERRIDE;
305
306 /// @brief Converts the string to a boolean.
307 /// @return The boolean value.
308 STDMETHOD_(BOOL, ToBool)(CTHIS) SCONST OVERRIDE;
309
310 /// @brief Extracts a substring from the string.
311 /// @param nFirst The starting index of the substring.
312 /// @return The extracted substring.
313 SStringW Mid(int nFirst) const;
314
315 /// @brief Extracts a substring from the string.
316 /// @param nFirst The starting index of the substring.
317 /// @param nCount The length of the substring.
318 /// @return The extracted substring.
319 SStringW Mid(int nFirst, int nCount) const;
320
321 /// @brief Extracts the rightmost part of the string.
322 /// @param nCount The length of the substring.
323 /// @return The extracted substring.
324 SStringW Right(int nCount) const;
325
326 /// @brief Extracts the leftmost part of the string.
327 /// @param nCount The length of the substring.
328 /// @return The extracted substring.
329 SStringW Left(int nCount) const;
330
331 /// @brief Converts the string to uppercase.
332 /// @return A reference to the modified string.
334
335 /// @brief Converts the string to lowercase.
336 /// @return A reference to the modified string.
338
339 /// @brief Checks if the string starts with a specified prefix.
340 /// @param prefix The prefix to check.
341 /// @param IgnoreCase Whether the comparison should be case-insensitive.
342 /// @return TRUE if the string starts with the prefix, FALSE otherwise.
343 bool StartsWith(const SStringW& prefix, bool IgnoreCase = false) const;
344
345 /// @brief Checks if the string ends with a specified suffix.
346 /// @param suffix The suffix to check.
347 /// @param IgnoreCase Whether the comparison should be case-insensitive.
348 /// @return TRUE if the string ends with the suffix, FALSE otherwise.
349 bool EndsWith(const SStringW& suffix, bool IgnoreCase = false) const;
350
351 /// @brief Loads a string resource from a module.
352 /// @param nID The resource identifier.
353 /// @param hInst The handle to the module.
354 /// @return TRUE if the string was successfully loaded, FALSE otherwise.
355 BOOL LoadString(UINT nID, HINSTANCE hInst);
356
357 /// @brief Formats a string using a format string and variable arguments.
358 /// @param hInst The handle to the module.
359 /// @param nFormatID The resource identifier of the format string.
360 /// @param ... The variable arguments list.
361 /// @return TRUE if the string was successfully formatted, FALSE otherwise.
362 BOOL __cdecl Format(HINSTANCE hInst, UINT nFormatID, ...);
363
364 /// @brief Appends formatted data to the string using a format string and variable arguments.
365 /// @param hInst The handle to the module.
366 /// @param nFormatID The resource identifier of the format string.
367 /// @param ... The variable arguments list.
368 void __cdecl AppendFormat(HINSTANCE hInst, UINT nFormatID, ...);
369
370 /// @brief Formats a string using a format string and variable arguments.
371 /// @param pszFormat The format string.
372 /// @param ... The variable arguments list.
373 /// @return The formatted string.
374 SStringW __cdecl Format(const wchar_t* pszFormat, ...);
375
376 /// @brief Appends formatted data to the string using a format string and variable arguments.
377 /// @param pszFormat The format string.
378 /// @param ... The variable arguments list.
379 /// @return The modified string.
380 SStringW __cdecl AppendFormat(const wchar_t* pszFormat, ...);
381
382 /// @brief Converts the string to a C-style string.
383 /// @return A pointer to the C-style string.
384 operator const wchar_t*() const;
385
386 /// @brief Retrieves the character at a specified index.
387 /// @param nIndex The index of the character.
388 /// @return The character at the specified index.
389 wchar_t operator[](int nIndex) const;
390
391 /// @brief Overloaded assignment operator from another SStringW object.
392 /// @param stringSrc The string to assign.
393 /// @return A reference to the modified string.
394 SStringW& operator=(const SStringW& stringSrc);
395
396 /// @brief Overloaded assignment operator from a character array.
397 /// @param psz The character array to assign.
398 /// @return A reference to the modified string.
399 SStringW& operator=(const wchar_t* psz);
400
401 /// @brief Overloaded assignment operator from a single character.
402 /// @param ch The character to assign.
403 /// @return A reference to the modified string.
404 const SStringW& operator=(wchar_t ch);
405
406 /// @brief Overloaded concatenation operator with a character array.
407 /// @param psz The character array to concatenate.
408 /// @return A reference to the modified string.
409 const SStringW& operator+=(const wchar_t* psz);
410
411 /// @brief Overloaded concatenation operator with a single character.
412 /// @param ch The character to concatenate.
413 /// @return A reference to the modified string.
414 const SStringW& operator+=(wchar_t ch);
415
416 /// @brief Overloaded concatenation operator with another SStringW object.
417 /// @param src The string to concatenate.
418 /// @return A reference to the modified string.
419 const SStringW& operator+=(const SStringW& src);
420
421 /// @brief Appends another SStringW object to the string.
422 /// @param src The string to append.
423 /// @return A reference to the modified string.
424 SStringW& Append(const SStringW& src);
425
426 /// @brief Checks if a character is a blank character.
427 /// @param c The character to check.
428 /// @return TRUE if the character is a blank character, FALSE otherwise.
429 static bool IsBlankChar(const wchar_t &c);
430
431 /// @brief Overloaded equality operator between two SStringW objects.
432 /// @param s1 The first string.
433 /// @param s2 The second string.
434 /// @return TRUE if the strings are equal, FALSE otherwise.
435 friend inline bool __stdcall operator==(const SStringW& s1, const SStringW& s2)
436 {
437 return s1.Compare(s2) == 0;
438 }
439
440 /// @brief Overloaded equality operator between an SStringW object and a character array.
441 /// @param s1 The string.
442 /// @param s2 The character array.
443 /// @return TRUE if the string and character array are equal, FALSE otherwise.
444 friend inline bool __stdcall operator==(const SStringW& s1, const wchar_t* s2)
445 {
446 return s1.Compare(s2) == 0;
447 }
448
449 /// @brief Overloaded equality operator between a character array and an SStringW object.
450 /// @param s1 The character array.
451 /// @param s2 The string.
452 /// @return TRUE if the character array and string are equal, FALSE otherwise.
453 friend inline bool __stdcall operator==(const wchar_t* s1, const SStringW& s2)
454 {
455 return s2.Compare(s1) == 0;
456 }
457
458 /// @brief Overloaded inequality operator between two SStringW objects.
459 /// @param s1 The first string.
460 /// @param s2 The second string.
461 /// @return TRUE if the strings are not equal, FALSE otherwise.
462 friend inline bool __stdcall operator!=(const SStringW& s1, const SStringW& s2)
463 {
464 return s1.Compare(s2) != 0;
465 }
466
467 /// @brief Overloaded inequality operator between an SStringW object and a character array.
468 /// @param s1 The string.
469 /// @param s2 The character array.
470 /// @return TRUE if the string and character array are not equal, FALSE otherwise.
471 friend inline bool __stdcall operator!=(const SStringW& s1, const wchar_t* s2)
472 {
473 return s1.Compare(s2) != 0;
474 }
475
476 /// @brief Overloaded inequality operator between a character array and an SStringW object.
477 /// @param s1 The character array.
478 /// @param s2 The string.
479 /// @return TRUE if the character array and string are not equal, FALSE otherwise.
480 friend inline bool __stdcall operator!=(const wchar_t* s1, const SStringW& s2)
481 {
482 return s2.Compare(s1) != 0;
483 }
484
485 /// @brief Overloaded less-than operator between two SStringW objects.
486 /// @param s1 The first string.
487 /// @param s2 The second string.
488 /// @return TRUE if the first string is less than the second string, FALSE otherwise.
489 friend inline bool __stdcall operator<(const SStringW& s1, const SStringW& s2)
490 {
491 return s1.Compare(s2) < 0;
492 }
493
494 /// @brief Overloaded less-than operator between an SStringW object and a character array.
495 /// @param s1 The string.
496 /// @param s2 The character array.
497 /// @return TRUE if the string is less than the character array, FALSE otherwise.
498 friend inline bool __stdcall operator<(const SStringW& s1, const wchar_t* s2)
499 {
500 return s1.Compare(s2) < 0;
501 }
502
503 /// @brief Overloaded less-than operator between a character array and an SStringW object.
504 /// @param s1 The character array.
505 /// @param s2 The string.
506 /// @return TRUE if the character array is less than the string, FALSE otherwise.
507 friend inline bool __stdcall operator<(const wchar_t* s1, const SStringW& s2)
508 {
509 return s2.Compare(s1) > 0;
510 }
511
512 /// @brief Overloaded greater-than operator between two SStringW objects.
513 /// @param s1 The first string.
514 /// @param s2 The second string.
515 /// @return TRUE if the first string is greater than the second string, FALSE otherwise.
516 friend inline bool __stdcall operator>(const SStringW& s1, const SStringW& s2)
517 {
518 return s1.Compare(s2) > 0;
519 }
520
521 /// @brief Overloaded greater-than operator between an SStringW object and a character array.
522 /// @param s1 The string.
523 /// @param s2 The character array.
524 /// @return TRUE if the string is greater than the character array, FALSE otherwise.
525 friend inline bool __stdcall operator>(const SStringW& s1, const wchar_t* s2)
526 {
527 return s1.Compare(s2) > 0;
528 }
529
530 /// @brief Overloaded greater-than operator between a character array and an SStringW object.
531 /// @param s1 The character array.
532 /// @param s2 The string.
533 /// @return TRUE if the character array is greater than the string, FALSE otherwise.
534 friend inline bool __stdcall operator>(const wchar_t* s1, const SStringW& s2)
535 {
536 return s2.Compare(s1) < 0;
537 }
538
539 /// @brief Overloaded less-than-or-equal-to operator between two SStringW objects.
540 /// @param s1 The first string.
541 /// @param s2 The second string.
542 /// @return TRUE if the first string is less than or equal to the second string, FALSE otherwise.
543 friend inline bool __stdcall operator<=(const SStringW& s1, const SStringW& s2)
544 {
545 return s1.Compare(s2) <= 0;
546 }
547
548 /// @brief Overloaded less-than-or-equal-to operator between an SStringW object and a character array.
549 /// @param s1 The string.
550 /// @param s2 The character array.
551 /// @return TRUE if the string is less than or equal to the character array, FALSE otherwise.
552 friend inline bool __stdcall operator<=(const SStringW& s1, const wchar_t* s2)
553 {
554 return s1.Compare(s2) <= 0;
555 }
556
557 /// @brief Overloaded less-than-or-equal-to operator between a character array and an SStringW object.
558 /// @param s1 The character array.
559 /// @param s2 The string.
560 /// @return TRUE if the character array is less than or equal to the string, FALSE otherwise.
561 friend inline bool __stdcall operator<=(const wchar_t* s1, const SStringW& s2)
562 {
563 return s2.Compare(s1) >= 0;
564 }
565
566 /// @brief Overloaded greater-than-or-equal-to operator between two SStringW objects.
567 /// @param s1 The first string.
568 /// @param s2 The second string.
569 /// @return TRUE if the first string is greater than or equal to the second string, FALSE otherwise.
570 friend inline bool __stdcall operator>=(const SStringW& s1, const SStringW& s2)
571 {
572 return s1.Compare(s2) >= 0;
573 }
574
575 /// @brief Overloaded greater-than-or-equal-to operator between an SStringW object and a character array.
576 /// @param s1 The string.
577 /// @param s2 The character array.
578 /// @return TRUE if the string is greater than or equal to the character array, FALSE otherwise.
579 friend inline bool __stdcall operator>=(const SStringW& s1, const wchar_t* s2)
580 {
581 return s1.Compare(s2) >= 0;
582 }
583
584 /// @brief Overloaded greater-than-or-equal-to operator between a character array and an SStringW object.
585 /// @param s1 The character array.
586 /// @param s2 The string.
587 /// @return TRUE if the character array is greater than or equal to the string, FALSE otherwise.
588 friend inline bool __stdcall operator>=(const wchar_t* s1, const SStringW& s2)
589 {
590 return s2.Compare(s1) <= 0;
591 }
592
593 /// @brief Overloaded addition operator between two SStringW objects.
594 /// @param string1 The first string.
595 /// @param string2 The second string.
596 /// @return A new string that is the concatenation of the two strings.
597 friend inline SStringW __stdcall operator+(const SStringW& string1, const SStringW& string2)
598 {
599 SStringW s;
600 s.ConcatCopy(string1.GetData()->nDataLength, string1.m_pszData, string2.GetData()->nDataLength, string2.m_pszData);
601 return s;
602 }
603
604 /// @brief Overloaded addition operator between an SStringW object and a character array.
605 /// @param string The string.
606 /// @param psz The character array.
607 /// @return A new string that is the concatenation of the string and the character array.
608 friend inline SStringW __stdcall operator+(const SStringW& string, const wchar_t* psz)
609 {
610 SASSERT(psz != NULL);
611 SStringW s;
612 s.ConcatCopy(string.GetData()->nDataLength, string.m_pszData, SStringW::SafeStrlen(psz), psz);
613 return s;
614 }
615
616 /// @brief Overloaded addition operator between a character array and an SStringW object.
617 /// @param psz The character array.
618 /// @param string The string.
619 /// @return A new string that is the concatenation of the character array and the string.
620 friend inline SStringW __stdcall operator+(const wchar_t* psz, const SStringW& string)
621 {
622 SASSERT(psz != NULL);
623 SStringW s;
624 s.ConcatCopy(SStringW::SafeStrlen(psz), psz, string.GetData()->nDataLength, string.m_pszData);
625 return s;
626 }
627
628 /// @brief Overloaded addition operator between an SStringW object and a single character.
629 /// @param string1 The string.
630 /// @param ch The character.
631 /// @return A new string that is the concatenation of the string and the character.
632 friend inline SStringW __stdcall operator+(const SStringW& string1, wchar_t ch)
633 {
634 SStringW s;
635 s.ConcatCopy(string1.GetData()->nDataLength, string1.m_pszData, 1, &ch);
636 return s;
637 }
638
639 /// @brief Overloaded addition operator between a single character and an SStringW object.
640 /// @param ch The character.
641 /// @param string The string.
642 /// @return A new string that is the concatenation of the character and the string.
643 friend inline SStringW __stdcall operator+(wchar_t ch, const SStringW& string)
644 {
645 SStringW s;
646 s.ConcatCopy(1, &ch, string.GetData()->nDataLength, string.m_pszData);
647 return s;
648 }
649
650 // Implementation
651protected:
652 /// @brief Retrieves the allocated length of the string buffer.
653 /// @return The allocated length of the string buffer.
654 int GetAllocLength() const;
655
656 /// @brief Computes the length of a null-terminated string safely.
657 /// @param psz The string to measure.
658 /// @return The length of the string.
659 static int SafeStrlen(const wchar_t* psz);
660
661 /// @brief Preallocates memory for the string buffer.
662 /// @param nLength The length to preallocate.
663 void Preallocate(int nLength);
664
665 /// @brief Frees any extra allocated memory in the string buffer.
666 void FreeExtra();
667
668protected:
669 // implementation helpers
670 /// @brief Retrieves the data structure of the string.
671 /// @return A pointer to the data structure.
672 TStringData* GetData() const;
673
674 /// @brief Initializes the string.
675 void Init();
676
677 /// @brief Initializes the string from an IStringW object.
678 /// @param stringSrc The IStringW object to initialize from.
679 void InitFromIString(const IStringW *stringSrc);
680
681 /// @brief Formats a string using a format string and variable arguments.
682 /// @param pszFormat The format string.
683 /// @param args The variable arguments list.
684 /// @return TRUE if the string was successfully formatted, FALSE otherwise.
685 BOOL _Format(const wchar_t* pszFormat, va_list & args);
686
687 /// @brief Appends formatted data to the string using a format string and variable arguments.
688 /// @param pszFormat The format string.
689 /// @param args The variable arguments list.
690 void _AppendFormat(const wchar_t* pszFormat, va_list & args);
691
692 // Assignment operators
693 // All assign a new value to the string
694 // (a) first see if the buffer is big enough
695 // (b) if enough room, copy on top of old buffer, set size and type
696 // (c) otherwise free old string data, and create a new one
697 //
698 // All routines return the new string (but as a 'const TStringT&' so that
699 // assigning it again will cause a copy, eg: s1 = s2 = "hi there".
700 //
701
702 /// @brief Allocates and copies a substring of the string.
703 /// @param dest The destination string.
704 /// @param nCopyLen The length of the substring to copy.
705 /// @param nCopyIndex The starting index of the substring.
706 /// @param nExtraLen The extra length to allocate.
707 void AllocCopy(SStringW& dest, int nCopyLen, int nCopyIndex, int nExtraLen) const;
708
709 /// @brief Assigns a substring of a character array to the string.
710 /// @param nSrcLen The length of the substring.
711 /// @param pszSrcData The character array to assign from.
712 void AssignCopy(int nSrcLen, const wchar_t* pszSrcData);
713
714 /// @brief Concatenates two substrings and copies the result to the string.
715 /// @param nSrc1Len The length of the first substring.
716 /// @param pszSrc1Data The first substring.
717 /// @param nSrc2Len The length of the second substring.
718 /// @param pszSrc2Data The second substring.
719 /// @return TRUE if the concatenation was successful, FALSE otherwise.
720 bool ConcatCopy(int nSrc1Len, const wchar_t* pszSrc1Data, int nSrc2Len, const wchar_t* pszSrc2Data);
721
722 /// @brief Concatenates a substring to the string in place.
723 /// @param nSrcLen The length of the substring.
724 /// @param pszSrcData The substring to concatenate.
725 void ConcatInPlace(int nSrcLen, const wchar_t* pszSrcData);
726
727 /// @brief Copies the string before writing to it.
728 void CopyBeforeWrite();
729
730 /// @brief Allocates memory for the string before writing to it.
731 /// @param nLen The length to allocate.
732 /// @return TRUE if the allocation was successful, FALSE otherwise.
733 bool AllocBeforeWrite(int nLen);
734
735 /// @brief Allocates memory for the string buffer.
736 /// @param nLength The length to allocate.
737 /// @return TRUE if the allocation was successful, FALSE otherwise.
738 bool AllocBuffer(int nLength);
739
740 /// @brief Reallocates memory for the string buffer.
741 /// @param nNewLength The new length to allocate.
742 /// @return TRUE if the reallocation was successful, FALSE otherwise.
743 bool ReallocBuffer(int nNewLength);
744
745 /// @brief Releases the data structure of the string.
746 void _ReleaseData();
747
748 // always allocate one extra character for '\0' termination
749 // assumes [optimistically] that data length will equal allocation length
750
751 /// @brief Allocates a new data structure for the string.
752 /// @param nLength The length to allocate.
753 /// @param pOldData The old data structure to reuse (optional).
754 /// @return A pointer to the new data structure.
755 static TStringData* AllocData(int nLength, TStringData* pOldData = NULL);
756
757 /// @brief Releases a data structure.
758 /// @param pData The data structure to release.
759 static void ReleaseData(TStringData* pData);
760
761protected:
762 /// @brief Pointer to the ref counted string data.
763 wchar_t* m_pszData;
764};
765
766SNSEND
767#endif//__TSTRINGW_H__
A class representing an ASCII string.
Definition sstringw.h:96
SStringW()
Default constructor.
void Empty()
Empties the string.
friend bool __stdcall operator==(const SStringW &s1, const wchar_t *s2)
Overloaded equality operator between an SStringW object and a character array.
Definition sstringw.h:444
void Assign2(LPCWSTR src, int nLen)
Assigns a substring of a character array to the string.
int CompareNoCase(const wchar_t *psz) SCONST
Compares the string with another string, ignoring case.
Definition sstringw.cpp:929
void ReleaseBuffer(int nNewLength=-1)
Releases the buffer and sets the new length of the string.
Definition sstringw.cpp:426
static bool IsBlankChar(const wchar_t &c)
Checks if a character is a blank character.
Definition sstringw.cpp:788
void SetLength(int nLength)
Sets the length of the string.
Definition sstringw.cpp:402
void TrimBlank()
Trims leading and trailing whitespace characters from the string.
Definition sstringw.cpp:762
friend SStringW __stdcall operator+(const SStringW &string1, wchar_t ch)
Overloaded addition operator between an SStringW object and a single character.
Definition sstringw.h:632
LPVOID GetPrivData() SCONST
Retrieves private data associated with the string.
long ToLong() SCONST OVERRIDE
Converts the string to a long integer.
int Remove(wchar_t chRemove)
Removes all occurrences of a character from the string.
Definition sstringw.cpp:551
friend bool __stdcall operator==(const SStringW &s1, const SStringW &s2)
Overloaded equality operator between two SStringW objects.
Definition sstringw.h:435
friend SStringW __stdcall operator+(wchar_t ch, const SStringW &string)
Overloaded addition operator between a single character and an SStringW object.
Definition sstringw.h:643
void AppendChar(wchar_t ch) OVERRIDE
Appends a character to the string.
Definition sstringw.cpp:952
friend SStringW __stdcall operator+(const SStringW &string, const wchar_t *psz)
Overloaded addition operator between an SStringW object and a character array.
Definition sstringw.h:608
SStringW & operator=(const SStringW &stringSrc)
Overloaded assignment operator from another SStringW object.
Definition sstringw.cpp:985
int Replace(const wchar_t *pszOld, const wchar_t *pszNew)
Replaces all occurrences of a substring with another substring.
Definition sstringw.cpp:575
friend bool __stdcall operator<=(const SStringW &s1, const SStringW &s2)
Overloaded less-than-or-equal-to operator between two SStringW objects.
Definition sstringw.h:543
const wchar_t * c_str() SCONST
Retrieves a C-style string representation of the string.
int ReplaceChar(wchar_t chOld, wchar_t chNew)
Replaces all occurrences of a character with another character.
Definition sstringw.cpp:636
BOOL IsEmpty() SCONST
Checks if the string is empty.
int Compare(const wchar_t *psz) SCONST
Compares the string with another string.
Definition sstringw.cpp:934
wchar_t * GetBufferSetLength(int nNewLength)
Retrieves a modifiable buffer for the string and sets the new length.
Definition sstringw.cpp:414
void TrimLeft(wchar_t chTarget=VK_SPACE) OVERRIDE
Trims leading whitespace characters from the string.
Definition sstringw.cpp:804
void Copy(const IStringW *src)
Copies the contents of another string into this string.
friend bool __stdcall operator>=(const SStringW &s1, const SStringW &s2)
Overloaded greater-than-or-equal-to operator between two SStringW objects.
Definition sstringw.h:570
friend bool __stdcall operator>=(const wchar_t *s1, const SStringW &s2)
Overloaded greater-than-or-equal-to operator between a character array and an SStringW object.
Definition sstringw.h:588
friend bool __stdcall operator<=(const SStringW &s1, const wchar_t *s2)
Overloaded less-than-or-equal-to operator between an SStringW object and a character array.
Definition sstringw.h:552
void Trim(wchar_t chTarget=VK_SPACE) OVERRIDE
Trims leading and trailing whitespace characters from the string.
Definition sstringw.cpp:798
friend bool __stdcall operator<=(const wchar_t *s1, const SStringW &s2)
Overloaded less-than-or-equal-to operator between a character array and an SStringW object.
Definition sstringw.h:561
int FindChar(wchar_t ch, int nStart=0) SCONST
Finds the first occurrence of a character in the string.
Definition sstringw.cpp:529
double ToDouble() SCONST OVERRIDE
Converts the string to a double.
friend bool __stdcall operator>(const SStringW &s1, const SStringW &s2)
Overloaded greater-than operator between two SStringW objects.
Definition sstringw.h:516
SStringW Mid(int nFirst) const
Extracts a substring from the string.
Definition sstringw.cpp:924
static int SafeStrlen(const wchar_t *psz)
Computes the length of a null-terminated string safely.
Definition sstringw.cpp:372
friend bool __stdcall operator>(const wchar_t *s1, const SStringW &s2)
Overloaded greater-than operator between a character array and an SStringW object.
Definition sstringw.h:534
BOOL __cdecl Format(HINSTANCE hInst, UINT nFormatID,...)
Formats a string using a format string and variable arguments.
Definition sstringw.cpp:490
friend bool __stdcall operator<(const wchar_t *s1, const SStringW &s2)
Overloaded less-than operator between a character array and an SStringW object.
Definition sstringw.h:507
SStringW & MakeLower()
Converts the string to lowercase.
Definition sstringw.cpp:869
friend SStringW __stdcall operator+(const wchar_t *psz, const SStringW &string)
Overloaded addition operator between a character array and an SStringW object.
Definition sstringw.h:620
bool EndsWith(const SStringW &suffix, bool IgnoreCase=false) const
Checks if the string ends with a specified suffix.
Definition sstringw.cpp:736
bool ConcatCopy(int nSrc1Len, const wchar_t *pszSrc1Data, int nSrc2Len, const wchar_t *pszSrc2Data)
Concatenates two substrings and copies the result to the string.
Definition sstringw.cpp:275
friend bool __stdcall operator>(const SStringW &s1, const wchar_t *s2)
Overloaded greater-than operator between an SStringW object and a character array.
Definition sstringw.h:525
SStringW & Append(const SStringW &src)
Appends another SStringW object to the string.
Definition sstringw.cpp:939
int InsertChar(int nIndex, wchar_t ch)
Inserts a character at a specified index.
Definition sstringw.cpp:711
void SetAt(int nIndex, wchar_t ch)
Sets the character at a specified index.
friend bool __stdcall operator<(const SStringW &s1, const wchar_t *s2)
Overloaded less-than operator between an SStringW object and a character array.
Definition sstringw.h:498
friend bool __stdcall operator<(const SStringW &s1, const SStringW &s2)
Overloaded less-than operator between two SStringW objects.
Definition sstringw.h:489
void TrimRight(wchar_t chTarget=VK_SPACE) OVERRIDE
Trims trailing whitespace characters from the string.
Definition sstringw.cpp:824
BOOL ToBool() SCONST OVERRIDE
Converts the string to a boolean.
friend bool __stdcall operator!=(const SStringW &s1, const SStringW &s2)
Overloaded inequality operator between two SStringW objects.
Definition sstringw.h:462
int Insert(int nIndex, const wchar_t *psz)
Inserts a substring at a specified index.
Definition sstringw.cpp:683
TStringData * GetData() const
Retrieves the data structure of the string.
Definition sstringw.cpp:366
float ToFloat() SCONST OVERRIDE
Converts the string to a float.
void Assign(LPCWSTR src)
Assigns a character array to the string.
SStringW Right(int nCount) const
Extracts the rightmost part of the string.
Definition sstringw.cpp:892
void Release() OVERRIDE
Releases the string and its resources.
SStringW & MakeUpper()
Converts the string to uppercase.
Definition sstringw.cpp:874
wchar_t * GetBuffer(int nMinBufLength=-1)
Retrieves a modifiable buffer for the string.
Definition sstringw.cpp:439
int Delete(int nIndex, int nCount=1)
Deletes a substring from the string.
Definition sstringw.cpp:662
int ToInt() SCONST OVERRIDE
Converts the string to an integer.
const SStringW & operator+=(const wchar_t *psz)
Overloaded concatenation operator with a character array.
Definition sstringw.cpp:967
friend bool __stdcall operator==(const wchar_t *s1, const SStringW &s2)
Overloaded equality operator between a character array and an SStringW object.
Definition sstringw.h:453
wchar_t * m_pszData
Pointer to the ref counted string data.
Definition sstringw.h:763
UINT ToUint() SCONST OVERRIDE
Converts the string to an unsigned integer.
void __cdecl AppendFormat(HINSTANCE hInst, UINT nFormatID,...)
Appends formatted data to the string using a format string and variable arguments.
Definition sstringw.cpp:469
int GetLength() SCONST
Retrieves the length of the string.
int Find(const wchar_t *pszSub, int nStart=0) SCONST
Finds the first occurrence of a substring in the string.
Definition sstringw.cpp:516
wchar_t GetAt(int nIndex) SCONST
Retrieves the character at a specified index.
SStringW Left(int nCount) const
Extracts the leftmost part of the string.
Definition sstringw.cpp:879
int ReverseFind(wchar_t ch) SCONST
Finds the last occurrence of a character in the string.
Definition sstringw.cpp:542
void ToUpper() OVERRIDE
Converts the string to uppercase.
Definition sstringw.cpp:861
wchar_t operator[](int nIndex) const
Retrieves the character at a specified index.
friend SStringW __stdcall operator+(const SStringW &string1, const SStringW &string2)
Overloaded addition operator between two SStringW objects.
Definition sstringw.h:597
void ToLower() OVERRIDE
Converts the string to lowercase.
Definition sstringw.cpp:853
friend bool __stdcall operator>=(const SStringW &s1, const wchar_t *s2)
Overloaded greater-than-or-equal-to operator between an SStringW object and a character array.
Definition sstringw.h:579
void AppendStr(const wchar_t *pszStr, int nLen=-1) OVERRIDE
Appends a substring to the string.
Definition sstringw.cpp:946
friend bool __stdcall operator!=(const wchar_t *s1, const SStringW &s2)
Overloaded inequality operator between a character array and an SStringW object.
Definition sstringw.h:480
friend bool __stdcall operator!=(const SStringW &s1, const wchar_t *s2)
Overloaded inequality operator between an SStringW object and a character array.
Definition sstringw.h:471
bool StartsWith(const SStringW &prefix, bool IgnoreCase=false) const
Checks if the string starts with a specified prefix.
Definition sstringw.cpp:749
BOOL LoadString(UINT nID, HINSTANCE hInst)
Loads a string resource from a module.
Definition sstringw.cpp:506
Character traits for ASCII strings.
Definition sstringw.h:12
static int Compare(const wchar_t *psz1, const wchar_t *psz2)
Compares two strings lexicographically.
Definition sstringw.cpp:120
static wchar_t * CharNext(wchar_t *psz)
Moves to the next character in a string.
Definition sstringw.cpp:70
static wchar_t CharLower(wchar_t ch)
Converts a character to lowercase.
Definition sstringw.cpp:80
static size_t StrLen(const wchar_t *psz)
Computes the length of a null-terminated string.
Definition sstringw.cpp:125
static int CompareNoCase(const wchar_t *psz1, const wchar_t *psz2)
Compares two strings lexicographically, ignoring case.
Definition sstringw.cpp:115
static int LoadString(HINSTANCE hInst, UINT uID, wchar_t *lpBuffer, int nBufferMax)
Loads a string resource from a module.
Definition sstringw.cpp:9
static wchar_t * StrLower(wchar_t *psz)
Converts a string to lowercase.
Definition sstringw.cpp:90
static const wchar_t * StrRChr(const wchar_t *psz, wchar_t ch)
Finds the last occurrence of a character in a string.
Definition sstringw.cpp:105
static wchar_t * StrUpper(wchar_t *psz)
Converts a string to uppercase.
Definition sstringw.cpp:95
static const wchar_t * StrChr(const wchar_t *psz, wchar_t ch)
Finds the first occurrence of a character in a string.
Definition sstringw.cpp:110
static int Format(wchar_t **ppszDst, const wchar_t *pszFormat, va_list &args)
Formats a string using a format string and variable arguments.
Definition sstringw.cpp:18
static wchar_t CharUpper(wchar_t ch)
Converts a character to uppercase.
Definition sstringw.cpp:75
static int IsSpace(wchar_t ch)
Checks if a character is a whitespace character.
Definition sstringw.cpp:85
static const wchar_t * StrStr(const wchar_t *psz, const wchar_t *psz2)
Finds the first occurrence of a substring in a string.
Definition sstringw.cpp:100