diff options
author | tsepez <tsepez@chromium.org> | 2016-04-11 10:56:00 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-04-11 10:56:00 -0700 |
commit | 184b82553000a41fd0c90be56fa2f1c1503e2e9e (patch) | |
tree | 568f3877f8f51cc80e8aacb355ba07674e673e9c /core/fxcrt/fx_basic_wstring.cpp | |
parent | f3209c1fe0fdea79bf2f4e2b6f77ea4ef5db1ad2 (diff) | |
download | pdfium-184b82553000a41fd0c90be56fa2f1c1503e2e9e.tar.xz |
Avoid copying in TrimRight() and TrimLeft() if possible.
Make Byte and Wide code identical while at it.
Review URL: https://codereview.chromium.org/1877553002
Diffstat (limited to 'core/fxcrt/fx_basic_wstring.cpp')
-rw-r--r-- | core/fxcrt/fx_basic_wstring.cpp | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/core/fxcrt/fx_basic_wstring.cpp b/core/fxcrt/fx_basic_wstring.cpp index b5ccaf6da5..08a9d212d4 100644 --- a/core/fxcrt/fx_basic_wstring.cpp +++ b/core/fxcrt/fx_basic_wstring.cpp @@ -840,7 +840,6 @@ void CFX_WideString::TrimRight(const CFX_WideStringC& pTargets) { if (!m_pData || pTargets.IsEmpty()) { return; } - ReallocBeforeWrite(m_pData->m_nDataLength); FX_STRSIZE pos = GetLength(); if (pos < 1) { return; @@ -852,6 +851,7 @@ void CFX_WideString::TrimRight(const CFX_WideStringC& pTargets) { pos--; } if (pos < m_pData->m_nDataLength) { + ReallocBeforeWrite(m_pData->m_nDataLength); m_pData->m_String[pos] = 0; m_pData->m_nDataLength = pos; } @@ -874,18 +874,21 @@ void CFX_WideString::TrimLeft(const CFX_WideStringC& pTargets) { if (len < 1) return; - ReallocBeforeWrite(len); - const FX_WCHAR* lpsz = m_pData->m_String; - while (*lpsz != 0) { - if (!FXSYS_wcschr(pTargets.c_str(), *lpsz)) { + FX_STRSIZE pos = 0; + while (pos < len) { + FX_STRSIZE i = 0; + while (i < pTargets.GetLength() && pTargets[i] != m_pData->m_String[pos]) { + i++; + } + if (i == pTargets.GetLength()) { break; } - lpsz++; + pos++; } - if (lpsz != m_pData->m_String) { - int nDataLength = - m_pData->m_nDataLength - (FX_STRSIZE)(lpsz - m_pData->m_String); - FXSYS_memmove(m_pData->m_String, lpsz, + if (pos) { + ReallocBeforeWrite(len); + FX_STRSIZE nDataLength = len - pos; + FXSYS_memmove(m_pData->m_String, m_pData->m_String + pos, (nDataLength + 1) * sizeof(FX_WCHAR)); m_pData->m_nDataLength = nDataLength; } |