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 <cctype> | 9 #include <cctype> | 
| 10 | 10 | 
| 11 #include "core/fxcrt/include/fx_basic.h" | 11 #include "core/fxcrt/include/fx_basic.h" | 
| 12 #include "third_party/base/numerics/safe_math.h" | 12 #include "third_party/base/numerics/safe_math.h" | 
| 13 | 13 | 
| 14 static int _Buffer_itoa(char* buf, int i, uint32_t flags) { | 14 template class CFX_StringDataTemplate<FX_CHAR>; | 
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 int Buffer_itoa(char* buf, int i, uint32_t flags) { | |
| 15 if (i == 0) { | 19 if (i == 0) { | 
| 16 buf[0] = '0'; | 20 buf[0] = '0'; | 
| 17 return 1; | 21 return 1; | 
| 18 } | 22 } | 
| 19 char buf1[32]; | 23 char buf1[32]; | 
| 20 int buf_pos = 31; | 24 int buf_pos = 31; | 
| 21 uint32_t u = i; | 25 uint32_t u = i; | 
| 22 if ((flags & FXFORMAT_SIGNED) && i < 0) { | 26 if ((flags & FXFORMAT_SIGNED) && i < 0) { | 
| 23 u = -i; | 27 u = -i; | 
| 24 } | 28 } | 
| (...skipping 12 matching lines...) Expand all Loading... | |
| 37 if ((flags & FXFORMAT_SIGNED) && i < 0) { | 41 if ((flags & FXFORMAT_SIGNED) && i < 0) { | 
| 38 buf1[buf_pos--] = '-'; | 42 buf1[buf_pos--] = '-'; | 
| 39 } | 43 } | 
| 40 int len = 31 - buf_pos; | 44 int len = 31 - buf_pos; | 
| 41 for (int ii = 0; ii < len; ii++) { | 45 for (int ii = 0; ii < len; ii++) { | 
| 42 buf[ii] = buf1[ii + buf_pos + 1]; | 46 buf[ii] = buf1[ii + buf_pos + 1]; | 
| 43 } | 47 } | 
| 44 return len; | 48 return len; | 
| 45 } | 49 } | 
| 46 | 50 | 
| 47 CFX_ByteString CFX_ByteString::FormatInteger(int i, uint32_t flags) { | 51 } // namespace | 
| 48 char buf[32]; | |
| 49 return CFX_ByteStringC(buf, _Buffer_itoa(buf, i, flags)); | |
| 50 } | |
| 51 | |
| 52 // static | |
| 53 CFX_ByteString::StringData* CFX_ByteString::StringData::Create( | |
| 54 FX_STRSIZE nLen) { | |
| 55 FXSYS_assert(nLen > 0); | |
| 56 | |
| 57 // Fixed portion of header plus a NUL char not included in m_nAllocLength. | |
| 58 // sizeof(FX_CHAR) is always 1, used for consistency with CFX_Widestring. | |
| 59 int overhead = offsetof(StringData, m_String) + sizeof(FX_CHAR); | |
| 60 pdfium::base::CheckedNumeric<int> nSize = nLen; | |
| 61 nSize += overhead; | |
| 62 | |
| 63 // Now round to an 8-byte boundary. We'd expect that this is the minimum | |
| 64 // granularity of any of the underlying allocators, so there may be cases | |
| 65 // where we can save a re-alloc when adding a few characters to a string | |
| 66 // by using this otherwise wasted space. | |
| 67 nSize += 7; | |
| 68 int totalSize = nSize.ValueOrDie() & ~7; | |
| 69 int usableSize = totalSize - overhead; | |
| 70 FXSYS_assert(usableSize >= nLen); | |
| 71 | |
| 72 void* pData = FX_Alloc(uint8_t, totalSize); | |
| 73 return new (pData) StringData(nLen, usableSize); | |
| 74 } | |
| 75 | |
| 76 CFX_ByteString::StringData* CFX_ByteString::StringData::Create( | |
| 77 const StringData& other) { | |
| 78 StringData* result = Create(other.m_nDataLength); | |
| 79 result->CopyContents(other); | |
| 80 return result; | |
| 81 } | |
| 82 | |
| 83 CFX_ByteString::StringData* CFX_ByteString::StringData::Create( | |
| 84 const FX_CHAR* pStr, | |
| 85 FX_STRSIZE nLen) { | |
| 86 StringData* result = Create(nLen); | |
| 87 result->CopyContents(pStr, nLen); | |
| 88 return result; | |
| 89 } | |
| 90 | |
| 91 CFX_ByteString::StringData::StringData(FX_STRSIZE dataLen, FX_STRSIZE allocLen) | |
| 92 : m_nRefs(0), m_nDataLength(dataLen), m_nAllocLength(allocLen) { | |
| 93 FXSYS_assert(dataLen >= 0); | |
| 94 FXSYS_assert(dataLen <= allocLen); | |
| 95 m_String[dataLen] = 0; | |
| 96 } | |
| 97 | |
| 98 void CFX_ByteString::StringData::CopyContents(const StringData& other) { | |
| 99 FXSYS_assert(other.m_nDataLength <= m_nAllocLength); | |
| 100 FXSYS_memcpy(m_String, other.m_String, other.m_nDataLength + 1); | |
| 101 } | |
| 102 | |
| 103 void CFX_ByteString::StringData::CopyContents(const FX_CHAR* pStr, | |
| 104 FX_STRSIZE nLen) { | |
| 105 FXSYS_assert(nLen >= 0 && nLen <= m_nAllocLength); | |
| 106 FXSYS_memcpy(m_String, pStr, nLen); | |
| 107 m_String[nLen] = 0; | |
| 108 } | |
| 109 | |
| 110 void CFX_ByteString::StringData::CopyContentsAt(FX_STRSIZE offset, | |
| 111 const FX_CHAR* pStr, | |
| 112 FX_STRSIZE nLen) { | |
| 113 FXSYS_assert(offset >= 0 && nLen >= 0 && offset + nLen <= m_nAllocLength); | |
| 114 FXSYS_memcpy(m_String + offset, pStr, nLen); | |
| 115 m_String[offset + nLen] = 0; | |
| 116 } | |
| 117 | 52 | 
| 118 CFX_ByteString::CFX_ByteString(const FX_CHAR* pStr, FX_STRSIZE nLen) { | 53 CFX_ByteString::CFX_ByteString(const FX_CHAR* pStr, FX_STRSIZE nLen) { | 
| 119 if (nLen < 0) | 54 if (nLen < 0) | 
| 120 nLen = pStr ? FXSYS_strlen(pStr) : 0; | 55 nLen = pStr ? FXSYS_strlen(pStr) : 0; | 
| 121 | 56 | 
| 122 if (nLen) | 57 if (nLen) | 
| 123 m_pData.Reset(StringData::Create(pStr, nLen)); | 58 m_pData.Reset(StringData::Create(pStr, nLen)); | 
| 124 } | 59 } | 
| 125 | 60 | 
| 126 CFX_ByteString::CFX_ByteString(const uint8_t* pStr, FX_STRSIZE nLen) { | 61 CFX_ByteString::CFX_ByteString(const uint8_t* pStr, FX_STRSIZE nLen) { | 
| (...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 434 | 369 | 
| 435 CFX_RetainPtr<StringData> pNewData( | 370 CFX_RetainPtr<StringData> pNewData( | 
| 436 StringData::Create(m_pData->m_String + nCopyIndex, nCopyLen)); | 371 StringData::Create(m_pData->m_String + nCopyIndex, nCopyLen)); | 
| 437 dest.m_pData.Swap(pNewData); | 372 dest.m_pData.Swap(pNewData); | 
| 438 } | 373 } | 
| 439 | 374 | 
| 440 #define FORCE_ANSI 0x10000 | 375 #define FORCE_ANSI 0x10000 | 
| 441 #define FORCE_UNICODE 0x20000 | 376 #define FORCE_UNICODE 0x20000 | 
| 442 #define FORCE_INT64 0x40000 | 377 #define FORCE_INT64 0x40000 | 
| 443 | 378 | 
| 379 CFX_ByteString CFX_ByteString::FormatInteger(int i, uint32_t flags) { | |
| 380 char buf[32]; | |
| 
 
Lei Zhang
2016/03/31 00:07:19
You only need 21 bytes right? 2^63 is 19 digits +
 
Tom Sepez
2016/03/31 17:16:50
Acknowledged. Gonna leave it for now.
 
 | |
| 381 return CFX_ByteStringC(buf, Buffer_itoa(buf, i, flags)); | |
| 382 } | |
| 383 | |
| 444 void CFX_ByteString::FormatV(const FX_CHAR* pFormat, va_list argList) { | 384 void CFX_ByteString::FormatV(const FX_CHAR* pFormat, va_list argList) { | 
| 445 va_list argListSave; | 385 va_list argListSave; | 
| 446 #if defined(__ARMCC_VERSION) || \ | 386 #if defined(__ARMCC_VERSION) || \ | 
| 447 (!defined(_MSC_VER) && (_FX_CPU_ == _FX_X64_ || _FX_CPU_ == _FX_IA64_ || \ | 387 (!defined(_MSC_VER) && (_FX_CPU_ == _FX_X64_ || _FX_CPU_ == _FX_IA64_ || \ | 
| 448 _FX_CPU_ == _FX_ARM64_)) || \ | 388 _FX_CPU_ == _FX_ARM64_)) || \ | 
| 449 defined(__native_client__) | 389 defined(__native_client__) | 
| 450 va_copy(argListSave, argList); | 390 va_copy(argListSave, argList); | 
| 451 #else | 391 #else | 
| 452 argListSave = argList; | 392 argListSave = argList; | 
| 453 #endif | 393 #endif | 
| (...skipping 593 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1047 fraction %= scale; | 987 fraction %= scale; | 
| 1048 scale /= 10; | 988 scale /= 10; | 
| 1049 } | 989 } | 
| 1050 return buf_size; | 990 return buf_size; | 
| 1051 } | 991 } | 
| 1052 CFX_ByteString CFX_ByteString::FormatFloat(FX_FLOAT d, int precision) { | 992 CFX_ByteString CFX_ByteString::FormatFloat(FX_FLOAT d, int precision) { | 
| 1053 FX_CHAR buf[32]; | 993 FX_CHAR buf[32]; | 
| 1054 FX_STRSIZE len = FX_ftoa(d, buf); | 994 FX_STRSIZE len = FX_ftoa(d, buf); | 
| 1055 return CFX_ByteString(buf, len); | 995 return CFX_ByteString(buf, len); | 
| 1056 } | 996 } | 
| OLD | NEW |