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