X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/9ef1ad0d2ccb6dee00b1adc495ce055edd27844b..cbb003b1053e30ea93a5cb956c360a4a39c35622:/src/common/string.cpp diff --git a/src/common/string.cpp b/src/common/string.cpp index e68c7f03c0..2530f0718f 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -1239,27 +1239,56 @@ size_t wxString::Replace(const wxString& strOld, size_t uiCount = 0; // count of replacements made - size_t uiOldLen = strOld.length(); - size_t uiNewLen = strNew.length(); + // optimize the special common case: replacement of one character by + // another one (in UTF-8 case we can only do this for ASCII characters) + // + // benchmarks show that this special version is around 3 times faster + // (depending on the proportion of matching characters and UTF-8/wchar_t + // build) + if ( strOld.m_impl.length() == 1 && strNew.m_impl.length() == 1 ) + { + const wxStringCharType chOld = strOld.m_impl[0], + chNew = strNew.m_impl[0]; + + // this loop is the simplified version of the one below + for ( size_t pos = 0; ; ) + { + pos = m_impl.find(chOld, pos); + if ( pos == npos ) + break; + + m_impl[pos++] = chNew; - for ( size_t dwPos = 0; dwPos < length(); ) + uiCount++; + + if ( !bReplaceAll ) + break; + } + } + else // general case { - dwPos = find(strOld, dwPos); - if ( dwPos == npos ) - break; + const size_t uiOldLen = strOld.m_impl.length(); + const size_t uiNewLen = strNew.m_impl.length(); + + for ( size_t pos = 0; ; ) + { + pos = m_impl.find(strOld.m_impl, pos); + if ( pos == npos ) + break; - // replace this occurance of the old string with the new one - replace(dwPos, uiOldLen, strNew, uiNewLen); + // replace this occurrence of the old string with the new one + m_impl.replace(pos, uiOldLen, strNew.m_impl); - // move up pos past the string that was replaced - dwPos += uiNewLen; + // move up pos past the string that was replaced + pos += uiNewLen; - // increase replace count - ++uiCount; + // increase replace count + uiCount++; - // stop after the first one? - if ( !bReplaceAll ) - break; + // stop after the first one? + if ( !bReplaceAll ) + break; + } } return uiCount; @@ -1334,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 // --------------------------------------------------------------------------- @@ -1938,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 // ---------------------------------------------------------------------------- @@ -1973,3 +2009,15 @@ wxUTF8StringBufferLength::~wxUTF8StringBufferLength() wbuf.SetLength(wlen); } #endif // wxUSE_UNICODE_WCHAR + +// ---------------------------------------------------------------------------- +// wxCharBufferType +// ---------------------------------------------------------------------------- + +template<> +wxCharTypeBuffer::Data +wxCharTypeBuffer::NullData(NULL); + +template<> +wxCharTypeBuffer::Data +wxCharTypeBuffer::NullData(NULL);