diff --git a/docs/changes.txt b/docs/changes.txt index abad727d08..41c0899b87 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -288,6 +288,7 @@ All: - Fix reading/writing UTF-7-encoded text streams. - Corrected bug in wxTimeSpan::IsShorterThan() for equal time spans. - Use std::unordered_{map,set} for wxHashMap/Set if available (Jan van Dijk). +- Added wxString::Capitalize() and MakeCapitalized(). All (Unix): diff --git a/include/wx/string.h b/include/wx/string.h index 0a72bfe320..35491795df 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -1704,12 +1704,17 @@ public: // convert to upper case in place, return the string itself wxString& MakeUpper(); // convert to upper case, return the copy of the string - // Here's something to remember: BC++ doesn't like returns in inlines. - wxString Upper() const ; + wxString Upper() const { return wxString(*this).MakeUpper(); } // convert to lower case in place, return the string itself wxString& MakeLower(); // convert to lower case, return the copy of the string - wxString Lower() const ; + wxString Lower() const { return wxString(*this).MakeLower(); } + // convert the first character to the upper case and the rest to the + // lower one, return the modified string itself + wxString& MakeCapitalized(); + // convert the first character to the upper case and the rest to the + // lower one, return the copy of the string + wxString Capitalize() const { return wxString(*this).MakeCapitalized(); } // trimming/padding whitespace (either side) and truncating // remove spaces from left or from right (default) side diff --git a/interface/wx/string.h b/interface/wx/string.h index 58ac520807..99b3c9aac1 100644 --- a/interface/wx/string.h +++ b/interface/wx/string.h @@ -27,10 +27,10 @@ @endcode 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 + enabled. If wxUSE_STL is enabled, wxStringBuffer creates a separate empty character buffer, and if wxUSE_STL is disabled, it uses GetWriteBuf() from - wxString, keeping the same buffer wxString uses intact. In other words, - relying on wxStringBuffer containing the old wxString data is not a good + wxString, keeping the same buffer wxString uses intact. In other words, + relying on wxStringBuffer containing the old wxString data is not a good idea if you want to build your program both with and without wxUSE_STL. @library{wxbase} @@ -75,16 +75,16 @@ public: internally even if wxUSE_STL is not defined. Since wxWidgets 3.0 wxString internally uses UCS-2 (basically 2-byte per - character wchar_t and nearly the same as UTF-16) under Windows and - UTF-8 under Unix, Linux and OS X to store its content. + character wchar_t and nearly the same as UTF-16) under Windows and + UTF-8 under Unix, Linux and OS X to store its content. Much work has been done to make existing code using ANSI string literals work as before. If you need to have a wxString that uses wchar_t on Unix and Linux, too, you can specify this on the command line with the @c configure @c --disable-utf8 switch. - + If you need a Unicode string class with O(1) access on all platforms you should consider using wxUString. - + Since iterating over a wxString by index can become inefficient in UTF-8 mode iterators should be used instead of index based access: @@ -154,7 +154,7 @@ public: failure in @ref overview_debugging "debug build", but no checks are done in release builds. This section also contains both implicit and explicit conversions to C style - strings. Although implicit conversion is quite convenient, you are advised + strings. Although implicit conversion is quite convenient, you are advised to use wc_str() for the sake of clarity. @li GetChar() @@ -226,7 +226,7 @@ public: @li Empty() @li Clear() - These functions allow you to extract a substring from the string. The + These functions allow you to extract a substring from the string. The original string is not modified and the function returns the extracted substring. @@ -463,6 +463,16 @@ public: wxString BeforeLast(wxUniChar ch) const; + /** + Return the copy of the string with the first string character in the + upper case and the subsequent ones in the lower case. + + @since 2.9.0 + + @see MakeCapitalized() + */ + wxString Capitalize() const; + /** Empties the string and frees memory occupied by it. See also: Empty() @@ -759,6 +769,8 @@ public: /** Returns this string converted to the lower case. + + @see MakeLower() */ wxString Lower() const; @@ -769,13 +781,27 @@ public: */ void LowerCase(); + /** + Converts the first characters of the string to the upper case and all + the subsequent ones to the lower case and returns the result. + + @since 2.9.0 + + @see Capitalize() + */ + wxString& MakeCapitalized(); + /** Converts all characters to lower case and returns the result. + + @see Lower() */ wxString& MakeLower(); /** Converts all characters to upper case and returns the result. + + @see Upper() */ wxString& MakeUpper(); @@ -1018,6 +1044,8 @@ public: /** Returns this string converted to upper case. + + @see MakeUpper() */ wxString Upper() const; @@ -1033,14 +1061,14 @@ public: convertible to both @c const @c char* and to @c const @c wchar_t*. Given this ambiguity it is mostly better to use wc_str(), mb_str() or utf8_str() instead. - + Please see the @ref overview_unicode "Unicode overview" for more information about it. - + Note that the returned value is not convertible to @c char* or @c wchar_t*, use char_str() or wchar_str() if you need to pass string value to a function expecting non-const pointer. - + @see wc_str(), utf8_str(), c_str(), mb_str(), fn_str() */ const wxCStrData c_str() const; @@ -1091,8 +1119,8 @@ public: /** Returns the multibyte (C string) representation of the string - using @e conv's wxMBConv::cWC2MB method and returns wxCharBuffer. - + using @e conv's wxMBConv::cWC2MB method and returns wxCharBuffer. + @see wc_str(), utf8_str(), c_str(), wxMBConv */ const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const; @@ -1180,7 +1208,7 @@ public: Converts the strings contents to UTF-8 and returns it either as a temporary wxCharBuffer object or as a pointer to the internal string contents in UTF-8 build. - + @see wc_str(), c_str(), mb_str() */ const char* utf8_str() const; @@ -1190,7 +1218,7 @@ public: //@{ /** Converts the strings contents to the wide character represention - and returns it as a temporary wxWCharBuffer object (Unix and OS X) + 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). @@ -1214,7 +1242,7 @@ public: */ wxWritableWCharBuffer wchar_str() const; - /** + /** Explicit conversion to C string in the internal representation (either wchar_t* or UTF-8-encoded char*, depending on the build). */ @@ -1373,10 +1401,10 @@ wxString wxEmptyString; @endcode 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 + enabled. If wxUSE_STL is enabled, wxStringBuffer creates a separate empty character buffer, and if wxUSE_STL is disabled, it uses GetWriteBuf() from - wxString, keeping the same buffer wxString uses intact. In other words, - relying on wxStringBuffer containing the old wxString data is not a good + wxString, keeping the same buffer wxString uses intact. In other words, + relying on wxStringBuffer containing the old wxString data is not a good idea if you want to build your program both with and without wxUSE_STL. Note that SetLength @c must be called before wxStringBufferLength destructs. diff --git a/src/common/string.cpp b/src/common/string.cpp index 362d682fb2..16f764f44a 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -1363,6 +1363,20 @@ wxString& wxString::MakeLower() return *this; } +wxString& wxString::MakeCapitalized() +{ + const iterator en = end(); + iterator it = begin(); + if ( it != en ) + { + *it = (wxChar)wxToupper(*it); + for ( ++it; it != en; ++it ) + *it = (wxChar)wxTolower(*it); + } + + return *this; +} + // --------------------------------------------------------------------------- // trimming and padding // --------------------------------------------------------------------------- @@ -1967,13 +1981,6 @@ int wxString::Freq(wxUniChar ch) const return count; } -// convert to upper case, return the copy of the string -wxString wxString::Upper() const -{ wxString s(*this); return s.MakeUpper(); } - -// convert to lower case, return the copy of the string -wxString wxString::Lower() const { wxString s(*this); return s.MakeLower(); } - // ---------------------------------------------------------------------------- // wxUTF8StringBuffer // ---------------------------------------------------------------------------- diff --git a/tests/strings/strings.cpp b/tests/strings/strings.cpp index faa4548375..09e0dca52c 100644 --- a/tests/strings/strings.cpp +++ b/tests/strings/strings.cpp @@ -366,14 +366,26 @@ void StringTestCase::CaseChanges() wxString s1l(s1); s1u.MakeUpper(); s1l.MakeLower(); + + CPPUNIT_ASSERT_EQUAL( _T("HELLO!"), s1u ); + CPPUNIT_ASSERT_EQUAL( _T("hello!"), s1l ); + wxString s2u, s2l; s2u.MakeUpper(); s2l.MakeLower(); - CPPUNIT_ASSERT( s1u == _T("HELLO!") ); - CPPUNIT_ASSERT( s1l == _T("hello!") ); - CPPUNIT_ASSERT( s2u == wxEmptyString ); - CPPUNIT_ASSERT( s2l == wxEmptyString ); + CPPUNIT_ASSERT_EQUAL( "", s2u ); + CPPUNIT_ASSERT_EQUAL( "", s2l ); + + + wxString s3("good bye"); + CPPUNIT_ASSERT_EQUAL( "Good bye", s3.Capitalize() ); + s3.MakeCapitalized(); + CPPUNIT_ASSERT_EQUAL( "Good bye", s3 ); + + CPPUNIT_ASSERT_EQUAL( "Abc", wxString("ABC").Capitalize() ); + + CPPUNIT_ASSERT_EQUAL( "", wxString().Capitalize() ); } void StringTestCase::Compare()