]> git.saurik.com Git - wxWidgets.git/blame - interface/string.h
remove wxTextAttr::CreateFont(); return wxNullFont from GetFont() if we have no font...
[wxWidgets.git] / interface / string.h
CommitLineData
23324ae1
FM
1/////////////////////////////////////////////////////////////////////////////
2// Name: string.h
3// Purpose: documentation for wxStringBuffer class
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
70/**
71 @class wxString
72 @wxheader{string.h}
7c913512
FM
73
74 wxString is a class representing a character string. Please see the
23324ae1
FM
75 @ref overview_wxstringoverview "wxString overview" for more information about
76 it.
7c913512 77
23324ae1
FM
78 As explained there, wxString implements most of the methods of the std::string
79 class.
80 These standard functions are not documented in this manual, please see the
81 STL documentation).
82 The behaviour of all these functions is identical to the behaviour described
83 there.
7c913512 84
23324ae1 85 You may notice that wxString sometimes has many functions which do the same
7c913512 86 thing like, for example, wxString::Length,
23324ae1
FM
87 wxString::Len and @c length() which all return the string
88 length. In all cases of such duplication the @c std::string-compatible
89 method (@c length() in this case, always the lowercase version) should be
90 used as it will ensure smoother transition to @c std::string when wxWidgets
91 starts using it instead of wxString.
7c913512 92
23324ae1
FM
93 @library{wxbase}
94 @category{data}
7c913512 95
23324ae1
FM
96 @stdobjects
97 Objects:
98 wxEmptyString
7c913512 99
23324ae1
FM
100 @seealso
101 @ref overview_wxstringoverview "wxString overview", @ref overview_unicode
102 "Unicode overview"
103*/
7c913512 104class wxString
23324ae1
FM
105{
106public:
107 //@{
108 /**
4cc4bfaf 109 Initializes the string from first @a nLength characters of C string.
23324ae1 110 The default value of @c wxSTRING_MAXLEN means take all the string.
7c913512 111 In Unicode build, @e conv's
23324ae1 112 wxMBConv::MB2WC method is called to
4cc4bfaf 113 convert @a psz to wide string (the default converter uses current locale's
23324ae1
FM
114 charset). It is ignored in ANSI build.
115
4cc4bfaf
FM
116 @see @ref overview_mbconvclasses "wxMBConv classes", @ref mbstr()
117 mb_str, @ref wcstr() wc_str
23324ae1
FM
118 */
119 wxString();
7c913512
FM
120 wxString(const wxString& x);
121 wxString(wxChar ch, size_t n = 1);
122 wxString(const wxChar* psz, size_t nLength = wxSTRING_MAXLEN);
123 wxString(const unsigned char* psz,
124 size_t nLength = wxSTRING_MAXLEN);
125 wxString(const wchar_t* psz, const wxMBConv& conv,
126 size_t nLength = wxSTRING_MAXLEN);
127 wxString(const char* psz, const wxMBConv& conv = wxConvLibc,
128 size_t nLength = wxSTRING_MAXLEN);
23324ae1
FM
129 //@}
130
131 /**
132 String destructor. Note that this is not virtual, so wxString must not be
133 inherited from.
134 */
135 ~wxString();
136
137 /**
138 Gets all the characters after the first occurrence of @e ch.
4cc4bfaf 139 Returns the empty string if @a ch is not found.
23324ae1
FM
140 */
141 wxString AfterFirst(wxChar ch);
142
143 /**
144 Gets all the characters after the last occurrence of @e ch.
4cc4bfaf 145 Returns the whole string if @a ch is not found.
23324ae1
FM
146 */
147 wxString AfterLast(wxChar ch);
148
149 /**
4cc4bfaf 150 Preallocate enough space for wxString to store @a nLen characters. This function
23324ae1
FM
151 may be used to increase speed when the string is constructed by repeated
152 concatenation as in
4cc4bfaf 153
23324ae1
FM
154 because it will avoid the need to reallocate string memory many times (in case
155 of long strings). Note that it does not set the maximal length of a string - it
4cc4bfaf 156 will still expand if more than @a nLen characters are stored in it. Also, it
7c913512 157 does not truncate the existing string (use
23324ae1
FM
158 Truncate() for this) even if its current length is
159 greater than @e nLen
160 */
161 void Alloc(size_t nLen);
162
163 //@{
164 /**
4cc4bfaf 165 Concatenates character @a ch to this string, @a count times, returning a
23324ae1
FM
166 reference
167 to it.
168 */
169 wxString Append(const wxChar* psz);
7c913512 170 wxString Append(wxChar ch, int count = 1);
23324ae1
FM
171 //@}
172
173 /**
174 Gets all characters before the first occurrence of @e ch.
4cc4bfaf 175 Returns the whole string if @a ch is not found.
23324ae1
FM
176 */
177 wxString BeforeFirst(wxChar ch);
178
179 /**
180 Gets all characters before the last occurrence of @e ch.
4cc4bfaf 181 Returns the empty string if @a ch is not found.
23324ae1
FM
182 */
183 wxString BeforeLast(wxChar ch);
184
185 /**
186 The MakeXXX() variants modify the string in place, while the other functions
187 return a new string which contains the original text converted to the upper or
188 lower case and leave the original string unchanged.
23324ae1
FM
189 MakeUpper()
190
191 Upper()
192
193 MakeLower()
194
195 Lower()
196 */
197
198
199 /**
200 Many functions in this section take a character index in the string. As with C
201 strings and/or arrays, the indices start from 0, so the first character of a
202 string is string[0]. Attempt to access a character beyond the end of the
203 string (which may be even 0 if the string is empty) will provoke an assert
204 failure in @ref overview_debuggingoverview "debug build", but no checks are
205 done in
206 release builds.
23324ae1
FM
207 This section also contains both implicit and explicit conversions to C style
208 strings. Although implicit conversion is quite convenient, it is advised to use
209 explicit @ref cstr() c_str method for the sake of clarity. Also
210 see overview for the cases where it is necessary to
211 use it.
23324ae1
FM
212 GetChar()
213
214 GetWritableChar()
215
216 SetChar()
217
218 Last()
219
220 @ref operatorbracket() "operator []"
221
222 @ref cstr() c_str
223
224 @ref mbstr() mb_str
225
226 @ref wcstr() wc_str
227
228 @ref fnstr() fn_str
229
230 @ref operatorconstcharpt() "operator const char*"
231 */
232
233
234 /**
235 Empties the string and frees memory occupied by it.
23324ae1
FM
236 See also: Empty()
237 */
238 void Clear();
239
240 //@{
241 /**
242 Case-sensitive comparison.
23324ae1
FM
243 Returns a positive value if the string is greater than the argument, zero if
244 it is equal to it or a negative value if it is less than the argument (same
245 semantics
246 as the standard @e strcmp() function).
23324ae1
FM
247 See also CmpNoCase(), IsSameAs().
248 */
249 int Cmp(const wxString& s);
7c913512 250 int Cmp(const wxChar* psz);
23324ae1
FM
251 //@}
252
253 //@{
254 /**
255 Case-insensitive comparison.
23324ae1
FM
256 Returns a positive value if the string is greater than the argument, zero if
257 it is equal to it or a negative value if it is less than the argument (same
258 semantics
259 as the standard @e strcmp() function).
23324ae1
FM
260 See also Cmp(), IsSameAs().
261 */
262 int CmpNoCase(const wxString& s);
7c913512 263 int CmpNoCase(const wxChar* psz);
23324ae1
FM
264 //@}
265
266 /**
267 Case-sensitive comparison. Returns 0 if equal, 1 if greater or -1 if less.
23324ae1
FM
268 This is a wxWidgets 1.xx compatibility function; use Cmp() instead.
269 */
270 int CompareTo(const wxChar* psz, caseCompare cmp = exact);
271
272 /**
273 The default comparison function Cmp() is case-sensitive and
274 so is the default version of IsSameAs(). For case
275 insensitive comparisons you should use CmpNoCase() or
276 give a second parameter to IsSameAs. This last function is may be more
277 convenient if only equality of the strings matters because it returns a boolean
278 @true value if the strings are the same and not 0 (which is usually @false in
279 C)
280 as @c Cmp() does.
23324ae1
FM
281 Matches() is a poor man's regular expression matcher:
282 it only understands '*' and '?' metacharacters in the sense of DOS command line
283 interpreter.
23324ae1
FM
284 StartsWith() is helpful when parsing a line of
285 text which should start with some predefined prefix and is more efficient than
286 doing direct string comparison as you would also have to precalculate the
287 length of the prefix then.
23324ae1
FM
288 Cmp()
289
290 CmpNoCase()
291
292 IsSameAs()
293
294 Matches()
295
296 StartsWith()
297
298 EndsWith()
299 */
300
301
302 //@{
303 /**
304
305 */
306 bool operator ==(const wxString& x, const wxString& y);
7c913512
FM
307 bool operator ==(const wxString& x, const wxChar* t);
308 bool operator !=(const wxString& x, const wxString& y);
309 bool operator !=(const wxString& x, const wxChar* t);
310 bool operator(const wxString& x, const wxString& y);
311 bool operator(const wxString& x, const wxChar* t);
312 bool operator =(const wxString& x, const wxString& y);
313 bool operator =(const wxString& x, const wxChar* t);
314 bool operator(const wxString& x, const wxString& y);
315 bool operator(const wxString& x, const wxChar* t);
316 bool operator =(const wxString& x, const wxString& y);
317 bool operator =(const wxString& x, const wxChar* t);
23324ae1
FM
318 //@}
319
320 /**
321 Anything may be concatenated (appended to) with a string. However, you can't
322 append something to a C string (including literal constants), so to do this it
323 should be converted to a wxString first.
23324ae1
FM
324 @ref operatorout() "operator "
325
326 @ref plusequal() "operator +="
327
328 @ref operatorplus() "operator +"
329
330 Append()
331
332 Prepend()
333 */
334
335
336 /**
337 A string may be constructed either from a C string, (some number of copies of)
338 a single character or a wide (UNICODE) string. For all constructors (except the
339 default which creates an empty string) there is also a corresponding assignment
340 operator.
23324ae1
FM
341 @ref construct() wxString
342
343 @ref operatorassign() "operator ="
344
345 @ref destruct() ~wxString
346 */
347
348
349 /**
350 Returns @true if target appears anywhere in wxString; else @false.
23324ae1
FM
351 This is a wxWidgets 1.xx compatibility function; you should not use it in new
352 code.
353 */
354 bool Contains(const wxString& str);
355
356 /**
357 The string provides functions for conversion to signed and unsigned integer and
358 floating point numbers. All three functions take a pointer to the variable to
359 put the numeric value in and return @true if the @b entire string could be
360 converted to a number.
23324ae1
FM
361 ToLong()
362
363 ToLongLong()
364
365 ToULong()
366
367 ToULongLong()
368
369 ToDouble()
370 */
371
372
373 /**
374 Makes the string empty, but doesn't free memory occupied by the string.
23324ae1
FM
375 See also: Clear().
376 */
377 void Empty();
378
379 /**
7c913512 380 This function can be used to test if the string ends with the specified
23324ae1 381 @e suffix. If it does, the function will return @true and put the
4cc4bfaf 382 beginning of the string before the suffix into @a rest string if it is not
23324ae1
FM
383 @NULL. Otherwise, the function returns @false and doesn't
384 modify the @e rest.
385 */
4cc4bfaf 386 bool EndsWith(const wxString& suffix, wxString rest = NULL);
23324ae1
FM
387
388 //@{
389 /**
390 Searches for the given string. Returns the starting index, or @c wxNOT_FOUND if
391 not found.
392 */
4cc4bfaf 393 int Find(wxUniChar ch, bool fromEnd = false);
7c913512 394 int Find(const wxString& sub);
23324ae1
FM
395 //@}
396
397 //@{
398 /**
399 Same as Find().
23324ae1
FM
400 This is a wxWidgets 1.xx compatibility function; you should not use it in new
401 code.
402 */
403 int First(wxChar c);
7c913512
FM
404 int First(const wxChar* psz);
405 int First(const wxString& str);
23324ae1
FM
406 //@}
407
408 /**
7c913512 409 This static function returns the string containing the result of calling
23324ae1
FM
410 Printf() with the passed parameters on it.
411
4cc4bfaf 412 @see FormatV(), Printf()
23324ae1 413 */
4cc4bfaf 414 static wxString Format(const wxChar format, ...);
23324ae1
FM
415
416 /**
7c913512 417 This static function returns the string containing the result of calling
23324ae1
FM
418 PrintfV() with the passed parameters on it.
419
4cc4bfaf 420 @see Format(), PrintfV()
23324ae1
FM
421 */
422 static wxString FormatV(const wxChar format, va_list argptr);
423
424 /**
4cc4bfaf 425 Returns the number of occurrences of @a ch in the string.
23324ae1
FM
426 This is a wxWidgets 1.xx compatibility function; you should not use it in new
427 code.
428 */
429 int Freq(wxChar ch);
430
431 //@{
432 /**
433 Converts given buffer of binary data from 8-bit string to wxString. In Unicode
434 build, the string is interpreted as being in ISO-8859-1 encoding. The version
4cc4bfaf 435 without @a len parameter takes NUL-terminated data.
23324ae1 436 This is a convenience method useful when storing binary data in wxString.
23324ae1
FM
437 This function is new since wxWidgets version 2.8.4
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
FM
473 */
474 wxChar GetChar(size_t n);
475
476 /**
477 wxWidgets compatibility conversion. Returns a constant pointer to the data in
478 the string.
479 */
480 const wxChar* GetData();
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 */
505 size_t Index(wxChar ch);
7c913512 506 size_t Index(const wxChar* sz);
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 */
514 bool IsAscii();
515
516 /**
517 Returns @true if the string is empty.
518 */
519 bool IsEmpty();
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 */
526 bool IsNull();
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 */
533 bool IsNumber();
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 */
4cc4bfaf
FM
543 bool IsSameAs(const wxChar* psz, bool caseSensitive = true);
544 bool IsSameAs(wxChar c, bool caseSensitive = true);
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 */
552 bool IsWord();
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();
7c913512 561 wxChar Last();
23324ae1
FM
562 //@}
563
564 /**
4cc4bfaf 565 Returns the first @a count characters of the string.
23324ae1
FM
566 */
567 wxString Left(size_t count);
568
569 /**
570 Returns the length of the string.
571 */
4cc4bfaf 572 size_t Len();
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 */
579 size_t Length();
580
581 /**
582 Returns this string converted to the lower case.
583 */
584 wxString Lower();
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 */
606 bool Matches(const wxString& mask);
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
FM
616 Alloc()
617
618 Shrink()
619
620 wxStringBuffer
621
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 */
4cc4bfaf 630 wxString Mid(size_t first, size_t count = wxSTRING_MAXLEN);
23324ae1
FM
631
632 /**
633 Other string functions.
23324ae1
FM
634 Trim()
635
636 Truncate()
637
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:
4cc4bfaf 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
FM
700 */
701 wxString Right(size_t count);
702
703 /**
7c913512 704 These functions replace the standard @e strchr() and @e strstr()
23324ae1 705 functions.
23324ae1
FM
706 Find()
707
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 */
4cc4bfaf 730 bool StartsWith(const wxString& prefix, wxString rest = NULL);
23324ae1
FM
731
732 /**
733 These functions return the string length and check whether the string is empty
734 or empty it.
23324ae1
FM
735 Len()
736
737 IsEmpty()
738
739 @ref operatornot() operator!
740
741 Empty()
742
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 */
753 wxString Strip(stripType s = trailing);
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 */
761 wxString SubString(size_t from, size_t to);
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
FM
767 Mid()
768
769 @ref operatorparenth() operator
770
771 Left()
772
773 Right()
774
775 BeforeFirst()
776
777 BeforeLast()
778
779 AfterFirst()
780
781 AfterLast()
782
783 StartsWith()
784
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.
23324ae1
FM
794 This function is new since wxWidgets version 2.8.4
795
4cc4bfaf 796 @see wxString::From8BitData
23324ae1
FM
797 */
798 const char* To8BitData();
7c913512 799 const wxCharBuffer To8BitData();
23324ae1
FM
800 //@}
801
802 //@{
803 /**
804 Converts the string to an ASCII, 7-bit string in the form of
805 a wxCharBuffer (Unicode builds only) or a C string (ANSI builds).
23324ae1
FM
806 Note that this conversion only works if the string contains only ASCII
807 characters. The @ref mbstr() mb_str method provides more
808 powerful means of converting wxString to C string.
809 */
810 const char* ToAscii();
7c913512 811 const wxCharBuffer ToAscii();
23324ae1
FM
812 //@}
813
814 /**
815 Attempts to convert the string to a floating point number. Returns @true on
816 success (the number is stored in the location pointed to by @e val) or @false
4cc4bfaf 817 if the string does not represent such number (the value of @a val is not
23324ae1
FM
818 modified in this case).
819
4cc4bfaf 820 @see ToLong(), ToULong()
23324ae1
FM
821 */
822 bool ToDouble(double val);
823
824 /**
825 Attempts to convert the string to a signed integer in base @e base. Returns
826 @true on success in which case the number is stored in the location
4cc4bfaf
FM
827 pointed to by @a val or @false if the string does not represent a
828 valid number in the given base (the value of @a val is not modified
23324ae1 829 in this case).
4cc4bfaf 830 The value of @a base must be comprised between 2 and 36, inclusive, or
23324ae1
FM
831 be a special value 0 which means that the usual rules of @c C numbers are
832 applied: if the number starts with @c 0x it is considered to be in base
833 16, if it starts with @c 0 - in base 8 and in base 10 otherwise. Note
834 that you may not want to specify the base 0 if you are parsing the numbers
835 which may have leading zeroes as they can yield unexpected (to the user not
836 familiar with C) results.
837
4cc4bfaf 838 @see ToDouble(), ToULong()
23324ae1
FM
839 */
840 bool ToLong(long val, int base = 10);
841
842 /**
843 This is exactly the same as ToLong() but works with 64
844 bit integer numbers.
23324ae1
FM
845 Notice that currently it doesn't work (always returns @false) if parsing of 64
846 bit numbers is not supported by the underlying C run-time library. Compilers
847 with C99 support and Microsoft Visual C++ version 7 and higher do support this.
848
4cc4bfaf 849 @see ToLong(), ToULongLong()
23324ae1
FM
850 */
851 bool ToLongLong(wxLongLong_t val, int base = 10);
852
853 /**
854 Attempts to convert the string to an unsigned integer in base @e base.
855 Returns @true on success in which case the number is stored in the
4cc4bfaf
FM
856 location pointed to by @a val or @false if the string does not
857 represent a valid number in the given base (the value of @a val is not
23324ae1
FM
858 modified in this case). Please notice that this function
859 behaves in the same way as the standard @c strtoul() and so it simply
860 converts negative numbers to unsigned representation instead of rejecting them
861 (e.g. -1 is returned as @c ULONG_MAX).
23324ae1 862 See ToLong() for the more detailed
4cc4bfaf 863 description of the @a base parameter.
23324ae1 864
4cc4bfaf 865 @see ToDouble(), ToLong()
23324ae1
FM
866 */
867 bool ToULong(unsigned long val, int base = 10);
868
869 /**
870 This is exactly the same as ToULong() but works with 64
871 bit integer numbers.
23324ae1
FM
872 Please see ToLongLong() for additional remarks.
873 */
874 bool ToULongLong(wxULongLong_t val, int base = 10);
875
876 //@{
877 /**
878 Same as @ref wxString::utf8str utf8_str.
879 */
880 const char* ToUTF8();
7c913512 881 const wxCharBuffer ToUF8();
23324ae1
FM
882 //@}
883
884 /**
885 Removes white-space (space, tabs, form feed, newline and carriage return) from
886 the left or from the right end of the string (right is default).
887 */
4cc4bfaf 888 wxString Trim(bool fromRight = true);
23324ae1
FM
889
890 /**
891 Truncate the string to the given length.
892 */
893 wxString Truncate(size_t len);
894
895 //@{
896 /**
897 Puts the string back into a reasonable state (in which it can be used
898 normally), after
899 GetWriteBuf() was called.
4cc4bfaf 900 The version of the function without the @a len parameter will calculate the
23324ae1
FM
901 new string length itself assuming that the string is terminated by the first
902 @c NUL character in it while the second one will use the specified length
903 and thus is the only version which should be used with the strings with
7c913512 904 embedded @c NULs (it is also slightly more efficient as @c strlen()
23324ae1 905 doesn't have to be called).
23324ae1
FM
906 This method is deprecated, please use
907 wxStringBuffer or
908 wxStringBufferLength instead.
909 */
910 void UngetWriteBuf();
7c913512 911 void UngetWriteBuf(size_t len);
23324ae1
FM
912 //@}
913
914 /**
915 Returns this string converted to upper case.
916 */
917 wxString Upper();
918
919 /**
920 The same as MakeUpper.
23324ae1
FM
921 This is a wxWidgets 1.xx compatibility function; you should not use it in new
922 code.
923 */
924 void UpperCase();
925
926 /**
927 Both formatted versions (wxString::Printf) and stream-like
7c913512 928 insertion operators exist (for basic types only). Additionally, the
23324ae1
FM
929 Format() function allows to use simply append
930 formatted value to a string:
4cc4bfaf 931
23324ae1
FM
932 Format()
933
934 FormatV()
935
936 Printf()
937
938 PrintfV()
939
940 @ref operatorout() "operator "
941 */
942
943
944 /**
945 Returns a pointer to the string data (@c const char* in ANSI build,
946 @c const wchar_t* in Unicode build).
23324ae1
FM
947 Note that the returned value is not convertible to @c char* or
948 @c wchar_t*, use @ref charstr() char_str or
949 @ref wcharstr() wchar_string if you need to pass string value
950 to a function expecting non-const pointer.
951
4cc4bfaf
FM
952 @see @ref mbstr() mb_str, @ref wcstr() wc_str, @ref
953 fnstr() fn_str, @ref charstr() char_str, @ref
954 wcharstr() wchar_string
23324ae1 955 */
4cc4bfaf 956 const wxChar* c_str();
23324ae1
FM
957
958 /**
959 Returns an object with string data that is implicitly convertible to
960 @c char* pointer. Note that any change to the returned buffer is lost and so
961 this function is only usable for passing strings to legacy libraries that
962 don't have const-correct API. Use wxStringBuffer if
963 you want to modify the string.
964
4cc4bfaf
FM
965 @see @ref mbstr() mb_str, @ref wcstr() wc_str, @ref
966 fnstr() fn_str, @ref cstr() c_str, @ref
967 wcharstr() wchar_str
23324ae1
FM
968 */
969 wxWritableCharBuffer char_str(const wxMBConv& conv = wxConvLibc);
970
971 //@{
972 /**
973 Returns string representation suitable for passing to OS' functions for
974 file handling. In ANSI build, this is same as @ref cstr() c_str.
975 In Unicode build, returned value can be either wide character string
976 or C string in charset matching the @c wxConvFileName object, depending on
977 the OS.
978
4cc4bfaf 979 @see wxMBConv, @ref wcstr() wc_str, @ref wcstr() mb_str
23324ae1
FM
980 */
981 const wchar_t* fn_str();
7c913512
FM
982 const char* fn_str();
983 const wxCharBuffer fn_str();
23324ae1
FM
984 //@}
985
986 //@{
987 /**
988 Returns multibyte (C string) representation of the string.
989 In Unicode build, converts using @e conv's wxMBConv::cWC2MB
990 method and returns wxCharBuffer. In ANSI build, this function is same
991 as @ref cstr() c_str.
992 The macro wxWX2MBbuf is defined as the correct return type (without const).
993
4cc4bfaf
FM
994 @see wxMBConv, @ref cstr() c_str, @ref wcstr() wc_str, @ref
995 fnstr() fn_str, @ref charstr() char_str
23324ae1
FM
996 */
997 const char* mb_str(const wxMBConv& conv = wxConvLibc);
7c913512 998 const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc);
23324ae1
FM
999 //@}
1000
1001 /**
1002 Extraction from a stream.
1003 */
1004 friend istream operator(istream& is, wxString& str);
1005
1006 //@{
1007 /**
1008 These functions work as C++ stream insertion operators: they insert the given
1009 value into the string. Precision or format cannot be set using them, you can
7c913512 1010 use
23324ae1
FM
1011 Printf() for this.
1012 */
1013 wxString operator(const wxString& str);
7c913512
FM
1014 wxString operator(const wxChar* psz);
1015 wxString operator(wxChar ch);
1016 wxString operator(int i);
1017 wxString operator(float f);
1018 wxString operator(double d);
23324ae1
FM
1019 //@}
1020
1021 /**
1022 Same as Mid (substring extraction).
1023 */
1024 wxString operator ()(size_t start, size_t len);
1025
1026 //@{
1027 /**
1028 Concatenation: all these operators return a new string equal to the
1029 concatenation of the operands.
1030 */
1031 wxString operator +(const wxString& x, const wxString& y);
7c913512
FM
1032 wxString operator +(const wxString& x, const wxChar* y);
1033 wxString operator +(const wxString& x, wxChar y);
1034 wxString operator +(const wxChar* x, const wxString& y);
23324ae1
FM
1035 //@}
1036
1037 //@{
1038 /**
1039 Concatenation in place: the argument is appended to the string.
1040 */
1041 void operator +=(const wxString& str);
7c913512
FM
1042 void operator +=(const wxChar* psz);
1043 void operator +=(wxChar c);
23324ae1
FM
1044 //@}
1045
1046 //@{
1047 /**
1048 Assignment: the effect of each operation is the same as for the corresponding
1049 constructor (see @ref construct() "wxString constructors").
1050 */
1051 wxString operator =(const wxString& str);
7c913512
FM
1052 wxString operator =(const wxChar* psz);
1053 wxString operator =(wxChar c);
23324ae1
FM
1054 //@}
1055
1056 //@{
1057 /**
1058 Element extraction.
1059 */
1060 wxChar operator [](size_t i);
7c913512
FM
1061 wxChar operator [](size_t i);
1062 wxChar operator [](int i);
1063 wxChar operator [](int i);
23324ae1
FM
1064 //@}
1065
1066 /**
1067 Implicit conversion to a C string.
1068 */
7c913512 1069 operator const wxChar*();
23324ae1
FM
1070
1071 /**
1072 Empty string is @false, so !string will only return @true if the string is
1073 empty.
1074 This allows the tests for @NULLness of a @e const wxChar * pointer and emptiness
1075 of the string to look the same in the code and makes it easier to port old code
1076 to wxString.
23324ae1
FM
1077 See also IsEmpty().
1078 */
1079 bool operator!();
1080
1081 /**
1082 The supported functions are only listed here, please see any STL reference for
1083 their documentation.
1084 */
1085
1086
1087 //@{
1088 /**
1089 Converts the strings contents to UTF-8 and returns it either as a temporary
1090 wxCharBuffer object or as a pointer to the internal string contents in
1091 UTF-8 build.
1092 */
1093 const char* utf8_str();
7c913512 1094 const wxCharBuffer utf8_str();
23324ae1
FM
1095 //@}
1096
1097 //@{
1098 /**
1099 Returns wide character representation of the string.
1100 In ANSI build, converts using @e conv's wxMBConv::cMB2WC
1101 method and returns wxWCharBuffer. In Unicode build, this function is same
1102 as @ref cstr() c_str.
1103 The macro wxWX2WCbuf is defined as the correct return type (without const).
1104
4cc4bfaf
FM
1105 @see wxMBConv, @ref cstr() c_str, @ref wcstr() mb_str, @ref
1106 fnstr() fn_str, @ref wcharstr() wchar_str
23324ae1
FM
1107 */
1108 const wchar_t* wc_str(const wxMBConv& conv);
7c913512 1109 const wxWCharBuffer wc_str(const wxMBConv& conv);
23324ae1
FM
1110 //@}
1111
1112 /**
1113 Returns an object with string data that is implicitly convertible to
1114 @c char* pointer. Note that changes to the returned buffer may or may
1115 not be lost (depending on the build) and so this function is only usable for
1116 passing strings to legacy libraries that don't have const-correct API. Use
1117 wxStringBuffer if you want to modify the string.
1118
4cc4bfaf
FM
1119 @see @ref mbstr() mb_str, @ref wcstr() wc_str, @ref
1120 fnstr() fn_str, @ref cstr() c_str, @ref
1121 charstr() char_str
23324ae1
FM
1122 */
1123 wxWritableWCharBuffer wchar_str();
1124
1125 /**
1126 These functions are deprecated, please consider using new wxWidgets 2.0
1127 functions instead of them (or, even better, std::string compatible variants).
23324ae1
FM
1128 CompareTo()
1129
1130 Contains()
1131
1132 First()
1133
1134 Freq()
1135
1136 Index()
1137
1138 IsAscii()
1139
1140 IsNull()
1141
1142 IsNumber()
1143
1144 IsWord()
1145
1146 Last()
1147
1148 Length()
1149
1150 LowerCase()
1151
1152 Remove()
1153
1154 Strip()
1155
1156 SubString()
1157
1158 UpperCase()
1159 */
1160};
1161
1162
1163/**
1164 @class wxStringBufferLength
1165 @wxheader{string.h}
7c913512
FM
1166
1167 This tiny class allows to conveniently access the wxString
23324ae1
FM
1168 internal buffer as a writable pointer without any risk of forgetting to restore
1169 the string to the usable state later, and allows the user to set the internal
1170 length of the string.
7c913512
FM
1171
1172 For example, assuming you have a low-level OS function called
23324ae1
FM
1173 @c int GetMeaningOfLifeAsString(char *) copying the value in the provided
1174 buffer (which must be writable, of course), and returning the actual length
1175 of the string, you might call it like this:
7c913512 1176
23324ae1
FM
1177 @code
1178 wxString theAnswer;
1179 wxStringBuffer theAnswerBuffer(theAnswer, 1024);
1180 int nLength = GetMeaningOfLifeAsString(theAnswerBuffer);
1181 theAnswerBuffer.SetLength(nLength);
1182 if ( theAnswer != "42" )
1183 {
1184 wxLogError("Something is very wrong!");
1185 }
1186 @endcode
7c913512 1187
23324ae1
FM
1188 Note that the exact usage of this depends on whether on not wxUSE_STL is
1189 enabled. If
1190 wxUSE_STL is enabled, wxStringBuffer creates a separate empty character buffer,
1191 and
1192 if wxUSE_STL is disabled, it uses GetWriteBuf() from wxString, keeping the same
1193 buffer
1194 wxString uses intact. In other words, relying on wxStringBuffer containing the
7c913512 1195 old
23324ae1
FM
1196 wxString data is probably not a good idea if you want to build your program in
1197 both
1198 with and without wxUSE_STL.
7c913512 1199
23324ae1 1200 Note that SetLength @c must be called before wxStringBufferLength destructs.
7c913512 1201
23324ae1
FM
1202 @library{wxbase}
1203 @category{FIXME}
1204*/
7c913512 1205class wxStringBufferLength
23324ae1
FM
1206{
1207public:
1208 /**
1209 Constructs a writable string buffer object associated with the given string
4cc4bfaf 1210 and containing enough space for at least @a len characters. Basically, this
23324ae1
FM
1211 is equivalent to calling wxString::GetWriteBuf and
1212 saving the result.
1213 */
1214 wxStringBufferLength(const wxString& str, size_t len);
1215
1216 /**
7c913512 1217 Restores the string passed to the constructor to the usable state by calling
23324ae1
FM
1218 wxString::UngetWriteBuf on it.
1219 */
1220 ~wxStringBufferLength();
1221
1222 /**
7c913512 1223 Sets the internal length of the string referred to by wxStringBufferLength to
4cc4bfaf 1224 @a nLength characters.
23324ae1
FM
1225 Must be called before wxStringBufferLength destructs.
1226 */
1227 void SetLength(size_t nLength);
1228
1229 /**
1230 Returns the writable pointer to a buffer of the size at least equal to the
1231 length specified in the constructor.
1232 */
4cc4bfaf 1233 wxChar* operator wxChar *();
23324ae1
FM
1234};
1235
1236
1237// ============================================================================
1238// Global functions/macros
1239// ============================================================================
1240
1241//@{
1242/**
1243 Converts its argument to string.
1244 See also: wxFromString.
1245*/
1246wxString wxToString(const wxColour& col);
7c913512 1247wxString wxToString(const wxFont& col);
23324ae1
FM
1248//@}
1249
1250//@{
1251/**
1252 Converts string to the type of the second argument. Returns @true on success.
1253 See also: wxToString.
1254*/
1255bool wxFromString(const wxString& str, wxColour* col);
7c913512 1256bool wxFromString(const wxString& str, wxFont* col);
23324ae1
FM
1257//@}
1258