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 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 m_pData.Reset(StringData::Create(nNewLength)); | 269 m_pData.Reset(StringData::Create(nNewLength)); |
270 } | 270 } |
271 | 271 |
272 void CFX_ByteString::ReleaseBuffer(FX_STRSIZE nNewLength) { | 272 void CFX_ByteString::ReleaseBuffer(FX_STRSIZE nNewLength) { |
273 if (!m_pData) | 273 if (!m_pData) |
274 return; | 274 return; |
275 | 275 |
276 if (nNewLength == -1) | 276 if (nNewLength == -1) |
277 nNewLength = FXSYS_strlen(m_pData->m_String); | 277 nNewLength = FXSYS_strlen(m_pData->m_String); |
278 | 278 |
| 279 nNewLength = std::min(nNewLength, m_pData->m_nAllocLength); |
279 if (nNewLength == 0) { | 280 if (nNewLength == 0) { |
280 clear(); | 281 clear(); |
281 return; | 282 return; |
282 } | 283 } |
283 | 284 |
284 FXSYS_assert(nNewLength <= m_pData->m_nAllocLength); | 285 FXSYS_assert(m_pData->m_nRefs == 1); |
285 ReallocBeforeWrite(nNewLength); | |
286 m_pData->m_nDataLength = nNewLength; | 286 m_pData->m_nDataLength = nNewLength; |
287 m_pData->m_String[nNewLength] = 0; | 287 m_pData->m_String[nNewLength] = 0; |
| 288 if (m_pData->m_nAllocLength - nNewLength >= 32) { |
| 289 // Over arbitrary threshold, so pay the price to relocate. Force copy to |
| 290 // always occur by holding a second reference to the string. |
| 291 CFX_ByteString preserve(*this); |
| 292 ReallocBeforeWrite(nNewLength); |
| 293 } |
288 } | 294 } |
289 | 295 |
290 void CFX_ByteString::Reserve(FX_STRSIZE len) { | 296 void CFX_ByteString::Reserve(FX_STRSIZE len) { |
291 GetBuffer(len); | 297 GetBuffer(len); |
292 ReleaseBuffer(GetLength()); | |
293 } | 298 } |
294 | 299 |
295 FX_CHAR* CFX_ByteString::GetBuffer(FX_STRSIZE nMinBufLength) { | 300 FX_CHAR* CFX_ByteString::GetBuffer(FX_STRSIZE nMinBufLength) { |
296 if (!m_pData) { | 301 if (!m_pData) { |
297 if (nMinBufLength == 0) | 302 if (nMinBufLength == 0) |
298 return nullptr; | 303 return nullptr; |
299 | 304 |
300 m_pData.Reset(StringData::Create(nMinBufLength)); | 305 m_pData.Reset(StringData::Create(nMinBufLength)); |
301 m_pData->m_nDataLength = 0; | 306 m_pData->m_nDataLength = 0; |
302 m_pData->m_String[0] = 0; | 307 m_pData->m_String[0] = 0; |
(...skipping 687 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
990 fraction %= scale; | 995 fraction %= scale; |
991 scale /= 10; | 996 scale /= 10; |
992 } | 997 } |
993 return buf_size; | 998 return buf_size; |
994 } | 999 } |
995 CFX_ByteString CFX_ByteString::FormatFloat(FX_FLOAT d, int precision) { | 1000 CFX_ByteString CFX_ByteString::FormatFloat(FX_FLOAT d, int precision) { |
996 FX_CHAR buf[32]; | 1001 FX_CHAR buf[32]; |
997 FX_STRSIZE len = FX_ftoa(d, buf); | 1002 FX_STRSIZE len = FX_ftoa(d, buf); |
998 return CFX_ByteString(buf, len); | 1003 return CFX_ByteString(buf, len); |
999 } | 1004 } |
OLD | NEW |