]> git.saurik.com Git - wxWidgets.git/blame - interface/string.h
compilation fix for ANSI build: use reinterptre_, not static_cast, to convert from...
[wxWidgets.git] / interface / string.h
CommitLineData
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
11 @wxheader{string.h}
7c913512
FM
12
13 This tiny class allows to conveniently access the wxString
23324ae1
FM
14 internal buffer as a writable pointer without any risk of forgetting to restore
15 the string to the usable state later.
7c913512
FM
16
17 For example, assuming you have a low-level OS function called
23324ae1
FM
18 @c GetMeaningOfLifeAsString(char *) returning the value in the provided
19 buffer (which must be writable, of course) you might call it like this:
7c913512 20
23324ae1
FM
21 @code
22 wxString theAnswer;
23 GetMeaningOfLifeAsString(wxStringBuffer(theAnswer, 1024));
24 if ( theAnswer != "42" )
25 {
26 wxLogError("Something is very wrong!");
27 }
28 @endcode
7c913512 29
23324ae1
FM
30 Note that the exact usage of this depends on whether on not wxUSE_STL is
31 enabled. If
32 wxUSE_STL is enabled, wxStringBuffer creates a separate empty character buffer,
33 and
34 if wxUSE_STL is disabled, it uses GetWriteBuf() from wxString, keeping the same
35 buffer
36 wxString uses intact. In other words, relying on wxStringBuffer containing the
7c913512 37 old
23324ae1
FM
38 wxString data is probably not a good idea if you want to build your program in
39 both
40 with and without wxUSE_STL.
7c913512 41
23324ae1
FM
42 @library{wxbase}
43 @category{FIXME}
44*/
7c913512 45class wxStringBuffer
23324ae1
FM
46{
47public:
48 /**
49 Constructs a writable string buffer object associated with the given string
4cc4bfaf 50 and containing enough space for at least @a len characters. Basically, this
23324ae1
FM
51 is equivalent to calling wxString::GetWriteBuf and
52 saving the result.
53 */
54 wxStringBuffer(const wxString& str, size_t len);
55
56 /**
7c913512 57 Restores the string passed to the constructor to the usable state by calling
23324ae1
FM
58 wxString::UngetWriteBuf on it.
59 */
60 ~wxStringBuffer();
61
62 /**
63 Returns the writable pointer to a buffer of the size at least equal to the
64 length specified in the constructor.
65 */
4cc4bfaf 66 wxChar* operator wxChar *();
23324ae1
FM
67};
68
69
e54c96f1 70
23324ae1
FM
71/**
72 @class wxString
73 @wxheader{string.h}
7c913512
FM
74
75 wxString is a class representing a character string. Please see the
23324ae1
FM
76 @ref overview_wxstringoverview "wxString overview" for more information about
77 it.
7c913512 78
23324ae1
FM
79 As explained there, wxString implements most of the methods of the std::string
80 class.
81 These standard functions are not documented in this manual, please see the
82 STL documentation).
83 The behaviour of all these functions is identical to the behaviour described
84 there.
7c913512 85
23324ae1 86 You may notice that wxString sometimes has many functions which do the same
7c913512 87 thing like, for example, wxString::Length,
23324ae1
FM
88 wxString::Len and @c length() which all return the string
89 length. In all cases of such duplication the @c std::string-compatible
90 method (@c length() in this case, always the lowercase version) should be
91 used as it will ensure smoother transition to @c std::string when wxWidgets
92 starts using it instead of wxString.
7c913512 93
23324ae1
FM
94 @library{wxbase}
95 @category{data}
7c913512 96
23324ae1 97 @stdobjects
e54c96f1 98 ::Objects:, ::wxEmptyString,
7c913512 99
e54c96f1 100 @see @ref overview_wxstringoverview "wxString overview", @ref overview_unicode
23324ae1
FM
101 "Unicode overview"
102*/
7c913512 103class wxString
23324ae1
FM
104{
105public:
106 //@{
107 /**
4cc4bfaf 108 Initializes the string from first @a nLength characters of C string.
23324ae1 109 The default value of @c wxSTRING_MAXLEN means take all the string.
7c913512 110 In Unicode build, @e conv's
23324ae1 111 wxMBConv::MB2WC method is called to
4cc4bfaf 112 convert @a psz to wide string (the default converter uses current locale's
23324ae1 113 charset). It is ignored in ANSI build.
3c4f71cc 114
4cc4bfaf
FM
115 @see @ref overview_mbconvclasses "wxMBConv classes", @ref mbstr()
116 mb_str, @ref wcstr() wc_str
23324ae1
FM
117 */
118 wxString();
7c913512
FM
119 wxString(const wxString& x);
120 wxString(wxChar ch, size_t n = 1);
121 wxString(const wxChar* psz, size_t nLength = wxSTRING_MAXLEN);
122 wxString(const unsigned char* psz,
123 size_t nLength = wxSTRING_MAXLEN);
124 wxString(const wchar_t* psz, const wxMBConv& conv,
125 size_t nLength = wxSTRING_MAXLEN);
126 wxString(const char* psz, const wxMBConv& conv = wxConvLibc,
127 size_t nLength = wxSTRING_MAXLEN);
23324ae1
FM
128 //@}
129
130 /**
131 String destructor. Note that this is not virtual, so wxString must not be
132 inherited from.
133 */
134 ~wxString();
135
136 /**
137 Gets all the characters after the first occurrence of @e ch.
4cc4bfaf 138 Returns the empty string if @a ch is not found.
23324ae1 139 */
328f5751 140 wxString AfterFirst(wxChar ch) const;
23324ae1
FM
141
142 /**
143 Gets all the characters after the last occurrence of @e ch.
4cc4bfaf 144 Returns the whole string if @a ch is not found.
23324ae1 145 */
328f5751 146 wxString AfterLast(wxChar ch) const;
23324ae1
FM
147
148 /**
4cc4bfaf 149 Preallocate enough space for wxString to store @a nLen characters. This function
23324ae1
FM
150 may be used to increase speed when the string is constructed by repeated
151 concatenation as in
3c4f71cc 152
23324ae1
FM
153 because it will avoid the need to reallocate string memory many times (in case
154 of long strings). Note that it does not set the maximal length of a string - it
4cc4bfaf 155 will still expand if more than @a nLen characters are stored in it. Also, it
7c913512 156 does not truncate the existing string (use
23324ae1
FM
157 Truncate() for this) even if its current length is
158 greater than @e nLen
159 */
160 void Alloc(size_t nLen);
161
162 //@{
163 /**
4cc4bfaf 164 Concatenates character @a ch to this string, @a count times, returning a
23324ae1
FM
165 reference
166 to it.
167 */
168 wxString Append(const wxChar* psz);
7c913512 169 wxString Append(wxChar ch, int count = 1);
23324ae1
FM
170 //@}
171
172 /**
173 Gets all characters before the first occurrence of @e ch.
4cc4bfaf 174 Returns the whole string if @a ch is not found.
23324ae1 175 */
328f5751 176 wxString BeforeFirst(wxChar ch) const;
23324ae1
FM
177
178 /**
179 Gets all characters before the last occurrence of @e ch.
4cc4bfaf 180 Returns the empty string if @a ch is not found.
23324ae1 181 */
328f5751 182 wxString BeforeLast(wxChar ch) const;
23324ae1
FM
183
184 /**
185 The MakeXXX() variants modify the string in place, while the other functions
186 return a new string which contains the original text converted to the upper or
187 lower case and leave the original string unchanged.
23324ae1 188 MakeUpper()
3c4f71cc 189
23324ae1 190 Upper()
3c4f71cc 191
23324ae1 192 MakeLower()
3c4f71cc 193
23324ae1
FM
194 Lower()
195 */
196
197
198 /**
199 Many functions in this section take a character index in the string. As with C
200 strings and/or arrays, the indices start from 0, so the first character of a
201 string is string[0]. Attempt to access a character beyond the end of the
202 string (which may be even 0 if the string is empty) will provoke an assert
203 failure in @ref overview_debuggingoverview "debug build", but no checks are
204 done in
205 release builds.
23324ae1
FM
206 This section also contains both implicit and explicit conversions to C style
207 strings. Although implicit conversion is quite convenient, it is advised to use
208 explicit @ref cstr() c_str method for the sake of clarity. Also
e54c96f1 209 see overview() for the cases where it is necessary to
23324ae1 210 use it.
23324ae1 211 GetChar()
3c4f71cc 212
23324ae1 213 GetWritableChar()
3c4f71cc 214
23324ae1 215 SetChar()
3c4f71cc 216
23324ae1 217 Last()
3c4f71cc 218
23324ae1 219 @ref operatorbracket() "operator []"
3c4f71cc 220
23324ae1 221 @ref cstr() c_str
3c4f71cc 222
23324ae1 223 @ref mbstr() mb_str
3c4f71cc 224
23324ae1 225 @ref wcstr() wc_str
3c4f71cc 226
23324ae1 227 @ref fnstr() fn_str
3c4f71cc 228
23324ae1
FM
229 @ref operatorconstcharpt() "operator const char*"
230 */
231
232
233 /**
234 Empties the string and frees memory occupied by it.
23324ae1
FM
235 See also: Empty()
236 */
237 void Clear();
238
239 //@{
240 /**
241 Case-sensitive comparison.
23324ae1
FM
242 Returns a positive value if the string is greater than the argument, zero if
243 it is equal to it or a negative value if it is less than the argument (same
244 semantics
245 as the standard @e strcmp() function).
23324ae1
FM
246 See also CmpNoCase(), IsSameAs().
247 */
328f5751
FM
248 int Cmp(const wxString& s) const;
249 const int Cmp(const wxChar* psz) const;
23324ae1
FM
250 //@}
251
252 //@{
253 /**
254 Case-insensitive comparison.
23324ae1
FM
255 Returns a positive value if the string is greater than the argument, zero if
256 it is equal to it or a negative value if it is less than the argument (same
257 semantics
258 as the standard @e strcmp() function).
23324ae1
FM
259 See also Cmp(), IsSameAs().
260 */
328f5751
FM
261 int CmpNoCase(const wxString& s) const;
262 const int CmpNoCase(const wxChar* psz) const;
23324ae1
FM
263 //@}
264
265 /**
266 Case-sensitive comparison. Returns 0 if equal, 1 if greater or -1 if less.
23324ae1
FM
267 This is a wxWidgets 1.xx compatibility function; use Cmp() instead.
268 */
328f5751 269 int CompareTo(const wxChar* psz, caseCompare cmp = exact) const;
23324ae1
FM
270
271 /**
272 The default comparison function Cmp() is case-sensitive and
273 so is the default version of IsSameAs(). For case
274 insensitive comparisons you should use CmpNoCase() or
275 give a second parameter to IsSameAs. This last function is may be more
276 convenient if only equality of the strings matters because it returns a boolean
277 @true value if the strings are the same and not 0 (which is usually @false in
278 C)
279 as @c Cmp() does.
23324ae1
FM
280 Matches() is a poor man's regular expression matcher:
281 it only understands '*' and '?' metacharacters in the sense of DOS command line
282 interpreter.
23324ae1
FM
283 StartsWith() is helpful when parsing a line of
284 text which should start with some predefined prefix and is more efficient than
285 doing direct string comparison as you would also have to precalculate the
286 length of the prefix then.
23324ae1 287 Cmp()
3c4f71cc 288
23324ae1 289 CmpNoCase()
3c4f71cc 290
23324ae1 291 IsSameAs()
3c4f71cc 292
23324ae1 293 Matches()
3c4f71cc 294
23324ae1 295 StartsWith()
3c4f71cc 296
23324ae1
FM
297 EndsWith()
298 */
299
300
301 //@{
302 /**
3c4f71cc 303
23324ae1
FM
304 */
305 bool operator ==(const wxString& x, const wxString& y);
7c913512
FM
306 bool operator ==(const wxString& x, const wxChar* t);
307 bool operator !=(const wxString& x, const wxString& y);
308 bool operator !=(const wxString& x, const wxChar* t);
309 bool operator(const wxString& x, const wxString& y);
310 bool operator(const wxString& x, const wxChar* t);
311 bool operator =(const wxString& x, const wxString& y);
312 bool operator =(const wxString& x, const wxChar* t);
313 bool operator(const wxString& x, const wxString& y);
314 bool operator(const wxString& x, const wxChar* t);
315 bool operator =(const wxString& x, const wxString& y);
316 bool operator =(const wxString& x, const wxChar* t);
23324ae1
FM
317 //@}
318
319 /**
320 Anything may be concatenated (appended to) with a string. However, you can't
321 append something to a C string (including literal constants), so to do this it
322 should be converted to a wxString first.
23324ae1 323 @ref operatorout() "operator "
3c4f71cc 324
23324ae1 325 @ref plusequal() "operator +="
3c4f71cc 326
23324ae1 327 @ref operatorplus() "operator +"
3c4f71cc 328
23324ae1 329 Append()
3c4f71cc 330
23324ae1
FM
331 Prepend()
332 */
333
334
335 /**
336 A string may be constructed either from a C string, (some number of copies of)
337 a single character or a wide (UNICODE) string. For all constructors (except the
338 default which creates an empty string) there is also a corresponding assignment
339 operator.
23324ae1 340 @ref construct() wxString
3c4f71cc 341
23324ae1 342 @ref operatorassign() "operator ="
3c4f71cc 343
23324ae1
FM
344 @ref destruct() ~wxString
345 */
346
347
348 /**
349 Returns @true if target appears anywhere in wxString; else @false.
23324ae1
FM
350 This is a wxWidgets 1.xx compatibility function; you should not use it in new
351 code.
352 */
328f5751 353 bool Contains(const wxString& str) const;
23324ae1
FM
354
355 /**
356 The string provides functions for conversion to signed and unsigned integer and
357 floating point numbers. All three functions take a pointer to the variable to
358 put the numeric value in and return @true if the @b entire string could be
359 converted to a number.
23324ae1 360 ToLong()
3c4f71cc 361
23324ae1 362 ToLongLong()
3c4f71cc 363
23324ae1 364 ToULong()
3c4f71cc 365
23324ae1 366 ToULongLong()
3c4f71cc 367
23324ae1
FM
368 ToDouble()
369 */
370
371
372 /**
373 Makes the string empty, but doesn't free memory occupied by the string.
23324ae1
FM
374 See also: Clear().
375 */
376 void Empty();
377
378 /**
7c913512 379 This function can be used to test if the string ends with the specified
23324ae1 380 @e suffix. If it does, the function will return @true and put the
4cc4bfaf 381 beginning of the string before the suffix into @a rest string if it is not
23324ae1
FM
382 @NULL. Otherwise, the function returns @false and doesn't
383 modify the @e rest.
384 */
328f5751 385 bool EndsWith(const wxString& suffix, wxString rest = NULL) const;
23324ae1
FM
386
387 //@{
388 /**
389 Searches for the given string. Returns the starting index, or @c wxNOT_FOUND if
390 not found.
391 */
328f5751
FM
392 int Find(wxUniChar ch, bool fromEnd = false) const;
393 const int Find(const wxString& sub) const;
23324ae1
FM
394 //@}
395
396 //@{
397 /**
398 Same as Find().
23324ae1
FM
399 This is a wxWidgets 1.xx compatibility function; you should not use it in new
400 code.
401 */
328f5751
FM
402 int First(wxChar c) const;
403 int First(const wxChar* psz) const;
404 const int First(const wxString& str) const;
23324ae1
FM
405 //@}
406
407 /**
7c913512 408 This static function returns the string containing the result of calling
23324ae1 409 Printf() with the passed parameters on it.
3c4f71cc 410
4cc4bfaf 411 @see FormatV(), Printf()
23324ae1 412 */
4cc4bfaf 413 static wxString Format(const wxChar format, ...);
23324ae1
FM
414
415 /**
7c913512 416 This static function returns the string containing the result of calling
23324ae1 417 PrintfV() with the passed parameters on it.
3c4f71cc 418
4cc4bfaf 419 @see Format(), PrintfV()
23324ae1
FM
420 */
421 static wxString FormatV(const wxChar format, va_list argptr);
422
423 /**
4cc4bfaf 424 Returns the number of occurrences of @a ch in the string.
23324ae1
FM
425 This is a wxWidgets 1.xx compatibility function; you should not use it in new
426 code.
427 */
328f5751 428 int Freq(wxChar ch) const;
23324ae1
FM
429
430 //@{
431 /**
432 Converts given buffer of binary data from 8-bit string to wxString. In Unicode
433 build, the string is interpreted as being in ISO-8859-1 encoding. The version
4cc4bfaf 434 without @a len parameter takes NUL-terminated data.
23324ae1 435 This is a convenience method useful when storing binary data in wxString.
3c4f71cc 436
e54c96f1 437 @wxsince{2.8.4}
3c4f71cc 438
4cc4bfaf 439 @see wxString::To8BitData
23324ae1
FM
440 */
441 static wxString From8BitData(const char* buf, size_t len);
7c913512 442 static wxString From8BitData(const char* buf);
23324ae1
FM
443 //@}
444
445 //@{
446 /**
447 Converts the string or character from an ASCII, 7-bit form
448 to the native wxString representation. Most useful when using
449 a Unicode build of wxWidgets (note the use of @c char instead of @c wxChar).
450 Use @ref construct() "wxString constructors" if you
451 need to convert from another charset.
452 */
453 static wxString FromAscii(const char* s);
7c913512
FM
454 static wxString FromAscii(const unsigned char* s);
455 static wxString FromAscii(const char* s, size_t len);
456 static wxString FromAscii(const unsigned char* s, size_t len);
457 static wxString FromAscii(char c);
23324ae1
FM
458 //@}
459
460 //@{
461 /**
462 Converts C string encoded in UTF-8 to wxString.
4cc4bfaf 463 Note that this method assumes that @a s is a valid UTF-8 sequence and
23324ae1
FM
464 doesn't do any validation in release builds, it's validity is only checked in
465 debug builds.
466 */
467 static wxString FromUTF8(const char* s);
7c913512 468 static wxString FromUTF8(const char* s, size_t len);
23324ae1
FM
469 //@}
470
471 /**
4cc4bfaf 472 Returns the character at position @a n (read-only).
23324ae1 473 */
328f5751 474 wxChar GetChar(size_t n) const;
23324ae1
FM
475
476 /**
477 wxWidgets compatibility conversion. Returns a constant pointer to the data in
478 the string.
479 */
328f5751 480 const wxChar* GetData() const;
23324ae1
FM
481
482 /**
483 Returns a reference to the character at position @e n.
484 */
485 wxChar GetWritableChar(size_t n);
486
487 /**
4cc4bfaf 488 Returns a writable buffer of at least @a len bytes.
23324ae1
FM
489 It returns a pointer to a new memory block, and the
490 existing data will not be copied.
23324ae1
FM
491 Call UngetWriteBuf() as soon as
492 possible to put the string back into a reasonable state.
23324ae1
FM
493 This method is deprecated, please use
494 wxStringBuffer or
495 wxStringBufferLength instead.
496 */
497 wxChar* GetWriteBuf(size_t len);
498
499 //@{
500 /**
501 Same as Find().
23324ae1
FM
502 This is a wxWidgets 1.xx compatibility function; you should not use it in new
503 code.
504 */
328f5751
FM
505 size_t Index(wxChar ch) const;
506 const size_t Index(const wxChar* sz) const;
23324ae1
FM
507 //@}
508
509 /**
510 Returns @true if the string contains only ASCII characters.
23324ae1
FM
511 This is a wxWidgets 1.xx compatibility function; you should not use it in new
512 code.
513 */
328f5751 514 bool IsAscii() const;
23324ae1
FM
515
516 /**
517 Returns @true if the string is empty.
518 */
328f5751 519 bool IsEmpty() const;
23324ae1
FM
520
521 /**
522 Returns @true if the string is empty (same as wxString::IsEmpty).
23324ae1
FM
523 This is a wxWidgets 1.xx compatibility function; you should not use it in new
524 code.
525 */
328f5751 526 bool IsNull() const;
23324ae1
FM
527
528 /**
529 Returns @true if the string is an integer (with possible sign).
23324ae1
FM
530 This is a wxWidgets 1.xx compatibility function; you should not use it in new
531 code.
532 */
328f5751 533 bool IsNumber() const;
23324ae1
FM
534
535 //@{
536 /**
537 Test whether the string is equal to the single character @e c. The test is
4cc4bfaf 538 case-sensitive if @a caseSensitive is @true (default) or not if it is @c
23324ae1 539 @false.
23324ae1 540 Returns @true if the string is equal to the character, @false otherwise.
23324ae1
FM
541 See also Cmp(), CmpNoCase()
542 */
328f5751
FM
543 bool IsSameAs(const wxChar* psz, bool caseSensitive = true) const;
544 const bool IsSameAs(wxChar c, bool caseSensitive = true) const;
23324ae1
FM
545 //@}
546
547 /**
548 Returns @true if the string is a word.
23324ae1
FM
549 This is a wxWidgets 1.xx compatibility function; you should not use it in new
550 code.
551 */
328f5751 552 bool IsWord() const;
23324ae1
FM
553
554 //@{
555 /**
556 Returns a reference to the last character (writable).
23324ae1
FM
557 This is a wxWidgets 1.xx compatibility function; you should not use it in new
558 code.
559 */
560 wxChar Last();
328f5751 561 const wxChar Last();
23324ae1
FM
562 //@}
563
564 /**
4cc4bfaf 565 Returns the first @a count characters of the string.
23324ae1 566 */
328f5751 567 wxString Left(size_t count) const;
23324ae1
FM
568
569 /**
570 Returns the length of the string.
571 */
328f5751 572 size_t Len() const;
23324ae1
FM
573
574 /**
575 Returns the length of the string (same as Len).
23324ae1
FM
576 This is a wxWidgets 1.xx compatibility function; you should not use it in new
577 code.
578 */
328f5751 579 size_t Length() const;
23324ae1
FM
580
581 /**
582 Returns this string converted to the lower case.
583 */
328f5751 584 wxString Lower() const;
23324ae1
FM
585
586 /**
587 Same as MakeLower.
23324ae1
FM
588 This is a wxWidgets 1.xx compatibility function; you should not use it in new
589 code.
590 */
591 void LowerCase();
592
593 /**
594 Converts all characters to lower case and returns the result.
595 */
596 wxString MakeLower();
597
598 /**
599 Converts all characters to upper case and returns the result.
600 */
601 wxString MakeUpper();
602
603 /**
604 Returns @true if the string contents matches a mask containing '*' and '?'.
605 */
328f5751 606 bool Matches(const wxString& mask) const;
23324ae1
FM
607
608 /**
7c913512 609 These are "advanced" functions and they will be needed quite rarely.
23324ae1 610 Alloc() and Shrink() are only
7c913512 611 interesting for optimization purposes.
23324ae1
FM
612 wxStringBuffer
613 and wxStringBufferLength classes may be very
614 useful when working with some external API which requires the caller to provide
615 a writable buffer.
23324ae1 616 Alloc()
3c4f71cc 617
23324ae1 618 Shrink()
3c4f71cc 619
23324ae1 620 wxStringBuffer
3c4f71cc 621
23324ae1
FM
622 wxStringBufferLength
623 */
624
625
626 /**
627 Returns a substring starting at @e first, with length @e count, or the rest of
4cc4bfaf 628 the string if @a count is the default value.
23324ae1 629 */
328f5751 630 wxString Mid(size_t first, size_t count = wxSTRING_MAXLEN) const;
23324ae1
FM
631
632 /**
633 Other string functions.
23324ae1 634 Trim()
3c4f71cc 635
23324ae1 636 Truncate()
3c4f71cc 637
23324ae1
FM
638 Pad()
639 */
640
641
642 /**
4cc4bfaf 643 Adds @a count copies of @a pad to the beginning, or to the end of the string
23324ae1 644 (the default).
23324ae1
FM
645 Removes spaces from the left or from the right (default).
646 */
4cc4bfaf
FM
647 wxString Pad(size_t count, wxChar pad = ' ',
648 bool fromRight = true);
23324ae1
FM
649
650 /**
4cc4bfaf 651 Prepends @a str to this string, returning a reference to this string.
23324ae1
FM
652 */
653 wxString Prepend(const wxString& str);
654
655 /**
656 Similar to the standard function @e sprintf(). Returns the number of
657 characters written, or an integer less than zero on error.
23324ae1
FM
658 Note that if @c wxUSE_PRINTF_POS_PARAMS is set to 1, then this function supports
659 Unix98-style positional parameters:
3c4f71cc 660
7c913512 661 @b NB: This function will use a safe version of @e vsprintf() (usually called
23324ae1
FM
662 @e vsnprintf()) whenever available to always allocate the buffer of correct
663 size. Unfortunately, this function is not available on all platforms and the
664 dangerous @e vsprintf() will be used then which may lead to buffer overflows.
665 */
4cc4bfaf 666 int Printf(const wxChar* pszFormat, ...);
23324ae1
FM
667
668 /**
669 Similar to vprintf. Returns the number of characters written, or an integer
670 less than zero
671 on error.
672 */
673 int PrintfV(const wxChar* pszFormat, va_list argPtr);
674
675 //@{
676 /**
4cc4bfaf 677 Removes @a len characters from the string, starting at @e pos.
23324ae1
FM
678 This is a wxWidgets 1.xx compatibility function; you should not use it in new
679 code.
680 */
681 wxString Remove(size_t pos);
7c913512 682 wxString Remove(size_t pos, size_t len);
23324ae1
FM
683 //@}
684
685 /**
686 Removes the last character.
687 */
688 wxString RemoveLast();
689
690 /**
691 Replace first (or all) occurrences of substring with another one.
23324ae1 692 @e replaceAll: global replace (default), or only the first occurrence.
23324ae1
FM
693 Returns the number of replacements made.
694 */
695 size_t Replace(const wxString& strOld, const wxString& strNew,
4cc4bfaf 696 bool replaceAll = true);
23324ae1
FM
697
698 /**
4cc4bfaf 699 Returns the last @a count characters.
23324ae1 700 */
328f5751 701 wxString Right(size_t count) const;
23324ae1
FM
702
703 /**
7c913512 704 These functions replace the standard @e strchr() and @e strstr()
23324ae1 705 functions.
23324ae1 706 Find()
3c4f71cc 707
23324ae1
FM
708 Replace()
709 */
710
711
712 /**
713 Sets the character at position @e n.
714 */
715 void SetChar(size_t n, wxChar ch);
716
717 /**
7c913512 718 Minimizes the string's memory. This can be useful after a call to
23324ae1
FM
719 Alloc() if too much memory were preallocated.
720 */
721 void Shrink();
722
723 /**
7c913512 724 This function can be used to test if the string starts with the specified
23324ae1 725 @e prefix. If it does, the function will return @true and put the rest
4cc4bfaf 726 of the string (i.e. after the prefix) into @a rest string if it is not
23324ae1
FM
727 @NULL. Otherwise, the function returns @false and doesn't modify the
728 @e rest.
729 */
328f5751 730 bool StartsWith(const wxString& prefix, wxString rest = NULL) const;
23324ae1
FM
731
732 /**
733 These functions return the string length and check whether the string is empty
734 or empty it.
23324ae1 735 Len()
3c4f71cc 736
23324ae1 737 IsEmpty()
3c4f71cc 738
23324ae1 739 @ref operatornot() operator!
3c4f71cc 740
23324ae1 741 Empty()
3c4f71cc 742
23324ae1
FM
743 Clear()
744 */
745
746
747 /**
748 Strip characters at the front and/or end. The same as Trim except that it
749 doesn't change this string.
23324ae1
FM
750 This is a wxWidgets 1.xx compatibility function; you should not use it in new
751 code.
752 */
328f5751 753 wxString Strip(stripType s = trailing) const;
23324ae1
FM
754
755 /**
4cc4bfaf 756 Returns the part of the string between the indices @a from and @e to
23324ae1 757 inclusive.
23324ae1
FM
758 This is a wxWidgets 1.xx compatibility function, use Mid()
759 instead (but note that parameters have different meaning).
760 */
328f5751 761 wxString SubString(size_t from, size_t to) const;
23324ae1
FM
762
763 /**
764 These functions allow to extract substring from this string. All of them don't
765 modify the original string and return a new string containing the extracted
766 substring.
23324ae1 767 Mid()
3c4f71cc 768
23324ae1 769 @ref operatorparenth() operator
3c4f71cc 770
23324ae1 771 Left()
3c4f71cc 772
23324ae1 773 Right()
3c4f71cc 774
23324ae1 775 BeforeFirst()
3c4f71cc 776
23324ae1 777 BeforeLast()
3c4f71cc 778
23324ae1 779 AfterFirst()
3c4f71cc 780
23324ae1 781 AfterLast()
3c4f71cc 782
23324ae1 783 StartsWith()
3c4f71cc 784
23324ae1
FM
785 EndsWith()
786 */
787
788
789 //@{
790 /**
791 Converts the string to an 8-bit string in ISO-8859-1 encoding in the form of
792 a wxCharBuffer (Unicode builds only).
23324ae1 793 This is a convenience method useful when storing binary data in wxString.
3c4f71cc 794
e54c96f1 795 @wxsince{2.8.4}
3c4f71cc 796
4cc4bfaf 797 @see wxString::From8BitData
23324ae1 798 */
328f5751
FM
799 const char* To8BitData() const;
800 const const wxCharBuffer To8BitData() const;
23324ae1
FM
801 //@}
802
803 //@{
804 /**
805 Converts the string to an ASCII, 7-bit string in the form of
806 a wxCharBuffer (Unicode builds only) or a C string (ANSI builds).
23324ae1
FM
807 Note that this conversion only works if the string contains only ASCII
808 characters. The @ref mbstr() mb_str method provides more
809 powerful means of converting wxString to C string.
810 */
328f5751
FM
811 const char* ToAscii() const;
812 const const wxCharBuffer ToAscii() const;
23324ae1
FM
813 //@}
814
815 /**
816 Attempts to convert the string to a floating point number. Returns @true on
817 success (the number is stored in the location pointed to by @e val) or @false
4cc4bfaf 818 if the string does not represent such number (the value of @a val is not
23324ae1 819 modified in this case).
3c4f71cc 820
4cc4bfaf 821 @see ToLong(), ToULong()
23324ae1 822 */
328f5751 823 bool ToDouble(double val) const;
23324ae1
FM
824
825 /**
826 Attempts to convert the string to a signed integer in base @e base. Returns
827 @true on success in which case the number is stored in the location
4cc4bfaf
FM
828 pointed to by @a val or @false if the string does not represent a
829 valid number in the given base (the value of @a val is not modified
23324ae1 830 in this case).
4cc4bfaf 831 The value of @a base must be comprised between 2 and 36, inclusive, or
23324ae1
FM
832 be a special value 0 which means that the usual rules of @c C numbers are
833 applied: if the number starts with @c 0x it is considered to be in base
834 16, if it starts with @c 0 - in base 8 and in base 10 otherwise. Note
835 that you may not want to specify the base 0 if you are parsing the numbers
836 which may have leading zeroes as they can yield unexpected (to the user not
837 familiar with C) results.
3c4f71cc 838
4cc4bfaf 839 @see ToDouble(), ToULong()
23324ae1 840 */
328f5751 841 bool ToLong(long val, int base = 10) const;
23324ae1
FM
842
843 /**
844 This is exactly the same as ToLong() but works with 64
845 bit integer numbers.
23324ae1
FM
846 Notice that currently it doesn't work (always returns @false) if parsing of 64
847 bit numbers is not supported by the underlying C run-time library. Compilers
848 with C99 support and Microsoft Visual C++ version 7 and higher do support this.
3c4f71cc 849
4cc4bfaf 850 @see ToLong(), ToULongLong()
23324ae1 851 */
328f5751 852 bool ToLongLong(wxLongLong_t val, int base = 10) const;
23324ae1
FM
853
854 /**
855 Attempts to convert the string to an unsigned integer in base @e base.
856 Returns @true on success in which case the number is stored in the
4cc4bfaf
FM
857 location pointed to by @a val or @false if the string does not
858 represent a valid number in the given base (the value of @a val is not
23324ae1
FM
859 modified in this case). Please notice that this function
860 behaves in the same way as the standard @c strtoul() and so it simply
861 converts negative numbers to unsigned representation instead of rejecting them
862 (e.g. -1 is returned as @c ULONG_MAX).
23324ae1 863 See ToLong() for the more detailed
4cc4bfaf 864 description of the @a base parameter.
3c4f71cc 865
4cc4bfaf 866 @see ToDouble(), ToLong()
23324ae1 867 */
328f5751 868 bool ToULong(unsigned long val, int base = 10) const;
23324ae1
FM
869
870 /**
871 This is exactly the same as ToULong() but works with 64
872 bit integer numbers.
23324ae1
FM
873 Please see ToLongLong() for additional remarks.
874 */
328f5751 875 bool ToULongLong(wxULongLong_t val, int base = 10) const;
23324ae1
FM
876
877 //@{
878 /**
879 Same as @ref wxString::utf8str utf8_str.
880 */
328f5751
FM
881 const char* ToUTF8() const;
882 const const wxCharBuffer ToUF8() const;
23324ae1
FM
883 //@}
884
885 /**
886 Removes white-space (space, tabs, form feed, newline and carriage return) from
887 the left or from the right end of the string (right is default).
888 */
4cc4bfaf 889 wxString Trim(bool fromRight = true);
23324ae1
FM
890
891 /**
892 Truncate the string to the given length.
893 */
894 wxString Truncate(size_t len);
895
896 //@{
897 /**
898 Puts the string back into a reasonable state (in which it can be used
899 normally), after
900 GetWriteBuf() was called.
4cc4bfaf 901 The version of the function without the @a len parameter will calculate the
23324ae1
FM
902 new string length itself assuming that the string is terminated by the first
903 @c NUL character in it while the second one will use the specified length
904 and thus is the only version which should be used with the strings with
7c913512 905 embedded @c NULs (it is also slightly more efficient as @c strlen()
23324ae1 906 doesn't have to be called).
23324ae1
FM
907 This method is deprecated, please use
908 wxStringBuffer or
909 wxStringBufferLength instead.
910 */
911 void UngetWriteBuf();
7c913512 912 void UngetWriteBuf(size_t len);
23324ae1
FM
913 //@}
914
915 /**
916 Returns this string converted to upper case.
917 */
328f5751 918 wxString Upper() const;
23324ae1
FM
919
920 /**
921 The same as MakeUpper.
23324ae1
FM
922 This is a wxWidgets 1.xx compatibility function; you should not use it in new
923 code.
924 */
925 void UpperCase();
926
927 /**
928 Both formatted versions (wxString::Printf) and stream-like
7c913512 929 insertion operators exist (for basic types only). Additionally, the
23324ae1
FM
930 Format() function allows to use simply append
931 formatted value to a string:
3c4f71cc 932
23324ae1 933 Format()
3c4f71cc 934
23324ae1 935 FormatV()
3c4f71cc 936
23324ae1 937 Printf()
3c4f71cc 938
23324ae1 939 PrintfV()
3c4f71cc 940
23324ae1
FM
941 @ref operatorout() "operator "
942 */
943
944
945 /**
946 Returns a pointer to the string data (@c const char* in ANSI build,
947 @c const wchar_t* in Unicode build).
23324ae1
FM
948 Note that the returned value is not convertible to @c char* or
949 @c wchar_t*, use @ref charstr() char_str or
950 @ref wcharstr() wchar_string if you need to pass string value
951 to a function expecting non-const pointer.
3c4f71cc 952
4cc4bfaf
FM
953 @see @ref mbstr() mb_str, @ref wcstr() wc_str, @ref
954 fnstr() fn_str, @ref charstr() char_str, @ref
955 wcharstr() wchar_string
23324ae1 956 */
328f5751 957 const wxChar* c_str() const;
23324ae1
FM
958
959 /**
960 Returns an object with string data that is implicitly convertible to
961 @c char* pointer. Note that any change to the returned buffer is lost and so
962 this function is only usable for passing strings to legacy libraries that
963 don't have const-correct API. Use wxStringBuffer if
964 you want to modify the string.
3c4f71cc 965
4cc4bfaf
FM
966 @see @ref mbstr() mb_str, @ref wcstr() wc_str, @ref
967 fnstr() fn_str, @ref cstr() c_str, @ref
968 wcharstr() wchar_str
23324ae1 969 */
328f5751 970 wxWritableCharBuffer char_str(const wxMBConv& conv = wxConvLibc) const;
23324ae1
FM
971
972 //@{
973 /**
974 Returns string representation suitable for passing to OS' functions for
975 file handling. In ANSI build, this is same as @ref cstr() c_str.
976 In Unicode build, returned value can be either wide character string
977 or C string in charset matching the @c wxConvFileName object, depending on
978 the OS.
3c4f71cc 979
4cc4bfaf 980 @see wxMBConv, @ref wcstr() wc_str, @ref wcstr() mb_str
23324ae1 981 */
328f5751
FM
982 const wchar_t* fn_str() const;
983 const const char* fn_str() const;
984 const const wxCharBuffer fn_str() const;
23324ae1
FM
985 //@}
986
987 //@{
988 /**
989 Returns multibyte (C string) representation of the string.
990 In Unicode build, converts using @e conv's wxMBConv::cWC2MB
991 method and returns wxCharBuffer. In ANSI build, this function is same
992 as @ref cstr() c_str.
993 The macro wxWX2MBbuf is defined as the correct return type (without const).
3c4f71cc 994
4cc4bfaf
FM
995 @see wxMBConv, @ref cstr() c_str, @ref wcstr() wc_str, @ref
996 fnstr() fn_str, @ref charstr() char_str
23324ae1 997 */
328f5751
FM
998 const char* mb_str(const wxMBConv& conv = wxConvLibc) const;
999 const const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const;
23324ae1
FM
1000 //@}
1001
1002 /**
1003 Extraction from a stream.
1004 */
1005 friend istream operator(istream& is, wxString& str);
1006
1007 //@{
1008 /**
1009 These functions work as C++ stream insertion operators: they insert the given
1010 value into the string. Precision or format cannot be set using them, you can
7c913512 1011 use
23324ae1
FM
1012 Printf() for this.
1013 */
1014 wxString operator(const wxString& str);
7c913512
FM
1015 wxString operator(const wxChar* psz);
1016 wxString operator(wxChar ch);
1017 wxString operator(int i);
1018 wxString operator(float f);
1019 wxString operator(double d);
23324ae1
FM
1020 //@}
1021
1022 /**
1023 Same as Mid (substring extraction).
1024 */
1025 wxString operator ()(size_t start, size_t len);
1026
1027 //@{
1028 /**
1029 Concatenation: all these operators return a new string equal to the
1030 concatenation of the operands.
1031 */
1032 wxString operator +(const wxString& x, const wxString& y);
7c913512
FM
1033 wxString operator +(const wxString& x, const wxChar* y);
1034 wxString operator +(const wxString& x, wxChar y);
1035 wxString operator +(const wxChar* x, const wxString& y);
23324ae1
FM
1036 //@}
1037
1038 //@{
1039 /**
1040 Concatenation in place: the argument is appended to the string.
1041 */
1042 void operator +=(const wxString& str);
7c913512
FM
1043 void operator +=(const wxChar* psz);
1044 void operator +=(wxChar c);
23324ae1
FM
1045 //@}
1046
1047 //@{
1048 /**
1049 Assignment: the effect of each operation is the same as for the corresponding
1050 constructor (see @ref construct() "wxString constructors").
1051 */
1052 wxString operator =(const wxString& str);
7c913512
FM
1053 wxString operator =(const wxChar* psz);
1054 wxString operator =(wxChar c);
23324ae1
FM
1055 //@}
1056
1057 //@{
1058 /**
1059 Element extraction.
1060 */
328f5751
FM
1061 wxChar operator [](size_t i) const;
1062 wxChar operator [](size_t i) const;
1063 const wxChar operator [](int i) const;
1064 wxChar operator [](int i) const;
23324ae1
FM
1065 //@}
1066
1067 /**
1068 Implicit conversion to a C string.
1069 */
328f5751 1070 operator const wxChar*() const;
23324ae1
FM
1071
1072 /**
1073 Empty string is @false, so !string will only return @true if the string is
1074 empty.
1075 This allows the tests for @NULLness of a @e const wxChar * pointer and emptiness
1076 of the string to look the same in the code and makes it easier to port old code
1077 to wxString.
23324ae1
FM
1078 See also IsEmpty().
1079 */
328f5751 1080 bool operator!() const;
23324ae1
FM
1081
1082 /**
1083 The supported functions are only listed here, please see any STL reference for
1084 their documentation.
1085 */
1086
1087
1088 //@{
1089 /**
1090 Converts the strings contents to UTF-8 and returns it either as a temporary
1091 wxCharBuffer object or as a pointer to the internal string contents in
1092 UTF-8 build.
1093 */
328f5751
FM
1094 const char* utf8_str() const;
1095 const const wxCharBuffer utf8_str() const;
23324ae1
FM
1096 //@}
1097
1098 //@{
1099 /**
1100 Returns wide character representation of the string.
1101 In ANSI build, converts using @e conv's wxMBConv::cMB2WC
1102 method and returns wxWCharBuffer. In Unicode build, this function is same
1103 as @ref cstr() c_str.
1104 The macro wxWX2WCbuf is defined as the correct return type (without const).
3c4f71cc 1105
4cc4bfaf
FM
1106 @see wxMBConv, @ref cstr() c_str, @ref wcstr() mb_str, @ref
1107 fnstr() fn_str, @ref wcharstr() wchar_str
23324ae1 1108 */
328f5751
FM
1109 const wchar_t* wc_str(const wxMBConv& conv) const;
1110 const const wxWCharBuffer wc_str(const wxMBConv& conv) const;
23324ae1
FM
1111 //@}
1112
1113 /**
1114 Returns an object with string data that is implicitly convertible to
1115 @c char* pointer. Note that changes to the returned buffer may or may
1116 not be lost (depending on the build) and so this function is only usable for
1117 passing strings to legacy libraries that don't have const-correct API. Use
1118 wxStringBuffer if you want to modify the string.
3c4f71cc 1119
4cc4bfaf
FM
1120 @see @ref mbstr() mb_str, @ref wcstr() wc_str, @ref
1121 fnstr() fn_str, @ref cstr() c_str, @ref
1122 charstr() char_str
23324ae1 1123 */
328f5751 1124 wxWritableWCharBuffer wchar_str() const;
23324ae1
FM
1125
1126 /**
1127 These functions are deprecated, please consider using new wxWidgets 2.0
1128 functions instead of them (or, even better, std::string compatible variants).
23324ae1 1129 CompareTo()
3c4f71cc 1130
23324ae1 1131 Contains()
3c4f71cc 1132
23324ae1 1133 First()
3c4f71cc 1134
23324ae1 1135 Freq()
3c4f71cc 1136
23324ae1 1137 Index()
3c4f71cc 1138
23324ae1 1139 IsAscii()
3c4f71cc 1140
23324ae1 1141 IsNull()
3c4f71cc 1142
23324ae1 1143 IsNumber()
3c4f71cc 1144
23324ae1 1145 IsWord()
3c4f71cc 1146
23324ae1 1147 Last()
3c4f71cc 1148
23324ae1 1149 Length()
3c4f71cc 1150
23324ae1 1151 LowerCase()
3c4f71cc 1152
23324ae1 1153 Remove()
3c4f71cc 1154
23324ae1 1155 Strip()
3c4f71cc 1156
23324ae1 1157 SubString()
3c4f71cc 1158
23324ae1
FM
1159 UpperCase()
1160 */
1161};
1162
1163
e54c96f1
FM
1164/**
1165 FIXME
1166*/
1167wxString Objects:
1168;
1169
1170/**
1171 FIXME
1172*/
1173wxString wxEmptyString;
1174
1175
1176
1177
23324ae1
FM
1178/**
1179 @class wxStringBufferLength
1180 @wxheader{string.h}
7c913512
FM
1181
1182 This tiny class allows to conveniently access the wxString
23324ae1
FM
1183 internal buffer as a writable pointer without any risk of forgetting to restore
1184 the string to the usable state later, and allows the user to set the internal
1185 length of the string.
7c913512
FM
1186
1187 For example, assuming you have a low-level OS function called
23324ae1
FM
1188 @c int GetMeaningOfLifeAsString(char *) copying the value in the provided
1189 buffer (which must be writable, of course), and returning the actual length
1190 of the string, you might call it like this:
7c913512 1191
23324ae1
FM
1192 @code
1193 wxString theAnswer;
1194 wxStringBuffer theAnswerBuffer(theAnswer, 1024);
1195 int nLength = GetMeaningOfLifeAsString(theAnswerBuffer);
1196 theAnswerBuffer.SetLength(nLength);
1197 if ( theAnswer != "42" )
1198 {
1199 wxLogError("Something is very wrong!");
1200 }
1201 @endcode
7c913512 1202
23324ae1
FM
1203 Note that the exact usage of this depends on whether on not wxUSE_STL is
1204 enabled. If
1205 wxUSE_STL is enabled, wxStringBuffer creates a separate empty character buffer,
1206 and
1207 if wxUSE_STL is disabled, it uses GetWriteBuf() from wxString, keeping the same
1208 buffer
1209 wxString uses intact. In other words, relying on wxStringBuffer containing the
7c913512 1210 old
23324ae1
FM
1211 wxString data is probably not a good idea if you want to build your program in
1212 both
1213 with and without wxUSE_STL.
7c913512 1214
23324ae1 1215 Note that SetLength @c must be called before wxStringBufferLength destructs.
7c913512 1216
23324ae1
FM
1217 @library{wxbase}
1218 @category{FIXME}
1219*/
7c913512 1220class wxStringBufferLength
23324ae1
FM
1221{
1222public:
1223 /**
1224 Constructs a writable string buffer object associated with the given string
4cc4bfaf 1225 and containing enough space for at least @a len characters. Basically, this
23324ae1
FM
1226 is equivalent to calling wxString::GetWriteBuf and
1227 saving the result.
1228 */
1229 wxStringBufferLength(const wxString& str, size_t len);
1230
1231 /**
7c913512 1232 Restores the string passed to the constructor to the usable state by calling
23324ae1
FM
1233 wxString::UngetWriteBuf on it.
1234 */
1235 ~wxStringBufferLength();
1236
1237 /**
7c913512 1238 Sets the internal length of the string referred to by wxStringBufferLength to
4cc4bfaf 1239 @a nLength characters.
23324ae1
FM
1240 Must be called before wxStringBufferLength destructs.
1241 */
1242 void SetLength(size_t nLength);
1243
1244 /**
1245 Returns the writable pointer to a buffer of the size at least equal to the
1246 length specified in the constructor.
1247 */
4cc4bfaf 1248 wxChar* operator wxChar *();
23324ae1
FM
1249};
1250