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