diff options
author | tsepez <tsepez@chromium.org> | 2016-04-11 18:01:13 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-04-11 18:01:13 -0700 |
commit | e09c1e4db92e28a332f55aa3c80ceb44f4b74287 (patch) | |
tree | 2ba0b13dadf379f7673317fdd2909db21cce9412 /core/fxcrt/fx_basic_wstring.cpp | |
parent | 24a48881c5407651a58cfd6547ac9b6a9823a63e (diff) | |
download | pdfium-e09c1e4db92e28a332f55aa3c80ceb44f4b74287.tar.xz |
Make CFX_{Byte,Wide}String::Remove() no-touch if possible
Don't try to copy the string until we are sure we need to
change it.
Review URL: https://codereview.chromium.org/1877993002
Diffstat (limited to 'core/fxcrt/fx_basic_wstring.cpp')
-rw-r--r-- | core/fxcrt/fx_basic_wstring.cpp | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/core/fxcrt/fx_basic_wstring.cpp b/core/fxcrt/fx_basic_wstring.cpp index 08a9d212d4..069b9fa42d 100644 --- a/core/fxcrt/fx_basic_wstring.cpp +++ b/core/fxcrt/fx_basic_wstring.cpp @@ -672,17 +672,30 @@ FX_STRSIZE CFX_WideString::Remove(FX_WCHAR chRemove) { if (!m_pData || m_pData->m_nDataLength < 1) return 0; - ReallocBeforeWrite(m_pData->m_nDataLength); FX_WCHAR* pstrSource = m_pData->m_String; - FX_WCHAR* pstrDest = m_pData->m_String; FX_WCHAR* pstrEnd = m_pData->m_String + m_pData->m_nDataLength; while (pstrSource < pstrEnd) { + if (*pstrSource == chRemove) + break; + pstrSource++; + } + if (pstrSource == pstrEnd) + return 0; + + ptrdiff_t copied = pstrSource - m_pData->m_String; + ReallocBeforeWrite(m_pData->m_nDataLength); + pstrSource = m_pData->m_String + copied; + pstrEnd = m_pData->m_String + m_pData->m_nDataLength; + + FX_WCHAR* pstrDest = pstrSource; + while (pstrSource < pstrEnd) { if (*pstrSource != chRemove) { *pstrDest = *pstrSource; pstrDest++; } pstrSource++; } + *pstrDest = 0; FX_STRSIZE nCount = (FX_STRSIZE)(pstrSource - pstrDest); m_pData->m_nDataLength -= nCount; |