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 <algorithm> | 9 #include <algorithm> |
10 #include <cctype> | 10 #include <cctype> |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 FX_CHAR* buffer = result.GetBuffer(len * 2 + 2); | 296 FX_CHAR* buffer = result.GetBuffer(len * 2 + 2); |
297 for (int i = 0; i < len; i++) { | 297 for (int i = 0; i < len; i++) { |
298 buffer[i * 2] = m_pData->m_String[i] & 0xff; | 298 buffer[i * 2] = m_pData->m_String[i] & 0xff; |
299 buffer[i * 2 + 1] = m_pData->m_String[i] >> 8; | 299 buffer[i * 2 + 1] = m_pData->m_String[i] >> 8; |
300 } | 300 } |
301 buffer[len * 2] = 0; | 301 buffer[len * 2] = 0; |
302 buffer[len * 2 + 1] = 0; | 302 buffer[len * 2 + 1] = 0; |
303 result.ReleaseBuffer(len * 2 + 2); | 303 result.ReleaseBuffer(len * 2 + 2); |
304 return result; | 304 return result; |
305 } | 305 } |
306 void CFX_WideString::ConvertFrom(const CFX_ByteString& str, | 306 |
307 CFX_CharMap* pCharMap) { | |
308 if (!pCharMap) { | |
309 pCharMap = CFX_CharMap::GetDefaultMapper(); | |
310 } | |
311 *this = pCharMap->m_GetWideString(pCharMap, str); | |
312 } | |
313 void CFX_WideString::Reserve(FX_STRSIZE len) { | 307 void CFX_WideString::Reserve(FX_STRSIZE len) { |
314 GetBuffer(len); | 308 GetBuffer(len); |
315 ReleaseBuffer(GetLength()); | 309 ReleaseBuffer(GetLength()); |
316 } | 310 } |
317 FX_WCHAR* CFX_WideString::GetBuffer(FX_STRSIZE nMinBufLength) { | 311 FX_WCHAR* CFX_WideString::GetBuffer(FX_STRSIZE nMinBufLength) { |
318 if (!m_pData && nMinBufLength == 0) { | 312 if (!m_pData && nMinBufLength == 0) { |
319 return NULL; | 313 return NULL; |
320 } | 314 } |
321 if (m_pData && m_pData->m_nRefs <= 1 && | 315 if (m_pData && m_pData->m_nRefs <= 1 && |
322 m_pData->m_nAllocLength >= nMinBufLength) { | 316 m_pData->m_nAllocLength >= nMinBufLength) { |
(...skipping 16 matching lines...) Expand all Loading... |
339 m_pData = StringData::Create(nMinBufLength); | 333 m_pData = StringData::Create(nMinBufLength); |
340 if (!m_pData) { | 334 if (!m_pData) { |
341 return NULL; | 335 return NULL; |
342 } | 336 } |
343 FXSYS_memcpy(m_pData->m_String, pOldData->m_String, | 337 FXSYS_memcpy(m_pData->m_String, pOldData->m_String, |
344 (nOldLen + 1) * sizeof(FX_WCHAR)); | 338 (nOldLen + 1) * sizeof(FX_WCHAR)); |
345 m_pData->m_nDataLength = nOldLen; | 339 m_pData->m_nDataLength = nOldLen; |
346 pOldData->Release(); | 340 pOldData->Release(); |
347 return m_pData->m_String; | 341 return m_pData->m_String; |
348 } | 342 } |
349 CFX_WideString CFX_WideString::FromLocal(const char* str, FX_STRSIZE len) { | 343 |
350 CFX_WideString result; | 344 // static |
351 result.ConvertFrom(CFX_ByteString(str, len)); | 345 CFX_WideString CFX_WideString::FromLocal(const CFX_ByteString& str) { |
352 return result; | 346 return FromCodePage(str, 0); |
353 } | 347 } |
| 348 |
| 349 // static |
| 350 CFX_WideString CFX_WideString::FromCodePage(const CFX_ByteString& str, |
| 351 FX_WORD codepage) { |
| 352 CFX_CharMap* pCharMap = CFX_CharMap::GetDefaultMapper(codepage); |
| 353 return pCharMap->m_GetWideString(pCharMap, str); |
| 354 } |
| 355 |
| 356 // static |
354 CFX_WideString CFX_WideString::FromUTF8(const char* str, FX_STRSIZE len) { | 357 CFX_WideString CFX_WideString::FromUTF8(const char* str, FX_STRSIZE len) { |
355 if (!str || 0 == len) { | 358 if (!str || 0 == len) { |
356 return CFX_WideString(); | 359 return CFX_WideString(); |
357 } | 360 } |
358 | 361 |
359 CFX_UTF8Decoder decoder; | 362 CFX_UTF8Decoder decoder; |
360 for (FX_STRSIZE i = 0; i < len; i++) { | 363 for (FX_STRSIZE i = 0; i < len; i++) { |
361 decoder.Input(str[i]); | 364 decoder.Input(str[i]); |
362 } | 365 } |
363 return decoder.GetResult(); | 366 return decoder.GetResult(); |
364 } | 367 } |
| 368 |
| 369 // static |
365 CFX_WideString CFX_WideString::FromUTF16LE(const unsigned short* wstr, | 370 CFX_WideString CFX_WideString::FromUTF16LE(const unsigned short* wstr, |
366 FX_STRSIZE wlen) { | 371 FX_STRSIZE wlen) { |
367 if (!wstr || 0 == wlen) { | 372 if (!wstr || 0 == wlen) { |
368 return CFX_WideString(); | 373 return CFX_WideString(); |
369 } | 374 } |
370 | 375 |
371 CFX_WideString result; | 376 CFX_WideString result; |
372 FX_WCHAR* buf = result.GetBuffer(wlen); | 377 FX_WCHAR* buf = result.GetBuffer(wlen); |
373 for (int i = 0; i < wlen; i++) { | 378 for (int i = 0; i < wlen; i++) { |
374 buf[i] = wstr[i]; | 379 buf[i] = wstr[i]; |
375 } | 380 } |
376 result.ReleaseBuffer(wlen); | 381 result.ReleaseBuffer(wlen); |
377 return result; | 382 return result; |
378 } | 383 } |
| 384 |
379 FX_STRSIZE CFX_WideString::WStringLength(const unsigned short* str) { | 385 FX_STRSIZE CFX_WideString::WStringLength(const unsigned short* str) { |
380 FX_STRSIZE len = 0; | 386 FX_STRSIZE len = 0; |
381 if (str) | 387 if (str) |
382 while (str[len]) | 388 while (str[len]) |
383 len++; | 389 len++; |
384 return len; | 390 return len; |
385 } | 391 } |
386 | 392 |
387 void CFX_WideString::AllocCopy(CFX_WideString& dest, | 393 void CFX_WideString::AllocCopy(CFX_WideString& dest, |
388 FX_STRSIZE nCopyLen, | 394 FX_STRSIZE nCopyLen, |
(...skipping 663 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1052 return (CFX_CharMap*)&g_DefaultJISMapper; | 1058 return (CFX_CharMap*)&g_DefaultJISMapper; |
1053 case 936: | 1059 case 936: |
1054 return (CFX_CharMap*)&g_DefaultGBKMapper; | 1060 return (CFX_CharMap*)&g_DefaultGBKMapper; |
1055 case 949: | 1061 case 949: |
1056 return (CFX_CharMap*)&g_DefaultUHCMapper; | 1062 return (CFX_CharMap*)&g_DefaultUHCMapper; |
1057 case 950: | 1063 case 950: |
1058 return (CFX_CharMap*)&g_DefaultBig5Mapper; | 1064 return (CFX_CharMap*)&g_DefaultBig5Mapper; |
1059 } | 1065 } |
1060 return NULL; | 1066 return NULL; |
1061 } | 1067 } |
OLD | NEW |