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 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 m_pData.Reset(StringData::Create(nNewLength)); | 217 m_pData.Reset(StringData::Create(nNewLength)); |
218 } | 218 } |
219 | 219 |
220 void CFX_WideString::ReleaseBuffer(FX_STRSIZE nNewLength) { | 220 void CFX_WideString::ReleaseBuffer(FX_STRSIZE nNewLength) { |
221 if (!m_pData) | 221 if (!m_pData) |
222 return; | 222 return; |
223 | 223 |
224 if (nNewLength == -1) | 224 if (nNewLength == -1) |
225 nNewLength = FXSYS_wcslen(m_pData->m_String); | 225 nNewLength = FXSYS_wcslen(m_pData->m_String); |
226 | 226 |
| 227 nNewLength = std::min(nNewLength, m_pData->m_nAllocLength); |
227 if (nNewLength == 0) { | 228 if (nNewLength == 0) { |
228 clear(); | 229 clear(); |
229 return; | 230 return; |
230 } | 231 } |
231 | 232 |
232 FXSYS_assert(nNewLength <= m_pData->m_nAllocLength); | 233 FXSYS_assert(m_pData->m_nRefs == 1); |
233 ReallocBeforeWrite(nNewLength); | |
234 m_pData->m_nDataLength = nNewLength; | 234 m_pData->m_nDataLength = nNewLength; |
235 m_pData->m_String[nNewLength] = 0; | 235 m_pData->m_String[nNewLength] = 0; |
| 236 if (m_pData->m_nAllocLength - nNewLength >= 32) { |
| 237 // Over arbitrary threshold, so pay the price to relocate. Force copy to |
| 238 // always occur by holding a second reference to the string. |
| 239 CFX_WideString preserve(*this); |
| 240 ReallocBeforeWrite(nNewLength); |
| 241 } |
236 } | 242 } |
237 | 243 |
238 void CFX_WideString::Reserve(FX_STRSIZE len) { | 244 void CFX_WideString::Reserve(FX_STRSIZE len) { |
239 GetBuffer(len); | 245 GetBuffer(len); |
240 ReleaseBuffer(GetLength()); | |
241 } | 246 } |
242 | 247 |
243 FX_WCHAR* CFX_WideString::GetBuffer(FX_STRSIZE nMinBufLength) { | 248 FX_WCHAR* CFX_WideString::GetBuffer(FX_STRSIZE nMinBufLength) { |
244 if (!m_pData) { | 249 if (!m_pData) { |
245 if (nMinBufLength == 0) | 250 if (nMinBufLength == 0) |
246 return nullptr; | 251 return nullptr; |
247 | 252 |
248 m_pData.Reset(StringData::Create(nMinBufLength)); | 253 m_pData.Reset(StringData::Create(nMinBufLength)); |
249 m_pData->m_nDataLength = 0; | 254 m_pData->m_nDataLength = 0; |
250 m_pData->m_String[0] = 0; | 255 m_pData->m_String[0] = 0; |
(...skipping 735 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
986 FXSYS_MultiByteToWideChar(codepage, 0, bstr.c_str(), src_len, nullptr, 0); | 991 FXSYS_MultiByteToWideChar(codepage, 0, bstr.c_str(), src_len, nullptr, 0); |
987 CFX_WideString wstr; | 992 CFX_WideString wstr; |
988 if (dest_len) { | 993 if (dest_len) { |
989 FX_WCHAR* dest_buf = wstr.GetBuffer(dest_len); | 994 FX_WCHAR* dest_buf = wstr.GetBuffer(dest_len); |
990 FXSYS_MultiByteToWideChar(codepage, 0, bstr.c_str(), src_len, dest_buf, | 995 FXSYS_MultiByteToWideChar(codepage, 0, bstr.c_str(), src_len, dest_buf, |
991 dest_len); | 996 dest_len); |
992 wstr.ReleaseBuffer(dest_len); | 997 wstr.ReleaseBuffer(dest_len); |
993 } | 998 } |
994 return wstr; | 999 return wstr; |
995 } | 1000 } |
OLD | NEW |