]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: string.h | |
e54c96f1 | 3 | // Purpose: interface of wxStringBuffer |
23324ae1 FM |
4 | // Author: wxWidgets team |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | /** | |
10 | @class wxStringBuffer | |
7c913512 | 11 | |
bcc8c903 | 12 | This tiny class allows you to conveniently access the wxString |
23324ae1 FM |
13 | internal buffer as a writable pointer without any risk of forgetting to restore |
14 | the string to the usable state later. | |
7c913512 FM |
15 | |
16 | For example, assuming you have a low-level OS function called | |
23324ae1 FM |
17 | @c GetMeaningOfLifeAsString(char *) returning the value in the provided |
18 | buffer (which must be writable, of course) you might call it like this: | |
7c913512 | 19 | |
23324ae1 FM |
20 | @code |
21 | wxString theAnswer; | |
22 | GetMeaningOfLifeAsString(wxStringBuffer(theAnswer, 1024)); | |
23 | if ( theAnswer != "42" ) | |
24 | { | |
25 | wxLogError("Something is very wrong!"); | |
26 | } | |
27 | @endcode | |
7c913512 | 28 | |
bcc8c903 | 29 | Note that the exact usage of this depends on whether or not wxUSE_STL is |
0c7db140 | 30 | enabled. If wxUSE_STL is enabled, wxStringBuffer creates a separate empty |
bcc8c903 | 31 | character buffer, and if wxUSE_STL is disabled, it uses GetWriteBuf() from |
0c7db140 VZ |
32 | wxString, keeping the same buffer wxString uses intact. In other words, |
33 | relying on wxStringBuffer containing the old wxString data is not a good | |
bcc8c903 | 34 | idea if you want to build your program both with and without wxUSE_STL. |
7c913512 | 35 | |
23324ae1 | 36 | @library{wxbase} |
bcc8c903 | 37 | @category{data} |
23324ae1 | 38 | */ |
7c913512 | 39 | class wxStringBuffer |
23324ae1 FM |
40 | { |
41 | public: | |
42 | /** | |
43 | Constructs a writable string buffer object associated with the given string | |
4cc4bfaf | 44 | and containing enough space for at least @a len characters. Basically, this |
23324ae1 FM |
45 | is equivalent to calling wxString::GetWriteBuf and |
46 | saving the result. | |
47 | */ | |
48 | wxStringBuffer(const wxString& str, size_t len); | |
49 | ||
50 | /** | |
7c913512 | 51 | Restores the string passed to the constructor to the usable state by calling |
23324ae1 FM |
52 | wxString::UngetWriteBuf on it. |
53 | */ | |
54 | ~wxStringBuffer(); | |
55 | ||
56 | /** | |
57 | Returns the writable pointer to a buffer of the size at least equal to the | |
58 | length specified in the constructor. | |
59 | */ | |
b33e2f63 | 60 | wxStringCharType* operator wxStringCharType *(); |
23324ae1 FM |
61 | }; |
62 | ||
63 | ||
e54c96f1 | 64 | |
23324ae1 FM |
65 | /** |
66 | @class wxString | |
7c913512 | 67 | |
a6919a6a RR |
68 | The wxString class has been completely rewritten for wxWidgets 3.0 |
69 | and this change was actually the main reason for the calling that | |
70 | version wxWidgets 3.0. | |
71 | ||
062dc5fc | 72 | wxString is a class representing a Unicode character string. |
a7d23734 RR |
73 | wxString uses @c std::string internally to store its content |
74 | unless this is not supported by the compiler or disabled | |
a6919a6a RR |
75 | specifically when building wxWidgets and it therefore inherits |
76 | many features from @c std::string. Most implementations of | |
77 | @c std::string are thread-safe and don't use reference counting. | |
78 | By default, wxString uses @c std::string internally even if | |
79 | wxUSE_STL is not defined. | |
80 | ||
81 | wxString now internally uses UTF-16 under Windows and UTF-8 under | |
82 | Unix, Linux and OS X to store its content. Note that when iterating | |
83 | over a UTF-16 string under Windows, the user code has to take care | |
84 | of surrogate pair handling whereas Windows itself has built-in | |
85 | support pairs in UTF-16, such as for drawing strings on screen. | |
86 | ||
c73f1b33 | 87 | Much work has been done to make existing code using ANSI string literals |
a6919a6a RR |
88 | work as before. If you nonetheless need to have a wxString that uses wchar_t |
89 | on Unix and Linux, too, you can specify this on the command line with the | |
90 | @c configure @c --disable-utf8 switch or you can consider using wxUString | |
91 | or std::wstring instead. | |
92 | ||
93 | Accessing a UTF-8 string by index can be very inefficient because | |
94 | a single character is represented by a variable number of bytes so that | |
95 | the entire string has to be parsed in order to find the character. | |
96 | Since iterating over a string by index is a common programming technique and | |
97 | was also possible and encouraged by wxString using the access operator[]() | |
98 | wxString implements caching of the last used index so that iterating over | |
99 | a string is a linear operation even in UTF-8 mode. | |
100 | ||
101 | It is nonetheless recommended to use iterators (instead of index bases | |
102 | access) like this: | |
062dc5fc | 103 | |
a7d23734 RR |
104 | @code |
105 | wxString s = "hello"; | |
106 | wxString::const_iterator i; | |
107 | for (i = s.begin(); i != s.end(); ++i) | |
108 | { | |
109 | wxUniChar uni_ch = *i; | |
110 | // do something with it | |
111 | } | |
112 | @endcode | |
062dc5fc VZ |
113 | |
114 | Please see the | |
115 | @ref overview_string "wxString overview" and the | |
a7d23734 RR |
116 | @ref overview_unicode "Unicode overview" for more information |
117 | about it. | |
96c99165 | 118 | |
ee0b7af0 RR |
119 | wxString uses the current locale encoding to convert any C string |
120 | literal to Unicode. The same is done for converting to and from | |
121 | @c std::string and for the return value of c_str(). For this | |
8c1cd030 | 122 | conversion, the @a wxConvLibc class instance is used. See wxCSConv and wxMBConv. |
ee0b7af0 | 123 | |
a7d23734 | 124 | wxString implements most of the methods of the @c std::string class. |
062dc5fc | 125 | These standard functions are only listed here, but they are not |
a7d23734 RR |
126 | fully documented in this manual. Please see the STL documentation. |
127 | The behaviour of all these functions is identical to the behaviour | |
128 | described there. | |
96c99165 | 129 | |
8c1cd030 | 130 | You may notice that wxString sometimes has several functions which do |
bcc8c903 | 131 | the same thing like Length(), Len() and length() which |
a7d23734 RR |
132 | all return the string length. In all cases of such duplication the |
133 | @c std::string compatible method should be used. | |
7c913512 | 134 | |
bcc8c903 RR |
135 | Anything may be concatenated (appended to) with a string. However, you can't |
136 | append something to a C string (including literal constants), so to do this it | |
137 | should be converted to a wxString first. | |
062dc5fc | 138 | |
0c339e7c RR |
139 | @li insert() |
140 | @li append() | |
a7d23734 | 141 | @li operator<<() |
c3c772fa RR |
142 | @li operator+=() |
143 | @li operator+() | |
144 | @li Append() | |
145 | @li Prepend() | |
146 | ||
147 | A string may be constructed either from a C string, (some number of copies of) | |
bcc8c903 | 148 | a single character or a wide (Unicode) string. For all constructors (except the |
c3c772fa RR |
149 | default which creates an empty string) there is also a corresponding assignment |
150 | operator. | |
062dc5fc | 151 | |
c3c772fa RR |
152 | @li wxString() |
153 | @li operator=() | |
b33e2f63 | 154 | @li ~wxString() |
0c339e7c | 155 | @li assign() |
c3c772fa RR |
156 | |
157 | The MakeXXX() variants modify the string in place, while the other functions | |
158 | return a new string which contains the original text converted to the upper or | |
159 | lower case and leave the original string unchanged. | |
062dc5fc | 160 | |
c3c772fa RR |
161 | @li MakeUpper() |
162 | @li Upper() | |
163 | @li MakeLower() | |
164 | @li Lower() | |
0c339e7c RR |
165 | @li MakeCapitalized() |
166 | @li Capitalize() | |
c3c772fa | 167 | |
bcc8c903 RR |
168 | Many functions below take a character index in the string. As with C |
169 | strings and arrays, the indices start from 0, so the first character of a | |
170 | string is string[0]. An attempt to access a character beyond the end of the | |
171 | string (which may even be 0 if the string is empty) will provoke an assert | |
c3c772fa RR |
172 | failure in @ref overview_debugging "debug build", but no checks are |
173 | done in release builds. | |
174 | This section also contains both implicit and explicit conversions to C style | |
0c7db140 | 175 | strings. Although implicit conversion is quite convenient, you are advised |
c73f1b33 | 176 | to use wc_str() for the sake of clarity. |
062dc5fc | 177 | |
c3c772fa RR |
178 | @li GetChar() |
179 | @li GetWritableChar() | |
180 | @li SetChar() | |
181 | @li Last() | |
b33e2f63 | 182 | @li operator[]() |
6307d716 RR |
183 | @li wc_str() |
184 | @li utf8_str() | |
c3c772fa | 185 | @li c_str() |
6307d716 | 186 | @li wx_str() |
c3c772fa | 187 | @li mb_str() |
c3c772fa | 188 | @li fn_str() |
c3c772fa | 189 | |
0c339e7c RR |
190 | The default comparison function Cmp() is case-sensitive and so is the default |
191 | version of IsSameAs(). For case insensitive comparisons you should use CmpNoCase() | |
192 | or give a second parameter to IsSameAs(). This last function is maybe more | |
c3c772fa RR |
193 | convenient if only equality of the strings matters because it returns a boolean |
194 | @true value if the strings are the same and not 0 (which is usually @false | |
bcc8c903 | 195 | in C) as Cmp() does. |
062dc5fc | 196 | Matches() is a poor man's regular expression matcher: it only understands |
c3c772fa RR |
197 | '*' and '?' metacharacters in the sense of DOS command line interpreter. |
198 | StartsWith() is helpful when parsing a line of text which should start | |
199 | with some predefined prefix and is more efficient than doing direct string | |
bcc8c903 | 200 | comparison as you would also have to precalculate the length of the prefix. |
062dc5fc | 201 | |
0c339e7c | 202 | @li compare() |
c3c772fa RR |
203 | @li Cmp() |
204 | @li CmpNoCase() | |
205 | @li IsSameAs() | |
206 | @li Matches() | |
207 | @li StartsWith() | |
208 | @li EndsWith() | |
209 | ||
210 | The string provides functions for conversion to signed and unsigned integer and | |
bcc8c903 | 211 | floating point numbers. All functions take a pointer to the variable to |
c3c772fa RR |
212 | put the numeric value in and return @true if the @b entire string could be |
213 | converted to a number. | |
062dc5fc | 214 | |
c3c772fa RR |
215 | @li ToLong() |
216 | @li ToLongLong() | |
217 | @li ToULong() | |
218 | @li ToULongLong() | |
219 | @li ToDouble() | |
220 | ||
bcc8c903 | 221 | The following are "advanced" functions and they will be needed rarely. |
c3c772fa RR |
222 | Alloc() and Shrink() are only interesting for optimization purposes. |
223 | wxStringBuffer and wxStringBufferLength classes may be very useful | |
224 | when working with some external API which requires the caller to provide | |
225 | a writable buffer. | |
062dc5fc | 226 | |
0c339e7c RR |
227 | @li reserve() |
228 | @li resize() | |
c3c772fa RR |
229 | @li Alloc() |
230 | @li Shrink() | |
231 | @li wxStringBuffer | |
232 | @li wxStringBufferLength | |
233 | ||
bcc8c903 | 234 | Miscellaneous other string functions. |
062dc5fc | 235 | |
c3c772fa RR |
236 | @li Trim() |
237 | @li Truncate() | |
238 | @li Pad() | |
239 | ||
240 | These functions return the string length and check whether the string | |
bcc8c903 | 241 | is empty or they empty it. |
062dc5fc | 242 | |
0c339e7c RR |
243 | @li length() |
244 | @li size() | |
c3c772fa RR |
245 | @li Len() |
246 | @li IsEmpty() | |
247 | @li operator!() | |
248 | @li Empty() | |
249 | @li Clear() | |
250 | ||
0c7db140 | 251 | These functions allow you to extract a substring from the string. The |
bcc8c903 | 252 | original string is not modified and the function returns the extracted |
c3c772fa | 253 | substring. |
062dc5fc | 254 | |
0c339e7c | 255 | @li substr() |
c3c772fa RR |
256 | @li Mid() |
257 | @li operator()() | |
258 | @li Left() | |
259 | @li Right() | |
260 | @li BeforeFirst() | |
261 | @li BeforeLast() | |
262 | @li AfterFirst() | |
263 | @li AfterLast() | |
264 | @li StartsWith() | |
265 | @li EndsWith() | |
266 | ||
267 | These functions replace the standard @e strchr() and @e strstr() | |
268 | functions. | |
062dc5fc | 269 | |
0c339e7c RR |
270 | @li find() |
271 | @li rfind() | |
272 | @li replace() | |
c3c772fa RR |
273 | @li Find() |
274 | @li Replace() | |
275 | ||
276 | Both formatted versions (Printf/() and stream-like insertion operators | |
277 | exist (for basic types only). Additionally, the Format() function allows | |
bcc8c903 | 278 | you to simply append a formatted value to a string: |
c3c772fa RR |
279 | |
280 | @li Format() | |
281 | @li FormatV() | |
282 | @li Printf() | |
283 | @li PrintfV() | |
284 | @li operator>>() | |
285 | ||
bcc8c903 RR |
286 | The following functions are deprecated. Please consider using new wxWidgets 2.0 |
287 | functions instead (or, even better, @c std::string compatible variants). | |
062dc5fc | 288 | |
b33e2f63 | 289 | Contains(), First(), Freq(), IsAscii(), IsNull(), |
c3c772fa RR |
290 | IsNumber(), IsWord(), Last(), Length(), LowerCase(), Remove(), Strip(), |
291 | SubString(), UpperCase() | |
292 | ||
23324ae1 FM |
293 | @library{wxbase} |
294 | @category{data} | |
7c913512 | 295 | |
23324ae1 | 296 | @stdobjects |
bcc8c903 | 297 | ::Objects, ::wxEmptyString, |
7c913512 | 298 | |
96c99165 | 299 | @see @ref overview_string "wxString overview", @ref overview_unicode |
9a6d1438 | 300 | "Unicode overview", wxUString |
23324ae1 | 301 | */ |
7c913512 | 302 | class wxString |
23324ae1 FM |
303 | { |
304 | public: | |
b33e2f63 RR |
305 | /** |
306 | An 'invalid' value for string index | |
307 | */ | |
308 | static const size_t npos; | |
309 | ||
062dc5fc | 310 | /** |
f08b2466 | 311 | @name Standard types |
b33e2f63 | 312 | */ |
f08b2466 | 313 | //@{ |
b33e2f63 RR |
314 | typedef wxUniChar value_type; |
315 | typedef wxUniChar char_type; | |
316 | typedef wxUniCharRef reference; | |
317 | typedef wxChar* pointer; | |
318 | typedef const wxChar* const_pointer; | |
319 | typedef size_t size_type; | |
320 | typedef wxUniChar const_reference; | |
321 | //@} | |
322 | ||
23324ae1 | 323 | /** |
96c99165 | 324 | Default constructor |
23324ae1 FM |
325 | */ |
326 | wxString(); | |
062dc5fc | 327 | |
96c99165 | 328 | /** |
062dc5fc | 329 | Creates a string from another string. Just increases the ref |
96c99165 RR |
330 | count by 1. |
331 | */ | |
332 | wxString(const wxString& stringSrc); | |
062dc5fc | 333 | |
96c99165 RR |
334 | |
335 | /** | |
ee28ebc0 | 336 | Constructs a string from the string literal @e psz using |
8c1cd030 | 337 | the current locale encoding to convert it to Unicode (wxConvLibc). |
96c99165 RR |
338 | */ |
339 | wxString(const char *psz); | |
340 | ||
341 | /** | |
ee28ebc0 RR |
342 | Constructs a string from the string literal @e psz using |
343 | @e conv to convert it Unicode. | |
96c99165 RR |
344 | */ |
345 | wxString(const char *psz, const wxMBConv& conv); | |
346 | ||
347 | /** | |
ee28ebc0 | 348 | Constructs a string from the first @e nLength character of the string literal @e psz using |
8c1cd030 | 349 | the current locale encoding to convert it to Unicode (wxConvLibc). |
96c99165 RR |
350 | */ |
351 | wxString(const char *psz, size_t nLength); | |
352 | ||
353 | /** | |
ee28ebc0 RR |
354 | Constructs a string from the first @e nLength character of the string literal @e psz using |
355 | @e conv to convert it Unicode. | |
96c99165 RR |
356 | */ |
357 | wxString(const char *psz, const wxMBConv& conv, size_t nLength); | |
358 | ||
359 | /** | |
ee28ebc0 | 360 | Constructs a string from the string literal @e pwz. |
96c99165 RR |
361 | */ |
362 | wxString(const wchar_t *pwz); | |
363 | ||
364 | /** | |
ee28ebc0 | 365 | Constructs a string from the first @e nLength characters of the string literal @e pwz. |
96c99165 RR |
366 | */ |
367 | wxString(const wchar_t *pwz, size_t nLength); | |
368 | ||
369 | /** | |
ee28ebc0 | 370 | Constructs a string from @e buf using the using |
96c99165 RR |
371 | the current locale encoding to convert it to Unicode. |
372 | */ | |
373 | wxString(const wxCharBuffer& buf); | |
062dc5fc | 374 | |
96c99165 | 375 | /** |
ee28ebc0 | 376 | Constructs a string from @e buf. |
96c99165 RR |
377 | */ |
378 | wxString(const wxWCharBuffer& buf); | |
379 | ||
380 | /** | |
062dc5fc | 381 | Constructs a string from @e str using the using the current locale encoding |
8c1cd030 | 382 | to convert it to Unicode (wxConvLibc). |
96c99165 RR |
383 | */ |
384 | wxString(const std::string& str); | |
062dc5fc | 385 | |
96c99165 | 386 | /** |
ee28ebc0 | 387 | Constructs a string from @e str. |
96c99165 RR |
388 | */ |
389 | wxString(const std::wstring& str); | |
062dc5fc | 390 | |
23324ae1 FM |
391 | |
392 | /** | |
393 | String destructor. Note that this is not virtual, so wxString must not be | |
394 | inherited from. | |
395 | */ | |
396 | ~wxString(); | |
397 | ||
398 | /** | |
399 | Gets all the characters after the first occurrence of @e ch. | |
ee28ebc0 | 400 | Returns the empty string if @e ch is not found. |
23324ae1 | 401 | */ |
b33e2f63 | 402 | wxString AfterFirst(wxUniChar ch) const; |
23324ae1 FM |
403 | |
404 | /** | |
405 | Gets all the characters after the last occurrence of @e ch. | |
ee28ebc0 | 406 | Returns the whole string if @e ch is not found. |
23324ae1 | 407 | */ |
b33e2f63 | 408 | wxString AfterLast(wxUniChar ch) const; |
23324ae1 FM |
409 | |
410 | /** | |
0367b928 VZ |
411 | Preallocate enough space for wxString to store @a nLen characters. |
412 | ||
413 | Please note that this method does the same thing as the standard | |
414 | reserve() one and shouldn't be used in new code. | |
415 | ||
416 | This function may be used to increase speed when the string is | |
417 | constructed by repeated concatenation as in | |
418 | ||
419 | @code | |
420 | // delete all vowels from the string | |
421 | wxString DeleteAllVowels(const wxString& original) | |
422 | { | |
423 | wxString result; | |
424 | ||
425 | size_t len = original.length(); | |
426 | ||
427 | result.Alloc(len); | |
428 | ||
429 | for ( size_t n = 0; n < len; n++ ) | |
430 | { | |
431 | if ( strchr("aeuio", tolower(original[n])) == NULL ) | |
432 | result += original[n]; | |
433 | } | |
434 | ||
435 | return result; | |
436 | } | |
437 | @endcode | |
438 | ||
439 | because it will avoid the need to reallocate string memory many times | |
440 | (in case of long strings). Note that it does not set the maximal length | |
441 | of a string -- it will still expand if more than @a nLen characters are | |
442 | stored in it. Also, it does not truncate the existing string (use | |
443 | Truncate() for this) even if its current length is greater than @a nLen. | |
444 | ||
445 | @return @true if memory was successfully allocated, @false otherwise. | |
23324ae1 | 446 | */ |
0367b928 | 447 | bool Alloc(size_t nLen); |
23324ae1 | 448 | |
23324ae1 | 449 | /** |
77da37be RR |
450 | Appends the string literal @e psz. |
451 | */ | |
452 | wxString& Append(const char* psz); | |
453 | ||
454 | /** | |
455 | Appends the wide string literal @e pwz. | |
456 | */ | |
457 | wxString& Append(const wchar_t* pwz) | |
458 | ||
459 | /** | |
460 | Appends the string literal @e psz with max length @e nLen. | |
23324ae1 | 461 | */ |
b33e2f63 | 462 | wxString& Append(const char* psz, size_t nLen); |
77da37be RR |
463 | |
464 | /** | |
465 | Appends the wide string literal @e psz with max length @e nLen. | |
466 | */ | |
b33e2f63 | 467 | wxString& Append(const wchar_t* pwz, size_t nLen) |
77da37be RR |
468 | |
469 | /** | |
470 | Appends the string @e s. | |
471 | */ | |
b33e2f63 | 472 | wxString &Append(const wxString &s); |
77da37be RR |
473 | |
474 | /** | |
475 | Appends the character @e ch @e count times. | |
476 | */ | |
b33e2f63 | 477 | wxString &Append(wxUniChar ch, size_t count = 1u); |
23324ae1 FM |
478 | |
479 | /** | |
480 | Gets all characters before the first occurrence of @e ch. | |
4cc4bfaf | 481 | Returns the whole string if @a ch is not found. |
23324ae1 | 482 | */ |
b33e2f63 | 483 | wxString BeforeFirst(wxUniChar ch) const; |
23324ae1 FM |
484 | |
485 | /** | |
486 | Gets all characters before the last occurrence of @e ch. | |
4cc4bfaf | 487 | Returns the empty string if @a ch is not found. |
23324ae1 | 488 | */ |
b33e2f63 | 489 | wxString BeforeLast(wxUniChar ch) const; |
23324ae1 | 490 | |
23324ae1 | 491 | |
0c7db140 VZ |
492 | /** |
493 | Return the copy of the string with the first string character in the | |
494 | upper case and the subsequent ones in the lower case. | |
495 | ||
496 | @since 2.9.0 | |
497 | ||
498 | @see MakeCapitalized() | |
499 | */ | |
500 | wxString Capitalize() const; | |
501 | ||
23324ae1 FM |
502 | /** |
503 | Empties the string and frees memory occupied by it. | |
23324ae1 FM |
504 | See also: Empty() |
505 | */ | |
506 | void Clear(); | |
507 | ||
06e9cf13 VS |
508 | /** |
509 | Returns a deep copy of the string. | |
510 | ||
511 | That is, the returned string is guaranteed to not share data with this | |
512 | string when using reference-counted wxString implementation. | |
513 | ||
514 | This method is primarily useful for passing strings between threads | |
515 | (because wxString is not thread-safe). Unlike creating a copy using | |
516 | @c wxString(c_str()), Clone() handles embedded NULs correctly. | |
517 | ||
518 | @since 2.9.0 | |
519 | */ | |
520 | wxString Clone() const; | |
521 | ||
23324ae1 FM |
522 | /** |
523 | Case-sensitive comparison. | |
b33e2f63 RR |
524 | Returns a positive value if the string is greater than the argument, |
525 | zero if it is equal to it or a negative value if it is less than the | |
77da37be | 526 | argument (same semantics as the standard @c strcmp() function). |
062dc5fc | 527 | |
23324ae1 FM |
528 | See also CmpNoCase(), IsSameAs(). |
529 | */ | |
328f5751 | 530 | int Cmp(const wxString& s) const; |
23324ae1 | 531 | |
23324ae1 FM |
532 | /** |
533 | Case-insensitive comparison. | |
b33e2f63 RR |
534 | Returns a positive value if the string is greater than the argument, |
535 | zero if it is equal to it or a negative value if it is less than the | |
77da37be | 536 | argument (same semantics as the standard @c strcmp() function). |
062dc5fc | 537 | |
23324ae1 FM |
538 | See also Cmp(), IsSameAs(). |
539 | */ | |
328f5751 | 540 | int CmpNoCase(const wxString& s) const; |
23324ae1 FM |
541 | |
542 | ||
543 | //@{ | |
544 | /** | |
b33e2f63 | 545 | Comparison operators |
23324ae1 FM |
546 | */ |
547 | bool operator ==(const wxString& x, const wxString& y); | |
b33e2f63 | 548 | bool operator ==(const wxString& x, wxUniChar ch); |
7c913512 | 549 | bool operator !=(const wxString& x, const wxString& y); |
b33e2f63 | 550 | bool operator !=(const wxString& x, wxUniChar ch); |
7c913512 | 551 | bool operator(const wxString& x, const wxString& y); |
b33e2f63 | 552 | bool operator(const wxString& x, wxUniChar ch); |
7c913512 | 553 | bool operator =(const wxString& x, const wxString& y); |
b33e2f63 | 554 | bool operator =(const wxString& x, wxUniChar ch); |
7c913512 | 555 | bool operator(const wxString& x, const wxString& y); |
b33e2f63 | 556 | bool operator(const wxString& x, wxUniChar ch); |
7c913512 | 557 | bool operator =(const wxString& x, const wxString& y); |
b33e2f63 | 558 | bool operator =(const wxString& x, wxUniChar ch); |
23324ae1 FM |
559 | //@} |
560 | ||
23324ae1 FM |
561 | |
562 | /** | |
563 | Returns @true if target appears anywhere in wxString; else @false. | |
23324ae1 FM |
564 | This is a wxWidgets 1.xx compatibility function; you should not use it in new |
565 | code. | |
566 | */ | |
328f5751 | 567 | bool Contains(const wxString& str) const; |
23324ae1 | 568 | |
23324ae1 FM |
569 | |
570 | /** | |
571 | Makes the string empty, but doesn't free memory occupied by the string. | |
23324ae1 FM |
572 | See also: Clear(). |
573 | */ | |
574 | void Empty(); | |
575 | ||
576 | /** | |
7c913512 | 577 | This function can be used to test if the string ends with the specified |
23324ae1 | 578 | @e suffix. If it does, the function will return @true and put the |
77da37be | 579 | beginning of the string before the suffix into @e rest string if it is not |
23324ae1 FM |
580 | @NULL. Otherwise, the function returns @false and doesn't |
581 | modify the @e rest. | |
582 | */ | |
6d95e7be | 583 | bool EndsWith(const wxString& suffix, wxString *rest = NULL) const; |
23324ae1 | 584 | |
23324ae1 | 585 | /** |
77da37be | 586 | Searches for the given character @e ch. Returns the position or |
b33e2f63 | 587 | @c wxNOT_FOUND if not found. |
23324ae1 | 588 | */ |
328f5751 | 589 | int Find(wxUniChar ch, bool fromEnd = false) const; |
062dc5fc | 590 | |
77da37be | 591 | /** |
062dc5fc | 592 | Searches for the given string @e sub. Returns the starting position or |
77da37be RR |
593 | @c wxNOT_FOUND if not found. |
594 | */ | |
b33e2f63 | 595 | int Find(const wxString& sub) const; |
23324ae1 FM |
596 | |
597 | //@{ | |
598 | /** | |
599 | Same as Find(). | |
062dc5fc | 600 | This is a wxWidgets 1.xx compatibility function; |
b33e2f63 | 601 | you should not use it in new code. |
23324ae1 | 602 | */ |
b33e2f63 RR |
603 | int First(wxUniChar ch) const; |
604 | int First(const wxString& str) const; | |
23324ae1 FM |
605 | //@} |
606 | ||
607 | /** | |
7c913512 | 608 | This static function returns the string containing the result of calling |
23324ae1 | 609 | Printf() with the passed parameters on it. |
3c4f71cc | 610 | |
4cc4bfaf | 611 | @see FormatV(), Printf() |
23324ae1 | 612 | */ |
4cc4bfaf | 613 | static wxString Format(const wxChar format, ...); |
23324ae1 FM |
614 | |
615 | /** | |
7c913512 | 616 | This static function returns the string containing the result of calling |
23324ae1 | 617 | PrintfV() with the passed parameters on it. |
3c4f71cc | 618 | |
4cc4bfaf | 619 | @see Format(), PrintfV() |
23324ae1 FM |
620 | */ |
621 | static wxString FormatV(const wxChar format, va_list argptr); | |
622 | ||
623 | /** | |
77da37be RR |
624 | Returns the number of occurrences of @e ch in the string. |
625 | This is a wxWidgets 1.xx compatibility function; you should not | |
626 | use it in new code. | |
23324ae1 | 627 | */ |
b33e2f63 | 628 | int Freq(wxUniChar ch) const; |
23324ae1 FM |
629 | |
630 | //@{ | |
631 | /** | |
70897a70 VS |
632 | Converts given buffer of binary data from 8-bit string to wxString. In |
633 | Unicode build, the string is interpreted as being in ISO-8859-1 | |
77da37be | 634 | encoding. The version without @e len parameter takes NUL-terminated |
70897a70 VS |
635 | data. |
636 | ||
637 | This is a convenience method useful when storing binary data in | |
638 | wxString. It should be used @em only for that purpose and only in | |
639 | conjunction with To8BitData(). Use mb_str() for conversion of character | |
640 | data to known encoding. | |
3c4f71cc | 641 | |
1e24c2af | 642 | @since 2.8.4 |
3c4f71cc | 643 | |
70897a70 | 644 | @see wxString::To8BitData() |
23324ae1 FM |
645 | */ |
646 | static wxString From8BitData(const char* buf, size_t len); | |
7c913512 | 647 | static wxString From8BitData(const char* buf); |
23324ae1 FM |
648 | //@} |
649 | ||
650 | //@{ | |
651 | /** | |
652 | Converts the string or character from an ASCII, 7-bit form | |
062dc5fc | 653 | to the native wxString representation. |
23324ae1 FM |
654 | */ |
655 | static wxString FromAscii(const char* s); | |
7c913512 FM |
656 | static wxString FromAscii(const unsigned char* s); |
657 | static wxString FromAscii(const char* s, size_t len); | |
658 | static wxString FromAscii(const unsigned char* s, size_t len); | |
659 | static wxString FromAscii(char c); | |
23324ae1 FM |
660 | //@} |
661 | ||
662 | //@{ | |
663 | /** | |
664 | Converts C string encoded in UTF-8 to wxString. | |
cc209a51 VZ |
665 | |
666 | If @a s is not a valid UTF-8 string, an empty string is returned. | |
667 | ||
668 | Notice that when using UTF-8 wxWidgets build there is a more efficient | |
669 | alternative to this function called FromUTF8Unchecked() which, unlike | |
670 | this one, doesn't check that the input string is valid. | |
671 | ||
672 | @since 2.8.4 | |
23324ae1 FM |
673 | */ |
674 | static wxString FromUTF8(const char* s); | |
7c913512 | 675 | static wxString FromUTF8(const char* s, size_t len); |
23324ae1 FM |
676 | //@} |
677 | ||
cc209a51 VZ |
678 | //@{ |
679 | /** | |
680 | Converts C string encoded in UTF-8 to wxString without checking its | |
681 | validity. | |
682 | ||
683 | This method assumes that @a s is a valid UTF-8 sequence and doesn't do | |
684 | any validation (although an assert failure is triggered in debug builds | |
685 | if the string is invalid). Only use it if you are absolutely sure that | |
686 | @a s is a correct UTF-8 string (e.g. because it comes from another | |
687 | library using UTF-8) and if the performance matters, otherwise use | |
688 | slower (in UTF-8 build) but safer FromUTF8(). Passing a bad UTF-8 | |
689 | string to this function will result in creating a corrupted wxString | |
690 | and all the subsequent operations on it will be undefined. | |
691 | ||
692 | @since 2.8.9 | |
693 | */ | |
694 | static wxString FromUTF8Unchecked(const char* s); | |
695 | static wxString FromUTF8Unchecked(const char* s, size_t len); | |
696 | //@} | |
697 | ||
23324ae1 | 698 | /** |
4cc4bfaf | 699 | Returns the character at position @a n (read-only). |
23324ae1 | 700 | */ |
b33e2f63 | 701 | wxUniChar GetChar(size_t n) const; |
23324ae1 FM |
702 | |
703 | /** | |
b33e2f63 | 704 | wxWidgets compatibility conversion. Same as c_str(). |
23324ae1 | 705 | */ |
b33e2f63 | 706 | const wxCStrData* GetData() const; |
23324ae1 FM |
707 | |
708 | /** | |
709 | Returns a reference to the character at position @e n. | |
710 | */ | |
b33e2f63 | 711 | wxUniCharRef GetWritableChar(size_t n); |
23324ae1 FM |
712 | |
713 | /** | |
4cc4bfaf | 714 | Returns a writable buffer of at least @a len bytes. |
23324ae1 FM |
715 | It returns a pointer to a new memory block, and the |
716 | existing data will not be copied. | |
b33e2f63 RR |
717 | Call UngetWriteBuf() as soon as possible to put the |
718 | string back into a reasonable state. | |
719 | This method is deprecated, please use wxStringBuffer or | |
23324ae1 FM |
720 | wxStringBufferLength instead. |
721 | */ | |
b33e2f63 | 722 | wxStringCharType* GetWriteBuf(size_t len); |
23324ae1 FM |
723 | |
724 | /** | |
725 | Returns @true if the string contains only ASCII characters. | |
23324ae1 FM |
726 | This is a wxWidgets 1.xx compatibility function; you should not use it in new |
727 | code. | |
728 | */ | |
328f5751 | 729 | bool IsAscii() const; |
23324ae1 FM |
730 | |
731 | /** | |
732 | Returns @true if the string is empty. | |
733 | */ | |
328f5751 | 734 | bool IsEmpty() const; |
23324ae1 FM |
735 | |
736 | /** | |
737 | Returns @true if the string is empty (same as wxString::IsEmpty). | |
23324ae1 FM |
738 | This is a wxWidgets 1.xx compatibility function; you should not use it in new |
739 | code. | |
740 | */ | |
328f5751 | 741 | bool IsNull() const; |
23324ae1 FM |
742 | |
743 | /** | |
744 | Returns @true if the string is an integer (with possible sign). | |
23324ae1 FM |
745 | This is a wxWidgets 1.xx compatibility function; you should not use it in new |
746 | code. | |
747 | */ | |
328f5751 | 748 | bool IsNumber() const; |
23324ae1 FM |
749 | |
750 | //@{ | |
751 | /** | |
752 | Test whether the string is equal to the single character @e c. The test is | |
4cc4bfaf | 753 | case-sensitive if @a caseSensitive is @true (default) or not if it is @c |
23324ae1 | 754 | @false. |
23324ae1 | 755 | Returns @true if the string is equal to the character, @false otherwise. |
23324ae1 FM |
756 | See also Cmp(), CmpNoCase() |
757 | */ | |
b33e2f63 RR |
758 | bool IsSameAs(const wxString &s, bool caseSensitive = true) const; |
759 | bool IsSameAs(wxUniChar ch, bool caseSensitive = true) const; | |
23324ae1 FM |
760 | //@} |
761 | ||
762 | /** | |
763 | Returns @true if the string is a word. | |
23324ae1 FM |
764 | This is a wxWidgets 1.xx compatibility function; you should not use it in new |
765 | code. | |
766 | */ | |
328f5751 | 767 | bool IsWord() const; |
23324ae1 FM |
768 | |
769 | //@{ | |
770 | /** | |
771 | Returns a reference to the last character (writable). | |
062dc5fc | 772 | This is a wxWidgets 1.xx compatibility function; |
b33e2f63 | 773 | you should not use it in new code. |
23324ae1 | 774 | */ |
b33e2f63 RR |
775 | wxUniCharRef Last(); |
776 | const wxUniChar Last(); | |
23324ae1 FM |
777 | //@} |
778 | ||
779 | /** | |
4cc4bfaf | 780 | Returns the first @a count characters of the string. |
23324ae1 | 781 | */ |
328f5751 | 782 | wxString Left(size_t count) const; |
23324ae1 FM |
783 | |
784 | /** | |
785 | Returns the length of the string. | |
786 | */ | |
328f5751 | 787 | size_t Len() const; |
23324ae1 FM |
788 | |
789 | /** | |
790 | Returns the length of the string (same as Len). | |
23324ae1 FM |
791 | This is a wxWidgets 1.xx compatibility function; you should not use it in new |
792 | code. | |
793 | */ | |
328f5751 | 794 | size_t Length() const; |
23324ae1 FM |
795 | |
796 | /** | |
797 | Returns this string converted to the lower case. | |
0c7db140 VZ |
798 | |
799 | @see MakeLower() | |
23324ae1 | 800 | */ |
328f5751 | 801 | wxString Lower() const; |
23324ae1 FM |
802 | |
803 | /** | |
804 | Same as MakeLower. | |
23324ae1 FM |
805 | This is a wxWidgets 1.xx compatibility function; you should not use it in new |
806 | code. | |
807 | */ | |
808 | void LowerCase(); | |
809 | ||
0c7db140 VZ |
810 | /** |
811 | Converts the first characters of the string to the upper case and all | |
812 | the subsequent ones to the lower case and returns the result. | |
813 | ||
814 | @since 2.9.0 | |
815 | ||
816 | @see Capitalize() | |
817 | */ | |
818 | wxString& MakeCapitalized(); | |
819 | ||
23324ae1 FM |
820 | /** |
821 | Converts all characters to lower case and returns the result. | |
0c7db140 VZ |
822 | |
823 | @see Lower() | |
23324ae1 | 824 | */ |
b33e2f63 | 825 | wxString& MakeLower(); |
23324ae1 FM |
826 | |
827 | /** | |
828 | Converts all characters to upper case and returns the result. | |
0c7db140 VZ |
829 | |
830 | @see Upper() | |
23324ae1 | 831 | */ |
b33e2f63 | 832 | wxString& MakeUpper(); |
23324ae1 FM |
833 | |
834 | /** | |
835 | Returns @true if the string contents matches a mask containing '*' and '?'. | |
836 | */ | |
328f5751 | 837 | bool Matches(const wxString& mask) const; |
23324ae1 | 838 | |
23324ae1 FM |
839 | /** |
840 | Returns a substring starting at @e first, with length @e count, or the rest of | |
4cc4bfaf | 841 | the string if @a count is the default value. |
23324ae1 | 842 | */ |
328f5751 | 843 | wxString Mid(size_t first, size_t count = wxSTRING_MAXLEN) const; |
23324ae1 | 844 | |
23324ae1 FM |
845 | |
846 | /** | |
c3c772fa RR |
847 | Adds @a count copies of @a pad to the beginning, or to the end of the |
848 | string (the default). Removes spaces from the left or from the right (default). | |
23324ae1 | 849 | */ |
b33e2f63 | 850 | wxString& Pad(size_t count, wxUniChar pad = ' ', |
4cc4bfaf | 851 | bool fromRight = true); |
23324ae1 FM |
852 | |
853 | /** | |
4cc4bfaf | 854 | Prepends @a str to this string, returning a reference to this string. |
23324ae1 | 855 | */ |
b33e2f63 | 856 | wxString& Prepend(const wxString& str); |
23324ae1 FM |
857 | |
858 | /** | |
859 | Similar to the standard function @e sprintf(). Returns the number of | |
860 | characters written, or an integer less than zero on error. | |
23324ae1 FM |
861 | Note that if @c wxUSE_PRINTF_POS_PARAMS is set to 1, then this function supports |
862 | Unix98-style positional parameters: | |
3c4f71cc | 863 | |
1f1d2182 | 864 | @note This function will use a safe version of @e vsprintf() (usually called |
23324ae1 FM |
865 | @e vsnprintf()) whenever available to always allocate the buffer of correct |
866 | size. Unfortunately, this function is not available on all platforms and the | |
867 | dangerous @e vsprintf() will be used then which may lead to buffer overflows. | |
868 | */ | |
4cc4bfaf | 869 | int Printf(const wxChar* pszFormat, ...); |
23324ae1 FM |
870 | |
871 | /** | |
872 | Similar to vprintf. Returns the number of characters written, or an integer | |
873 | less than zero | |
874 | on error. | |
875 | */ | |
876 | int PrintfV(const wxChar* pszFormat, va_list argPtr); | |
877 | ||
878 | //@{ | |
879 | /** | |
4cc4bfaf | 880 | Removes @a len characters from the string, starting at @e pos. |
23324ae1 FM |
881 | This is a wxWidgets 1.xx compatibility function; you should not use it in new |
882 | code. | |
883 | */ | |
884 | wxString Remove(size_t pos); | |
7c913512 | 885 | wxString Remove(size_t pos, size_t len); |
23324ae1 FM |
886 | //@} |
887 | ||
888 | /** | |
889 | Removes the last character. | |
890 | */ | |
891 | wxString RemoveLast(); | |
892 | ||
893 | /** | |
894 | Replace first (or all) occurrences of substring with another one. | |
23324ae1 | 895 | @e replaceAll: global replace (default), or only the first occurrence. |
23324ae1 FM |
896 | Returns the number of replacements made. |
897 | */ | |
898 | size_t Replace(const wxString& strOld, const wxString& strNew, | |
4cc4bfaf | 899 | bool replaceAll = true); |
23324ae1 FM |
900 | |
901 | /** | |
4cc4bfaf | 902 | Returns the last @a count characters. |
23324ae1 | 903 | */ |
328f5751 | 904 | wxString Right(size_t count) const; |
23324ae1 | 905 | |
23324ae1 FM |
906 | /** |
907 | Sets the character at position @e n. | |
908 | */ | |
b33e2f63 | 909 | void SetChar(size_t n, wxUniChar ch); |
23324ae1 FM |
910 | |
911 | /** | |
7c913512 | 912 | Minimizes the string's memory. This can be useful after a call to |
23324ae1 FM |
913 | Alloc() if too much memory were preallocated. |
914 | */ | |
915 | void Shrink(); | |
916 | ||
917 | /** | |
7c913512 | 918 | This function can be used to test if the string starts with the specified |
23324ae1 | 919 | @e prefix. If it does, the function will return @true and put the rest |
4cc4bfaf | 920 | of the string (i.e. after the prefix) into @a rest string if it is not |
23324ae1 FM |
921 | @NULL. Otherwise, the function returns @false and doesn't modify the |
922 | @e rest. | |
923 | */ | |
6d95e7be | 924 | bool StartsWith(const wxString& prefix, wxString *rest = NULL) const; |
23324ae1 | 925 | |
23324ae1 FM |
926 | /** |
927 | Strip characters at the front and/or end. The same as Trim except that it | |
928 | doesn't change this string. | |
23324ae1 FM |
929 | This is a wxWidgets 1.xx compatibility function; you should not use it in new |
930 | code. | |
931 | */ | |
328f5751 | 932 | wxString Strip(stripType s = trailing) const; |
23324ae1 FM |
933 | |
934 | /** | |
4cc4bfaf | 935 | Returns the part of the string between the indices @a from and @e to |
23324ae1 | 936 | inclusive. |
23324ae1 FM |
937 | This is a wxWidgets 1.xx compatibility function, use Mid() |
938 | instead (but note that parameters have different meaning). | |
939 | */ | |
328f5751 | 940 | wxString SubString(size_t from, size_t to) const; |
23324ae1 | 941 | |
23324ae1 FM |
942 | //@{ |
943 | /** | |
70897a70 VS |
944 | Converts the string to an 8-bit string in ISO-8859-1 encoding in the |
945 | form of a wxCharBuffer (Unicode builds only). | |
946 | ||
947 | This is a convenience method useful when storing binary data in | |
948 | wxString. It should be used @em only for this purpose. It is only valid | |
949 | to call this method on strings created using From8BitData(). | |
3c4f71cc | 950 | |
1e24c2af | 951 | @since 2.8.4 |
3c4f71cc | 952 | |
70897a70 | 953 | @see wxString::From8BitData() |
23324ae1 | 954 | */ |
328f5751 | 955 | const char* To8BitData() const; |
8c1cd030 | 956 | const wxCharBuffer To8BitData() const; |
23324ae1 FM |
957 | //@} |
958 | ||
959 | //@{ | |
960 | /** | |
961 | Converts the string to an ASCII, 7-bit string in the form of | |
962 | a wxCharBuffer (Unicode builds only) or a C string (ANSI builds). | |
23324ae1 | 963 | Note that this conversion only works if the string contains only ASCII |
bcc8c903 | 964 | characters. The @ref mb_str() "mb_str" method provides more |
23324ae1 FM |
965 | powerful means of converting wxString to C string. |
966 | */ | |
328f5751 | 967 | const char* ToAscii() const; |
8c1cd030 | 968 | const wxCharBuffer ToAscii() const; |
23324ae1 FM |
969 | //@} |
970 | ||
971 | /** | |
972 | Attempts to convert the string to a floating point number. Returns @true on | |
973 | success (the number is stored in the location pointed to by @e val) or @false | |
4cc4bfaf | 974 | if the string does not represent such number (the value of @a val is not |
23324ae1 | 975 | modified in this case). |
3c4f71cc | 976 | |
4cc4bfaf | 977 | @see ToLong(), ToULong() |
23324ae1 | 978 | */ |
328f5751 | 979 | bool ToDouble(double val) const; |
23324ae1 FM |
980 | |
981 | /** | |
982 | Attempts to convert the string to a signed integer in base @e base. Returns | |
983 | @true on success in which case the number is stored in the location | |
4cc4bfaf FM |
984 | pointed to by @a val or @false if the string does not represent a |
985 | valid number in the given base (the value of @a val is not modified | |
23324ae1 | 986 | in this case). |
4cc4bfaf | 987 | The value of @a base must be comprised between 2 and 36, inclusive, or |
23324ae1 FM |
988 | be a special value 0 which means that the usual rules of @c C numbers are |
989 | applied: if the number starts with @c 0x it is considered to be in base | |
990 | 16, if it starts with @c 0 - in base 8 and in base 10 otherwise. Note | |
991 | that you may not want to specify the base 0 if you are parsing the numbers | |
992 | which may have leading zeroes as they can yield unexpected (to the user not | |
993 | familiar with C) results. | |
3c4f71cc | 994 | |
4cc4bfaf | 995 | @see ToDouble(), ToULong() |
23324ae1 | 996 | */ |
328f5751 | 997 | bool ToLong(long val, int base = 10) const; |
23324ae1 FM |
998 | |
999 | /** | |
1000 | This is exactly the same as ToLong() but works with 64 | |
1001 | bit integer numbers. | |
23324ae1 FM |
1002 | Notice that currently it doesn't work (always returns @false) if parsing of 64 |
1003 | bit numbers is not supported by the underlying C run-time library. Compilers | |
1004 | with C99 support and Microsoft Visual C++ version 7 and higher do support this. | |
3c4f71cc | 1005 | |
4cc4bfaf | 1006 | @see ToLong(), ToULongLong() |
23324ae1 | 1007 | */ |
328f5751 | 1008 | bool ToLongLong(wxLongLong_t val, int base = 10) const; |
23324ae1 FM |
1009 | |
1010 | /** | |
1011 | Attempts to convert the string to an unsigned integer in base @e base. | |
1012 | Returns @true on success in which case the number is stored in the | |
4cc4bfaf FM |
1013 | location pointed to by @a val or @false if the string does not |
1014 | represent a valid number in the given base (the value of @a val is not | |
23324ae1 FM |
1015 | modified in this case). Please notice that this function |
1016 | behaves in the same way as the standard @c strtoul() and so it simply | |
1017 | converts negative numbers to unsigned representation instead of rejecting them | |
1018 | (e.g. -1 is returned as @c ULONG_MAX). | |
23324ae1 | 1019 | See ToLong() for the more detailed |
4cc4bfaf | 1020 | description of the @a base parameter. |
3c4f71cc | 1021 | |
4cc4bfaf | 1022 | @see ToDouble(), ToLong() |
23324ae1 | 1023 | */ |
328f5751 | 1024 | bool ToULong(unsigned long val, int base = 10) const; |
23324ae1 FM |
1025 | |
1026 | /** | |
1027 | This is exactly the same as ToULong() but works with 64 | |
1028 | bit integer numbers. | |
23324ae1 FM |
1029 | Please see ToLongLong() for additional remarks. |
1030 | */ | |
328f5751 | 1031 | bool ToULongLong(wxULongLong_t val, int base = 10) const; |
23324ae1 FM |
1032 | |
1033 | //@{ | |
1034 | /** | |
b33e2f63 | 1035 | Same as utf8_str(). |
23324ae1 | 1036 | */ |
328f5751 | 1037 | const char* ToUTF8() const; |
c73f1b33 | 1038 | const wxCharBuffer ToUTF8() const; |
23324ae1 FM |
1039 | //@} |
1040 | ||
1041 | /** | |
1042 | Removes white-space (space, tabs, form feed, newline and carriage return) from | |
1043 | the left or from the right end of the string (right is default). | |
1044 | */ | |
b33e2f63 | 1045 | wxString& Trim(bool fromRight = true); |
23324ae1 FM |
1046 | |
1047 | /** | |
1048 | Truncate the string to the given length. | |
1049 | */ | |
b33e2f63 | 1050 | wxString& Truncate(size_t len); |
23324ae1 FM |
1051 | |
1052 | //@{ | |
1053 | /** | |
1054 | Puts the string back into a reasonable state (in which it can be used | |
1055 | normally), after | |
1056 | GetWriteBuf() was called. | |
4cc4bfaf | 1057 | The version of the function without the @a len parameter will calculate the |
23324ae1 FM |
1058 | new string length itself assuming that the string is terminated by the first |
1059 | @c NUL character in it while the second one will use the specified length | |
1060 | and thus is the only version which should be used with the strings with | |
7c913512 | 1061 | embedded @c NULs (it is also slightly more efficient as @c strlen() |
23324ae1 | 1062 | doesn't have to be called). |
23324ae1 FM |
1063 | This method is deprecated, please use |
1064 | wxStringBuffer or | |
1065 | wxStringBufferLength instead. | |
1066 | */ | |
1067 | void UngetWriteBuf(); | |
7c913512 | 1068 | void UngetWriteBuf(size_t len); |
23324ae1 FM |
1069 | //@} |
1070 | ||
1071 | /** | |
1072 | Returns this string converted to upper case. | |
0c7db140 VZ |
1073 | |
1074 | @see MakeUpper() | |
23324ae1 | 1075 | */ |
328f5751 | 1076 | wxString Upper() const; |
23324ae1 FM |
1077 | |
1078 | /** | |
1079 | The same as MakeUpper. | |
23324ae1 FM |
1080 | This is a wxWidgets 1.xx compatibility function; you should not use it in new |
1081 | code. | |
1082 | */ | |
1083 | void UpperCase(); | |
1084 | ||
23324ae1 | 1085 | /** |
b2ceedad RR |
1086 | Returns a lightweight intermediate class which is in turn implicitly |
1087 | convertible to both @c const @c char* and to @c const @c wchar_t*. | |
6307d716 RR |
1088 | Given this ambiguity it is mostly better to use wc_str(), mb_str() or |
1089 | utf8_str() instead. | |
0c7db140 | 1090 | |
b2ceedad RR |
1091 | Please see the @ref overview_unicode "Unicode overview" for more |
1092 | information about it. | |
0c7db140 | 1093 | |
23324ae1 | 1094 | Note that the returned value is not convertible to @c char* or |
a456700f RR |
1095 | @c wchar_t*, use char_str() or wchar_str() if you need to pass |
1096 | string value to a function expecting non-const pointer. | |
0c7db140 | 1097 | |
6307d716 | 1098 | @see wc_str(), utf8_str(), c_str(), mb_str(), fn_str() |
23324ae1 | 1099 | */ |
b33e2f63 | 1100 | const wxCStrData c_str() const; |
23324ae1 FM |
1101 | |
1102 | /** | |
1103 | Returns an object with string data that is implicitly convertible to | |
1104 | @c char* pointer. Note that any change to the returned buffer is lost and so | |
1105 | this function is only usable for passing strings to legacy libraries that | |
a456700f RR |
1106 | don't have const-correct API. Use wxStringBuffer if you want to modify |
1107 | the string. | |
3c4f71cc | 1108 | |
a456700f | 1109 | @see c_str() |
23324ae1 | 1110 | */ |
328f5751 | 1111 | wxWritableCharBuffer char_str(const wxMBConv& conv = wxConvLibc) const; |
23324ae1 | 1112 | |
062dc5fc VZ |
1113 | /** |
1114 | Returns buffer of the specified type containing the string data. | |
1115 | ||
1116 | This method is only useful in template code, otherwise you should | |
1117 | directly call mb_str() or wc_str() if you need to retrieve a narrow or | |
1118 | wide string from this wxString. The template parameter @a t should be | |
1119 | either @c char or @c wchar_t. | |
1120 | ||
1121 | Notice that retrieving a char buffer in UTF-8 build will return the | |
1122 | internal string representation in UTF-8 while in wchar_t build the char | |
1123 | buffer will contain the conversion of the string to the encoding of the | |
1124 | current locale (and so can fail). | |
1125 | ||
1126 | @param len If non-@NULL, filled with the length of the returned buffer. | |
1127 | @return | |
1128 | buffer containing the string contents in the specified type, | |
1129 | notice that it may be @NULL if the conversion failed (e.g. Unicode | |
1130 | string couldn't be converted to the current encoding when @a T is | |
1131 | @c char). | |
1132 | */ | |
1133 | template <typename T> | |
1134 | wxCharTypeBuffer<T> tchar_str(size_t *len = NULL) const; | |
1135 | ||
23324ae1 FM |
1136 | //@{ |
1137 | /** | |
b33e2f63 | 1138 | Returns string representation suitable for passing to OS' functions |
062dc5fc | 1139 | for file handling. |
23324ae1 | 1140 | */ |
328f5751 | 1141 | const wchar_t* fn_str() const; |
b33e2f63 RR |
1142 | const char* fn_str() const; |
1143 | const wxCharBuffer fn_str() const; | |
23324ae1 FM |
1144 | //@} |
1145 | ||
23324ae1 | 1146 | /** |
6307d716 | 1147 | Returns the multibyte (C string) representation of the string |
0c7db140 VZ |
1148 | using @e conv's wxMBConv::cWC2MB method and returns wxCharBuffer. |
1149 | ||
6307d716 | 1150 | @see wc_str(), utf8_str(), c_str(), wxMBConv |
23324ae1 | 1151 | */ |
8c1cd030 | 1152 | const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const; |
23324ae1 FM |
1153 | |
1154 | /** | |
1155 | Extraction from a stream. | |
1156 | */ | |
bcc8c903 RR |
1157 | friend istream operator>>(istream& is, wxString& str); |
1158 | ||
1159 | /** | |
1160 | These functions work as C++ stream insertion operators. They insert the | |
1161 | given value into the string. Precision and format cannot be set using them. | |
1162 | Use Printf() instead. | |
1163 | */ | |
1164 | wxString& operator<<(const wxString& s); | |
1165 | wxString& operator<<(const char* psz) | |
1166 | wxString& operator<<(const wchar_t* pwz) | |
1167 | wxString& operator<<(const wxCStrData& psz) | |
1168 | wxString& operator<<(wxUniChar ch); | |
1169 | wxString& operator<<(wxUniCharRef ch) | |
1170 | wxString& operator<<(char ch) | |
1171 | wxString& operator<<(unsigned char ch) | |
1172 | wxString& operator<<(wchar_t ch) | |
1173 | wxString& operator<<(const wxCharBuffer& s) | |
1174 | wxString& operator<<(const wxWCharBuffer& s) | |
1175 | wxString& operator<<(int i); | |
1176 | wxString& operator<<(unsigned int ui); | |
1177 | wxString& operator<<(long l); | |
1178 | wxString& operator<<(unsigned long ul); | |
1179 | wxString& operator<<(wxLongLong_t ll); | |
1180 | wxString& operator<<(wxULongLong_t ul); | |
1181 | wxString& operator<<(float f); | |
1182 | wxString& operator<<(double d); | |
23324ae1 FM |
1183 | |
1184 | /** | |
1185 | Same as Mid (substring extraction). | |
1186 | */ | |
1187 | wxString operator ()(size_t start, size_t len); | |
1188 | ||
1189 | //@{ | |
1190 | /** | |
b33e2f63 | 1191 | Concatenation: these operators return a new string equal to the |
23324ae1 FM |
1192 | concatenation of the operands. |
1193 | */ | |
1194 | wxString operator +(const wxString& x, const wxString& y); | |
b33e2f63 | 1195 | wxString operator +(const wxString& x, wxUniChar y); |
23324ae1 FM |
1196 | //@} |
1197 | ||
1198 | //@{ | |
1199 | /** | |
1200 | Concatenation in place: the argument is appended to the string. | |
1201 | */ | |
1202 | void operator +=(const wxString& str); | |
b33e2f63 | 1203 | void operator +=(wxUniChar c); |
23324ae1 FM |
1204 | //@} |
1205 | ||
1206 | //@{ | |
1207 | /** | |
1208 | Assignment: the effect of each operation is the same as for the corresponding | |
bcc8c903 | 1209 | constructor (see @ref wxString() "wxString constructors"). |
23324ae1 FM |
1210 | */ |
1211 | wxString operator =(const wxString& str); | |
b33e2f63 | 1212 | wxString operator =(wxUniChar c); |
23324ae1 FM |
1213 | //@} |
1214 | ||
1215 | //@{ | |
1216 | /** | |
1217 | Element extraction. | |
1218 | */ | |
b33e2f63 RR |
1219 | wxUniChar operator [](size_t i) const; |
1220 | wxUniCharRef operator [](size_t i); | |
23324ae1 FM |
1221 | //@} |
1222 | ||
1223 | /** | |
b33e2f63 RR |
1224 | Empty string is @false, so !string will only return @true if the |
1225 | string is empty. | |
062dc5fc | 1226 | |
23324ae1 FM |
1227 | See also IsEmpty(). |
1228 | */ | |
328f5751 | 1229 | bool operator!() const; |
23324ae1 | 1230 | |
23324ae1 FM |
1231 | |
1232 | //@{ | |
1233 | /** | |
b33e2f63 RR |
1234 | Converts the strings contents to UTF-8 and returns it either as a |
1235 | temporary wxCharBuffer object or as a pointer to the internal | |
1236 | string contents in UTF-8 build. | |
0c7db140 | 1237 | |
6307d716 | 1238 | @see wc_str(), c_str(), mb_str() |
23324ae1 | 1239 | */ |
328f5751 | 1240 | const char* utf8_str() const; |
b33e2f63 | 1241 | const wxCharBuffer utf8_str() const; |
23324ae1 FM |
1242 | //@} |
1243 | ||
1244 | //@{ | |
1245 | /** | |
b33e2f63 | 1246 | Converts the strings contents to the wide character represention |
0c7db140 | 1247 | and returns it as a temporary wxWCharBuffer object (Unix and OS X) |
6307d716 RR |
1248 | or returns a pointer to the internal string contents in wide character |
1249 | mode (Windows). | |
062dc5fc | 1250 | |
c3c772fa RR |
1251 | The macro wxWX2WCbuf is defined as the correct return |
1252 | type (without const). | |
3c4f71cc | 1253 | |
c73f1b33 | 1254 | @see utf8_str(), c_str(), mb_str(), fn_str(), wchar_str() |
23324ae1 | 1255 | */ |
b33e2f63 RR |
1256 | const wchar_t* wc_str() const; |
1257 | const wxWCharBuffer wc_str() const; | |
23324ae1 FM |
1258 | //@} |
1259 | ||
1260 | /** | |
1261 | Returns an object with string data that is implicitly convertible to | |
1262 | @c char* pointer. Note that changes to the returned buffer may or may | |
1263 | not be lost (depending on the build) and so this function is only usable for | |
1264 | passing strings to legacy libraries that don't have const-correct API. Use | |
1265 | wxStringBuffer if you want to modify the string. | |
3c4f71cc | 1266 | |
c3c772fa | 1267 | @see mb_str(), wc_str(), fn_str(), c_str(), char_str() |
23324ae1 | 1268 | */ |
328f5751 | 1269 | wxWritableWCharBuffer wchar_str() const; |
23324ae1 | 1270 | |
0c7db140 | 1271 | /** |
6307d716 RR |
1272 | Explicit conversion to C string in the internal representation (either |
1273 | wchar_t* or UTF-8-encoded char*, depending on the build). | |
1274 | */ | |
1275 | const wxStringCharType *wx_str() const; | |
1276 | ||
1277 | ||
b33e2f63 | 1278 | /** |
e846cf87 | 1279 | @name Iterator interface |
062dc5fc | 1280 | |
f08b2466 RR |
1281 | These methods return iterators to the beginnnig or |
1282 | end of the string. | |
b33e2f63 RR |
1283 | */ |
1284 | //@{ | |
1285 | const_iterator begin() const; | |
1286 | iterator begin(); | |
1287 | const_iterator end() const; | |
1288 | iterator end(); | |
1289 | ||
1290 | const_reverse_iterator rbegin() const; | |
1291 | reverse_iterator rbegin(); | |
1292 | const_reverse_iterator rend() const; | |
1293 | reverse_iterator rend(); | |
f08b2466 | 1294 | //@} |
b33e2f63 | 1295 | |
f08b2466 RR |
1296 | /** |
1297 | @name STL interface | |
062dc5fc VZ |
1298 | |
1299 | The supported STL functions are listed here. Please see any | |
f08b2466 RR |
1300 | STL reference for their documentation. |
1301 | */ | |
1302 | //@{ | |
b33e2f63 RR |
1303 | size_t length() const; |
1304 | size_type size() const; | |
1305 | size_type max_size() const; | |
1306 | size_type capacity() const; | |
1307 | void reserve(size_t sz); | |
1308 | ||
e846cf87 | 1309 | void resize(size_t nSize, wxUniChar ch = '\0'); |
b33e2f63 RR |
1310 | |
1311 | wxString& append(const wxString& str, size_t pos, size_t n); | |
1312 | wxString& append(const wxString& str); | |
1313 | wxString& append(const char *sz, size_t n); | |
e846cf87 | 1314 | wxString& append(const wchar_t *sz, size_t n); |
b33e2f63 RR |
1315 | wxString& append(size_t n, wxUniChar ch); |
1316 | wxString& append(const_iterator first, const_iterator last); | |
1317 | ||
1318 | wxString& assign(const wxString& str, size_t pos, size_t n); | |
1319 | wxString& assign(const wxString& str); | |
1320 | wxString& assign(const char *sz, size_t n); | |
1321 | wxString& assign(const wchar_t *sz, size_t n); | |
1322 | wxString& assign(size_t n, wxUniChar ch); | |
1323 | wxString& assign(const_iterator first, const_iterator last); | |
1324 | ||
1325 | void clear(); | |
062dc5fc | 1326 | |
b33e2f63 RR |
1327 | int compare(const wxString& str) const; |
1328 | int compare(size_t nStart, size_t nLen, const wxString& str) const; | |
1329 | int compare(size_t nStart, size_t nLen, | |
1330 | const wxString& str, size_t nStart2, size_t nLen2) const; | |
1331 | int compare(size_t nStart, size_t nLen, | |
1332 | const char* sz, size_t nCount = npos) const; | |
1333 | int compare(size_t nStart, size_t nLen, | |
1334 | const wchar_t* sz, size_t nCount = npos) const; | |
1335 | ||
1336 | bool empty() const; | |
1337 | ||
1338 | wxString& erase(size_type pos = 0, size_type n = npos); | |
1339 | iterator erase(iterator first, iterator last); | |
1340 | iterator erase(iterator first); | |
1341 | ||
1342 | size_t find(const wxString& str, size_t nStart = 0) const; | |
1343 | size_t find(const char* sz, size_t nStart = 0, size_t n = npos) const; | |
1344 | size_t find(const wchar_t* sz, size_t nStart = 0, size_t n = npos) const; | |
1345 | size_t find(wxUniChar ch, size_t nStart = 0) const; | |
1346 | ||
1347 | wxString& insert(size_t nPos, const wxString& str); | |
1348 | wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n); | |
1349 | wxString& insert(size_t nPos, const char *sz, size_t n); | |
1350 | wxString& insert(size_t nPos, const wchar_t *sz, size_t n); | |
1351 | wxString& insert(size_t nPos, size_t n, wxUniChar ch); | |
1352 | iterator insert(iterator it, wxUniChar ch); | |
1353 | void insert(iterator it, const_iterator first, const_iterator last); | |
1354 | void insert(iterator it, size_type n, wxUniChar ch); | |
1355 | ||
1356 | wxString& replace(size_t nStart, size_t nLen, const wxString& str); | |
1357 | wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch); | |
1358 | wxString& replace(size_t nStart, size_t nLen, | |
1359 | const wxString& str, size_t nStart2, size_t nLen2); | |
1360 | wxString& replace(size_t nStart, size_t nLen, | |
1361 | const char* sz, size_t nCount); | |
1362 | wxString& replace(size_t nStart, size_t nLen, | |
1363 | const wchar_t* sz, size_t nCount); | |
1364 | wxString& replace(size_t nStart, size_t nLen, | |
1365 | const wxString& s, size_t nCount); | |
1366 | wxString& replace(iterator first, iterator last, const wxString& s); | |
1367 | wxString& replace(iterator first, iterator last, const char* s, size_type n); | |
1368 | wxString& replace(iterator first, iterator last, const wchar_t* s, size_type n); | |
1369 | wxString& replace(iterator first, iterator last, size_type n, wxUniChar ch); | |
1370 | wxString& replace(iterator first, iterator last, | |
1371 | const_iterator first1, const_iterator last1); | |
1372 | wxString& replace(iterator first, iterator last, | |
1373 | const char *first1, const char *last1); | |
1374 | wxString& replace(iterator first, iterator last, | |
f08b2466 | 1375 | const wchar_t *first1, const wchar_t *last1); |
b33e2f63 RR |
1376 | |
1377 | size_t rfind(const wxString& str, size_t nStart = npos) const; | |
1378 | size_t rfind(const char* sz, size_t nStart = npos, size_t n = npos) const; | |
1379 | size_t rfind(const wchar_t* sz, size_t nStart = npos, size_t n = npos) const; | |
1380 | size_t rfind(wxUniChar ch, size_t nStart = npos) const; | |
1381 | ||
1382 | wxString substr(size_t nStart = 0, size_t nLen = npos) const; | |
062dc5fc | 1383 | |
f08b2466 | 1384 | void swap(wxString& str); |
062dc5fc | 1385 | |
b33e2f63 RR |
1386 | //@} |
1387 | ||
23324ae1 FM |
1388 | }; |
1389 | ||
1390 | ||
e54c96f1 FM |
1391 | /** |
1392 | FIXME | |
1393 | */ | |
1394 | wxString Objects: | |
1395 | ; | |
1396 | ||
1397 | /** | |
1398 | FIXME | |
1399 | */ | |
1400 | wxString wxEmptyString; | |
1401 | ||
1402 | ||
1403 | ||
1404 | ||
23324ae1 FM |
1405 | /** |
1406 | @class wxStringBufferLength | |
7c913512 | 1407 | |
bcc8c903 | 1408 | This tiny class allows you to conveniently access the wxString |
23324ae1 FM |
1409 | internal buffer as a writable pointer without any risk of forgetting to restore |
1410 | the string to the usable state later, and allows the user to set the internal | |
1411 | length of the string. | |
7c913512 FM |
1412 | |
1413 | For example, assuming you have a low-level OS function called | |
23324ae1 FM |
1414 | @c int GetMeaningOfLifeAsString(char *) copying the value in the provided |
1415 | buffer (which must be writable, of course), and returning the actual length | |
1416 | of the string, you might call it like this: | |
7c913512 | 1417 | |
23324ae1 FM |
1418 | @code |
1419 | wxString theAnswer; | |
1420 | wxStringBuffer theAnswerBuffer(theAnswer, 1024); | |
1421 | int nLength = GetMeaningOfLifeAsString(theAnswerBuffer); | |
1422 | theAnswerBuffer.SetLength(nLength); | |
1423 | if ( theAnswer != "42" ) | |
1424 | { | |
1425 | wxLogError("Something is very wrong!"); | |
1426 | } | |
1427 | @endcode | |
7c913512 | 1428 | |
bcc8c903 | 1429 | Note that the exact usage of this depends on whether or not wxUSE_STL is |
0c7db140 | 1430 | enabled. If wxUSE_STL is enabled, wxStringBuffer creates a separate empty |
bcc8c903 | 1431 | character buffer, and if wxUSE_STL is disabled, it uses GetWriteBuf() from |
0c7db140 VZ |
1432 | wxString, keeping the same buffer wxString uses intact. In other words, |
1433 | relying on wxStringBuffer containing the old wxString data is not a good | |
bcc8c903 | 1434 | idea if you want to build your program both with and without wxUSE_STL. |
7c913512 | 1435 | |
23324ae1 | 1436 | Note that SetLength @c must be called before wxStringBufferLength destructs. |
7c913512 | 1437 | |
23324ae1 | 1438 | @library{wxbase} |
bcc8c903 | 1439 | @category{data} |
23324ae1 | 1440 | */ |
7c913512 | 1441 | class wxStringBufferLength |
23324ae1 FM |
1442 | { |
1443 | public: | |
1444 | /** | |
1445 | Constructs a writable string buffer object associated with the given string | |
4cc4bfaf | 1446 | and containing enough space for at least @a len characters. Basically, this |
23324ae1 FM |
1447 | is equivalent to calling wxString::GetWriteBuf and |
1448 | saving the result. | |
1449 | */ | |
1450 | wxStringBufferLength(const wxString& str, size_t len); | |
1451 | ||
1452 | /** | |
7c913512 | 1453 | Restores the string passed to the constructor to the usable state by calling |
23324ae1 FM |
1454 | wxString::UngetWriteBuf on it. |
1455 | */ | |
1456 | ~wxStringBufferLength(); | |
1457 | ||
1458 | /** | |
7c913512 | 1459 | Sets the internal length of the string referred to by wxStringBufferLength to |
4cc4bfaf | 1460 | @a nLength characters. |
23324ae1 FM |
1461 | Must be called before wxStringBufferLength destructs. |
1462 | */ | |
1463 | void SetLength(size_t nLength); | |
1464 | ||
1465 | /** | |
1466 | Returns the writable pointer to a buffer of the size at least equal to the | |
1467 | length specified in the constructor. | |
1468 | */ | |
4cc4bfaf | 1469 | wxChar* operator wxChar *(); |
23324ae1 FM |
1470 | }; |
1471 |