soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
sstringa.h
1#ifndef __TSTRINGA_H__
2#define __TSTRINGA_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 char_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 char* 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 char* psz1, const char* 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 char* psz1, const char* 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 char* StrChr(const char* psz, char 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 char* StrRChr(const char* psz, char 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 char* StrStr(const char* psz, const char* 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 char* StrUpper(char* 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 char* StrLower(char* 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(char 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 char CharLower(char 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 char CharUpper(char 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 char* CharNext(char* 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(char** ppszDst, const char* 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, char* lpBuffer, int nBufferMax);
92};
93
94/// @brief A class representing an ASCII string.
95class UTILITIES_API SStringA : public IStringA
96{
97public:
98 typedef const char* pctstr;
99 typedef char _tchar;
100 typedef char_traits _tchar_traits;
101
102 /// @brief Default constructor.
103 SStringA();
104
105 /// @brief Copy constructor.
106 /// @param stringSrc The string to copy.
107 SStringA(const SStringA& stringSrc);
108
109 /// @brief Constructor from an IStringA object.
110 /// @param stringSrc The string to copy.
111 SStringA(const IStringA* 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 SStringA(char 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 SStringA(const char* psz, int nLength);
122
123 /// @brief Constructor from a character array.
124 /// @param psz The character array.
125 SStringA(const char* psz);
126
127 /// @brief Destructor.
128 ~SStringA();
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_(char, 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, char 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 char *, 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 char* 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 char* 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, char 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 char* 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_ char chOld, char 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 char* pszOld, const char* 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_ char 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_ char 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_ char 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 char* 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_(char*, 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_(char*, 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 IStringA* src);
242
243 /// @brief Assigns a character array to the string.
244 /// @param src The character array to assign.
245 STDMETHOD_(void, Assign)(THIS_ LPCSTR 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_ LPCSTR 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_ char 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_ char 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_ char 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_ char 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 char* 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 SStringA 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 SStringA 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 SStringA 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 SStringA 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 SStringA& 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 SStringA& 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 SStringA __cdecl Format(const char* 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 SStringA __cdecl AppendFormat(const char* pszFormat, ...);
381
382 /// @brief Converts the string to a C-style string.
383 /// @return A pointer to the C-style string.
384 operator const char*() 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 char operator[](int nIndex) const;
390
391 /// @brief Overloaded assignment operator from another SStringA object.
392 /// @param stringSrc The string to assign.
393 /// @return A reference to the modified string.
394 SStringA& operator=(const SStringA& 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 SStringA& operator=(const char* 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 SStringA& operator=(char 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 SStringA& operator+=(const char* 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 SStringA& operator+=(char ch);
415
416 /// @brief Overloaded concatenation operator with another SStringA object.
417 /// @param src The string to concatenate.
418 /// @return A reference to the modified string.
419 const SStringA& operator+=(const SStringA& src);
420
421 /// @brief Appends another SStringA object to the string.
422 /// @param src The string to append.
423 /// @return A reference to the modified string.
424 SStringA& Append(const SStringA& 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 char &c);
430
431 /// @brief Overloaded equality operator between two SStringA 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 SStringA& s1, const SStringA& s2)
436 {
437 return s1.Compare(s2) == 0;
438 }
439
440 /// @brief Overloaded equality operator between an SStringA 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 SStringA& s1, const char* s2)
445 {
446 return s1.Compare(s2) == 0;
447 }
448
449 /// @brief Overloaded equality operator between a character array and an SStringA 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 char* s1, const SStringA& s2)
454 {
455 return s2.Compare(s1) == 0;
456 }
457
458 /// @brief Overloaded inequality operator between two SStringA 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 SStringA& s1, const SStringA& s2)
463 {
464 return s1.Compare(s2) != 0;
465 }
466
467 /// @brief Overloaded inequality operator between an SStringA 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 SStringA& s1, const char* s2)
472 {
473 return s1.Compare(s2) != 0;
474 }
475
476 /// @brief Overloaded inequality operator between a character array and an SStringA 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 char* s1, const SStringA& s2)
481 {
482 return s2.Compare(s1) != 0;
483 }
484
485 /// @brief Overloaded less-than operator between two SStringA 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 SStringA& s1, const SStringA& s2)
490 {
491 return s1.Compare(s2) < 0;
492 }
493
494 /// @brief Overloaded less-than operator between an SStringA 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 SStringA& s1, const char* s2)
499 {
500 return s1.Compare(s2) < 0;
501 }
502
503 /// @brief Overloaded less-than operator between a character array and an SStringA 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 char* s1, const SStringA& s2)
508 {
509 return s2.Compare(s1) > 0;
510 }
511
512 /// @brief Overloaded greater-than operator between two SStringA 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 SStringA& s1, const SStringA& s2)
517 {
518 return s1.Compare(s2) > 0;
519 }
520
521 /// @brief Overloaded greater-than operator between an SStringA 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 SStringA& s1, const char* s2)
526 {
527 return s1.Compare(s2) > 0;
528 }
529
530 /// @brief Overloaded greater-than operator between a character array and an SStringA 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 char* s1, const SStringA& s2)
535 {
536 return s2.Compare(s1) < 0;
537 }
538
539 /// @brief Overloaded less-than-or-equal-to operator between two SStringA 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 SStringA& s1, const SStringA& s2)
544 {
545 return s1.Compare(s2) <= 0;
546 }
547
548 /// @brief Overloaded less-than-or-equal-to operator between an SStringA 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 SStringA& s1, const char* 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 SStringA 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 char* s1, const SStringA& s2)
562 {
563 return s2.Compare(s1) >= 0;
564 }
565
566 /// @brief Overloaded greater-than-or-equal-to operator between two SStringA 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 SStringA& s1, const SStringA& s2)
571 {
572 return s1.Compare(s2) >= 0;
573 }
574
575 /// @brief Overloaded greater-than-or-equal-to operator between an SStringA 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 SStringA& s1, const char* 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 SStringA 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 char* s1, const SStringA& s2)
589 {
590 return s2.Compare(s1) <= 0;
591 }
592
593 /// @brief Overloaded addition operator between two SStringA 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 SStringA __stdcall operator+(const SStringA& string1, const SStringA& string2)
598 {
599 SStringA 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 SStringA 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 SStringA __stdcall operator+(const SStringA& string, const char* psz)
609 {
610 SASSERT(psz != NULL);
611 SStringA s;
612 s.ConcatCopy(string.GetData()->nDataLength, string.m_pszData, SStringA::SafeStrlen(psz), psz);
613 return s;
614 }
615
616 /// @brief Overloaded addition operator between a character array and an SStringA 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 SStringA __stdcall operator+(const char* psz, const SStringA& string)
621 {
622 SASSERT(psz != NULL);
623 SStringA s;
624 s.ConcatCopy(SStringA::SafeStrlen(psz), psz, string.GetData()->nDataLength, string.m_pszData);
625 return s;
626 }
627
628 /// @brief Overloaded addition operator between an SStringA 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 SStringA __stdcall operator+(const SStringA& string1, char ch)
633 {
634 SStringA 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 SStringA 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 SStringA __stdcall operator+(char ch, const SStringA& string)
644 {
645 SStringA 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 char* 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 IStringA object.
678 /// @param stringSrc The IStringA object to initialize from.
679 void InitFromIString(const IStringA *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 char* 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 char* 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(SStringA& 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 char* 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 char* pszSrc1Data, int nSrc2Len, const char* 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 char* 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.
764};
765
766SNSEND
767#endif//__TSTRINGA_H__
A class representing an ASCII string.
Definition sstringa.h:96
SStringA & MakeUpper()
Converts the string to uppercase.
Definition sstringa.cpp:919
BOOL __cdecl Format(HINSTANCE hInst, UINT nFormatID,...)
Formats a string using a format string and variable arguments.
Definition sstringa.cpp:534
friend bool __stdcall operator<(const char *s1, const SStringA &s2)
Overloaded less-than operator between a character array and an SStringA object.
Definition sstringa.h:507
friend bool __stdcall operator<(const SStringA &s1, const char *s2)
Overloaded less-than operator between an SStringA object and a character array.
Definition sstringa.h:498
void Trim(char chTarget=VK_SPACE) OVERRIDE
Trims leading and trailing whitespace characters from the string.
Definition sstringa.cpp:842
void Copy(const IStringA *src)
Copies the contents of another string into this string.
Definition sstringa.cpp:420
void Assign2(LPCSTR src, int nLen)
Assigns a substring of a character array to the string.
Definition sstringa.cpp:447
SStringA & operator=(const SStringA &stringSrc)
Overloaded assignment operator from another SStringA object.
void ToUpper() OVERRIDE
Converts the string to uppercase.
Definition sstringa.cpp:905
int Compare(const char *psz) SCONST
Compares the string with another string.
Definition sstringa.cpp:980
UINT ToUint() SCONST OVERRIDE
Converts the string to an unsigned integer.
LPVOID GetPrivData() SCONST
Retrieves private data associated with the string.
Definition sstringa.cpp:452
int GetLength() SCONST
Retrieves the length of the string.
void ReleaseBuffer(int nNewLength=-1)
Releases the buffer and sets the new length of the string.
Definition sstringa.cpp:469
int Delete(int nIndex, int nCount=1)
Deletes a substring from the string.
Definition sstringa.cpp:706
void AppendStr(const char *pszStr, int nLen=-1) OVERRIDE
Appends a substring to the string.
Definition sstringa.cpp:991
int Find(const char *pszSub, int nStart=0) SCONST
Finds the first occurrence of a substring in the string.
Definition sstringa.cpp:560
int ToInt() SCONST OVERRIDE
Converts the string to an integer.
void Assign(LPCSTR src)
Assigns a character array to the string.
Definition sstringa.cpp:442
static int SafeStrlen(const char *psz)
Computes the length of a null-terminated string safely.
Definition sstringa.cpp:378
char GetAt(int nIndex) SCONST
Retrieves the character at a specified index.
bool EndsWith(const SStringA &suffix, bool IgnoreCase=false) const
Checks if the string ends with a specified suffix.
Definition sstringa.cpp:780
bool StartsWith(const SStringA &prefix, bool IgnoreCase=false) const
Checks if the string starts with a specified prefix.
Definition sstringa.cpp:793
friend bool __stdcall operator>(const SStringA &s1, const char *s2)
Overloaded greater-than operator between an SStringA object and a character array.
Definition sstringa.h:525
friend SStringA __stdcall operator+(char ch, const SStringA &string)
Overloaded addition operator between a single character and an SStringA object.
Definition sstringa.h:643
friend bool __stdcall operator!=(const SStringA &s1, const char *s2)
Overloaded inequality operator between an SStringA object and a character array.
Definition sstringa.h:471
int ReplaceChar(char chOld, char chNew)
Replaces all occurrences of a character with another character.
Definition sstringa.cpp:680
char * GetBuffer(int nMinBufLength=-1)
Retrieves a modifiable buffer for the string.
Definition sstringa.cpp:482
void SetAt(int nIndex, char ch)
Sets the character at a specified index.
SStringA & Append(const SStringA &src)
Appends another SStringA object to the string.
Definition sstringa.cpp:985
void TrimRight(char chTarget=VK_SPACE) OVERRIDE
Trims trailing whitespace characters from the string.
Definition sstringa.cpp:868
friend bool __stdcall operator>=(const SStringA &s1, const char *s2)
Overloaded greater-than-or-equal-to operator between an SStringA object and a character array.
Definition sstringa.h:579
friend bool __stdcall operator==(const char *s1, const SStringA &s2)
Overloaded equality operator between a character array and an SStringA object.
Definition sstringa.h:453
void TrimBlank()
Trims leading and trailing whitespace characters from the string.
Definition sstringa.cpp:806
friend SStringA __stdcall operator+(const SStringA &string1, const SStringA &string2)
Overloaded addition operator between two SStringA objects.
Definition sstringa.h:597
friend bool __stdcall operator>(const SStringA &s1, const SStringA &s2)
Overloaded greater-than operator between two SStringA objects.
Definition sstringa.h:516
int Insert(int nIndex, const char *psz)
Inserts a substring at a specified index.
Definition sstringa.cpp:727
BOOL ToBool() SCONST OVERRIDE
Converts the string to a boolean.
float ToFloat() SCONST OVERRIDE
Converts the string to a float.
friend bool __stdcall operator!=(const char *s1, const SStringA &s2)
Overloaded inequality operator between a character array and an SStringA object.
Definition sstringa.h:480
friend bool __stdcall operator<=(const SStringA &s1, const SStringA &s2)
Overloaded less-than-or-equal-to operator between two SStringA objects.
Definition sstringa.h:543
void SetLength(int nLength)
Sets the length of the string.
Definition sstringa.cpp:408
SStringA Mid(int nFirst) const
Extracts a substring from the string.
Definition sstringa.cpp:970
int Remove(char chRemove)
Removes all occurrences of a character from the string.
Definition sstringa.cpp:595
SStringA Left(int nCount) const
Extracts the leftmost part of the string.
Definition sstringa.cpp:925
friend SStringA __stdcall operator+(const SStringA &string, const char *psz)
Overloaded addition operator between an SStringA object and a character array.
Definition sstringa.h:608
long ToLong() SCONST OVERRIDE
Converts the string to a long integer.
SStringA & MakeLower()
Converts the string to lowercase.
Definition sstringa.cpp:913
int FindChar(char ch, int nStart=0) SCONST
Finds the first occurrence of a character in the string.
Definition sstringa.cpp:573
void Empty()
Empties the string.
friend bool __stdcall operator<=(const char *s1, const SStringA &s2)
Overloaded less-than-or-equal-to operator between a character array and an SStringA object.
Definition sstringa.h:561
int Replace(const char *pszOld, const char *pszNew)
Replaces all occurrences of a substring with another substring.
Definition sstringa.cpp:619
bool ConcatCopy(int nSrc1Len, const char *pszSrc1Data, int nSrc2Len, const char *pszSrc2Data)
Concatenates two substrings and copies the result to the string.
Definition sstringa.cpp:281
char * GetBufferSetLength(int nNewLength)
Retrieves a modifiable buffer for the string and sets the new length.
Definition sstringa.cpp:457
friend bool __stdcall operator!=(const SStringA &s1, const SStringA &s2)
Overloaded inequality operator between two SStringA objects.
Definition sstringa.h:462
void __cdecl AppendFormat(HINSTANCE hInst, UINT nFormatID,...)
Appends formatted data to the string using a format string and variable arguments.
Definition sstringa.cpp:513
friend bool __stdcall operator>=(const char *s1, const SStringA &s2)
Overloaded greater-than-or-equal-to operator between a character array and an SStringA object.
Definition sstringa.h:588
friend bool __stdcall operator==(const SStringA &s1, const SStringA &s2)
Overloaded equality operator between two SStringA objects.
Definition sstringa.h:435
BOOL LoadString(UINT nID, HINSTANCE hInst)
Loads a string resource from a module.
Definition sstringa.cpp:550
int ReverseFind(char ch) SCONST
Finds the last occurrence of a character in the string.
Definition sstringa.cpp:586
char operator[](int nIndex) const
Retrieves the character at a specified index.
void Release() OVERRIDE
Releases the string and its resources.
friend bool __stdcall operator>=(const SStringA &s1, const SStringA &s2)
Overloaded greater-than-or-equal-to operator between two SStringA objects.
Definition sstringa.h:570
double ToDouble() SCONST OVERRIDE
Converts the string to a double.
int InsertChar(int nIndex, char ch)
Inserts a character at a specified index.
Definition sstringa.cpp:755
const SStringA & operator+=(const char *psz)
Overloaded concatenation operator with a character array.
friend bool __stdcall operator==(const SStringA &s1, const char *s2)
Overloaded equality operator between an SStringA object and a character array.
Definition sstringa.h:444
const char * c_str() SCONST
Retrieves a C-style string representation of the string.
friend bool __stdcall operator>(const char *s1, const SStringA &s2)
Overloaded greater-than operator between a character array and an SStringA object.
Definition sstringa.h:534
char * m_pszData
Pointer to the ref counted string data.
Definition sstringa.h:763
void AppendChar(char ch) OVERRIDE
Appends a character to the string.
Definition sstringa.cpp:997
static bool IsBlankChar(const char &c)
Checks if a character is a blank character.
Definition sstringa.cpp:832
void TrimLeft(char chTarget=VK_SPACE) OVERRIDE
Trims leading whitespace characters from the string.
Definition sstringa.cpp:848
friend bool __stdcall operator<(const SStringA &s1, const SStringA &s2)
Overloaded less-than operator between two SStringA objects.
Definition sstringa.h:489
int CompareNoCase(const char *psz) SCONST
Compares the string with another string, ignoring case.
Definition sstringa.cpp:975
friend SStringA __stdcall operator+(const SStringA &string1, char ch)
Overloaded addition operator between an SStringA object and a single character.
Definition sstringa.h:632
friend bool __stdcall operator<=(const SStringA &s1, const char *s2)
Overloaded less-than-or-equal-to operator between an SStringA object and a character array.
Definition sstringa.h:552
SStringA()
Default constructor.
TStringData * GetData() const
Retrieves the data structure of the string.
Definition sstringa.cpp:372
void ToLower() OVERRIDE
Converts the string to lowercase.
Definition sstringa.cpp:897
SStringA Right(int nCount) const
Extracts the rightmost part of the string.
Definition sstringa.cpp:938
BOOL IsEmpty() SCONST
Checks if the string is empty.
friend SStringA __stdcall operator+(const char *psz, const SStringA &string)
Overloaded addition operator between a character array and an SStringA object.
Definition sstringa.h:620
Character traits for ASCII strings.
Definition sstringa.h:12
static const char * StrStr(const char *psz, const char *psz2)
Finds the first occurrence of a substring in a string.
Definition sstringa.cpp:86
static size_t StrLen(const char *psz)
Computes the length of a null-terminated string.
Definition sstringa.cpp:131
static char * StrUpper(char *psz)
Converts a string to uppercase.
Definition sstringa.cpp:77
static char * CharNext(char *psz)
Moves to the next character in a string.
Definition sstringa.cpp:40
static int Format(char **ppszDst, const char *pszFormat, va_list &args)
Formats a string using a format string and variable arguments.
Definition sstringa.cpp:24
static char CharUpper(char ch)
Converts a character to uppercase.
Definition sstringa.cpp:49
static int IsSpace(char ch)
Checks if a character is a whitespace character.
Definition sstringa.cpp:59
static char CharLower(char ch)
Converts a character to lowercase.
Definition sstringa.cpp:54
static const char * StrRChr(const char *psz, char ch)
Finds the last occurrence of a character in a string.
Definition sstringa.cpp:95
static int CompareNoCase(const char *psz1, const char *psz2)
Compares two strings lexicographically, ignoring case.
Definition sstringa.cpp:113
static char * StrLower(char *psz)
Converts a string to lowercase.
Definition sstringa.cpp:68
static const char * StrChr(const char *psz, char ch)
Finds the first occurrence of a character in a string.
Definition sstringa.cpp:104
static int Compare(const char *psz1, const char *psz2)
Compares two strings lexicographically.
Definition sstringa.cpp:122
static int LoadString(HINSTANCE hInst, UINT uID, char *lpBuffer, int nBufferMax)
Loads a string resource from a module.
Definition sstringa.cpp:15