From e09c1e4db92e28a332f55aa3c80ceb44f4b74287 Mon Sep 17 00:00:00 2001 From: tsepez Date: Mon, 11 Apr 2016 18:01:13 -0700 Subject: 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 --- core/fxcrt/fx_basic_bstring.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'core/fxcrt/fx_basic_bstring.cpp') diff --git a/core/fxcrt/fx_basic_bstring.cpp b/core/fxcrt/fx_basic_bstring.cpp index 4a48539578..23fdcaa25e 100644 --- a/core/fxcrt/fx_basic_bstring.cpp +++ b/core/fxcrt/fx_basic_bstring.cpp @@ -719,10 +719,22 @@ FX_STRSIZE CFX_ByteString::Remove(FX_CHAR chRemove) { if (!m_pData || m_pData->m_nDataLength < 1) return 0; - ReallocBeforeWrite(m_pData->m_nDataLength); FX_CHAR* pstrSource = m_pData->m_String; - FX_CHAR* pstrDest = m_pData->m_String; FX_CHAR* 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_CHAR* pstrDest = pstrSource; while (pstrSource < pstrEnd) { if (*pstrSource != chRemove) { *pstrDest = *pstrSource; @@ -730,6 +742,7 @@ FX_STRSIZE CFX_ByteString::Remove(FX_CHAR chRemove) { } pstrSource++; } + *pstrDest = 0; FX_STRSIZE nCount = (FX_STRSIZE)(pstrSource - pstrDest); m_pData->m_nDataLength -= nCount; -- cgit v1.2.3