| 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 #ifndef CORE_FXCRT_INCLUDE_FX_STRING_H_ | 7 #ifndef CORE_FXCRT_INCLUDE_FX_STRING_H_ |
| 8 #define CORE_FXCRT_INCLUDE_FX_STRING_H_ | 8 #define CORE_FXCRT_INCLUDE_FX_STRING_H_ |
| 9 | 9 |
| 10 #include <stdint.h> // For intptr_t. | 10 #include <stdint.h> // For intptr_t. |
| (...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 return rhs != lhs; | 475 return rhs != lhs; |
| 476 } | 476 } |
| 477 #define FX_WSTRC(wstr) CFX_WideStringC(wstr, FX_ArraySize(wstr) - 1) | 477 #define FX_WSTRC(wstr) CFX_WideStringC(wstr, FX_ArraySize(wstr) - 1) |
| 478 | 478 |
| 479 // A mutable string with shared buffers using copy-on-write semantics that | 479 // A mutable string with shared buffers using copy-on-write semantics that |
| 480 // avoids the cost of std::string's iterator stability guarantees. | 480 // avoids the cost of std::string's iterator stability guarantees. |
| 481 class CFX_WideString { | 481 class CFX_WideString { |
| 482 public: | 482 public: |
| 483 typedef FX_WCHAR value_type; | 483 typedef FX_WCHAR value_type; |
| 484 | 484 |
| 485 CFX_WideString() : m_pData(nullptr) {} | 485 CFX_WideString() {} |
| 486 | 486 CFX_WideString(const CFX_WideString& other) : m_pData(other.m_pData) {} |
| 487 // Copy constructor. | 487 CFX_WideString(CFX_WideString&& other) { m_pData.Swap(other.m_pData); } |
| 488 CFX_WideString(const CFX_WideString& str); | |
| 489 | |
| 490 // Move constructor. | |
| 491 CFX_WideString(CFX_WideString&& other) { | |
| 492 m_pData = other.m_pData; | |
| 493 other.m_pData = nullptr; | |
| 494 } | |
| 495 | 488 |
| 496 CFX_WideString(const FX_WCHAR* ptr) | 489 CFX_WideString(const FX_WCHAR* ptr) |
| 497 : CFX_WideString(ptr, ptr ? FXSYS_wcslen(ptr) : 0) {} | 490 : CFX_WideString(ptr, ptr ? FXSYS_wcslen(ptr) : 0) {} |
| 498 | 491 |
| 499 CFX_WideString(const FX_WCHAR* ptr, FX_STRSIZE len); | 492 CFX_WideString(const FX_WCHAR* ptr, FX_STRSIZE len); |
| 500 CFX_WideString(FX_WCHAR ch); | 493 CFX_WideString(FX_WCHAR ch); |
| 501 | 494 |
| 502 CFX_WideString(const CFX_WideStringC& str); | 495 CFX_WideString(const CFX_WideStringC& str); |
| 503 CFX_WideString(const CFX_WideStringC& str1, const CFX_WideStringC& str2); | 496 CFX_WideString(const CFX_WideStringC& str1, const CFX_WideStringC& str2); |
| 504 | 497 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 520 // Implicit conversion to C-style wide string -- deprecated. | 513 // Implicit conversion to C-style wide string -- deprecated. |
| 521 // Note: |this| must outlive the use of the result. | 514 // Note: |this| must outlive the use of the result. |
| 522 operator const FX_WCHAR*() const { return m_pData ? m_pData->m_String : L""; } | 515 operator const FX_WCHAR*() const { return m_pData ? m_pData->m_String : L""; } |
| 523 | 516 |
| 524 // Explicit conversion to CFX_WideStringC. | 517 // Explicit conversion to CFX_WideStringC. |
| 525 // Note: |this| must outlive the use of the result. | 518 // Note: |this| must outlive the use of the result. |
| 526 CFX_WideStringC AsWideStringC() const { | 519 CFX_WideStringC AsWideStringC() const { |
| 527 return CFX_WideStringC(c_str(), GetLength()); | 520 return CFX_WideStringC(c_str(), GetLength()); |
| 528 } | 521 } |
| 529 | 522 |
| 530 void Empty(); | 523 // Deprecated -- use clear(). |
| 524 void Empty() { m_pData.Reset(); } |
| 525 void clear() { m_pData.Reset(); } |
| 526 |
| 527 FX_STRSIZE GetLength() const { return m_pData ? m_pData->m_nDataLength : 0; } |
| 531 bool IsEmpty() const { return !GetLength(); } | 528 bool IsEmpty() const { return !GetLength(); } |
| 532 FX_STRSIZE GetLength() const { return m_pData ? m_pData->m_nDataLength : 0; } | |
| 533 | 529 |
| 534 const CFX_WideString& operator=(const FX_WCHAR* str); | 530 const CFX_WideString& operator=(const FX_WCHAR* str); |
| 535 const CFX_WideString& operator=(const CFX_WideString& stringSrc); | 531 const CFX_WideString& operator=(const CFX_WideString& stringSrc); |
| 536 const CFX_WideString& operator=(const CFX_WideStringC& stringSrc); | 532 const CFX_WideString& operator=(const CFX_WideStringC& stringSrc); |
| 537 | 533 |
| 538 const CFX_WideString& operator+=(const FX_WCHAR* str); | 534 const CFX_WideString& operator+=(const FX_WCHAR* str); |
| 539 const CFX_WideString& operator+=(FX_WCHAR ch); | 535 const CFX_WideString& operator+=(FX_WCHAR ch); |
| 540 const CFX_WideString& operator+=(const CFX_WideString& str); | 536 const CFX_WideString& operator+=(const CFX_WideString& str); |
| 541 const CFX_WideString& operator+=(const CFX_WideStringC& str); | 537 const CFX_WideString& operator+=(const CFX_WideStringC& str); |
| 542 | 538 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 580 FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1); | 576 FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1); |
| 581 | 577 |
| 582 void Format(const FX_WCHAR* lpszFormat, ...); | 578 void Format(const FX_WCHAR* lpszFormat, ...); |
| 583 void FormatV(const FX_WCHAR* lpszFormat, va_list argList); | 579 void FormatV(const FX_WCHAR* lpszFormat, va_list argList); |
| 584 | 580 |
| 585 void MakeLower(); | 581 void MakeLower(); |
| 586 void MakeUpper(); | 582 void MakeUpper(); |
| 587 | 583 |
| 588 void TrimRight(); | 584 void TrimRight(); |
| 589 void TrimRight(FX_WCHAR chTarget); | 585 void TrimRight(FX_WCHAR chTarget); |
| 590 void TrimRight(const FX_WCHAR* lpszTargets); | 586 void TrimRight(const CFX_WideStringC& pTargets); |
| 591 | 587 |
| 592 void TrimLeft(); | 588 void TrimLeft(); |
| 593 void TrimLeft(FX_WCHAR chTarget); | 589 void TrimLeft(FX_WCHAR chTarget); |
| 594 void TrimLeft(const FX_WCHAR* lpszTargets); | 590 void TrimLeft(const CFX_WideStringC& pTargets); |
| 595 | 591 |
| 596 void Reserve(FX_STRSIZE len); | 592 void Reserve(FX_STRSIZE len); |
| 597 FX_WCHAR* GetBuffer(FX_STRSIZE len); | 593 FX_WCHAR* GetBuffer(FX_STRSIZE len); |
| 598 void ReleaseBuffer(FX_STRSIZE len = -1); | 594 void ReleaseBuffer(FX_STRSIZE len = -1); |
| 599 | 595 |
| 600 int GetInteger() const; | 596 int GetInteger() const; |
| 601 FX_FLOAT GetFloat() const; | 597 FX_FLOAT GetFloat() const; |
| 602 | 598 |
| 603 FX_STRSIZE Find(const FX_WCHAR* lpszSub, FX_STRSIZE start = 0) const; | 599 FX_STRSIZE Find(const CFX_WideStringC& pSub, FX_STRSIZE start = 0) const; |
| 604 FX_STRSIZE Find(FX_WCHAR ch, FX_STRSIZE start = 0) const; | 600 FX_STRSIZE Find(FX_WCHAR ch, FX_STRSIZE start = 0) const; |
| 605 FX_STRSIZE Replace(const FX_WCHAR* lpszOld, const FX_WCHAR* lpszNew); | 601 FX_STRSIZE Replace(const CFX_WideStringC& pOld, const CFX_WideStringC& pNew); |
| 606 FX_STRSIZE Remove(FX_WCHAR ch); | 602 FX_STRSIZE Remove(FX_WCHAR ch); |
| 607 | 603 |
| 608 CFX_ByteString UTF8Encode() const; | 604 CFX_ByteString UTF8Encode() const; |
| 609 CFX_ByteString UTF16LE_Encode() const; | 605 CFX_ByteString UTF16LE_Encode() const; |
| 610 | 606 |
| 611 protected: | 607 protected: |
| 612 class StringData { | 608 using StringData = CFX_StringDataTemplate<FX_WCHAR>; |
| 613 public: | |
| 614 static StringData* Create(int nLen); | |
| 615 void Retain() { ++m_nRefs; } | |
| 616 void Release() { | |
| 617 if (--m_nRefs <= 0) | |
| 618 FX_Free(this); | |
| 619 } | |
| 620 | 609 |
| 621 intptr_t m_nRefs; // Would prefer ssize_t, but no windows support. | 610 void ReallocBeforeWrite(FX_STRSIZE nLen); |
| 622 FX_STRSIZE m_nDataLength; | |
| 623 FX_STRSIZE m_nAllocLength; | |
| 624 FX_WCHAR m_String[1]; | |
| 625 | |
| 626 private: | |
| 627 StringData(FX_STRSIZE dataLen, FX_STRSIZE allocLen) | |
| 628 : m_nRefs(1), m_nDataLength(dataLen), m_nAllocLength(allocLen) { | |
| 629 FXSYS_assert(dataLen >= 0); | |
| 630 FXSYS_assert(allocLen >= 0); | |
| 631 FXSYS_assert(dataLen <= allocLen); | |
| 632 m_String[dataLen] = 0; | |
| 633 } | |
| 634 ~StringData() = delete; | |
| 635 }; | |
| 636 | |
| 637 void CopyBeforeWrite(); | |
| 638 void AllocBeforeWrite(FX_STRSIZE nLen); | 611 void AllocBeforeWrite(FX_STRSIZE nLen); |
| 639 void ConcatInPlace(FX_STRSIZE nSrcLen, const FX_WCHAR* lpszSrcData); | |
| 640 void ConcatCopy(FX_STRSIZE nSrc1Len, | |
| 641 const FX_WCHAR* lpszSrc1Data, | |
| 642 FX_STRSIZE nSrc2Len, | |
| 643 const FX_WCHAR* lpszSrc2Data); | |
| 644 void AssignCopy(FX_STRSIZE nSrcLen, const FX_WCHAR* lpszSrcData); | |
| 645 void AllocCopy(CFX_WideString& dest, | 612 void AllocCopy(CFX_WideString& dest, |
| 646 FX_STRSIZE nCopyLen, | 613 FX_STRSIZE nCopyLen, |
| 647 FX_STRSIZE nCopyIndex) const; | 614 FX_STRSIZE nCopyIndex) const; |
| 615 void AssignCopy(const FX_WCHAR* pSrcData, FX_STRSIZE nSrcLen); |
| 616 void Concat(const FX_WCHAR* lpszSrcData, FX_STRSIZE nSrcLen); |
| 648 | 617 |
| 649 StringData* m_pData; | 618 CFX_RetainPtr<StringData> m_pData; |
| 650 friend class fxcrt_WideStringConcatInPlace_Test; | 619 friend class fxcrt_WideStringConcatInPlace_Test; |
| 651 }; | 620 }; |
| 652 | 621 |
| 653 inline CFX_WideStringC& CFX_WideStringC::operator=(const CFX_WideString& src) { | 622 inline CFX_WideStringC& CFX_WideStringC::operator=(const CFX_WideString& src) { |
| 654 m_Ptr = src.c_str(); | 623 m_Ptr = src.c_str(); |
| 655 m_Length = src.GetLength(); | 624 m_Length = src.GetLength(); |
| 656 return *this; | 625 return *this; |
| 657 } | 626 } |
| 658 | 627 |
| 659 inline CFX_WideString operator+(const CFX_WideStringC& str1, | 628 inline CFX_WideString operator+(const CFX_WideStringC& str1, |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 722 } | 691 } |
| 723 | 692 |
| 724 FX_FLOAT FX_atof(const CFX_ByteStringC& str); | 693 FX_FLOAT FX_atof(const CFX_ByteStringC& str); |
| 725 inline FX_FLOAT FX_atof(const CFX_WideStringC& wsStr) { | 694 inline FX_FLOAT FX_atof(const CFX_WideStringC& wsStr) { |
| 726 return FX_atof(FX_UTF8Encode(wsStr.c_str(), wsStr.GetLength()).c_str()); | 695 return FX_atof(FX_UTF8Encode(wsStr.c_str(), wsStr.GetLength()).c_str()); |
| 727 } | 696 } |
| 728 void FX_atonum(const CFX_ByteStringC& str, FX_BOOL& bInteger, void* pData); | 697 void FX_atonum(const CFX_ByteStringC& str, FX_BOOL& bInteger, void* pData); |
| 729 FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_CHAR* buf); | 698 FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_CHAR* buf); |
| 730 | 699 |
| 731 #endif // CORE_FXCRT_INCLUDE_FX_STRING_H_ | 700 #endif // CORE_FXCRT_INCLUDE_FX_STRING_H_ |
| OLD | NEW |