| 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(). | 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" |
| 11 | 11 |
| 12 static CFX_StringDataW* FX_AllocStringW(int nLen) | 12 // static |
| 13 CFX_WideString::StringData* CFX_WideString::StringData::Create(int nLen) |
| 13 { | 14 { |
| 14 // TODO(palmer): |nLen| should really be declared as |size_t|, or | 15 // TODO(palmer): |nLen| should really be declared as |size_t|, or |
| 15 // at least unsigned. | 16 // at least unsigned. |
| 16 if (nLen == 0 || nLen < 0) { | 17 if (nLen == 0 || nLen < 0) { |
| 17 return NULL; | 18 return NULL; |
| 18 } | 19 } |
| 19 | 20 |
| 20 // Fixed portion of header plus a NUL wide char not in m_nAllocLength. | 21 // Fixed portion of header plus a NUL wide char not in m_nAllocLength. |
| 21 int overhead = offsetof(CFX_StringDataW, m_String) + sizeof(FX_WCHAR); | 22 int overhead = offsetof(StringData, m_String) + sizeof(FX_WCHAR); |
| 22 pdfium::base::CheckedNumeric<int> iSize = nLen; | 23 pdfium::base::CheckedNumeric<int> iSize = nLen; |
| 23 iSize *= sizeof(FX_WCHAR); | 24 iSize *= sizeof(FX_WCHAR); |
| 24 iSize += overhead; | 25 iSize += overhead; |
| 25 | 26 |
| 26 // Now round to an 8-byte boundary. We'd expect that this is the minimum | 27 // Now round to an 8-byte boundary. We'd expect that this is the minimum |
| 27 // granularity of any of the underlying allocators, so there may be cases | 28 // granularity of any of the underlying allocators, so there may be cases |
| 28 // where we can save a re-alloc when adding a few characters to a string | 29 // where we can save a re-alloc when adding a few characters to a string |
| 29 // by using this otherwise wasted space. | 30 // by using this otherwise wasted space. |
| 30 iSize += 7; | 31 iSize += 7; |
| 31 int totalSize = iSize.ValueOrDie() & ~7; | 32 int totalSize = iSize.ValueOrDie() & ~7; |
| 32 int usableLen = (totalSize - overhead) / sizeof(FX_WCHAR); | 33 int usableLen = (totalSize - overhead) / sizeof(FX_WCHAR); |
| 33 FXSYS_assert(usableLen >= nLen); | 34 FXSYS_assert(usableLen >= nLen); |
| 34 | 35 |
| 35 CFX_StringDataW* pData = (CFX_StringDataW*)FX_Alloc(FX_BYTE, iSize.ValueOrDi
e()); | 36 void* pData = FX_Alloc(FX_BYTE, iSize.ValueOrDie()); |
| 36 if (!pData) { | 37 if (!pData) { |
| 37 return NULL; | 38 return NULL; |
| 38 } | 39 } |
| 39 | 40 return new (pData) StringData(nLen, usableLen); |
| 40 pData->m_nAllocLength = usableLen; | |
| 41 pData->m_nDataLength = nLen; | |
| 42 pData->m_nRefs = 1; | |
| 43 pData->m_String[nLen] = 0; | |
| 44 return pData; | |
| 45 } | |
| 46 static void FX_ReleaseStringW(CFX_StringDataW* pData) | |
| 47 { | |
| 48 if (pData == NULL) { | |
| 49 return; | |
| 50 } | |
| 51 pData->m_nRefs --; | |
| 52 if (pData->m_nRefs <= 0) { | |
| 53 FX_Free(pData); | |
| 54 } | |
| 55 } | 41 } |
| 56 CFX_WideString::~CFX_WideString() | 42 CFX_WideString::~CFX_WideString() |
| 57 { | 43 { |
| 58 if (m_pData == NULL) { | 44 if (m_pData) { |
| 59 return; | 45 m_pData->Release(); |
| 60 } | |
| 61 m_pData->m_nRefs --; | |
| 62 if (m_pData->m_nRefs < 1) { | |
| 63 FX_Free(m_pData); | |
| 64 } | 46 } |
| 65 } | 47 } |
| 66 CFX_WideString::CFX_WideString(const CFX_WideString& stringSrc) | 48 CFX_WideString::CFX_WideString(const CFX_WideString& stringSrc) |
| 67 { | 49 { |
| 68 if (stringSrc.m_pData == NULL) { | 50 if (stringSrc.m_pData == NULL) { |
| 69 m_pData = NULL; | 51 m_pData = NULL; |
| 70 return; | 52 return; |
| 71 } | 53 } |
| 72 if (stringSrc.m_pData->m_nRefs >= 0) { | 54 if (stringSrc.m_pData->m_nRefs >= 0) { |
| 73 m_pData = stringSrc.m_pData; | 55 m_pData = stringSrc.m_pData; |
| 74 m_pData->m_nRefs ++; | 56 m_pData->Retain(); |
| 75 } else { | 57 } else { |
| 76 m_pData = NULL; | 58 m_pData = NULL; |
| 77 *this = stringSrc; | 59 *this = stringSrc; |
| 78 } | 60 } |
| 79 } | 61 } |
| 80 CFX_WideString::CFX_WideString(FX_LPCWSTR lpsz, FX_STRSIZE nLen) { | 62 CFX_WideString::CFX_WideString(FX_LPCWSTR lpsz, FX_STRSIZE nLen) { |
| 81 if (nLen < 0) { | 63 if (nLen < 0) { |
| 82 nLen = lpsz ? FXSYS_wcslen(lpsz) : 0; | 64 nLen = lpsz ? FXSYS_wcslen(lpsz) : 0; |
| 83 } | 65 } |
| 84 if (nLen) { | 66 if (nLen) { |
| 85 m_pData = FX_AllocStringW(nLen); | 67 m_pData = StringData::Create(nLen); |
| 86 if (m_pData) { | 68 if (m_pData) { |
| 87 FXSYS_memcpy32(m_pData->m_String, lpsz, nLen * sizeof(FX_WCHAR)); | 69 FXSYS_memcpy32(m_pData->m_String, lpsz, nLen * sizeof(FX_WCHAR)); |
| 88 } | 70 } |
| 89 } else { | 71 } else { |
| 90 m_pData = NULL; | 72 m_pData = NULL; |
| 91 } | 73 } |
| 92 } | 74 } |
| 93 CFX_WideString::CFX_WideString(FX_WCHAR ch) | 75 CFX_WideString::CFX_WideString(FX_WCHAR ch) |
| 94 { | 76 { |
| 95 m_pData = FX_AllocStringW(1); | 77 m_pData = StringData::Create(1); |
| 96 if (m_pData) { | 78 if (m_pData) { |
| 97 m_pData->m_String[0] = ch; | 79 m_pData->m_String[0] = ch; |
| 98 } | 80 } |
| 99 } | 81 } |
| 100 CFX_WideString::CFX_WideString(const CFX_WideStringC& str) | 82 CFX_WideString::CFX_WideString(const CFX_WideStringC& str) |
| 101 { | 83 { |
| 102 if (str.IsEmpty()) { | 84 if (str.IsEmpty()) { |
| 103 m_pData = NULL; | 85 m_pData = NULL; |
| 104 return; | 86 return; |
| 105 } | 87 } |
| 106 m_pData = FX_AllocStringW(str.GetLength()); | 88 m_pData = StringData::Create(str.GetLength()); |
| 107 if (m_pData) { | 89 if (m_pData) { |
| 108 FXSYS_memcpy32(m_pData->m_String, str.GetPtr(), str.GetLength()*sizeof(F
X_WCHAR)); | 90 FXSYS_memcpy32(m_pData->m_String, str.GetPtr(), str.GetLength()*sizeof(F
X_WCHAR)); |
| 109 } | 91 } |
| 110 } | 92 } |
| 111 CFX_WideString::CFX_WideString(const CFX_WideStringC& str1, const CFX_WideString
C& str2) | 93 CFX_WideString::CFX_WideString(const CFX_WideStringC& str1, const CFX_WideString
C& str2) |
| 112 { | 94 { |
| 113 m_pData = NULL; | 95 m_pData = NULL; |
| 114 int nNewLen = str1.GetLength() + str2.GetLength(); | 96 int nNewLen = str1.GetLength() + str2.GetLength(); |
| 115 if (nNewLen == 0) { | 97 if (nNewLen == 0) { |
| 116 return; | 98 return; |
| 117 } | 99 } |
| 118 m_pData = FX_AllocStringW(nNewLen); | 100 m_pData = StringData::Create(nNewLen); |
| 119 if (m_pData) { | 101 if (m_pData) { |
| 120 FXSYS_memcpy32(m_pData->m_String, str1.GetPtr(), str1.GetLength()*sizeof
(FX_WCHAR)); | 102 FXSYS_memcpy32(m_pData->m_String, str1.GetPtr(), str1.GetLength()*sizeof
(FX_WCHAR)); |
| 121 FXSYS_memcpy32(m_pData->m_String + str1.GetLength(), str2.GetPtr(), str2
.GetLength()*sizeof(FX_WCHAR)); | 103 FXSYS_memcpy32(m_pData->m_String + str1.GetLength(), str2.GetPtr(), str2
.GetLength()*sizeof(FX_WCHAR)); |
| 122 } | 104 } |
| 123 } | 105 } |
| 124 void CFX_WideString::ReleaseBuffer(FX_STRSIZE nNewLength) | 106 void CFX_WideString::ReleaseBuffer(FX_STRSIZE nNewLength) |
| 125 { | 107 { |
| 126 if (m_pData == NULL) { | 108 if (m_pData == NULL) { |
| 127 return; | 109 return; |
| 128 } | 110 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 } | 145 } |
| 164 if (stringSrc.IsEmpty()) { | 146 if (stringSrc.IsEmpty()) { |
| 165 Empty(); | 147 Empty(); |
| 166 } else if ((m_pData && m_pData->m_nRefs < 0) || | 148 } else if ((m_pData && m_pData->m_nRefs < 0) || |
| 167 (stringSrc.m_pData && stringSrc.m_pData->m_nRefs < 0)) { | 149 (stringSrc.m_pData && stringSrc.m_pData->m_nRefs < 0)) { |
| 168 AssignCopy(stringSrc.m_pData->m_nDataLength, stringSrc.m_pData->m_String
); | 150 AssignCopy(stringSrc.m_pData->m_nDataLength, stringSrc.m_pData->m_String
); |
| 169 } else { | 151 } else { |
| 170 Empty(); | 152 Empty(); |
| 171 m_pData = stringSrc.m_pData; | 153 m_pData = stringSrc.m_pData; |
| 172 if (m_pData) { | 154 if (m_pData) { |
| 173 m_pData->m_nRefs ++; | 155 m_pData->Retain(); |
| 174 } | 156 } |
| 175 } | 157 } |
| 176 return *this; | 158 return *this; |
| 177 } | 159 } |
| 178 const CFX_WideString& CFX_WideString::operator+=(FX_WCHAR ch) | 160 const CFX_WideString& CFX_WideString::operator+=(FX_WCHAR ch) |
| 179 { | 161 { |
| 180 ConcatInPlace(1, &ch); | 162 ConcatInPlace(1, &ch); |
| 181 return *this; | 163 return *this; |
| 182 } | 164 } |
| 183 const CFX_WideString& CFX_WideString::operator+=(FX_LPCWSTR lpsz) | 165 const CFX_WideString& CFX_WideString::operator+=(FX_LPCWSTR lpsz) |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 if (other.IsEmpty()) { | 212 if (other.IsEmpty()) { |
| 231 return false; | 213 return false; |
| 232 } | 214 } |
| 233 return other.m_pData->m_nDataLength == m_pData->m_nDataLength && | 215 return other.m_pData->m_nDataLength == m_pData->m_nDataLength && |
| 234 wmemcmp(other.m_pData->m_String, | 216 wmemcmp(other.m_pData->m_String, |
| 235 m_pData->m_String, | 217 m_pData->m_String, |
| 236 m_pData->m_nDataLength) == 0; | 218 m_pData->m_nDataLength) == 0; |
| 237 } | 219 } |
| 238 void CFX_WideString::Empty() | 220 void CFX_WideString::Empty() |
| 239 { | 221 { |
| 240 if (m_pData == NULL) { | 222 if (m_pData) { |
| 241 return; | 223 m_pData->Release(); |
| 224 m_pData = NULL; |
| 242 } | 225 } |
| 243 if (m_pData->m_nRefs > 1) { | |
| 244 m_pData->m_nRefs --; | |
| 245 } else { | |
| 246 FX_Free(m_pData); | |
| 247 } | |
| 248 m_pData = NULL; | |
| 249 } | 226 } |
| 250 void CFX_WideString::ConcatInPlace(FX_STRSIZE nSrcLen, FX_LPCWSTR lpszSrcData) | 227 void CFX_WideString::ConcatInPlace(FX_STRSIZE nSrcLen, FX_LPCWSTR lpszSrcData) |
| 251 { | 228 { |
| 252 if (nSrcLen == 0 || lpszSrcData == NULL) { | 229 if (nSrcLen == 0 || lpszSrcData == NULL) { |
| 253 return; | 230 return; |
| 254 } | 231 } |
| 255 if (m_pData == NULL) { | 232 if (m_pData == NULL) { |
| 256 m_pData = FX_AllocStringW(nSrcLen); | 233 m_pData = StringData::Create(nSrcLen); |
| 257 if (m_pData) { | 234 if (m_pData) { |
| 258 FXSYS_memcpy32(m_pData->m_String, lpszSrcData, nSrcLen * sizeof(FX_W
CHAR)); | 235 FXSYS_memcpy32(m_pData->m_String, lpszSrcData, nSrcLen * sizeof(FX_W
CHAR)); |
| 259 } | 236 } |
| 260 return; | 237 return; |
| 261 } | 238 } |
| 262 if (m_pData->m_nRefs > 1 || m_pData->m_nDataLength + nSrcLen > m_pData->m_nA
llocLength) { | 239 if (m_pData->m_nRefs > 1 || m_pData->m_nDataLength + nSrcLen > m_pData->m_nA
llocLength) { |
| 263 CFX_StringDataW* pOldData = m_pData; | 240 StringData* pOldData = m_pData; |
| 264 ConcatCopy(m_pData->m_nDataLength, m_pData->m_String, nSrcLen, lpszSrcDa
ta); | 241 ConcatCopy(m_pData->m_nDataLength, m_pData->m_String, nSrcLen, lpszSrcDa
ta); |
| 265 FX_ReleaseStringW(pOldData); | 242 pOldData->Release(); |
| 266 } else { | 243 } else { |
| 267 FXSYS_memcpy32(m_pData->m_String + m_pData->m_nDataLength, lpszSrcData,
nSrcLen * sizeof(FX_WCHAR)); | 244 FXSYS_memcpy32(m_pData->m_String + m_pData->m_nDataLength, lpszSrcData,
nSrcLen * sizeof(FX_WCHAR)); |
| 268 m_pData->m_nDataLength += nSrcLen; | 245 m_pData->m_nDataLength += nSrcLen; |
| 269 m_pData->m_String[m_pData->m_nDataLength] = 0; | 246 m_pData->m_String[m_pData->m_nDataLength] = 0; |
| 270 } | 247 } |
| 271 } | 248 } |
| 272 void CFX_WideString::ConcatCopy(FX_STRSIZE nSrc1Len, FX_LPCWSTR lpszSrc1Data, | 249 void CFX_WideString::ConcatCopy(FX_STRSIZE nSrc1Len, FX_LPCWSTR lpszSrc1Data, |
| 273 FX_STRSIZE nSrc2Len, FX_LPCWSTR lpszSrc2Data) | 250 FX_STRSIZE nSrc2Len, FX_LPCWSTR lpszSrc2Data) |
| 274 { | 251 { |
| 275 FX_STRSIZE nNewLen = nSrc1Len + nSrc2Len; | 252 FX_STRSIZE nNewLen = nSrc1Len + nSrc2Len; |
| 276 if (nNewLen == 0) { | 253 if (nNewLen == 0) { |
| 277 return; | 254 return; |
| 278 } | 255 } |
| 279 m_pData = FX_AllocStringW(nNewLen); | 256 m_pData = StringData::Create(nNewLen); |
| 280 if (m_pData) { | 257 if (m_pData) { |
| 281 FXSYS_memcpy32(m_pData->m_String, lpszSrc1Data, nSrc1Len * sizeof(FX_WCH
AR)); | 258 FXSYS_memcpy32(m_pData->m_String, lpszSrc1Data, nSrc1Len * sizeof(FX_WCH
AR)); |
| 282 FXSYS_memcpy32(m_pData->m_String + nSrc1Len, lpszSrc2Data, nSrc2Len * si
zeof(FX_WCHAR)); | 259 FXSYS_memcpy32(m_pData->m_String + nSrc1Len, lpszSrc2Data, nSrc2Len * si
zeof(FX_WCHAR)); |
| 283 } | 260 } |
| 284 } | 261 } |
| 285 void CFX_WideString::CopyBeforeWrite() | 262 void CFX_WideString::CopyBeforeWrite() |
| 286 { | 263 { |
| 287 if (m_pData == NULL || m_pData->m_nRefs <= 1) { | 264 if (m_pData == NULL || m_pData->m_nRefs <= 1) { |
| 288 return; | 265 return; |
| 289 } | 266 } |
| 290 CFX_StringDataW* pData = m_pData; | 267 StringData* pData = m_pData; |
| 291 m_pData->m_nRefs --; | 268 m_pData->Release(); |
| 292 FX_STRSIZE nDataLength = pData->m_nDataLength; | 269 FX_STRSIZE nDataLength = pData->m_nDataLength; |
| 293 m_pData = FX_AllocStringW(nDataLength); | 270 m_pData = StringData::Create(nDataLength); |
| 294 if (m_pData != NULL) { | 271 if (m_pData != NULL) { |
| 295 FXSYS_memcpy32(m_pData->m_String, pData->m_String, (nDataLength + 1) * s
izeof(FX_WCHAR)); | 272 FXSYS_memcpy32(m_pData->m_String, pData->m_String, (nDataLength + 1) * s
izeof(FX_WCHAR)); |
| 296 } | 273 } |
| 297 } | 274 } |
| 298 void CFX_WideString::AllocBeforeWrite(FX_STRSIZE nLen) | 275 void CFX_WideString::AllocBeforeWrite(FX_STRSIZE nLen) |
| 299 { | 276 { |
| 300 if (m_pData && m_pData->m_nRefs <= 1 && m_pData->m_nAllocLength >= nLen) { | 277 if (m_pData && m_pData->m_nRefs <= 1 && m_pData->m_nAllocLength >= nLen) { |
| 301 return; | 278 return; |
| 302 } | 279 } |
| 303 Empty(); | 280 Empty(); |
| 304 m_pData = FX_AllocStringW(nLen); | 281 m_pData = StringData::Create(nLen); |
| 305 } | 282 } |
| 306 void CFX_WideString::AssignCopy(FX_STRSIZE nSrcLen, FX_LPCWSTR lpszSrcData) | 283 void CFX_WideString::AssignCopy(FX_STRSIZE nSrcLen, FX_LPCWSTR lpszSrcData) |
| 307 { | 284 { |
| 308 AllocBeforeWrite(nSrcLen); | 285 AllocBeforeWrite(nSrcLen); |
| 309 FXSYS_memcpy32(m_pData->m_String, lpszSrcData, nSrcLen * sizeof(FX_WCHAR)); | 286 FXSYS_memcpy32(m_pData->m_String, lpszSrcData, nSrcLen * sizeof(FX_WCHAR)); |
| 310 m_pData->m_nDataLength = nSrcLen; | 287 m_pData->m_nDataLength = nSrcLen; |
| 311 m_pData->m_String[nSrcLen] = 0; | 288 m_pData->m_String[nSrcLen] = 0; |
| 312 } | 289 } |
| 313 int CFX_WideString::Compare(FX_LPCWSTR lpsz) const | 290 int CFX_WideString::Compare(FX_LPCWSTR lpsz) const |
| 314 { | 291 { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 } | 329 } |
| 353 FX_LPWSTR CFX_WideString::GetBuffer(FX_STRSIZE nMinBufLength) | 330 FX_LPWSTR CFX_WideString::GetBuffer(FX_STRSIZE nMinBufLength) |
| 354 { | 331 { |
| 355 if (m_pData == NULL && nMinBufLength == 0) { | 332 if (m_pData == NULL && nMinBufLength == 0) { |
| 356 return NULL; | 333 return NULL; |
| 357 } | 334 } |
| 358 if (m_pData && m_pData->m_nRefs <= 1 && m_pData->m_nAllocLength >= nMinBufLe
ngth) { | 335 if (m_pData && m_pData->m_nRefs <= 1 && m_pData->m_nAllocLength >= nMinBufLe
ngth) { |
| 359 return m_pData->m_String; | 336 return m_pData->m_String; |
| 360 } | 337 } |
| 361 if (m_pData == NULL) { | 338 if (m_pData == NULL) { |
| 362 m_pData = FX_AllocStringW(nMinBufLength); | 339 m_pData = StringData::Create(nMinBufLength); |
| 363 if (!m_pData) { | 340 if (!m_pData) { |
| 364 return NULL; | 341 return NULL; |
| 365 } | 342 } |
| 366 m_pData->m_nDataLength = 0; | 343 m_pData->m_nDataLength = 0; |
| 367 m_pData->m_String[0] = 0; | 344 m_pData->m_String[0] = 0; |
| 368 return m_pData->m_String; | 345 return m_pData->m_String; |
| 369 } | 346 } |
| 370 CFX_StringDataW* pOldData = m_pData; | 347 StringData* pOldData = m_pData; |
| 371 FX_STRSIZE nOldLen = pOldData->m_nDataLength; | 348 FX_STRSIZE nOldLen = pOldData->m_nDataLength; |
| 372 if (nMinBufLength < nOldLen) { | 349 if (nMinBufLength < nOldLen) { |
| 373 nMinBufLength = nOldLen; | 350 nMinBufLength = nOldLen; |
| 374 } | 351 } |
| 375 m_pData = FX_AllocStringW(nMinBufLength); | 352 m_pData = StringData::Create(nMinBufLength); |
| 376 if (!m_pData) { | 353 if (!m_pData) { |
| 377 return NULL; | 354 return NULL; |
| 378 } | 355 } |
| 379 FXSYS_memcpy32(m_pData->m_String, pOldData->m_String, (nOldLen + 1)*sizeof(F
X_WCHAR)); | 356 FXSYS_memcpy32(m_pData->m_String, pOldData->m_String, (nOldLen + 1)*sizeof(F
X_WCHAR)); |
| 380 m_pData->m_nDataLength = nOldLen; | 357 m_pData->m_nDataLength = nOldLen; |
| 381 pOldData->m_nRefs --; | 358 pOldData->Release(); |
| 382 if (pOldData->m_nRefs <= 0) { | |
| 383 FX_Free(pOldData); | |
| 384 } | |
| 385 return m_pData->m_String; | 359 return m_pData->m_String; |
| 386 } | 360 } |
| 387 CFX_WideString CFX_WideString::FromLocal(const char* str, FX_STRSIZE len) | 361 CFX_WideString CFX_WideString::FromLocal(const char* str, FX_STRSIZE len) |
| 388 { | 362 { |
| 389 CFX_WideString result; | 363 CFX_WideString result; |
| 390 result.ConvertFrom(CFX_ByteString(str, len)); | 364 result.ConvertFrom(CFX_ByteString(str, len)); |
| 391 return result; | 365 return result; |
| 392 } | 366 } |
| 393 CFX_WideString CFX_WideString::FromUTF8(const char* str, FX_STRSIZE len) | 367 CFX_WideString CFX_WideString::FromUTF8(const char* str, FX_STRSIZE len) |
| 394 { | 368 { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 void CFX_WideString::AllocCopy(CFX_WideString& dest, FX_STRSIZE nCopyLen, FX_STR
SIZE nCopyIndex) const | 403 void CFX_WideString::AllocCopy(CFX_WideString& dest, FX_STRSIZE nCopyLen, FX_STR
SIZE nCopyIndex) const |
| 430 { | 404 { |
| 431 // |FX_STRSIZE| is currently typedef'd as in |int|. TODO(palmer): It | 405 // |FX_STRSIZE| is currently typedef'd as in |int|. TODO(palmer): It |
| 432 // should be a |size_t|, or at least unsigned. | 406 // should be a |size_t|, or at least unsigned. |
| 433 if (nCopyLen == 0 || nCopyLen < 0) { | 407 if (nCopyLen == 0 || nCopyLen < 0) { |
| 434 return; | 408 return; |
| 435 } | 409 } |
| 436 pdfium::base::CheckedNumeric<FX_STRSIZE> iSize = static_cast<FX_STRSIZE>(siz
eof(FX_WCHAR)); | 410 pdfium::base::CheckedNumeric<FX_STRSIZE> iSize = static_cast<FX_STRSIZE>(siz
eof(FX_WCHAR)); |
| 437 iSize *= nCopyLen; | 411 iSize *= nCopyLen; |
| 438 ASSERT(dest.m_pData == NULL); | 412 ASSERT(dest.m_pData == NULL); |
| 439 dest.m_pData = FX_AllocStringW(nCopyLen); | 413 dest.m_pData = StringData::Create(nCopyLen); |
| 440 if (dest.m_pData) { | 414 if (dest.m_pData) { |
| 441 FXSYS_memcpy32(dest.m_pData->m_String, m_pData->m_String + nCopyIndex, i
Size.ValueOrDie()); | 415 FXSYS_memcpy32(dest.m_pData->m_String, m_pData->m_String + nCopyIndex, i
Size.ValueOrDie()); |
| 442 } | 416 } |
| 443 } | 417 } |
| 444 CFX_WideString CFX_WideString::Left(FX_STRSIZE nCount) const | 418 CFX_WideString CFX_WideString::Left(FX_STRSIZE nCount) const |
| 445 { | 419 { |
| 446 if (m_pData == NULL) { | 420 if (m_pData == NULL) { |
| 447 return CFX_WideString(); | 421 return CFX_WideString(); |
| 448 } | 422 } |
| 449 if (nCount < 0) { | 423 if (nCount < 0) { |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 671 while ((lpszTarget = (FX_LPWSTR)FXSYS_wcsstr(lpszStart, lpszOld)) != NUL
L && lpszStart < lpszEnd) { | 645 while ((lpszTarget = (FX_LPWSTR)FXSYS_wcsstr(lpszStart, lpszOld)) != NUL
L && lpszStart < lpszEnd) { |
| 672 nCount++; | 646 nCount++; |
| 673 lpszStart = lpszTarget + nSourceLen; | 647 lpszStart = lpszTarget + nSourceLen; |
| 674 } | 648 } |
| 675 } | 649 } |
| 676 if (nCount > 0) { | 650 if (nCount > 0) { |
| 677 CopyBeforeWrite(); | 651 CopyBeforeWrite(); |
| 678 FX_STRSIZE nOldLength = m_pData->m_nDataLength; | 652 FX_STRSIZE nOldLength = m_pData->m_nDataLength; |
| 679 FX_STRSIZE nNewLength = nOldLength + (nReplacementLen - nSourceLen) * n
Count; | 653 FX_STRSIZE nNewLength = nOldLength + (nReplacementLen - nSourceLen) * n
Count; |
| 680 if (m_pData->m_nAllocLength < nNewLength || m_pData->m_nRefs > 1) { | 654 if (m_pData->m_nAllocLength < nNewLength || m_pData->m_nRefs > 1) { |
| 681 CFX_StringDataW* pOldData = m_pData; | 655 StringData* pOldData = m_pData; |
| 682 FX_LPCWSTR pstr = m_pData->m_String; | 656 FX_LPCWSTR pstr = m_pData->m_String; |
| 683 m_pData = FX_AllocStringW(nNewLength); | 657 m_pData = StringData::Create(nNewLength); |
| 684 if (!m_pData) { | 658 if (!m_pData) { |
| 685 return 0; | 659 return 0; |
| 686 } | 660 } |
| 687 FXSYS_memcpy32(m_pData->m_String, pstr, pOldData->m_nDataLength * si
zeof(FX_WCHAR)); | 661 FXSYS_memcpy32(m_pData->m_String, pstr, pOldData->m_nDataLength * si
zeof(FX_WCHAR)); |
| 688 FX_ReleaseStringW(pOldData); | 662 pOldData->Release(); |
| 689 } | 663 } |
| 690 lpszStart = m_pData->m_String; | 664 lpszStart = m_pData->m_String; |
| 691 lpszEnd = m_pData->m_String + FX_MAX(m_pData->m_nDataLength, nNewLength)
; | 665 lpszEnd = m_pData->m_String + FX_MAX(m_pData->m_nDataLength, nNewLength)
; |
| 692 { | 666 { |
| 693 while ((lpszTarget = (FX_LPWSTR)FXSYS_wcsstr(lpszStart, lpszOld)) !=
NULL && lpszStart < lpszEnd) { | 667 while ((lpszTarget = (FX_LPWSTR)FXSYS_wcsstr(lpszStart, lpszOld)) !=
NULL && lpszStart < lpszEnd) { |
| 694 FX_STRSIZE nBalance = nOldLength - (FX_STRSIZE)(lpszTarget - m_p
Data->m_String + nSourceLen); | 668 FX_STRSIZE nBalance = nOldLength - (FX_STRSIZE)(lpszTarget - m_p
Data->m_String + nSourceLen); |
| 695 FXSYS_memmove32(lpszTarget + nReplacementLen, lpszTarget + nSour
ceLen, nBalance * sizeof(FX_WCHAR)); | 669 FXSYS_memmove32(lpszTarget + nReplacementLen, lpszTarget + nSour
ceLen, nBalance * sizeof(FX_WCHAR)); |
| 696 FXSYS_memcpy32(lpszTarget, lpszNew, nReplacementLen * sizeof(FX_
WCHAR)); | 670 FXSYS_memcpy32(lpszTarget, lpszNew, nReplacementLen * sizeof(FX_
WCHAR)); |
| 697 lpszStart = lpszTarget + nReplacementLen; | 671 lpszStart = lpszTarget + nReplacementLen; |
| 698 lpszStart[nBalance] = 0; | 672 lpszStart[nBalance] = 0; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 709 CopyBeforeWrite(); | 683 CopyBeforeWrite(); |
| 710 if (nIndex < 0) { | 684 if (nIndex < 0) { |
| 711 nIndex = 0; | 685 nIndex = 0; |
| 712 } | 686 } |
| 713 FX_STRSIZE nNewLength = GetLength(); | 687 FX_STRSIZE nNewLength = GetLength(); |
| 714 if (nIndex > nNewLength) { | 688 if (nIndex > nNewLength) { |
| 715 nIndex = nNewLength; | 689 nIndex = nNewLength; |
| 716 } | 690 } |
| 717 nNewLength++; | 691 nNewLength++; |
| 718 if (m_pData == NULL || m_pData->m_nAllocLength < nNewLength) { | 692 if (m_pData == NULL || m_pData->m_nAllocLength < nNewLength) { |
| 719 CFX_StringDataW* pOldData = m_pData; | 693 StringData* pOldData = m_pData; |
| 720 FX_LPCWSTR pstr = m_pData->m_String; | 694 FX_LPCWSTR pstr = m_pData->m_String; |
| 721 m_pData = FX_AllocStringW(nNewLength); | 695 m_pData = StringData::Create(nNewLength); |
| 722 if (!m_pData) { | 696 if (!m_pData) { |
| 723 return 0; | 697 return 0; |
| 724 } | 698 } |
| 725 if(pOldData != NULL) { | 699 if(pOldData != NULL) { |
| 726 FXSYS_memmove32(m_pData->m_String, pstr, (pOldData->m_nDataLength +
1)*sizeof(FX_WCHAR)); | 700 FXSYS_memmove32(m_pData->m_String, pstr, (pOldData->m_nDataLength +
1)*sizeof(FX_WCHAR)); |
| 727 FX_ReleaseStringW(pOldData); | 701 pOldData->Release(); |
| 728 } else { | 702 } else { |
| 729 m_pData->m_String[0] = 0; | 703 m_pData->m_String[0] = 0; |
| 730 } | 704 } |
| 731 } | 705 } |
| 732 FXSYS_memmove32(m_pData->m_String + nIndex + 1, | 706 FXSYS_memmove32(m_pData->m_String + nIndex + 1, |
| 733 m_pData->m_String + nIndex, (nNewLength - nIndex)*sizeof(FX_
WCHAR)); | 707 m_pData->m_String + nIndex, (nNewLength - nIndex)*sizeof(FX_
WCHAR)); |
| 734 m_pData->m_String[nIndex] = ch; | 708 m_pData->m_String[nIndex] = ch; |
| 735 m_pData->m_nDataLength = nNewLength; | 709 m_pData->m_nDataLength = nNewLength; |
| 736 return nNewLength; | 710 return nNewLength; |
| 737 } | 711 } |
| (...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1105 return (CFX_CharMap*)&g_DefaultJISMapper; | 1079 return (CFX_CharMap*)&g_DefaultJISMapper; |
| 1106 case 936: | 1080 case 936: |
| 1107 return (CFX_CharMap*)&g_DefaultGBKMapper; | 1081 return (CFX_CharMap*)&g_DefaultGBKMapper; |
| 1108 case 949: | 1082 case 949: |
| 1109 return (CFX_CharMap*)&g_DefaultUHCMapper; | 1083 return (CFX_CharMap*)&g_DefaultUHCMapper; |
| 1110 case 950: | 1084 case 950: |
| 1111 return (CFX_CharMap*)&g_DefaultBig5Mapper; | 1085 return (CFX_CharMap*)&g_DefaultBig5Mapper; |
| 1112 } | 1086 } |
| 1113 return NULL; | 1087 return NULL; |
| 1114 } | 1088 } |
| OLD | NEW |