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 _FX_STRING_H_ | 7 #ifndef _FX_STRING_H_ |
8 #define _FX_STRING_H_ | 8 #define _FX_STRING_H_ |
9 | 9 |
10 #include <stdint.h> // For intptr_t. | 10 #include <stdint.h> // For intptr_t. |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
164 inline bool operator== (const char* lhs, const CFX_ByteStringC& rhs) { | 164 inline bool operator== (const char* lhs, const CFX_ByteStringC& rhs) { |
165 return rhs == lhs; | 165 return rhs == lhs; |
166 } | 166 } |
167 inline bool operator!= (const char* lhs, const CFX_ByteStringC& rhs) { | 167 inline bool operator!= (const char* lhs, const CFX_ByteStringC& rhs) { |
168 return rhs != lhs; | 168 return rhs != lhs; |
169 } | 169 } |
170 typedef const CFX_ByteStringC& FX_BSTR; | 170 typedef const CFX_ByteStringC& FX_BSTR; |
171 #define FX_BSTRC(str) CFX_ByteStringC(str, sizeof str-1) | 171 #define FX_BSTRC(str) CFX_ByteStringC(str, sizeof str-1) |
172 #define FXBSTR_ID(c1, c2, c3, c4) ((c1 << 24) | (c2 << 16) | (c3 << 8) | (c4)) | 172 #define FXBSTR_ID(c1, c2, c3, c4) ((c1 << 24) | (c2 << 16) | (c3 << 8) | (c4)) |
173 | 173 |
174 // To ensure ref counts do not overflow, consider the worst possible case: | |
175 // the entire address space contains nothing but pointers to this object. | |
176 // Since the count increments with each new pointer, the largest value is | |
177 // the number of pointers that can fit into the address space. The size of | |
178 // the address space itself is a good upper bound on it; we need not go | |
179 // larger. | |
180 struct CFX_StringData { | |
181 intptr_t m_nRefs; // Would prefer ssize_t, but no windows support. | |
182 FX_STRSIZE m_nDataLength; | |
183 FX_STRSIZE m_nAllocLength; | |
184 FX_CHAR m_String[1]; | |
185 }; | |
186 | |
187 // A mutable string with shared buffers using copy-on-write semantics that | 174 // A mutable string with shared buffers using copy-on-write semantics that |
188 // avoids the cost of std::string's iterator stability guarantees. | 175 // avoids the cost of std::string's iterator stability guarantees. |
189 class CFX_ByteString | 176 class CFX_ByteString |
190 { | 177 { |
191 public: | 178 public: |
192 typedef FX_CHAR value_type; | 179 typedef FX_CHAR value_type; |
193 | 180 |
194 CFX_ByteString() | 181 CFX_ByteString() |
195 { | 182 { |
196 m_pData = NULL; | 183 m_pData = NULL; |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
356 | 343 |
357 void ConvertFrom(const CFX_WideString & str, CFX_CharMap* pCharMap = NULL); | 344 void ConvertFrom(const CFX_WideString & str, CFX_CharMap* pCharMap = NULL); |
358 | 345 |
359 FX_DWORD GetID(FX_STRSIZE start_pos = 0) const; | 346 FX_DWORD GetID(FX_STRSIZE start_pos = 0) const; |
360 | 347 |
361 #define FXFORMAT_SIGNED 1 | 348 #define FXFORMAT_SIGNED 1 |
362 #define FXFORMAT_HEX 2 | 349 #define FXFORMAT_HEX 2 |
363 #define FXFORMAT_CAPITAL 4 | 350 #define FXFORMAT_CAPITAL 4 |
364 | 351 |
365 static CFX_ByteString FormatInteger(int i, FX_DWORD flags = 0); | 352 static CFX_ByteString FormatInteger(int i, FX_DWORD flags = 0); |
353 static CFX_ByteString FormatFloat(FX_FLOAT f, int precision = 0); | |
366 | 354 |
367 static CFX_ByteString FormatFloat(FX_FLOAT f, int precision = 0); | |
368 protected: | 355 protected: |
356 // To ensure ref counts do not overflow, consider the worst possible case: | |
357 // the entire address space contains nothing but pointers to this object. | |
358 // Since the count increments with each new pointer, the largest value is | |
359 // the number of pointers that can fit into the address space. The size of | |
360 // the address space itself is a good upper bound on it; we need not go | |
361 // larger. | |
362 struct StringData { | |
363 static StringData* Create(int nLen); | |
364 void Retain() { ++m_nRefs; } | |
Lei Zhang
2015/05/14 19:28:39
Once a struct starts having methods, it should pro
Tom Sepez
2015/05/14 19:54:43
Done.
| |
365 void Release() { if (--m_nRefs <= 0) FX_Free(this); } | |
369 | 366 |
370 struct CFX_StringData*» m_pData; | 367 intptr_t» m_nRefs; // Would prefer ssize_t, but no windows suppor t. |
Lei Zhang
2015/05/14 19:28:39
kill tabs
Tom Sepez
2015/05/14 19:54:43
Done.
| |
368 FX_STRSIZE» m_nDataLength; | |
369 FX_STRSIZE» m_nAllocLength; | |
370 FX_CHAR»» m_String[1]; | |
371 | |
372 private: | |
373 StringData(FX_STRSIZE dataLen, FX_STRSIZE allocLen) | |
374 : m_nRefs(1), m_nDataLength(dataLen), m_nAllocLength(allocLe n) { | |
Lei Zhang
2015/05/14 19:28:39
nit: too many spaces here?
Lei Zhang
2015/05/14 20:50:14
Shouldn't it be 4 spaces from the start of "String
Tom Sepez
2015/05/14 21:25:06
Should be a double-indent from the start of "Strin
| |
375 m_String[dataLen] = 0; | |
Lei Zhang
2015/05/14 19:28:39
I hate the fact that FX_STRSIZE is signed. Should
Tom Sepez
2015/05/14 19:54:43
Yes.
| |
376 } | |
377 }; | |
378 | |
371 void AllocCopy(CFX_ByteString& dest, FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex) const; | 379 void AllocCopy(CFX_ByteString& dest, FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex) const; |
372 void AssignCopy(FX_STRSIZE nSrcLen, F X_LPCSTR lpszSrcData); | 380 void AssignCopy(FX_STRSIZE nSrcLen, F X_LPCSTR lpszSrcData); |
373 void ConcatCopy(FX_STRSIZE nSrc1Len, FX_LPCSTR lpszSrc1Data, FX_STRSIZE nSrc2Len, FX_LPCSTR lpszSrc2Data); | 381 void ConcatCopy(FX_STRSIZE nSrc1Len, FX_LPCSTR lpszSrc1Data, FX_STRSIZE nSrc2Len, FX_LPCSTR lpszSrc2Data); |
374 void ConcatInPlace(FX_STRSIZE nSrcLen , FX_LPCSTR lpszSrcData); | 382 void ConcatInPlace(FX_STRSIZE nSrcLen , FX_LPCSTR lpszSrcData); |
375 void CopyBeforeWrite(); | 383 void CopyBeforeWrite(); |
376 void AllocBeforeWrite(FX_STRSIZE nLen ); | 384 void AllocBeforeWrite(FX_STRSIZE nLen ); |
385 | |
386 StringData* m_pData; | |
377 }; | 387 }; |
378 inline CFX_ByteStringC::CFX_ByteStringC(const CFX_ByteString& src) | 388 inline CFX_ByteStringC::CFX_ByteStringC(const CFX_ByteString& src) |
379 { | 389 { |
380 m_Ptr = (FX_LPCBYTE)src; | 390 m_Ptr = (FX_LPCBYTE)src; |
381 m_Length = src.GetLength(); | 391 m_Length = src.GetLength(); |
382 } | 392 } |
383 inline CFX_ByteStringC& CFX_ByteStringC::operator = (const CFX_ByteString& src) | 393 inline CFX_ByteStringC& CFX_ByteStringC::operator = (const CFX_ByteString& src) |
384 { | 394 { |
385 m_Ptr = (FX_LPCBYTE)src; | 395 m_Ptr = (FX_LPCBYTE)src; |
386 m_Length = src.GetLength(); | 396 m_Length = src.GetLength(); |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
593 }; | 603 }; |
594 inline bool operator== (const wchar_t* lhs, const CFX_WideStringC& rhs) { | 604 inline bool operator== (const wchar_t* lhs, const CFX_WideStringC& rhs) { |
595 return rhs == lhs; | 605 return rhs == lhs; |
596 } | 606 } |
597 inline bool operator!= (const wchar_t* lhs, const CFX_WideStringC& rhs) { | 607 inline bool operator!= (const wchar_t* lhs, const CFX_WideStringC& rhs) { |
598 return rhs != lhs; | 608 return rhs != lhs; |
599 } | 609 } |
600 typedef const CFX_WideStringC& FX_WSTR; | 610 typedef const CFX_WideStringC& FX_WSTR; |
601 #define FX_WSTRC(wstr) CFX_WideStringC(wstr, FX_ArraySize(wstr) - 1) | 611 #define FX_WSTRC(wstr) CFX_WideStringC(wstr, FX_ArraySize(wstr) - 1) |
602 | 612 |
603 struct CFX_StringDataW { | |
604 intptr_t m_nRefs; // Would prefer ssize_t, but no windows support. | |
605 FX_STRSIZE m_nDataLength; | |
606 FX_STRSIZE m_nAllocLength; | |
607 FX_WCHAR m_String[1]; | |
608 }; | |
609 | |
610 // A mutable string with shared buffers using copy-on-write semantics that | 613 // A mutable string with shared buffers using copy-on-write semantics that |
611 // avoids the cost of std::string's iterator stability guarantees. | 614 // avoids the cost of std::string's iterator stability guarantees. |
612 class CFX_WideString | 615 class CFX_WideString |
613 { | 616 { |
614 public: | 617 public: |
615 typedef FX_WCHAR value_type; | 618 typedef FX_WCHAR value_type; |
616 | 619 |
617 CFX_WideString() | 620 CFX_WideString() |
618 { | 621 { |
619 m_pData = NULL; | 622 m_pData = NULL; |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
770 | 773 |
771 FX_STRSIZE Remove(FX_WCHAR ch); | 774 FX_STRSIZE Remove(FX_WCHAR ch); |
772 | 775 |
773 CFX_ByteString UTF8Encode() const; | 776 CFX_ByteString UTF8Encode() const; |
774 | 777 |
775 CFX_ByteString UTF16LE_Encode() const; | 778 CFX_ByteString UTF16LE_Encode() const; |
776 | 779 |
777 void ConvertFrom(const CFX_ByteString & str, CFX_CharMap* pCharMap = NULL); | 780 void ConvertFrom(const CFX_ByteString & str, CFX_CharMap* pCharMap = NULL); |
778 | 781 |
779 protected: | 782 protected: |
783 class StringData { | |
Lei Zhang
2015/05/14 19:28:39
Hey, this one is a class! :-P
Tom Sepez
2015/05/14 19:54:43
So much for consistency.
| |
784 public: | |
785 static StringData* Create(int nLen); | |
786 void Retain() { ++m_nRefs; } | |
787 void Release() { if (--m_nRefs <= 0) FX_Free(this); } | |
788 | |
789 intptr_t m_nRefs; // Would prefer ssize_t, but no windows suppor t. | |
790 FX_STRSIZE m_nDataLength; | |
791 FX_STRSIZE m_nAllocLength; | |
792 FX_WCHAR m_String[1]; | |
793 | |
794 private: | |
795 StringData(FX_STRSIZE dataLen, FX_STRSIZE allocLen) | |
796 : m_nRefs(1), m_nDataLength(dataLen), m_nAllocLength(allocLe n) { | |
797 m_String[dataLen] = 0; | |
798 } | |
799 }; | |
800 | |
Tom Sepez
2015/05/14 19:54:43
Dawns on me that this should have a ~StringData()
| |
780 void CopyBeforeWrite(); | 801 void CopyBeforeWrite(); |
781 void AllocBeforeWrite(FX_STRSIZE nLen ); | 802 void AllocBeforeWrite(FX_STRSIZE nLen ); |
782 void ConcatInPlace(FX_STRSIZE nSrcLen , FX_LPCWSTR lpszSrcData); | 803 void ConcatInPlace(FX_STRSIZE nSrcLen , FX_LPCWSTR lpszSrcData); |
783 void ConcatCopy(FX_STRSIZE nSrc1Len, FX_LPCWSTR lpszSrc1Data, FX_STRSIZE nSrc2Len, FX_LPCWSTR lpszSrc2Data); | 804 void ConcatCopy(FX_STRSIZE nSrc1Len, FX_LPCWSTR lpszSrc1Data, FX_STRSIZE nSrc2Len, FX_LPCWSTR lpszSrc2Data); |
784 void AssignCopy(FX_STRSIZE nSrcLen, F X_LPCWSTR lpszSrcData); | 805 void AssignCopy(FX_STRSIZE nSrcLen, F X_LPCWSTR lpszSrcData); |
785 void AllocCopy(CFX_WideString& dest, FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex) const; | 806 void AllocCopy(CFX_WideString& dest, FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex) const; |
786 | 807 |
787 CFX_StringDataW*» » m_pData; | 808 StringData*»m_pData; |
788 }; | 809 }; |
789 inline CFX_WideStringC::CFX_WideStringC(const CFX_WideString& src) | 810 inline CFX_WideStringC::CFX_WideStringC(const CFX_WideString& src) |
790 { | 811 { |
791 m_Ptr = src.c_str(); | 812 m_Ptr = src.c_str(); |
792 m_Length = src.GetLength(); | 813 m_Length = src.GetLength(); |
793 } | 814 } |
794 inline CFX_WideStringC& CFX_WideStringC::operator = (const CFX_WideString& src) | 815 inline CFX_WideStringC& CFX_WideStringC::operator = (const CFX_WideString& src) |
795 { | 816 { |
796 m_Ptr = src.c_str(); | 817 m_Ptr = src.c_str(); |
797 m_Length = src.GetLength(); | 818 m_Length = src.GetLength(); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
864 CFX_ByteString FX_UTF8Encode(FX_LPCWSTR pwsStr, FX_STRSIZE len); | 885 CFX_ByteString FX_UTF8Encode(FX_LPCWSTR pwsStr, FX_STRSIZE len); |
865 inline CFX_ByteString FX_UTF8Encode(FX_WSTR wsStr) | 886 inline CFX_ByteString FX_UTF8Encode(FX_WSTR wsStr) |
866 { | 887 { |
867 return FX_UTF8Encode(wsStr.GetPtr(), wsStr.GetLength()); | 888 return FX_UTF8Encode(wsStr.GetPtr(), wsStr.GetLength()); |
868 } | 889 } |
869 inline CFX_ByteString FX_UTF8Encode(const CFX_WideString &wsStr) | 890 inline CFX_ByteString FX_UTF8Encode(const CFX_WideString &wsStr) |
870 { | 891 { |
871 return FX_UTF8Encode(wsStr.c_str(), wsStr.GetLength()); | 892 return FX_UTF8Encode(wsStr.c_str(), wsStr.GetLength()); |
872 } | 893 } |
873 #endif | 894 #endif |
OLD | NEW |