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 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 | 379 |
445 CFX_RetainPtr<StringData> pNewData( | 380 CFX_RetainPtr<StringData> pNewData( |
446 StringData::Create(m_pData->m_String + nCopyIndex, nCopyLen)); | 381 StringData::Create(m_pData->m_String + nCopyIndex, nCopyLen)); |
447 dest.m_pData.Swap(pNewData); | 382 dest.m_pData.Swap(pNewData); |
448 } | 383 } |
449 | 384 |
450 #define FORCE_ANSI 0x10000 | 385 #define FORCE_ANSI 0x10000 |
451 #define FORCE_UNICODE 0x20000 | 386 #define FORCE_UNICODE 0x20000 |
452 #define FORCE_INT64 0x40000 | 387 #define FORCE_INT64 0x40000 |
453 | 388 |
| 389 CFX_ByteString CFX_ByteString::FormatInteger(int i, uint32_t flags) { |
| 390 char buf[32]; |
| 391 return CFX_ByteStringC(buf, Buffer_itoa(buf, i, flags)); |
| 392 } |
| 393 |
454 void CFX_ByteString::FormatV(const FX_CHAR* pFormat, va_list argList) { | 394 void CFX_ByteString::FormatV(const FX_CHAR* pFormat, va_list argList) { |
455 va_list argListSave; | 395 va_list argListSave; |
456 #if defined(__ARMCC_VERSION) || \ | 396 #if defined(__ARMCC_VERSION) || \ |
457 (!defined(_MSC_VER) && (_FX_CPU_ == _FX_X64_ || _FX_CPU_ == _FX_IA64_ || \ | 397 (!defined(_MSC_VER) && (_FX_CPU_ == _FX_X64_ || _FX_CPU_ == _FX_IA64_ || \ |
458 _FX_CPU_ == _FX_ARM64_)) || \ | 398 _FX_CPU_ == _FX_ARM64_)) || \ |
459 defined(__native_client__) | 399 defined(__native_client__) |
460 va_copy(argListSave, argList); | 400 va_copy(argListSave, argList); |
461 #else | 401 #else |
462 argListSave = argList; | 402 argListSave = argList; |
463 #endif | 403 #endif |
(...skipping 575 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1039 fraction %= scale; | 979 fraction %= scale; |
1040 scale /= 10; | 980 scale /= 10; |
1041 } | 981 } |
1042 return buf_size; | 982 return buf_size; |
1043 } | 983 } |
1044 CFX_ByteString CFX_ByteString::FormatFloat(FX_FLOAT d, int precision) { | 984 CFX_ByteString CFX_ByteString::FormatFloat(FX_FLOAT d, int precision) { |
1045 FX_CHAR buf[32]; | 985 FX_CHAR buf[32]; |
1046 FX_STRSIZE len = FX_ftoa(d, buf); | 986 FX_STRSIZE len = FX_ftoa(d, buf); |
1047 return CFX_ByteString(buf, len); | 987 return CFX_ByteString(buf, len); |
1048 } | 988 } |
OLD | NEW |