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 class StringData { |
| 363 public: |
| 364 static StringData* Create(int nLen); |
| 365 void Retain() { ++m_nRefs; } |
| 366 void Release() { if (--m_nRefs <= 0) FX_Free(this); } |
369 | 367 |
370 struct CFX_StringData*» m_pData; | 368 intptr_t m_nRefs; // Would prefer ssize_t, but no windows support. |
| 369 FX_STRSIZE m_nDataLength; |
| 370 FX_STRSIZE m_nAllocLength; |
| 371 FX_CHAR m_String[1]; |
| 372 |
| 373 private: |
| 374 StringData(FX_STRSIZE dataLen, FX_STRSIZE allocLen) |
| 375 : m_nRefs(1), m_nDataLength(dataLen), m_nAllocLength(allocLen) { |
| 376 FXSYS_assert(dataLen >= 0); |
| 377 FXSYS_assert(allocLen >= 0); |
| 378 FXSYS_assert(dataLen <= allocLen); |
| 379 m_String[dataLen] = 0; |
| 380 } |
| 381 ~StringData() = delete; |
| 382 }; |
| 383 |
371 void AllocCopy(CFX_ByteString& dest,
FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex) const; | 384 void AllocCopy(CFX_ByteString& dest,
FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex) const; |
372 void AssignCopy(FX_STRSIZE nSrcLen, F
X_LPCSTR lpszSrcData); | 385 void AssignCopy(FX_STRSIZE nSrcLen, F
X_LPCSTR lpszSrcData); |
373 void ConcatCopy(FX_STRSIZE nSrc1Len,
FX_LPCSTR lpszSrc1Data, FX_STRSIZE nSrc2Len, FX_LPCSTR lpszSrc2Data); | 386 void ConcatCopy(FX_STRSIZE nSrc1Len,
FX_LPCSTR lpszSrc1Data, FX_STRSIZE nSrc2Len, FX_LPCSTR lpszSrc2Data); |
374 void ConcatInPlace(FX_STRSIZE nSrcLen
, FX_LPCSTR lpszSrcData); | 387 void ConcatInPlace(FX_STRSIZE nSrcLen
, FX_LPCSTR lpszSrcData); |
375 void CopyBeforeWrite(); | 388 void CopyBeforeWrite(); |
376 void AllocBeforeWrite(FX_STRSIZE nLen
); | 389 void AllocBeforeWrite(FX_STRSIZE nLen
); |
| 390 |
| 391 StringData* m_pData; |
377 }; | 392 }; |
378 inline CFX_ByteStringC::CFX_ByteStringC(const CFX_ByteString& src) | 393 inline CFX_ByteStringC::CFX_ByteStringC(const CFX_ByteString& src) |
379 { | 394 { |
380 m_Ptr = (FX_LPCBYTE)src; | 395 m_Ptr = (FX_LPCBYTE)src; |
381 m_Length = src.GetLength(); | 396 m_Length = src.GetLength(); |
382 } | 397 } |
383 inline CFX_ByteStringC& CFX_ByteStringC::operator = (const CFX_ByteString& src) | 398 inline CFX_ByteStringC& CFX_ByteStringC::operator = (const CFX_ByteString& src) |
384 { | 399 { |
385 m_Ptr = (FX_LPCBYTE)src; | 400 m_Ptr = (FX_LPCBYTE)src; |
386 m_Length = src.GetLength(); | 401 m_Length = src.GetLength(); |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
593 }; | 608 }; |
594 inline bool operator== (const wchar_t* lhs, const CFX_WideStringC& rhs) { | 609 inline bool operator== (const wchar_t* lhs, const CFX_WideStringC& rhs) { |
595 return rhs == lhs; | 610 return rhs == lhs; |
596 } | 611 } |
597 inline bool operator!= (const wchar_t* lhs, const CFX_WideStringC& rhs) { | 612 inline bool operator!= (const wchar_t* lhs, const CFX_WideStringC& rhs) { |
598 return rhs != lhs; | 613 return rhs != lhs; |
599 } | 614 } |
600 typedef const CFX_WideStringC& FX_WSTR; | 615 typedef const CFX_WideStringC& FX_WSTR; |
601 #define FX_WSTRC(wstr) CFX_WideStringC(wstr, FX_ArraySize(wstr) - 1) | 616 #define FX_WSTRC(wstr) CFX_WideStringC(wstr, FX_ArraySize(wstr) - 1) |
602 | 617 |
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 | 618 // A mutable string with shared buffers using copy-on-write semantics that |
611 // avoids the cost of std::string's iterator stability guarantees. | 619 // avoids the cost of std::string's iterator stability guarantees. |
612 class CFX_WideString | 620 class CFX_WideString |
613 { | 621 { |
614 public: | 622 public: |
615 typedef FX_WCHAR value_type; | 623 typedef FX_WCHAR value_type; |
616 | 624 |
617 CFX_WideString() | 625 CFX_WideString() |
618 { | 626 { |
619 m_pData = NULL; | 627 m_pData = NULL; |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
770 | 778 |
771 FX_STRSIZE Remove(FX_WCHAR ch); | 779 FX_STRSIZE Remove(FX_WCHAR ch); |
772 | 780 |
773 CFX_ByteString UTF8Encode() const; | 781 CFX_ByteString UTF8Encode() const; |
774 | 782 |
775 CFX_ByteString UTF16LE_Encode() const; | 783 CFX_ByteString UTF16LE_Encode() const; |
776 | 784 |
777 void ConvertFrom(const CFX_ByteString
& str, CFX_CharMap* pCharMap = NULL); | 785 void ConvertFrom(const CFX_ByteString
& str, CFX_CharMap* pCharMap = NULL); |
778 | 786 |
779 protected: | 787 protected: |
780 void» » » » » CopyBeforeWrite(); | 788 class StringData { |
781 void» » » » » AllocBeforeWrite(FX_STRSIZE nLen
); | 789 public: |
782 void» » » » » ConcatInPlace(FX_STRSIZE nSrcLen
, FX_LPCWSTR lpszSrcData); | 790 static StringData* Create(int nLen); |
783 void» » » » » ConcatCopy(FX_STRSIZE nSrc1Len,
FX_LPCWSTR lpszSrc1Data, FX_STRSIZE nSrc2Len, FX_LPCWSTR lpszSrc2Data); | 791 void Retain() { ++m_nRefs; } |
784 void» » » » » AssignCopy(FX_STRSIZE nSrcLen, F
X_LPCWSTR lpszSrcData); | 792 void Release() { if (--m_nRefs <= 0) FX_Free(this); } |
785 void» » » » » AllocCopy(CFX_WideString& dest,
FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex) const; | |
786 | 793 |
787 CFX_StringDataW*» » m_pData; | 794 intptr_t m_nRefs; // Would prefer ssize_t, but no windows support. |
| 795 FX_STRSIZE m_nDataLength; |
| 796 FX_STRSIZE m_nAllocLength; |
| 797 FX_WCHAR m_String[1]; |
| 798 |
| 799 private: |
| 800 StringData(FX_STRSIZE dataLen, FX_STRSIZE allocLen) |
| 801 : m_nRefs(1), m_nDataLength(dataLen), m_nAllocLength(allocLen) { |
| 802 FXSYS_assert(dataLen >= 0); |
| 803 FXSYS_assert(allocLen >= 0); |
| 804 FXSYS_assert(dataLen <= allocLen); |
| 805 m_String[dataLen] = 0; |
| 806 } |
| 807 ~StringData() = delete; |
| 808 }; |
| 809 |
| 810 void CopyBeforeWrite(); |
| 811 void AllocBeforeWrite(FX_STRSIZE nLen); |
| 812 void ConcatInPlace(FX_STRSIZE nSrcLen, FX_LPCWSTR lpszSrc
Data); |
| 813 void ConcatCopy(FX_STRSIZE nSrc1Len, FX_LPCWSTR lpszSrc1D
ata, FX_STRSIZE nSrc2Len, FX_LPCWSTR lpszSrc2Data); |
| 814 void AssignCopy(FX_STRSIZE nSrcLen, FX_LPCWSTR lpszSrcDat
a); |
| 815 void AllocCopy(CFX_WideString& dest, FX_STRSIZE nCopyLen,
FX_STRSIZE nCopyIndex) const; |
| 816 |
| 817 StringData* m_pData; |
788 }; | 818 }; |
789 inline CFX_WideStringC::CFX_WideStringC(const CFX_WideString& src) | 819 inline CFX_WideStringC::CFX_WideStringC(const CFX_WideString& src) |
790 { | 820 { |
791 m_Ptr = src.c_str(); | 821 m_Ptr = src.c_str(); |
792 m_Length = src.GetLength(); | 822 m_Length = src.GetLength(); |
793 } | 823 } |
794 inline CFX_WideStringC& CFX_WideStringC::operator = (const CFX_WideString& src) | 824 inline CFX_WideStringC& CFX_WideStringC::operator = (const CFX_WideString& src) |
795 { | 825 { |
796 m_Ptr = src.c_str(); | 826 m_Ptr = src.c_str(); |
797 m_Length = src.GetLength(); | 827 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); | 894 CFX_ByteString FX_UTF8Encode(FX_LPCWSTR pwsStr, FX_STRSIZE len); |
865 inline CFX_ByteString FX_UTF8Encode(FX_WSTR wsStr) | 895 inline CFX_ByteString FX_UTF8Encode(FX_WSTR wsStr) |
866 { | 896 { |
867 return FX_UTF8Encode(wsStr.GetPtr(), wsStr.GetLength()); | 897 return FX_UTF8Encode(wsStr.GetPtr(), wsStr.GetLength()); |
868 } | 898 } |
869 inline CFX_ByteString FX_UTF8Encode(const CFX_WideString &wsStr) | 899 inline CFX_ByteString FX_UTF8Encode(const CFX_WideString &wsStr) |
870 { | 900 { |
871 return FX_UTF8Encode(wsStr.c_str(), wsStr.GetLength()); | 901 return FX_UTF8Encode(wsStr.c_str(), wsStr.GetLength()); |
872 } | 902 } |
873 #endif | 903 #endif |
OLD | NEW |