Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <cctype> | 10 #include <cctype> |
| (...skipping 623 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 634 } | 634 } |
| 635 FX_STRSIZE nSourceLen = FXSYS_wcslen(lpszOld); | 635 FX_STRSIZE nSourceLen = FXSYS_wcslen(lpszOld); |
| 636 if (nSourceLen == 0) { | 636 if (nSourceLen == 0) { |
| 637 return 0; | 637 return 0; |
| 638 } | 638 } |
| 639 FX_STRSIZE nReplacementLen = lpszNew ? FXSYS_wcslen(lpszNew) : 0; | 639 FX_STRSIZE nReplacementLen = lpszNew ? FXSYS_wcslen(lpszNew) : 0; |
| 640 FX_STRSIZE nCount = 0; | 640 FX_STRSIZE nCount = 0; |
| 641 FX_WCHAR* lpszStart = m_pData->m_String; | 641 FX_WCHAR* lpszStart = m_pData->m_String; |
| 642 FX_WCHAR* lpszEnd = m_pData->m_String + m_pData->m_nDataLength; | 642 FX_WCHAR* lpszEnd = m_pData->m_String + m_pData->m_nDataLength; |
| 643 FX_WCHAR* lpszTarget; | 643 FX_WCHAR* lpszTarget; |
| 644 { | 644 while ((lpszTarget = FXSYS_wcsstr(lpszStart, lpszOld)) != nullptr && |
|
Tom Sepez
2016/03/17 16:40:05
nit: again, I think this reads cleaner as a while
Wei Li
2016/03/18 17:42:39
Absolutely agree with you. I originally planned to
| |
| 645 while ((lpszTarget = (FX_WCHAR*)FXSYS_wcsstr(lpszStart, lpszOld)) && | 645 lpszStart < lpszEnd) { |
| 646 lpszStart < lpszEnd) { | 646 nCount++; |
| 647 nCount++; | 647 lpszStart = lpszTarget + nSourceLen; |
| 648 lpszStart = lpszTarget + nSourceLen; | |
| 649 } | |
| 650 } | 648 } |
| 651 if (nCount > 0) { | 649 if (nCount > 0) { |
| 652 CopyBeforeWrite(); | 650 CopyBeforeWrite(); |
| 653 FX_STRSIZE nOldLength = m_pData->m_nDataLength; | 651 FX_STRSIZE nOldLength = m_pData->m_nDataLength; |
| 654 FX_STRSIZE nNewLength = | 652 FX_STRSIZE nNewLength = |
| 655 nOldLength + (nReplacementLen - nSourceLen) * nCount; | 653 nOldLength + (nReplacementLen - nSourceLen) * nCount; |
| 656 if (m_pData->m_nAllocLength < nNewLength || m_pData->m_nRefs > 1) { | 654 if (m_pData->m_nAllocLength < nNewLength || m_pData->m_nRefs > 1) { |
| 657 StringData* pOldData = m_pData; | 655 StringData* pOldData = m_pData; |
| 658 const FX_WCHAR* pstr = m_pData->m_String; | 656 const FX_WCHAR* pstr = m_pData->m_String; |
| 659 m_pData = StringData::Create(nNewLength); | 657 m_pData = StringData::Create(nNewLength); |
| 660 if (!m_pData) { | 658 if (!m_pData) { |
| 661 return 0; | 659 return 0; |
| 662 } | 660 } |
| 663 FXSYS_memcpy(m_pData->m_String, pstr, | 661 FXSYS_memcpy(m_pData->m_String, pstr, |
| 664 pOldData->m_nDataLength * sizeof(FX_WCHAR)); | 662 pOldData->m_nDataLength * sizeof(FX_WCHAR)); |
| 665 pOldData->Release(); | 663 pOldData->Release(); |
| 666 } | 664 } |
| 667 lpszStart = m_pData->m_String; | 665 lpszStart = m_pData->m_String; |
| 668 lpszEnd = m_pData->m_String + std::max(m_pData->m_nDataLength, nNewLength); | 666 lpszEnd = m_pData->m_String + std::max(m_pData->m_nDataLength, nNewLength); |
| 669 { | 667 { |
|
Tom Sepez
2016/03/17 16:40:05
nit: braces?
Wei Li
2016/03/18 17:42:39
Done.
| |
| 670 while ((lpszTarget = (FX_WCHAR*)FXSYS_wcsstr(lpszStart, lpszOld)) != | 668 while ((lpszTarget = FXSYS_wcsstr(lpszStart, lpszOld)) != nullptr && |
|
Tom Sepez
2016/03/17 16:40:05
nit: re-write as discussed above.
Wei Li
2016/03/18 17:42:39
Done.
| |
| 671 NULL && | |
| 672 lpszStart < lpszEnd) { | 669 lpszStart < lpszEnd) { |
| 673 FX_STRSIZE nBalance = | 670 FX_STRSIZE nBalance = |
| 674 nOldLength - | 671 nOldLength - |
| 675 (FX_STRSIZE)(lpszTarget - m_pData->m_String + nSourceLen); | 672 (FX_STRSIZE)(lpszTarget - m_pData->m_String + nSourceLen); |
| 676 FXSYS_memmove(lpszTarget + nReplacementLen, lpszTarget + nSourceLen, | 673 FXSYS_memmove(lpszTarget + nReplacementLen, lpszTarget + nSourceLen, |
| 677 nBalance * sizeof(FX_WCHAR)); | 674 nBalance * sizeof(FX_WCHAR)); |
| 678 FXSYS_memcpy(lpszTarget, lpszNew, nReplacementLen * sizeof(FX_WCHAR)); | 675 FXSYS_memcpy(lpszTarget, lpszNew, nReplacementLen * sizeof(FX_WCHAR)); |
| 679 lpszStart = lpszTarget + nReplacementLen; | 676 lpszStart = lpszTarget + nReplacementLen; |
| 680 lpszStart[nBalance] = 0; | 677 lpszStart[nBalance] = 0; |
| 681 nOldLength += (nReplacementLen - nSourceLen); | 678 nOldLength += (nReplacementLen - nSourceLen); |
| (...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1041 int dest_len = | 1038 int dest_len = |
| 1042 FXSYS_MultiByteToWideChar(codepage, 0, bstr, src_len, nullptr, 0); | 1039 FXSYS_MultiByteToWideChar(codepage, 0, bstr, src_len, nullptr, 0); |
| 1043 CFX_WideString wstr; | 1040 CFX_WideString wstr; |
| 1044 if (dest_len) { | 1041 if (dest_len) { |
| 1045 FX_WCHAR* dest_buf = wstr.GetBuffer(dest_len); | 1042 FX_WCHAR* dest_buf = wstr.GetBuffer(dest_len); |
| 1046 FXSYS_MultiByteToWideChar(codepage, 0, bstr, src_len, dest_buf, dest_len); | 1043 FXSYS_MultiByteToWideChar(codepage, 0, bstr, src_len, dest_buf, dest_len); |
| 1047 wstr.ReleaseBuffer(dest_len); | 1044 wstr.ReleaseBuffer(dest_len); |
| 1048 } | 1045 } |
| 1049 return wstr; | 1046 return wstr; |
| 1050 } | 1047 } |
| OLD | NEW |