add wxString::Capitalize() and MakeCapitalized() for consistency with Upper/Lower() we already have

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@54915 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2008-08-01 23:39:11 +00:00
parent 4b7d829b21
commit 0c7db140c5
5 changed files with 87 additions and 34 deletions

View File

@@ -288,6 +288,7 @@ All:
- Fix reading/writing UTF-7-encoded text streams. - Fix reading/writing UTF-7-encoded text streams.
- Corrected bug in wxTimeSpan::IsShorterThan() for equal time spans. - Corrected bug in wxTimeSpan::IsShorterThan() for equal time spans.
- Use std::unordered_{map,set} for wxHashMap/Set if available (Jan van Dijk). - Use std::unordered_{map,set} for wxHashMap/Set if available (Jan van Dijk).
- Added wxString::Capitalize() and MakeCapitalized().
All (Unix): All (Unix):

View File

@@ -1704,12 +1704,17 @@ public:
// convert to upper case in place, return the string itself // convert to upper case in place, return the string itself
wxString& MakeUpper(); wxString& MakeUpper();
// convert to upper case, return the copy of the string // 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 { return wxString(*this).MakeUpper(); }
wxString Upper() const ;
// convert to lower case in place, return the string itself // convert to lower case in place, return the string itself
wxString& MakeLower(); wxString& MakeLower();
// convert to lower case, return the copy of the string // 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 // trimming/padding whitespace (either side) and truncating
// remove spaces from left or from right (default) side // remove spaces from left or from right (default) side

View File

