From f6919e95ed1445b4d6b46cf1039250e6613489b6 Mon Sep 17 00:00:00 2001 From: tsepez Date: Fri, 1 Apr 2016 16:32:56 -0700 Subject: Re-land "Use CFX_RetainPtr to ref count CFX_ByteString"" This reverts commit b33dfdf68bafa30e90a65fb71ff6b343202e0561. BUG= Review URL: https://codereview.chromium.org/1848993002 --- core/fxcrt/include/fx_string.h | 117 ++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 72 deletions(-) (limited to 'core/fxcrt/include/fx_string.h') diff --git a/core/fxcrt/include/fx_string.h b/core/fxcrt/include/fx_string.h index 26000dd6c1..9aa67845f8 100644 --- a/core/fxcrt/include/fx_string.h +++ b/core/fxcrt/include/fx_string.h @@ -10,6 +10,7 @@ #include // For intptr_t. #include +#include "core/fxcrt/include/cfx_retain_ptr.h" #include "core/fxcrt/include/fx_memory.h" #include "core/fxcrt/include/fx_system.h" @@ -92,11 +93,9 @@ class CFX_ByteStringC { uint32_t GetID(FX_STRSIZE start_pos = 0) const; const uint8_t* GetPtr() const { return m_Ptr; } - const FX_CHAR* GetCStr() const { return (const FX_CHAR*)m_Ptr; } FX_STRSIZE GetLength() const { return m_Length; } - bool IsEmpty() const { return m_Length == 0; } uint8_t GetAt(FX_STRSIZE index) const { return m_Ptr[index]; } @@ -144,16 +143,9 @@ class CFX_ByteString { public: typedef FX_CHAR value_type; - CFX_ByteString() : m_pData(nullptr) {} - - // Copy constructor. - CFX_ByteString(const CFX_ByteString& str); - - // Move constructor. - inline CFX_ByteString(CFX_ByteString&& other) { - m_pData = other.m_pData; - other.m_pData = nullptr; - } + CFX_ByteString() {} + CFX_ByteString(const CFX_ByteString& other) : m_pData(other.m_pData) {} + CFX_ByteString(CFX_ByteString&& other) { m_pData.Swap(other.m_pData); } CFX_ByteString(char ch); CFX_ByteString(const FX_CHAR* ptr) @@ -167,8 +159,11 @@ class CFX_ByteString { ~CFX_ByteString(); - static CFX_ByteString FromUnicode(const FX_WCHAR* ptr, FX_STRSIZE len = -1); + // Deprecated -- use clear(). + void Empty() { m_pData.Reset(); } + void clear() { m_pData.Reset(); } + static CFX_ByteString FromUnicode(const FX_WCHAR* ptr, FX_STRSIZE len = -1); static CFX_ByteString FromUnicode(const CFX_WideString& str); // Explicit conversion to C-style string. @@ -190,7 +185,6 @@ class CFX_ByteString { } FX_STRSIZE GetLength() const { return m_pData ? m_pData->m_nDataLength : 0; } - bool IsEmpty() const { return !GetLength(); } int Compare(const CFX_ByteStringC& str) const; @@ -212,24 +206,16 @@ class CFX_ByteString { return result < 0 || (result == 0 && GetLength() < str.GetLength()); } - void Empty(); - const CFX_ByteString& operator=(const FX_CHAR* str); - const CFX_ByteString& operator=(const CFX_ByteStringC& bstrc); - const CFX_ByteString& operator=(const CFX_ByteString& stringSrc); - const CFX_ByteString& operator=(const CFX_BinaryBuf& buf); void Load(const uint8_t* str, FX_STRSIZE len); const CFX_ByteString& operator+=(FX_CHAR ch); - const CFX_ByteString& operator+=(const FX_CHAR* str); - const CFX_ByteString& operator+=(const CFX_ByteString& str); - const CFX_ByteString& operator+=(const CFX_ByteStringC& bstrc); uint8_t GetAt(FX_STRSIZE nIndex) const { @@ -241,49 +227,34 @@ class CFX_ByteString { } void SetAt(FX_STRSIZE nIndex, FX_CHAR ch); - FX_STRSIZE Insert(FX_STRSIZE index, FX_CHAR ch); - FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1); void Format(const FX_CHAR* lpszFormat, ...); - void FormatV(const FX_CHAR* lpszFormat, va_list argList); void Reserve(FX_STRSIZE len); - FX_CHAR* GetBuffer(FX_STRSIZE len); - void ReleaseBuffer(FX_STRSIZE len = -1); CFX_ByteString Mid(FX_STRSIZE first) const; - CFX_ByteString Mid(FX_STRSIZE first, FX_STRSIZE count) const; - CFX_ByteString Left(FX_STRSIZE count) const; - CFX_ByteString Right(FX_STRSIZE count) const; FX_STRSIZE Find(const CFX_ByteStringC& lpszSub, FX_STRSIZE start = 0) const; - FX_STRSIZE Find(FX_CHAR ch, FX_STRSIZE start = 0) const; - FX_STRSIZE ReverseFind(FX_CHAR ch) const; void MakeLower(); - void MakeUpper(); void TrimRight(); - void TrimRight(FX_CHAR chTarget); - void TrimRight(const CFX_ByteStringC& lpszTargets); void TrimLeft(); - void TrimLeft(FX_CHAR chTarget); - void TrimLeft(const CFX_ByteStringC& lpszTargets); FX_STRSIZE Replace(const CFX_ByteStringC& lpszOld, @@ -303,52 +274,62 @@ class CFX_ByteString { static CFX_ByteString FormatFloat(FX_FLOAT f, int precision = 0); protected: - // To ensure ref counts do not overflow, consider the worst possible case: - // the entire address space contains nothing but pointers to this object. - // Since the count increments with each new pointer, the largest value is - // the number of pointers that can fit into the address space. The size of - // the address space itself is a good upper bound on it; we need not go - // larger. class StringData { public: - static StringData* Create(int nLen); + static StringData* Create(FX_STRSIZE nLen); + static StringData* Create(const StringData& other); + static StringData* Create(const FX_CHAR* pStr, FX_STRSIZE nLen); + void Retain() { ++m_nRefs; } void Release() { if (--m_nRefs <= 0) FX_Free(this); } + bool CanOperateInPlace(FX_STRSIZE nTotalLen) const { + return m_nRefs <= 1 && nTotalLen <= m_nAllocLength; + } + + void CopyContents(const StringData& other); + void CopyContents(const FX_CHAR* pStr, FX_STRSIZE nLen); + void CopyContentsAt(FX_STRSIZE offset, + const FX_CHAR* pStr, + FX_STRSIZE nLen); + + // To ensure ref counts do not overflow, consider the worst possible case: + // the entire address space contains nothing but pointers to this object. + // Since the count increments with each new pointer, the largest value is + // the number of pointers that can fit into the address space. The size of + // the address space itself is a good upper bound on it. intptr_t m_nRefs; // Would prefer ssize_t, but no windows support. + + // |FX_STRSIZE| is currently typedef'd as |int|. + // TODO(palmer): It should be a |size_t|, or at least unsigned. + // These lengths do not include the terminating NUL, but the underlying + // buffer is sized to be capable of holding it. FX_STRSIZE m_nDataLength; FX_STRSIZE m_nAllocLength; + + // Not really 1, variable size. FX_CHAR m_String[1]; private: - StringData(FX_STRSIZE dataLen, FX_STRSIZE allocLen) - : m_nRefs(1), m_nDataLength(dataLen), m_nAllocLength(allocLen) { - FXSYS_assert(dataLen >= 0); - FXSYS_assert(allocLen >= 0); - FXSYS_assert(dataLen <= allocLen); - m_String[dataLen] = 0; - } + StringData(FX_STRSIZE dataLen, FX_STRSIZE allocLen); ~StringData() = delete; }; + void ReallocBeforeWrite(FX_STRSIZE nNewLen); + void AllocBeforeWrite(FX_STRSIZE nNewLen); void AllocCopy(CFX_ByteString& dest, FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex) const; - void AssignCopy(FX_STRSIZE nSrcLen, const FX_CHAR* lpszSrcData); - void ConcatCopy(FX_STRSIZE nSrc1Len, - const FX_CHAR* lpszSrc1Data, - FX_STRSIZE nSrc2Len, - const FX_CHAR* lpszSrc2Data); - void ConcatInPlace(FX_STRSIZE nSrcLen, const FX_CHAR* lpszSrcData); - void CopyBeforeWrite(); - void AllocBeforeWrite(FX_STRSIZE nLen); + void AssignCopy(const FX_CHAR* pSrcData, FX_STRSIZE nSrcLen); + void Concat(const FX_CHAR* lpszSrcData, FX_STRSIZE nSrcLen); - StringData* m_pData; - friend class fxcrt_ByteStringConcatInPlace_Test; + CFX_RetainPtr m_pData; + friend class fxcrt_ByteStringConcat_Test; }; + inline CFX_ByteStringC::CFX_ByteStringC(const CFX_ByteString& src) { m_Ptr = (const uint8_t*)src; m_Length = src.GetLength(); @@ -416,6 +397,7 @@ inline CFX_ByteString operator+(const CFX_ByteStringC& str1, const CFX_ByteString& str2) { return CFX_ByteString(str1, str2); } + class CFX_WideStringC { public: typedef FX_WCHAR value_type; @@ -473,7 +455,6 @@ class CFX_WideStringC { const FX_WCHAR* GetPtr() const { return m_Ptr; } FX_STRSIZE GetLength() const { return m_Length; } - bool IsEmpty() const { return m_Length == 0; } FX_WCHAR GetAt(FX_STRSIZE index) const { return m_Ptr[index]; } @@ -525,6 +506,7 @@ class CFX_WideStringC { private: void* operator new(size_t) throw() { return NULL; } }; + inline bool operator==(const wchar_t* lhs, const CFX_WideStringC& rhs) { return rhs == lhs; } @@ -545,7 +527,7 @@ class CFX_WideString { CFX_WideString(const CFX_WideString& str); // Move constructor. - inline CFX_WideString(CFX_WideString&& other) { + CFX_WideString(CFX_WideString&& other) { m_pData = other.m_pData; other.m_pData = nullptr; } @@ -554,22 +536,18 @@ class CFX_WideString { : CFX_WideString(ptr, ptr ? FXSYS_wcslen(ptr) : 0) {} CFX_WideString(const FX_WCHAR* ptr, FX_STRSIZE len); - CFX_WideString(FX_WCHAR ch); CFX_WideString(const CFX_WideStringC& str); - CFX_WideString(const CFX_WideStringC& str1, const CFX_WideStringC& str2); ~CFX_WideString(); static CFX_WideString FromLocal(const CFX_ByteString& str); - static CFX_WideString FromCodePage(const CFX_ByteString& str, uint16_t codepage); static CFX_WideString FromUTF8(const char* str, FX_STRSIZE len); - static CFX_WideString FromUTF16LE(const unsigned short* str, FX_STRSIZE len); static FX_STRSIZE WStringLength(const unsigned short* str); @@ -652,19 +630,14 @@ class CFX_WideString { void ReleaseBuffer(FX_STRSIZE len = -1); int GetInteger() const; - FX_FLOAT GetFloat() const; FX_STRSIZE Find(const FX_WCHAR* lpszSub, FX_STRSIZE start = 0) const; - FX_STRSIZE Find(FX_WCHAR ch, FX_STRSIZE start = 0) const; - FX_STRSIZE Replace(const FX_WCHAR* lpszOld, const FX_WCHAR* lpszNew); - FX_STRSIZE Remove(FX_WCHAR ch); CFX_ByteString UTF8Encode() const; - CFX_ByteString UTF16LE_Encode() const; protected: -- cgit v1.2.3