X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/529e491ce038ad3a3aef4f3247ff7e16767d9734..5ec18516f68f0fd34dff798b1ad7f63479adb714:/interface/wx/string.h diff --git a/interface/wx/string.h b/interface/wx/string.h index 3056895ba7..93ff1cbee8 100644 --- a/interface/wx/string.h +++ b/interface/wx/string.h @@ -3,7 +3,7 @@ // Purpose: interface of wxStringBuffer, wxString // Author: wxWidgets team // RCS-ID: $Id$ -// Licence: wxWindows license +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -66,7 +66,7 @@ @stdobjects ::wxEmptyString - @see @ref overview_string, @ref overview_unicode, + @see @ref overview_string, @ref overview_unicode, @ref group_funcmacro_string "String-related functions", wxUString, wxCharBuffer, wxUniChar, wxStringTokenizer, wxStringBuffer, wxStringBufferLength */ @@ -75,7 +75,7 @@ class wxString public: /** @name Standard types - + Types used with wxString. */ //@{ @@ -96,11 +96,11 @@ public: a single character or a wide (Unicode) string. For all constructors (except the default which creates an empty string) there is also a corresponding assignment operator. - + See also the assign() STL-like function. */ //@{ - + /** Default constructor */ @@ -112,6 +112,26 @@ public: */ wxString(const wxString& stringSrc); + /** + Construct a string consisting of @a nRepeat copies of ch. + */ + wxString(wxUniChar ch, size_t nRepeat = 1); + + /** + Construct a string consisting of @a nRepeat copies of ch. + */ + wxString(wxUniCharRef ch, size_t nRepeat = 1); + + /** + Construct a string consisting of @a nRepeat copies of ch + converted to Unicode using the current locale encoding. + */ + wxString(char ch, size_t nRepeat = 1); + + /** + Construct a string consisting of @a nRepeat copies of ch. + */ + wxString(wchar_t ch, size_t nRepeat = 1); /** Constructs a string from the string literal @a psz using @@ -161,14 +181,18 @@ public: /** Constructs a string from @a str using the using the current locale encoding to convert it to Unicode (wxConvLibc). + + @see ToStdString() */ wxString(const std::string& str); /** Constructs a string from @a str. + + @see ToStdWstring() */ wxString(const std::wstring& str); - + /** String destructor. @@ -187,7 +211,7 @@ public: wxString operator =(wxUniChar c); //@} - + /** @@ -195,11 +219,11 @@ public: These functions return the string length and/or check whether the string is empty. - + See also the length(), size() or empty() STL-like functions. */ //@{ - + /** Returns the length of the string. @@ -240,8 +264,8 @@ public: /** @member_group_name{ch_access, Character access} - Many functions below take a character index in the string. - As with C strings and arrays, the indices start from 0, so the first character + Many functions below take a character index in the string. + As with C strings and arrays, the indices start from 0, so the first character of a string is string[0]. An attempt to access a character beyond the end of the string (which may even be 0 if the string is empty) will provoke an assert failure in @ref overview_debugging "debug builds", but no checks are @@ -266,10 +290,10 @@ public: /** Returns a writable buffer of at least @a len bytes. - + It returns a pointer to a new memory block, and the existing data will not be copied. Call UngetWriteBuf() as soon as possible to put the string back into a reasonable state. - + This method is deprecated, please use wxStringBuffer or wxStringBufferLength instead. */ wxStringCharType* GetWriteBuf(size_t len); @@ -288,28 +312,28 @@ public: This method is deprecated, please use wxStringBuffer or wxStringBufferLength instead. */ void UngetWriteBuf(); - + /** @overload */ void UngetWriteBuf(size_t len); - + /** Sets the character at position @e n. */ void SetChar(size_t n, wxUniChar ch); /** - Returns a the last character. - + Returns the last character. + This is a wxWidgets 1.xx compatibility function; you should not use it in new code. */ wxUniChar Last() const; - + /** Returns a reference to the last character (writable). - + This is a wxWidgets 1.xx compatibility function; you should not use it in new code. */ @@ -324,13 +348,13 @@ public: Returns a writable reference to the @a i-th character of the string. */ wxUniCharRef operator [](size_t i); - + //@} - + /** @member_group_name{conv, Conversions} - + This section contains both implicit and explicit conversions to C style strings. Although implicit conversion is quite convenient, you are advised to use wc_str() for the sake of clarity. @@ -394,7 +418,7 @@ public: for file handling. */ const wchar_t* fn_str() const; - + /** @overload */ @@ -420,15 +444,10 @@ public: @see wc_str(), c_str(), mb_str() */ - const char* utf8_str() const; - - /** - @overload - */ - const wxCharBuffer utf8_str() const; + const wxScopedCharBuffer utf8_str() const; /** - Converts the strings contents to the wide character represention + Converts the strings contents to the wide character representation and returns it as a temporary wxWCharBuffer object (Unix and OS X) or returns a pointer to the internal string contents in wide character mode (Windows). @@ -460,7 +479,7 @@ public: wchar_t* or UTF-8-encoded char*, depending on the build). */ const wxStringCharType *wx_str() const; - + /** Converts the string to an 8-bit string in ISO-8859-1 encoding in the form of a wxCharBuffer (Unicode builds only). @@ -473,12 +492,7 @@ public: @see wxString::From8BitData() */ - const char* To8BitData() const; - - /** - @overload - */ - const wxCharBuffer To8BitData() const; + const wxScopedCharBuffer To8BitData() const; /** Converts the string to an ASCII, 7-bit string in the form of @@ -495,14 +509,39 @@ public: const wxCharBuffer ToAscii() const; /** - Same as utf8_str(). + Return the string as an std::string in current locale encoding. + + Note that if the conversion of (Unicode) string contents to the current + locale fails, the return string will be empty. Be sure to check for + this to avoid silent data loss. + + Instead of using this function it's also possible to write + @code + std::string s; + wxString wxs; + ... + s = std::string(wxs); + @endcode + but using ToStdString() may make the code more clear. + + @since 2.9.1 */ - const char* ToUTF8() const; + std::string ToStdString() const; /** - @overload + Return the string as an std::wstring. + + Unlike ToStdString(), there is no danger of data loss when using this + function. + + @since 2.9.1 + */ + std::wstring ToStdWstring() const; + + /** + Same as utf8_str(). */ - const wxCharBuffer ToUTF8() const; + const wxScopedCharBuffer ToUTF8() const; //@} @@ -511,9 +550,9 @@ public: @member_group_name{concat, Concatenation} Almost anything may be concatenated (appended to) with a string! - - Note that the various operator<<() overloads work as C++ stream insertion - operators. They insert the given value into the string. + + Note that the various operator<<() overloads work as C++ stream insertion + operators. They insert the given value into the string. Precision and format cannot be set using them. Use Printf() instead. See also the insert() and append() STL-like functions. @@ -559,7 +598,7 @@ public: Concatenation: returns a new string equal to the concatenation of the operands. */ wxString operator +(const wxString& x, const wxString& y); - + /** @overload */ @@ -574,6 +613,7 @@ public: wxString& operator<<(wchar_t ch); wxString& operator<<(const wxCharBuffer& s); wxString& operator<<(const wxWCharBuffer& s); + wxString& operator<<(wxUniChar ch); wxString& operator<<(wxUniCharRef ch); wxString& operator<<(unsigned int ui); wxString& operator<<(long l); @@ -587,14 +627,14 @@ public: Concatenation in place: the argument is appended to the string. */ void operator +=(const wxString& str); - + /** @overload */ void operator +=(wxUniChar c); - + //@} - + /** @member_group_name{cmp, Comparison} @@ -612,7 +652,7 @@ public: StartsWith() is helpful when parsing a line of text which should start with some predefined prefix and is more efficient than doing direct string comparison as you would also have to precalculate the length of the prefix. - + See also the compare() STL-like function. */ //@{ @@ -638,19 +678,26 @@ public: int CmpNoCase(const wxString& s) const; /** - Test whether the string is equal to the single character @a c. - + Test whether the string is equal to another string @a s. + The test is case-sensitive if @a caseSensitive is @true (default) or not if it is @false. - - Returns @true if the string is equal to the character, @false otherwise. - + + @return @true if the string is equal to the other one, @false otherwise. + @see Cmp(), CmpNoCase() */ - bool IsSameAs(const wxString &s, bool caseSensitive = true) const; - + bool IsSameAs(const wxString& s, bool caseSensitive = true) const; + /** - @overload + Test whether the string is equal to the single character @a ch. + + The test is case-sensitive if @a caseSensitive is @true (default) or not if it is + @false. + + @return @true if the string is equal to this character, @false otherwise. + + @see Cmp(), CmpNoCase() */ bool IsSameAs(wxUniChar ch, bool caseSensitive = true) const; @@ -661,9 +708,9 @@ public: /** This function can be used to test if the string starts with the specified - @a prefix. - - If it does, the function will return @true and put the rest of the string + @a prefix. + + If it does, the function will return @true and put the rest of the string (i.e. after the prefix) into @a rest string if it is not @NULL. Otherwise, the function returns @false and doesn't modify the @a rest. */ @@ -677,17 +724,17 @@ public: modify the @e rest. */ bool EndsWith(const wxString& suffix, wxString *rest = NULL) const; - + //@} - - + + /** @member_group_name{substring, Substring extraction} These functions allow you to extract a substring from the string. The original string is not modified and the function returns the extracted substring. - + See also the at() and the substr() STL-like functions. */ @@ -700,12 +747,12 @@ public: /** Returns the part of the string between the indices @a from and @a to inclusive. - + This is a wxWidgets 1.xx compatibility function, use Mid() instead (but note that parameters have different meaning). */ wxString SubString(size_t from, size_t to) const; - + /** Same as Mid() (substring extraction). */ @@ -736,18 +783,36 @@ public: /** Gets all characters before the first occurrence of @e ch. Returns the whole string if @a ch is not found. + + @param ch The character to look for. + @param rest Filled with the part of the string following the first + occurrence of @a ch or cleared if it was not found. The same string + is returned by AfterFirst() but it is more efficient to use this + output parameter if both the "before" and "after" parts are needed + than calling both functions one after the other. This parameter is + available in wxWidgets version 2.9.2 and later only. + @return Part of the string before the first occurrence of @a ch. */ - wxString BeforeFirst(wxUniChar ch) const; + wxString BeforeFirst(wxUniChar ch, wxString *rest = NULL) const; /** Gets all characters before the last occurrence of @e ch. Returns the empty string if @a ch is not found. + + @param ch The character to look for. + @param rest Filled with the part of the string following the last + occurrence of @a ch or the copy of this string if it was not found. + The same string is returned by AfterLast() but it is more efficient + to use this output parameter if both the "before" and "after" parts + are needed than calling both functions one after the other. This + parameter is available in wxWidgets version 2.9.2 and later only. + @return Part of the string before the last occurrence of @a ch. */ - wxString BeforeLast(wxUniChar ch) const; - + wxString BeforeLast(wxUniChar ch, wxString *rest = NULL) const; + //@} - - + + /** @member_group_name{caseconv, Case conversion} @@ -806,7 +871,7 @@ public: @see Upper() */ wxString& MakeUpper(); - + /** Returns this string converted to upper case. @@ -821,35 +886,35 @@ public: code. */ void UpperCase(); - + //@} - - + + /** @member_group_name{search, Searching and replacing} These functions replace the standard @c strchr() and @c strstr() functions. - + See also the find(), rfind(), replace() STL-like functions. */ //@{ /** - Searches for the given character @a ch. + Searches for the given character @a ch. Returns the position or @c wxNOT_FOUND if not found. */ int Find(wxUniChar ch, bool fromEnd = false) const; /** - Searches for the given string @a sub. + Searches for the given string @a sub. Returns the starting position or @c wxNOT_FOUND if not found. */ int Find(const wxString& sub) const; /** Same as Find(). - + This is a wxWidgets 1.xx compatibility function; you should not use it in new code. */ @@ -857,7 +922,7 @@ public: /** Same as Find(). - + This is a wxWidgets 1.xx compatibility function; you should not use it in new code. */ @@ -865,15 +930,15 @@ public: /** Replace first (or all) occurrences of substring with another one. - + @param strOld The string to search for replacing. @param strNew The substitution string. @param replaceAll - If @true a global replace will be done (default), otherwise only the + If @true a global replace will be done (default), otherwise only the first occurrence will be replaced. - + Returns the number of replacements made. */ size_t Replace(const wxString& strOld, const wxString& strNew, @@ -887,33 +952,44 @@ public: @member_group_name{numconv, Conversion to numbers} The string provides functions for conversion to signed and unsigned integer and - floating point numbers. All functions take a pointer to the variable to - put the numeric value in and return @true if the @b entire string could be - converted to a number. - */ + floating point numbers. + + All functions take a pointer to the variable to put the numeric value + in and return @true if the @b entire string could be converted to a + number. Notice if there is a valid number in the beginning of the + string, it is returned in the output parameter even if the function + returns @false because there is more text following it. + */ //@{ /** - Attempts to convert the string to a floating point number. - - Returns @true on success (the number is stored in the location pointed to by - @a val) or @false if the string does not represent such number (the value of - @a val is not modified in this case). - + Attempts to convert the string to a floating point number. + + Returns @true on success (the number is stored in the location pointed to by + @a val) or @false if the string does not represent such number (the value of + @a val may still be modified in this case). + Note that unlike ToCDouble() this function uses a localized version of @c wxStrtod() and thus needs as decimal point (and thousands separator) the locale-specific decimal point. Thus you should use this function only when you are sure that this string contains a floating point number formatted with the rules of the locale currently in use (see wxLocale). - - Refer to the docs of the standard function @c strtod() for more details about - the supported syntax. + + Also notice that even this function is locale-specific it does not + support strings with thousands separators in them, even if the current + locale uses digits grouping. You may use wxNumberFormatter::FromString() + to parse such strings. + + Please refer to the documentation of the standard function @c strtod() + for more details about the supported syntax. @see ToCDouble(), ToLong(), ToULong() */ bool ToDouble(double* val) const; /** + Variant of ToDouble() always working in "C" locale. + Works like ToDouble() but unlike it this function expects the floating point number to be formatted always with the rules dictated by the "C" locale (in particular, the decimal point must be a dot), independently from the @@ -924,13 +1000,13 @@ public: bool ToCDouble(double* val) const; /** - Attempts to convert the string to a signed integer in base @a base. - + Attempts to convert the string to a signed integer in base @a base. + Returns @true on success in which case the number is stored in the location pointed to by @a val or @false if the string does not represent a - valid number in the given base (the value of @a val is not modified - in this case). - + valid number in the given base (the value of @a val may still be + modified in this case). + The value of @a base must be comprised between 2 and 36, inclusive, or be a special value 0 which means that the usual rules of @c C numbers are applied: if the number starts with @c 0x it is considered to be in base @@ -938,22 +1014,28 @@ public: that you may not want to specify the base 0 if you are parsing the numbers which may have leading zeroes as they can yield unexpected (to the user not familiar with C) results. - + Note that unlike ToCLong() this function uses a localized version of - @c wxStrtol(). Thus you should use this function only when you are sure + @c wxStrtol(). Thus you should use this function only when you are sure that this string contains an integer number formatted with the rules of the locale currently in use (see wxLocale). - - Refer to the docs of the standard function @c strtol() for more details about - the supported syntax. + + As with ToDouble(), this function does not support strings containing + thousands separators even if the current locale uses digits grouping. + You may use wxNumberFormatter::FromString() to parse such strings. + + Please refer to the documentation of the standard function @c strtol() + for more details about the supported syntax. @see ToCDouble(), ToDouble(), ToULong() */ bool ToLong(long* val, int base = 10) const; /** + Variant of ToLong() always working in "C" locale. + Works like ToLong() but unlike it this function expects the integer - number to be formatted always with the rules dictated by the "C" locale, + number to be formatted always with the rules dictated by the "C" locale, independently from the current application-wide locale (see wxLocale). @see ToDouble(), ToLong(), ToULong() @@ -962,7 +1044,7 @@ public: /** This is exactly the same as ToLong() but works with 64 bit integer numbers. - + Notice that currently it doesn't work (always returns @false) if parsing of 64 bit numbers is not supported by the underlying C run-time library. Compilers with C99 support and Microsoft Visual C++ version 7 and higher do support this. @@ -973,11 +1055,11 @@ public: /** Attempts to convert the string to an unsigned integer in base @a base. - + Returns @true on success in which case the number is stored in the location pointed to by @a val or @false if the string does not - represent a valid number in the given base (the value of @a val is not - modified in this case). + represent a valid number in the given base (the value of @a val may + still be modified in this case). Please notice that this function behaves in the same way as the standard @c strtoul() and so it simply converts negative numbers to unsigned @@ -991,8 +1073,10 @@ public: bool ToULong(unsigned long* val, int base = 10) const; /** + Variant of ToULong() always working in "C" locale. + Works like ToULong() but unlike it this function expects the integer - number to be formatted always with the rules dictated by the "C" locale, + number to be formatted always with the rules dictated by the "C" locale, independently from the current application-wide locale (see wxLocale). @see ToDouble(), ToLong(), ToULong() @@ -1000,8 +1084,9 @@ public: bool ToCULong(unsigned long* val, int base = 10) const; /** - This is exactly the same as ToULong() but works with 64 - bit integer numbers. + This is exactly the same as ToULong() but works with 64 bit integer + numbers. + Please see ToLongLong() for additional remarks. */ bool ToULongLong(wxULongLong_t* val, int base = 10) const; @@ -1013,8 +1098,8 @@ public: @member_group_name{fmt, Formatting and printing} Both formatted versions (Printf/() and stream-like insertion operators - exist (for basic types only). - + exist (for basic types only). + See also the static Format() and FormatV() functions. */ //@{ @@ -1025,6 +1110,16 @@ public: Note that if @c wxUSE_PRINTF_POS_PARAMS is set to 1, then this function supports Unix98-style positional parameters: + @code + wxString str; + + str.Printf(wxT("%d %d %d"), 1, 2, 3); + // str now contains "1 2 3" + + str.Printf(wxT("%2$d %3$d %1$d"), 1, 2, 3); + // str now contains "2 3 1" + @endcode + @note This function will use a safe version of @e vsprintf() (usually called @e vsnprintf()) whenever available to always allocate the buffer of correct size. Unfortunately, this function is not available on all platforms and the @@ -1040,20 +1135,20 @@ public: int PrintfV(const wxString& pszFormat, va_list argPtr); //@} - - + + /** @member_group_name{mem, Memory management} - The following are "advanced" functions and they will be needed rarely. - Alloc() and Shrink() are only interesting for optimization purposes. - wxStringBuffer and wxStringBufferLength classes may be very useful when working + The following are "advanced" functions and they will be needed rarely. + Alloc() and Shrink() are only interesting for optimization purposes. + wxStringBuffer and wxStringBufferLength classes may be very useful when working with some external API which requires the caller to provide a writable buffer. - + See also the reserve() and resize() STL-like functions. */ //@{ - + /** Preallocate enough space for wxString to store @a nLen characters. @@ -1115,11 +1210,11 @@ public: /** Empties the string and frees memory occupied by it. - + @see Empty() */ void Clear(); - + //@} @@ -1133,21 +1228,21 @@ public: /** Returns @true if target appears anywhere in wxString; else @false. - + This is a wxWidgets 1.xx compatibility function; you should not use it in new code. */ bool Contains(const wxString& str) const; /** Makes the string empty, but doesn't free memory occupied by the string. - + @see Clear(). */ void Empty(); /** Returns the number of occurrences of @e ch in the string. - + This is a wxWidgets 1.xx compatibility function; you should not use it in new code. */ int Freq(wxUniChar ch) const; @@ -1163,14 +1258,14 @@ public: /** Returns @true if the string is an integer (with possible sign). - + This is a wxWidgets 1.xx compatibility function; you should not use it in new code. */ bool IsNumber() const; /** Returns @true if the string is a word. - + This is a wxWidgets 1.xx compatibility function; you should not use it in new code. */ bool IsWord() const; @@ -1178,22 +1273,22 @@ public: /** Adds @a count copies of @a chPad to the beginning, or to the end of the string (the default). - + Removes spaces from the left or from the right (default). */ wxString& Pad(size_t count, wxUniChar chPad = ' ', bool fromRight = true); - + /** Removes all characters from the string starting at @a pos. Use Truncate() as a more readable alternative. - + This is a wxWidgets 1.xx compatibility function; you should not use it in new code. */ wxString& Remove(size_t pos); - + /** Removes @a len characters from the string, starting at @a pos. - + This is a wxWidgets 1.xx compatibility function; you should not use it in new code. */ wxString& Remove(size_t pos, size_t len); @@ -1204,10 +1299,10 @@ public: wxString& RemoveLast(size_t n = 1); /** - Strip characters at the front and/or end. - + Strip characters at the front and/or end. + This is the same as Trim() except that it doesn't change this string. - + This is a wxWidgets 1.xx compatibility function; you should not use it in new code. */ wxString Strip(stripType s = trailing) const; @@ -1222,7 +1317,7 @@ public: Truncate the string to the given length. */ wxString& Truncate(size_t len); - + //@} @@ -1231,13 +1326,13 @@ public: /** @member_group_name{iter, Iterator interface} - These methods return iterators to the beginnnig or end of the string. - + These methods return iterators to the beginning or end of the string. + Please see any STL reference (e.g. http://www.cppreference.com/wiki/string/start) for their documentation. */ //@{ - + const_iterator begin() const; iterator begin(); const_iterator end() const; @@ -1247,7 +1342,7 @@ public: reverse_iterator rbegin(); const_reverse_iterator rend() const; reverse_iterator rend(); - + //@} @@ -1255,13 +1350,13 @@ public: /** @member_group_name{stl, STL interface} - The supported STL functions are listed here. - + The supported STL functions are listed here. + Please see any STL reference (e.g. http://www.cppreference.com/wiki/string/start) for their documentation. */ //@{ - + wxString& append(const wxString& str, size_t pos, size_t n); wxString& append(const wxString& str); wxString& append(const char *sz, size_t n); @@ -1372,13 +1467,13 @@ public: size_type size() const; wxString substr(size_t nStart = 0, size_t nLen = npos) const; void swap(wxString& str); - + //@} - + // STATIC FUNCTIONS - // Keep these functions separed from the other groups or Doxygen gets confused + // Keep these functions separated from the other groups or Doxygen gets confused // ----------------------------------------------------------------------------- /** @@ -1434,6 +1529,44 @@ public: static wxString FromAscii(char c); //@} + /** + Returns a string with the textual representation of the number in C + locale. + + Unlike FromDouble() the string returned by this function always uses + the period character as decimal separator, independently of the current + locale. Otherwise its behaviour is identical to the other function. + + @since 2.9.1 + + @see ToCDouble() + */ + static wxString FromCDouble(double val, int precision = -1); + + /** + Returns a string with the textual representation of the number. + + For the default value of @a precision, this function behaves as a + simple wrapper for @code wxString::Format("%g", val) @endcode. If @a + precision is positive (or zero), the @c %.Nf format is used with the + given precision value. + + Notice that the string returned by this function uses the decimal + separator appropriate for the current locale, e.g. @c "," and not a + period in French locale. Use FromCDouble() if this is unwanted. + + @param val + The value to format. + @param precision + The number of fractional digits to use in or -1 to use the most + appropriate format. This parameter is new in wxWidgets 2.9.2. + + @since 2.9.1 + + @see ToDouble() + */ + static wxString FromDouble(double val, int precision = -1); + //@{ /** Converts C string encoded in UTF-8 to wxString. @@ -1543,16 +1676,13 @@ wxString wxEmptyString; @code wxString theAnswer; - wxStringBuffer theAnswerBuffer(theAnswer, 1024); + wxStringBufferLength theAnswerBuffer(theAnswer, 1024); int nLength = GetMeaningOfLifeAsString(theAnswerBuffer); theAnswerBuffer.SetLength(nLength); if ( theAnswer != "42" ) wxLogError("Something is very wrong!"); @endcode - @todo - the example above does not make use of wxStringBufferLength?? - Note that the exact usage of this depends on whether or not wxUSE_STL is enabled. If wxUSE_STL is enabled, wxStringBuffer creates a separate empty character buffer, and if wxUSE_STL is disabled, it uses GetWriteBuf() from