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> // For offsetof(). |
| 8 |
7 #include "../../include/fxcrt/fx_basic.h" | 9 #include "../../include/fxcrt/fx_basic.h" |
8 #include "../../../third_party/base/numerics/safe_math.h" | 10 #include "../../../third_party/base/numerics/safe_math.h" |
9 | 11 |
10 static int _Buffer_itoa(char* buf, int i, FX_DWORD flags) | 12 static int _Buffer_itoa(char* buf, int i, FX_DWORD flags) |
11 { | 13 { |
12 if (i == 0) { | 14 if (i == 0) { |
13 buf[0] = '0'; | 15 buf[0] = '0'; |
14 return 1; | 16 return 1; |
15 } | 17 } |
16 char buf1[32]; | 18 char buf1[32]; |
(...skipping 29 matching lines...) Expand all Loading... |
46 return CFX_ByteStringC(buf, _Buffer_itoa(buf, i, flags)); | 48 return CFX_ByteStringC(buf, _Buffer_itoa(buf, i, flags)); |
47 } | 49 } |
48 static CFX_StringData* FX_AllocString(int nLen) | 50 static CFX_StringData* FX_AllocString(int nLen) |
49 { | 51 { |
50 // |nLen| is currently declared as in |int|. TODO(palmer): It should be | 52 // |nLen| is currently declared as in |int|. TODO(palmer): It should be |
51 // a |size_t|, or at least unsigned. | 53 // a |size_t|, or at least unsigned. |
52 if (nLen == 0 || nLen < 0) { | 54 if (nLen == 0 || nLen < 0) { |
53 return NULL; | 55 return NULL; |
54 } | 56 } |
55 | 57 |
56 int overhead = sizeof(long) * 3 + 1; // 3 longs in header plus 1 for NUL. | 58 // Fixed portion of header plus a NUL char not included in m_nAllocLength. |
| 59 // sizeof(FX_CHAR) is always 1, used for consistency with CFX_Widestring. |
| 60 int overhead = offsetof(CFX_StringData, m_String) + sizeof(FX_CHAR); |
57 pdfium::base::CheckedNumeric<int> nSize = nLen; | 61 pdfium::base::CheckedNumeric<int> nSize = nLen; |
58 nSize += overhead; | 62 nSize += overhead; |
59 | 63 |
60 // Now round to an 8-byte boundary. We'd expect that this is the minimum | 64 // Now round to an 8-byte boundary. We'd expect that this is the minimum |
61 // granularity of any of the underlying allocators, so there may be cases | 65 // granularity of any of the underlying allocators, so there may be cases |
62 // where we can save a re-alloc when adding a few characters to a string | 66 // where we can save a re-alloc when adding a few characters to a string |
63 // by using this otherwise wasted space. | 67 // by using this otherwise wasted space. |
64 nSize += 7; | 68 nSize += 7; |
65 int totalSize = nSize.ValueOrDie() & ~7; | 69 int totalSize = nSize.ValueOrDie() & ~7; |
66 int usableSize = totalSize - overhead; | 70 int usableSize = totalSize - overhead; |
(...skipping 1066 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1133 scale /= 10; | 1137 scale /= 10; |
1134 } | 1138 } |
1135 return buf_size; | 1139 return buf_size; |
1136 } | 1140 } |
1137 CFX_ByteString CFX_ByteString::FormatFloat(FX_FLOAT d, int precision) | 1141 CFX_ByteString CFX_ByteString::FormatFloat(FX_FLOAT d, int precision) |
1138 { | 1142 { |
1139 FX_CHAR buf[32]; | 1143 FX_CHAR buf[32]; |
1140 FX_STRSIZE len = FX_ftoa(d, buf); | 1144 FX_STRSIZE len = FX_ftoa(d, buf); |
1141 return CFX_ByteString(buf, len); | 1145 return CFX_ByteString(buf, len); |
1142 } | 1146 } |
OLD | NEW |