Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1092)

Side by Side Diff: core/src/fxcrt/fx_basic_bstring.cpp

Issue 1142533002: Make CFX_StringData be scoped by CFX_Bytestring and add methods. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: indent Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « core/include/fxcrt/fx_string.h ('k') | core/src/fxcrt/fx_basic_wstring.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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(). 7 #include <stddef.h> // For offsetof().
8 8
9 #include "../../include/fxcrt/fx_basic.h" 9 #include "../../include/fxcrt/fx_basic.h"
10 #include "../../../third_party/base/numerics/safe_math.h" 10 #include "../../../third_party/base/numerics/safe_math.h"
(...skipping 29 matching lines...) Expand all
40 for (int ii = 0; ii < len; ii ++) { 40 for (int ii = 0; ii < len; ii ++) {
41 buf[ii] = buf1[ii + buf_pos + 1]; 41 buf[ii] = buf1[ii + buf_pos + 1];
42 } 42 }
43 return len; 43 return len;
44 } 44 }
45 CFX_ByteString CFX_ByteString::FormatInteger(int i, FX_DWORD flags) 45 CFX_ByteString CFX_ByteString::FormatInteger(int i, FX_DWORD flags)
46 { 46 {
47 char buf[32]; 47 char buf[32];
48 return CFX_ByteStringC(buf, _Buffer_itoa(buf, i, flags)); 48 return CFX_ByteStringC(buf, _Buffer_itoa(buf, i, flags));
49 } 49 }
50 static CFX_StringData* FX_AllocString(int nLen) 50
51 // static
52 CFX_ByteString::StringData* CFX_ByteString::StringData::Create(int nLen)
51 { 53 {
52 // |nLen| is currently declared as in |int|. TODO(palmer): It should be 54 // |nLen| is currently declared as in |int|. TODO(palmer): It should be
53 // a |size_t|, or at least unsigned. 55 // a |size_t|, or at least unsigned.
54 if (nLen == 0 || nLen < 0) { 56 if (nLen == 0 || nLen < 0) {
55 return NULL; 57 return NULL;
56 } 58 }
57 59
58 // Fixed portion of header plus a NUL char not included in m_nAllocLength. 60 // 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. 61 // sizeof(FX_CHAR) is always 1, used for consistency with CFX_Widestring.
60 int overhead = offsetof(CFX_StringData, m_String) + sizeof(FX_CHAR); 62 int overhead = offsetof(StringData, m_String) + sizeof(FX_CHAR);
61 pdfium::base::CheckedNumeric<int> nSize = nLen; 63 pdfium::base::CheckedNumeric<int> nSize = nLen;
62 nSize += overhead; 64 nSize += overhead;
63 65
64 // Now round to an 8-byte boundary. We'd expect that this is the minimum 66 // Now round to an 8-byte boundary. We'd expect that this is the minimum
65 // granularity of any of the underlying allocators, so there may be cases 67 // granularity of any of the underlying allocators, so there may be cases
66 // where we can save a re-alloc when adding a few characters to a string 68 // where we can save a re-alloc when adding a few characters to a string
67 // by using this otherwise wasted space. 69 // by using this otherwise wasted space.
68 nSize += 7; 70 nSize += 7;
69 int totalSize = nSize.ValueOrDie() & ~7; 71 int totalSize = nSize.ValueOrDie() & ~7;
70 int usableSize = totalSize - overhead; 72 int usableSize = totalSize - overhead;
71 FXSYS_assert(usableSize >= nLen); 73 FXSYS_assert(usableSize >= nLen);
72 74
73 CFX_StringData* pData = (CFX_StringData*)FX_Alloc(FX_BYTE, totalSize); 75 void* pData = FX_Alloc(FX_BYTE, totalSize);
74 if (!pData) { 76 if (!pData) {
75 return NULL; 77 return NULL;
76 } 78 }
77 pData->m_nAllocLength = usableSize; 79 return new (pData) StringData(nLen, usableSize);
78 pData->m_nDataLength = nLen;
79 pData->m_nRefs = 1;
80 pData->m_String[nLen] = 0;
81 return pData;
82 }
83 static void FX_ReleaseString(CFX_StringData* pData)
84 {
85 if (pData == NULL) {
86 return;
87 }
88 pData->m_nRefs --;
89 if (pData->m_nRefs <= 0) {
90 FX_Free(pData);
91 }
92 } 80 }
93 CFX_ByteString::~CFX_ByteString() 81 CFX_ByteString::~CFX_ByteString()
94 { 82 {
95 if (m_pData == NULL) { 83 if (m_pData) {
96 return; 84 m_pData->Release();
97 }
98 m_pData->m_nRefs --;
99 if (m_pData->m_nRefs < 1) {
100 FX_Free(m_pData);
101 } 85 }
102 } 86 }
103 CFX_ByteString::CFX_ByteString(FX_LPCSTR lpsz, FX_STRSIZE nLen) 87 CFX_ByteString::CFX_ByteString(FX_LPCSTR lpsz, FX_STRSIZE nLen)
104 { 88 {
105 if (nLen < 0) { 89 if (nLen < 0) {
106 nLen = lpsz ? FXSYS_strlen(lpsz) : 0; 90 nLen = lpsz ? FXSYS_strlen(lpsz) : 0;
107 } 91 }
108 if (nLen) { 92 if (nLen) {
109 m_pData = FX_AllocString(nLen); 93 m_pData = StringData::Create(nLen);
110 if (m_pData) { 94 if (m_pData) {
111 FXSYS_memcpy32(m_pData->m_String, lpsz, nLen); 95 FXSYS_memcpy32(m_pData->m_String, lpsz, nLen);
112 } 96 }
113 } else { 97 } else {
114 m_pData = NULL; 98 m_pData = NULL;
115 } 99 }
116 } 100 }
117 CFX_ByteString::CFX_ByteString(FX_LPCBYTE lpsz, FX_STRSIZE nLen) 101 CFX_ByteString::CFX_ByteString(FX_LPCBYTE lpsz, FX_STRSIZE nLen)
118 { 102 {
119 if (nLen > 0) { 103 if (nLen > 0) {
120 m_pData = FX_AllocString(nLen); 104 m_pData = StringData::Create(nLen);
121 if (m_pData) { 105 if (m_pData) {
122 FXSYS_memcpy32(m_pData->m_String, lpsz, nLen); 106 FXSYS_memcpy32(m_pData->m_String, lpsz, nLen);
123 } 107 }
124 } else { 108 } else {
125 m_pData = NULL; 109 m_pData = NULL;
126 } 110 }
127 } 111 }
128 CFX_ByteString::CFX_ByteString(char ch) 112 CFX_ByteString::CFX_ByteString(char ch)
129 { 113 {
130 m_pData = FX_AllocString(1); 114 m_pData = StringData::Create(1);
131 if (m_pData) { 115 if (m_pData) {
132 m_pData->m_String[0] = ch; 116 m_pData->m_String[0] = ch;
133 } 117 }
134 } 118 }
135 CFX_ByteString::CFX_ByteString(const CFX_ByteString& stringSrc) 119 CFX_ByteString::CFX_ByteString(const CFX_ByteString& stringSrc)
136 { 120 {
137 if (stringSrc.m_pData == NULL) { 121 if (stringSrc.m_pData == NULL) {
138 m_pData = NULL; 122 m_pData = NULL;
139 return; 123 return;
140 } 124 }
141 if (stringSrc.m_pData->m_nRefs >= 0) { 125 if (stringSrc.m_pData->m_nRefs >= 0) {
142 m_pData = stringSrc.m_pData; 126 m_pData = stringSrc.m_pData;
143 m_pData->m_nRefs ++; 127 m_pData->Retain();
144 } else { 128 } else {
145 m_pData = NULL; 129 m_pData = NULL;
146 *this = stringSrc; 130 *this = stringSrc;
147 } 131 }
148 } 132 }
149 CFX_ByteString::CFX_ByteString(FX_BSTR stringSrc) 133 CFX_ByteString::CFX_ByteString(FX_BSTR stringSrc)
150 { 134 {
151 if (stringSrc.IsEmpty()) { 135 if (stringSrc.IsEmpty()) {
152 m_pData = NULL; 136 m_pData = NULL;
153 return; 137 return;
154 } else { 138 } else {
155 m_pData = NULL; 139 m_pData = NULL;
156 *this = stringSrc; 140 *this = stringSrc;
157 } 141 }
158 } 142 }
159 CFX_ByteString::CFX_ByteString(FX_BSTR str1, FX_BSTR str2) 143 CFX_ByteString::CFX_ByteString(FX_BSTR str1, FX_BSTR str2)
160 { 144 {
161 m_pData = NULL; 145 m_pData = NULL;
162 int nNewLen = str1.GetLength() + str2.GetLength(); 146 int nNewLen = str1.GetLength() + str2.GetLength();
163 if (nNewLen == 0) { 147 if (nNewLen == 0) {
164 return; 148 return;
165 } 149 }
166 m_pData = FX_AllocString(nNewLen); 150 m_pData = StringData::Create(nNewLen);
167 if (m_pData) { 151 if (m_pData) {
168 FXSYS_memcpy32(m_pData->m_String, str1.GetCStr(), str1.GetLength()); 152 FXSYS_memcpy32(m_pData->m_String, str1.GetCStr(), str1.GetLength());
169 FXSYS_memcpy32(m_pData->m_String + str1.GetLength(), str2.GetCStr(), str 2.GetLength()); 153 FXSYS_memcpy32(m_pData->m_String + str1.GetLength(), str2.GetCStr(), str 2.GetLength());
170 } 154 }
171 } 155 }
172 const CFX_ByteString& CFX_ByteString::operator=(FX_LPCSTR lpsz) 156 const CFX_ByteString& CFX_ByteString::operator=(FX_LPCSTR lpsz)
173 { 157 {
174 if (lpsz == NULL || lpsz[0] == 0) { 158 if (lpsz == NULL || lpsz[0] == 0) {
175 Empty(); 159 Empty();
176 } else { 160 } else {
(...skipping 17 matching lines...) Expand all
194 } 178 }
195 if (stringSrc.IsEmpty()) { 179 if (stringSrc.IsEmpty()) {
196 Empty(); 180 Empty();
197 } else if ((m_pData && m_pData->m_nRefs < 0) || 181 } else if ((m_pData && m_pData->m_nRefs < 0) ||
198 (stringSrc.m_pData && stringSrc.m_pData->m_nRefs < 0)) { 182 (stringSrc.m_pData && stringSrc.m_pData->m_nRefs < 0)) {
199 AssignCopy(stringSrc.m_pData->m_nDataLength, stringSrc.m_pData->m_String ); 183 AssignCopy(stringSrc.m_pData->m_nDataLength, stringSrc.m_pData->m_String );
200 } else { 184 } else {
201 Empty(); 185 Empty();
202 m_pData = stringSrc.m_pData; 186 m_pData = stringSrc.m_pData;
203 if (m_pData) { 187 if (m_pData) {
204 m_pData->m_nRefs ++; 188 m_pData->Retain();
205 } 189 }
206 } 190 }
207 return *this; 191 return *this;
208 } 192 }
209 const CFX_ByteString& CFX_ByteString::operator=(const CFX_BinaryBuf& buf) 193 const CFX_ByteString& CFX_ByteString::operator=(const CFX_BinaryBuf& buf)
210 { 194 {
211 Load(buf.GetBuffer(), buf.GetSize()); 195 Load(buf.GetBuffer(), buf.GetSize());
212 return *this; 196 return *this;
213 } 197 }
214 void CFX_ByteString::Load(FX_LPCBYTE buf, FX_STRSIZE len) 198 void CFX_ByteString::Load(FX_LPCBYTE buf, FX_STRSIZE len)
215 { 199 {
216 Empty(); 200 Empty();
217 if (len) { 201 if (len) {
218 m_pData = FX_AllocString(len); 202 m_pData = StringData::Create(len);
219 if (m_pData) { 203 if (m_pData) {
220 FXSYS_memcpy32(m_pData->m_String, buf, len); 204 FXSYS_memcpy32(m_pData->m_String, buf, len);
221 } 205 }
222 } else { 206 } else {
223 m_pData = NULL; 207 m_pData = NULL;
224 } 208 }
225 } 209 }
226 const CFX_ByteString& CFX_ByteString::operator+=(FX_LPCSTR lpsz) 210 const CFX_ByteString& CFX_ByteString::operator+=(FX_LPCSTR lpsz)
227 { 211 {
228 if (lpsz) { 212 if (lpsz) {
(...skipping 23 matching lines...) Expand all
252 return *this; 236 return *this;
253 } 237 }
254 bool CFX_ByteString::Equal(const char* ptr) const 238 bool CFX_ByteString::Equal(const char* ptr) const
255 { 239 {
256 if (!m_pData) { 240 if (!m_pData) {
257 return !ptr || ptr[0] == '\0'; 241 return !ptr || ptr[0] == '\0';
258 } 242 }
259 if (!ptr) { 243 if (!ptr) {
260 return m_pData->m_nDataLength == 0; 244 return m_pData->m_nDataLength == 0;
261 } 245 }
262 return strlen(ptr) == m_pData->m_nDataLength && 246 return FXSYS_strlen(ptr) == m_pData->m_nDataLength &&
263 FXSYS_memcmp32(ptr, m_pData->m_String, m_pData->m_nDataLength) == 0; 247 FXSYS_memcmp32(ptr, m_pData->m_String, m_pData->m_nDataLength) == 0;
264 } 248 }
265 bool CFX_ByteString::Equal(const CFX_ByteStringC& str) const 249 bool CFX_ByteString::Equal(const CFX_ByteStringC& str) const
266 { 250 {
267 if (m_pData == NULL) { 251 if (m_pData == NULL) {
268 return str.IsEmpty(); 252 return str.IsEmpty();
269 } 253 }
270 return m_pData->m_nDataLength == str.GetLength() && 254 return m_pData->m_nDataLength == str.GetLength() &&
271 FXSYS_memcmp32(m_pData->m_String, str.GetCStr(), str.GetLength()) == 0; 255 FXSYS_memcmp32(m_pData->m_String, str.GetCStr(), str.GetLength()) == 0;
272 } 256 }
273 bool CFX_ByteString::Equal(const CFX_ByteString& other) const 257 bool CFX_ByteString::Equal(const CFX_ByteString& other) const
274 { 258 {
275 if (IsEmpty()) { 259 if (IsEmpty()) {
276 return other.IsEmpty(); 260 return other.IsEmpty();
277 } 261 }
278 if (other.IsEmpty()) { 262 if (other.IsEmpty()) {
279 return false; 263 return false;
280 } 264 }
281 return other.m_pData->m_nDataLength == m_pData->m_nDataLength && 265 return other.m_pData->m_nDataLength == m_pData->m_nDataLength &&
282 FXSYS_memcmp32(other.m_pData->m_String, 266 FXSYS_memcmp32(other.m_pData->m_String,
283 m_pData->m_String, 267 m_pData->m_String,
284 m_pData->m_nDataLength) == 0; 268 m_pData->m_nDataLength) == 0;
285 } 269 }
286 void CFX_ByteString::Empty() 270 void CFX_ByteString::Empty()
287 { 271 {
288 if (m_pData == NULL) { 272 if (m_pData) {
289 return; 273 m_pData->Release();
274 m_pData = NULL;
290 } 275 }
291 if (m_pData->m_nRefs > 1) {
292 m_pData->m_nRefs --;
293 } else {
294 FX_Free(m_pData);
295 }
296 m_pData = NULL;
297 } 276 }
298 bool CFX_ByteString::EqualNoCase(FX_BSTR str) const 277 bool CFX_ByteString::EqualNoCase(FX_BSTR str) const
299 { 278 {
300 if (m_pData == NULL) { 279 if (m_pData == NULL) {
301 return str.IsEmpty(); 280 return str.IsEmpty();
302 } 281 }
303 FX_STRSIZE len = str.GetLength(); 282 FX_STRSIZE len = str.GetLength();
304 if (m_pData->m_nDataLength != len) { 283 if (m_pData->m_nDataLength != len) {
305 return false; 284 return false;
306 } 285 }
(...skipping 23 matching lines...) Expand all
330 AllocBeforeWrite(nSrcLen); 309 AllocBeforeWrite(nSrcLen);
331 FXSYS_memcpy32(m_pData->m_String, lpszSrcData, nSrcLen); 310 FXSYS_memcpy32(m_pData->m_String, lpszSrcData, nSrcLen);
332 m_pData->m_nDataLength = nSrcLen; 311 m_pData->m_nDataLength = nSrcLen;
333 m_pData->m_String[nSrcLen] = 0; 312 m_pData->m_String[nSrcLen] = 0;
334 } 313 }
335 void CFX_ByteString::CopyBeforeWrite() 314 void CFX_ByteString::CopyBeforeWrite()
336 { 315 {
337 if (m_pData == NULL || m_pData->m_nRefs <= 1) { 316 if (m_pData == NULL || m_pData->m_nRefs <= 1) {
338 return; 317 return;
339 } 318 }
340 CFX_StringData* pData = m_pData; 319 StringData* pData = m_pData;
341 m_pData->m_nRefs --; 320 m_pData->Release();
342 FX_STRSIZE nDataLength = pData->m_nDataLength; 321 FX_STRSIZE nDataLength = pData->m_nDataLength;
343 m_pData = FX_AllocString(nDataLength); 322 m_pData = StringData::Create(nDataLength);
344 if (m_pData != NULL) { 323 if (m_pData != NULL) {
345 FXSYS_memcpy32(m_pData->m_String, pData->m_String, nDataLength + 1); 324 FXSYS_memcpy32(m_pData->m_String, pData->m_String, nDataLength + 1);
346 } 325 }
347 } 326 }
348 void CFX_ByteString::AllocBeforeWrite(FX_STRSIZE nLen) 327 void CFX_ByteString::AllocBeforeWrite(FX_STRSIZE nLen)
349 { 328 {
350 if (m_pData && m_pData->m_nRefs <= 1 && m_pData->m_nAllocLength >= nLen) { 329 if (m_pData && m_pData->m_nRefs <= 1 && m_pData->m_nAllocLength >= nLen) {
351 return; 330 return;
352 } 331 }
353 Empty(); 332 Empty();
354 m_pData = FX_AllocString(nLen); 333 m_pData = StringData::Create(nLen);
355 } 334 }
356 void CFX_ByteString::ReleaseBuffer(FX_STRSIZE nNewLength) 335 void CFX_ByteString::ReleaseBuffer(FX_STRSIZE nNewLength)
357 { 336 {
358 if (m_pData == NULL) { 337 if (m_pData == NULL) {
359 return; 338 return;
360 } 339 }
361 CopyBeforeWrite(); 340 CopyBeforeWrite();
362 if (nNewLength == -1) { 341 if (nNewLength == -1) {
363 nNewLength = FXSYS_strlen((FX_LPCSTR)m_pData->m_String); 342 nNewLength = FXSYS_strlen((FX_LPCSTR)m_pData->m_String);
364 } 343 }
(...skipping 12 matching lines...) Expand all
377 } 356 }
378 FX_LPSTR CFX_ByteString::GetBuffer(FX_STRSIZE nMinBufLength) 357 FX_LPSTR CFX_ByteString::GetBuffer(FX_STRSIZE nMinBufLength)
379 { 358 {
380 if (m_pData == NULL && nMinBufLength == 0) { 359 if (m_pData == NULL && nMinBufLength == 0) {
381 return NULL; 360 return NULL;
382 } 361 }
383 if (m_pData && m_pData->m_nRefs <= 1 && m_pData->m_nAllocLength >= nMinBufLe ngth) { 362 if (m_pData && m_pData->m_nRefs <= 1 && m_pData->m_nAllocLength >= nMinBufLe ngth) {
384 return m_pData->m_String; 363 return m_pData->m_String;
385 } 364 }
386 if (m_pData == NULL) { 365 if (m_pData == NULL) {
387 m_pData = FX_AllocString(nMinBufLength); 366 m_pData = StringData::Create(nMinBufLength);
388 if (!m_pData) { 367 if (!m_pData) {
389 return NULL; 368 return NULL;
390 } 369 }
391 m_pData->m_nDataLength = 0; 370 m_pData->m_nDataLength = 0;
392 m_pData->m_String[0] = 0; 371 m_pData->m_String[0] = 0;
393 return m_pData->m_String; 372 return m_pData->m_String;
394 } 373 }
395 CFX_StringData* pOldData = m_pData; 374 StringData* pOldData = m_pData;
396 FX_STRSIZE nOldLen = pOldData->m_nDataLength; 375 FX_STRSIZE nOldLen = pOldData->m_nDataLength;
397 if (nMinBufLength < nOldLen) { 376 if (nMinBufLength < nOldLen) {
398 nMinBufLength = nOldLen; 377 nMinBufLength = nOldLen;
399 } 378 }
400 m_pData = FX_AllocString(nMinBufLength); 379 m_pData = StringData::Create(nMinBufLength);
401 if (!m_pData) { 380 if (!m_pData) {
402 return NULL; 381 return NULL;
403 } 382 }
404 FXSYS_memcpy32(m_pData->m_String, pOldData->m_String, (nOldLen + 1)); 383 FXSYS_memcpy32(m_pData->m_String, pOldData->m_String, (nOldLen + 1));
405 m_pData->m_nDataLength = nOldLen; 384 m_pData->m_nDataLength = nOldLen;
406 pOldData->m_nRefs --; 385 pOldData->Release();
407 if (pOldData->m_nRefs <= 0) {
408 FX_Free(pOldData);
409 }
410 return m_pData->m_String; 386 return m_pData->m_String;
411 } 387 }
412 FX_STRSIZE CFX_ByteString::Delete(FX_STRSIZE nIndex, FX_STRSIZE nCount) 388 FX_STRSIZE CFX_ByteString::Delete(FX_STRSIZE nIndex, FX_STRSIZE nCount)
413 { 389 {
414 if (m_pData == NULL) { 390 if (m_pData == NULL) {
415 return 0; 391 return 0;
416 } 392 }
417 if (nIndex < 0) { 393 if (nIndex < 0) {
418 nIndex = 0; 394 nIndex = 0;
419 } 395 }
(...skipping 11 matching lines...) Expand all
431 m_pData->m_nDataLength = nOldLength - nCount; 407 m_pData->m_nDataLength = nOldLength - nCount;
432 } 408 }
433 return m_pData->m_nDataLength; 409 return m_pData->m_nDataLength;
434 } 410 }
435 void CFX_ByteString::ConcatInPlace(FX_STRSIZE nSrcLen, FX_LPCSTR lpszSrcData) 411 void CFX_ByteString::ConcatInPlace(FX_STRSIZE nSrcLen, FX_LPCSTR lpszSrcData)
436 { 412 {
437 if (nSrcLen == 0 || lpszSrcData == NULL) { 413 if (nSrcLen == 0 || lpszSrcData == NULL) {
438 return; 414 return;
439 } 415 }
440 if (m_pData == NULL) { 416 if (m_pData == NULL) {
441 m_pData = FX_AllocString(nSrcLen); 417 m_pData = StringData::Create(nSrcLen);
442 if (!m_pData) { 418 if (!m_pData) {
443 return; 419 return;
444 } 420 }
445 FXSYS_memcpy32(m_pData->m_String, lpszSrcData, nSrcLen); 421 FXSYS_memcpy32(m_pData->m_String, lpszSrcData, nSrcLen);
446 return; 422 return;
447 } 423 }
448 if (m_pData->m_nRefs > 1 || m_pData->m_nDataLength + nSrcLen > m_pData->m_nA llocLength) { 424 if (m_pData->m_nRefs > 1 || m_pData->m_nDataLength + nSrcLen > m_pData->m_nA llocLength) {
449 CFX_StringData* pOldData = m_pData; 425 StringData* pOldData = m_pData;
450 ConcatCopy(m_pData->m_nDataLength, m_pData->m_String, nSrcLen, lpszSrcDa ta); 426 ConcatCopy(m_pData->m_nDataLength, m_pData->m_String, nSrcLen, lpszSrcDa ta);
451 FX_ReleaseString(pOldData); 427 pOldData->Release();
452 } else { 428 } else {
453 FXSYS_memcpy32(m_pData->m_String + m_pData->m_nDataLength, lpszSrcData, nSrcLen); 429 FXSYS_memcpy32(m_pData->m_String + m_pData->m_nDataLength, lpszSrcData, nSrcLen);
454 m_pData->m_nDataLength += nSrcLen; 430 m_pData->m_nDataLength += nSrcLen;
455 m_pData->m_String[m_pData->m_nDataLength] = 0; 431 m_pData->m_String[m_pData->m_nDataLength] = 0;
456 } 432 }
457 } 433 }
458 void CFX_ByteString::ConcatCopy(FX_STRSIZE nSrc1Len, FX_LPCSTR lpszSrc1Data, 434 void CFX_ByteString::ConcatCopy(FX_STRSIZE nSrc1Len, FX_LPCSTR lpszSrc1Data,
459 FX_STRSIZE nSrc2Len, FX_LPCSTR lpszSrc2Data) 435 FX_STRSIZE nSrc2Len, FX_LPCSTR lpszSrc2Data)
460 { 436 {
461 int nNewLen = nSrc1Len + nSrc2Len; 437 int nNewLen = nSrc1Len + nSrc2Len;
462 if (nNewLen == 0) { 438 if (nNewLen == 0) {
463 return; 439 return;
464 } 440 }
465 m_pData = FX_AllocString(nNewLen); 441 m_pData = StringData::Create(nNewLen);
466 if (m_pData) { 442 if (m_pData) {
467 FXSYS_memcpy32(m_pData->m_String, lpszSrc1Data, nSrc1Len); 443 FXSYS_memcpy32(m_pData->m_String, lpszSrc1Data, nSrc1Len);
468 FXSYS_memcpy32(m_pData->m_String + nSrc1Len, lpszSrc2Data, nSrc2Len); 444 FXSYS_memcpy32(m_pData->m_String + nSrc1Len, lpszSrc2Data, nSrc2Len);
469 } 445 }
470 } 446 }
471 CFX_ByteString CFX_ByteString::Mid(FX_STRSIZE nFirst) const 447 CFX_ByteString CFX_ByteString::Mid(FX_STRSIZE nFirst) const
472 { 448 {
473 if (m_pData == NULL) { 449 if (m_pData == NULL) {
474 return CFX_ByteString(); 450 return CFX_ByteString();
475 } 451 }
(...skipping 21 matching lines...) Expand all
497 return dest; 473 return dest;
498 } 474 }
499 void CFX_ByteString::AllocCopy(CFX_ByteString& dest, FX_STRSIZE nCopyLen, FX_STR SIZE nCopyIndex) const 475 void CFX_ByteString::AllocCopy(CFX_ByteString& dest, FX_STRSIZE nCopyLen, FX_STR SIZE nCopyIndex) const
500 { 476 {
501 // |FX_STRSIZE| is currently typedef'd as in |int|. TODO(palmer): It 477 // |FX_STRSIZE| is currently typedef'd as in |int|. TODO(palmer): It
502 // should be a |size_t|, or at least unsigned. 478 // should be a |size_t|, or at least unsigned.
503 if (nCopyLen == 0 || nCopyLen < 0) { 479 if (nCopyLen == 0 || nCopyLen < 0) {
504 return; 480 return;
505 } 481 }
506 ASSERT(dest.m_pData == NULL); 482 ASSERT(dest.m_pData == NULL);
507 dest.m_pData = FX_AllocString(nCopyLen); 483 dest.m_pData = StringData::Create(nCopyLen);
508 if (dest.m_pData) { 484 if (dest.m_pData) {
509 FXSYS_memcpy32(dest.m_pData->m_String, m_pData->m_String + nCopyIndex, n CopyLen); 485 FXSYS_memcpy32(dest.m_pData->m_String, m_pData->m_String + nCopyIndex, n CopyLen);
510 } 486 }
511 } 487 }
512 #define FORCE_ANSI 0x10000 488 #define FORCE_ANSI 0x10000
513 #define FORCE_UNICODE 0x20000 489 #define FORCE_UNICODE 0x20000
514 #define FORCE_INT64 0x40000 490 #define FORCE_INT64 0x40000
515 void CFX_ByteString::FormatV(FX_LPCSTR lpszFormat, va_list argList) 491 void CFX_ByteString::FormatV(FX_LPCSTR lpszFormat, va_list argList)
516 { 492 {
517 va_list argListSave; 493 va_list argListSave;
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
738 CopyBeforeWrite(); 714 CopyBeforeWrite();
739 if (nIndex < 0) { 715 if (nIndex < 0) {
740 nIndex = 0; 716 nIndex = 0;
741 } 717 }
742 FX_STRSIZE nNewLength = m_pData ? m_pData->m_nDataLength : 0; 718 FX_STRSIZE nNewLength = m_pData ? m_pData->m_nDataLength : 0;
743 if (nIndex > nNewLength) { 719 if (nIndex > nNewLength) {
744 nIndex = nNewLength; 720 nIndex = nNewLength;
745 } 721 }
746 nNewLength++; 722 nNewLength++;
747 if (m_pData == NULL || m_pData->m_nAllocLength < nNewLength) { 723 if (m_pData == NULL || m_pData->m_nAllocLength < nNewLength) {
748 CFX_StringData* pOldData = m_pData; 724 StringData* pOldData = m_pData;
749 FX_LPCSTR pstr = m_pData->m_String; 725 FX_LPCSTR pstr = m_pData->m_String;
750 m_pData = FX_AllocString(nNewLength); 726 m_pData = StringData::Create(nNewLength);
751 if (!m_pData) { 727 if (!m_pData) {
752 return 0; 728 return 0;
753 } 729 }
754 if(pOldData != NULL) { 730 if(pOldData != NULL) {
755 FXSYS_memmove32(m_pData->m_String, pstr, (pOldData->m_nDataLength + 1)); 731 FXSYS_memmove32(m_pData->m_String, pstr, (pOldData->m_nDataLength + 1));
756 FX_ReleaseString(pOldData); 732 pOldData->Release();
757 } else { 733 } else {
758 m_pData->m_String[0] = 0; 734 m_pData->m_String[0] = 0;
759 } 735 }
760 } 736 }
761 FXSYS_memmove32(m_pData->m_String + nIndex + 1, 737 FXSYS_memmove32(m_pData->m_String + nIndex + 1,
762 m_pData->m_String + nIndex, (nNewLength - nIndex)); 738 m_pData->m_String + nIndex, (nNewLength - nIndex));
763 m_pData->m_String[nIndex] = ch; 739 m_pData->m_String[nIndex] = ch;
764 m_pData->m_nDataLength = nNewLength; 740 m_pData->m_nDataLength = nNewLength;
765 return nNewLength; 741 return nNewLength;
766 } 742 }
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
922 pStart = pTarget + nSourceLen; 898 pStart = pTarget + nSourceLen;
923 } 899 }
924 if (nCount == 0) { 900 if (nCount == 0) {
925 return 0; 901 return 0;
926 } 902 }
927 FX_STRSIZE nNewLength = m_pData->m_nDataLength + (nReplacementLen - nSource Len) * nCount; 903 FX_STRSIZE nNewLength = m_pData->m_nDataLength + (nReplacementLen - nSource Len) * nCount;
928 if (nNewLength == 0) { 904 if (nNewLength == 0) {
929 Empty(); 905 Empty();
930 return nCount; 906 return nCount;
931 } 907 }
932 CFX_StringData* pNewData = FX_AllocString(nNewLength); 908 StringData* pNewData = StringData::Create(nNewLength);
933 if (!pNewData) { 909 if (!pNewData) {
934 return 0; 910 return 0;
935 } 911 }
936 pStart = m_pData->m_String; 912 pStart = m_pData->m_String;
937 FX_LPSTR pDest = pNewData->m_String; 913 FX_LPSTR pDest = pNewData->m_String;
938 for (FX_STRSIZE i = 0; i < nCount; i ++) { 914 for (FX_STRSIZE i = 0; i < nCount; i ++) {
939 FX_LPCSTR pTarget = FX_strstr(pStart, (FX_STRSIZE)(pEnd - pStart), lpszO ld.GetCStr(), nSourceLen); 915 FX_LPCSTR pTarget = FX_strstr(pStart, (FX_STRSIZE)(pEnd - pStart), lpszO ld.GetCStr(), nSourceLen);
940 FXSYS_memcpy32(pDest, pStart, pTarget - pStart); 916 FXSYS_memcpy32(pDest, pStart, pTarget - pStart);
941 pDest += pTarget - pStart; 917 pDest += pTarget - pStart;
942 FXSYS_memcpy32(pDest, lpszNew.GetCStr(), lpszNew.GetLength()); 918 FXSYS_memcpy32(pDest, lpszNew.GetCStr(), lpszNew.GetLength());
943 pDest += lpszNew.GetLength(); 919 pDest += lpszNew.GetLength();
944 pStart = pTarget + nSourceLen; 920 pStart = pTarget + nSourceLen;
945 } 921 }
946 FXSYS_memcpy32(pDest, pStart, pEnd - pStart); 922 FXSYS_memcpy32(pDest, pStart, pEnd - pStart);
947 FX_ReleaseString(m_pData); 923 m_pData->Release();
948 m_pData = pNewData; 924 m_pData = pNewData;
949 return nCount; 925 return nCount;
950 } 926 }
951 void CFX_ByteString::SetAt(FX_STRSIZE nIndex, FX_CHAR ch) 927 void CFX_ByteString::SetAt(FX_STRSIZE nIndex, FX_CHAR ch)
952 { 928 {
953 if (m_pData == NULL) { 929 if (m_pData == NULL) {
954 return; 930 return;
955 } 931 }
956 FXSYS_assert(nIndex >= 0); 932 FXSYS_assert(nIndex >= 0);
957 FXSYS_assert(nIndex < m_pData->m_nDataLength); 933 FXSYS_assert(nIndex < m_pData->m_nDataLength);
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
1150 scale /= 10; 1126 scale /= 10;
1151 } 1127 }
1152 return buf_size; 1128 return buf_size;
1153 } 1129 }
1154 CFX_ByteString CFX_ByteString::FormatFloat(FX_FLOAT d, int precision) 1130 CFX_ByteString CFX_ByteString::FormatFloat(FX_FLOAT d, int precision)
1155 { 1131 {
1156 FX_CHAR buf[32]; 1132 FX_CHAR buf[32];
1157 FX_STRSIZE len = FX_ftoa(d, buf); 1133 FX_STRSIZE len = FX_ftoa(d, buf);
1158 return CFX_ByteString(buf, len); 1134 return CFX_ByteString(buf, len);
1159 } 1135 }
OLDNEW
« no previous file with comments | « core/include/fxcrt/fx_string.h ('k') | core/src/fxcrt/fx_basic_wstring.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698