Skip to content

Commit

Permalink
Implement wxString::Shrink() in terms of shrink_to_fit()
Browse files Browse the repository at this point in the history
Now that we use C++11 there is no need to have our own Shrink()
implementation when we can just use the standard function.

Also mention that it's the same as shrink_to_fit() in Shrink()
documentation.

No real changes, just simplify the code and make it more efficient.
  • Loading branch information
vadz committed Mar 28, 2023
1 parent a1d289f commit 488950f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 12 deletions.
5 changes: 2 additions & 3 deletions include/wx/string.h
Expand Up @@ -1368,7 +1368,7 @@ class WXDLLIMPEXP_BASE wxString
size_type capacity() const { return m_impl.capacity(); }
void reserve(size_t sz) { m_impl.reserve(sz); }

void shrink_to_fit() { Shrink(); }
void shrink_to_fit() { m_impl.shrink_to_fit(); }

void resize(size_t nSize, wxUniChar ch = wxT('\0'))
{
Expand Down Expand Up @@ -2241,8 +2241,7 @@ class WXDLLIMPEXP_BASE wxString
// only works if the data of this string is not shared
bool Alloc(size_t nLen) { reserve(nLen); return capacity() >= nLen; }
// minimize the string's memory
// only works if the data of this string is not shared
bool Shrink();
bool Shrink() { shrink_to_fit(); return true; }

// wxWidgets version 1 compatibility functions

Expand Down
5 changes: 4 additions & 1 deletion interface/wx/string.h
Expand Up @@ -1422,7 +1422,7 @@ class wxString
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.
See also the reserve(), resize() and shrink_to_fit() STL-like functions.
*/
///@{

Expand Down Expand Up @@ -1468,6 +1468,9 @@ class wxString
/**
Minimizes the string's memory.
Please note that this method does the same thing as the standard
shrink_to_fit() one and shouldn't be used in new code.
This can be useful after a call to Alloc() if too much memory were
preallocated.
Expand Down
8 changes: 0 additions & 8 deletions src/common/string.cpp
Expand Up @@ -514,14 +514,6 @@ const char *wxString::AsChar(const wxMBConv& conv) const
return m_convertedToChar.m_str;
}

// shrink to minimal size (releasing extra memory)
bool wxString::Shrink()
{
wxString tmp(begin(), end());
swap(tmp);
return true;
}

// ---------------------------------------------------------------------------
// data access
// ---------------------------------------------------------------------------
Expand Down

0 comments on commit 488950f

Please sign in to comment.