@@ -27,10 +27,10 @@
@endcode @endcode
Note that the exact usage of this depends on whether or not wxUSE_STL is 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 character buffer, and if wxUSE_STL is disabled, it uses GetWriteBuf() from
wxString, keeping the same buffer wxString uses intact. In other words, wxString, keeping the same buffer wxString uses intact. In other words,
relying on wxStringBuffer containing the old wxString data is not a good 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. idea if you want to build your program both with and without wxUSE_STL.
@library{wxbase} @library{wxbase}
@@ -75,16 +75,16 @@ public:
internally even if wxUSE_STL is not defined. internally even if wxUSE_STL is not defined.
Since wxWidgets 3.0 wxString internally uses UCS-2 (basically 2-byte per 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 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. 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 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 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 and Linux, too, you can specify this on the command line with the
@c configure @c --disable-utf8 switch. @c configure @c --disable-utf8 switch.
If you need a Unicode string class with O(1) access on all platforms If you need a Unicode string class with O(1) access on all platforms
you should consider using wxUString. you should consider using wxUString.
Since iterating over a wxString by index can become inefficient in UTF-8 Since iterating over a wxString by index can become inefficient in UTF-8
mode iterators should be used instead of index based access: 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 failure in @ref overview_debugging "debug build", but no checks are
done in release builds. done in release builds.
This section also contains both implicit and explicit conversions to C style 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. to use wc_str() for the sake of clarity.
@li GetChar() @li GetChar()
@@ -226,7 +226,7 @@ public:
@li Empty() @li Empty()
@li Clear() @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 original string is not modified and the function returns the extracted
substring. substring.
@@ -463,6 +463,16 @@ public:
wxString BeforeLast(wxUniChar ch) const; 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. Empties the string and frees memory occupied by it.
See also: Empty() See also: Empty()
@@ -759,6 +769,8 @@ public:
/** /**
Returns this string converted to the lower case. Returns this string converted to the lower case.
@see MakeLower()
*/ */
wxString Lower() const; wxString Lower() const;
@@ -769,13 +781,27 @@ public:
*/ */
void LowerCase(); 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. Converts all characters to lower case and returns the result.
@see Lower()
*/ */
wxString& MakeLower(); wxString& MakeLower();
/** /**
Converts all characters to upper case and returns the result. Converts all characters to upper case and returns the result.
@see Upper()
*/ */
wxString& MakeUpper(); wxString& MakeUpper();
@@ -1018,6 +1044,8 @@ public:
/** /**
Returns this string converted to upper case. Returns this string converted to upper case.
@see MakeUpper()
*/ */
wxString Upper() const; wxString Upper() const;
@@ -1033,14 +1061,14 @@ public:
convertible to both @c const @c char* and to @c const @c wchar_t*. 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 Given this ambiguity it is mostly better to use wc_str(), mb_str() or
utf8_str() instead. utf8_str() instead.
Please see the @ref overview_unicode "Unicode overview" for more Please see the @ref overview_unicode "Unicode overview" for more
information about it. information about it.
Note that the returned value is not convertible to @c char* or 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 @c wchar_t*, use char_str() or wchar_str() if you need to pass
string value to a function expecting non-const pointer. string value to a function expecting non-const pointer.
@see wc_str(), utf8_str(), c_str(), mb_str(), fn_str() @see wc_str(), utf8_str(), c_str(), mb_str(), fn_str()
*/ */
const wxCStrData c_str() const; const wxCStrData c_str() const;
@@ -1091,8 +1119,8 @@ public:
/** /**
Returns the multibyte (C string) representation of the string 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 @see wc_str(), utf8_str(), c_str(), wxMBConv
*/ */
const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const; 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 Converts the strings contents to UTF-8 and returns it either as a
temporary wxCharBuffer object or as a pointer to the internal temporary wxCharBuffer object or as a pointer to the internal
string contents in UTF-8 build. string contents in UTF-8 build.
@see wc_str(), c_str(), mb_str() @see wc_str(), c_str(), mb_str()
*/ */
const char* utf8_str() const; const char* utf8_str() const;
@@ -1190,7 +1218,7 @@ public:
//@{ //@{
/** /**
Converts the strings contents to the wide character represention 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 or returns a pointer to the internal string contents in wide character
mode (Windows). mode (Windows).
@@ -1214,7 +1242,7 @@ public:
*/ */
wxWritableWCharBuffer wchar_str() const; wxWritableWCharBuffer wchar_str() const;
/** /**
Explicit conversion to C string in the internal representation (either Explicit conversion to C string in the internal representation (either
wchar_t* or UTF-8-encoded char*, depending on the build). wchar_t* or UTF-8-encoded char*, depending on the build).
*/ */
@@ -1373,10 +1401,10 @@ wxString wxEmptyString;
@endcode @endcode
Note that the exact usage of this depends on whether or not wxUSE_STL is 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 character buffer, and if wxUSE_STL is disabled, it uses GetWriteBuf() from
wxString, keeping the same buffer wxString uses intact. In other words, wxString, keeping the same buffer wxString uses intact. In other words,
relying on wxStringBuffer containing the old wxString data is not a good 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. idea if you want to build your program both with and without wxUSE_STL.
Note that SetLength @c must be called before wxStringBufferLength destructs. Note that SetLength @c must be called before wxStringBufferLength destructs.

View File

@@ -1363,6 +1363,20 @@ wxString& wxString::MakeLower()
return *this; 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 // trimming and padding
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -1967,13 +1981,6 @@ int wxString::Freq(wxUniChar ch) const
return count; 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 // wxUTF8StringBuffer
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@@ -366,14 +366,26 @@ void StringTestCase::CaseChanges()
wxString s1l(s1); wxString s1l(s1);
s1u.MakeUpper(); s1u.MakeUpper();
s1l.MakeLower(); s1l.MakeLower();
CPPUNIT_ASSERT_EQUAL( _T("HELLO!"), s1u );
CPPUNIT_ASSERT_EQUAL( _T("hello!"), s1l );
wxString s2u, s2l; wxString s2u, s2l;
s2u.MakeUpper(); s2u.MakeUpper();
s2l.MakeLower(); s2l.MakeLower();
CPPUNIT_ASSERT( s1u == _T("HELLO!") ); CPPUNIT_ASSERT_EQUAL( "", s2u );
CPPUNIT_ASSERT( s1l == _T("hello!") ); CPPUNIT_ASSERT_EQUAL( "", s2l );
CPPUNIT_ASSERT( s2u == wxEmptyString );
CPPUNIT_ASSERT( s2l == wxEmptyString );
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() void StringTestCase::Compare()