1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
11 class CFX_WideStringC;
15 typedef int FX_STRSIZE;
16 class CFX_ByteStringL;
17 class CFX_WideStringL;
19 // An immutable string with caller-provided storage which must outlive the
21 class CFX_ByteStringC : public CFX_Object
24 typedef FX_CHAR value_type;
32 CFX_ByteStringC(FX_LPCBYTE ptr, FX_STRSIZE size)
38 CFX_ByteStringC(FX_LPCSTR ptr)
40 m_Ptr = (FX_LPCBYTE)ptr;
41 m_Length = ptr ? (FX_STRSIZE)FXSYS_strlen(ptr) : 0;
44 // |ch| must be an lvalue that outlives the the CFX_ByteStringC. However,
45 // the use of char rvalues are not caught at compile time. They are
46 // implicitly promoted to CFX_ByteString (see below) and then the
47 // CFX_ByteStringC is constructed from the CFX_ByteString via the alternate
48 // constructor below. The CFX_ByteString then typically goes out of scope
49 // and |m_Ptr| may be left pointing to invalid memory. Beware.
50 // TODO(tsepez): Mark single-argument string constructors as explicit.
51 CFX_ByteStringC(FX_CHAR& ch)
53 m_Ptr = (FX_LPCBYTE)&ch;
57 CFX_ByteStringC(FX_LPCSTR ptr, FX_STRSIZE len)
59 m_Ptr = (FX_LPCBYTE)ptr;
61 m_Length = (FX_STRSIZE)FXSYS_strlen(ptr);
67 CFX_ByteStringC(const CFX_ByteStringC& src)
70 m_Length = src.m_Length;
73 CFX_ByteStringC(const CFX_ByteString& src);
75 CFX_ByteStringC& operator = (FX_LPCSTR src)
77 m_Ptr = (FX_LPCBYTE)src;
78 m_Length = m_Ptr ? (FX_STRSIZE)FXSYS_strlen(src) : 0;
82 CFX_ByteStringC& operator = (const CFX_ByteStringC& src)
85 m_Length = src.m_Length;
89 CFX_ByteStringC& operator = (const CFX_ByteString& src);
91 bool operator == (const CFX_ByteStringC& str) const
93 return str.m_Length == m_Length && FXSYS_memcmp32(str.m_Ptr, m_Ptr, m_Length) == 0;
96 bool operator != (const CFX_ByteStringC& str) const
98 return str.m_Length != m_Length || FXSYS_memcmp32(str.m_Ptr, m_Ptr, m_Length) != 0;
100 #define FXBSTR_ID(c1, c2, c3, c4) ((c1 << 24) | (c2 << 16) | (c3 << 8) | (c4))
102 FX_DWORD GetID(FX_STRSIZE start_pos = 0) const;
104 FX_LPCBYTE GetPtr() const
109 FX_LPCSTR GetCStr() const
111 return (FX_LPCSTR)m_Ptr;
114 FX_STRSIZE GetLength() const
121 return m_Length == 0;
124 operator FX_LPCBYTE() const
129 FX_BYTE GetAt(FX_STRSIZE index) const
134 CFX_ByteStringC Mid(FX_STRSIZE index, FX_STRSIZE count = -1) const
139 if (index > m_Length) {
140 return CFX_ByteStringC();
142 if (count < 0 || count > m_Length - index) {
143 count = m_Length - index;
145 return CFX_ByteStringC(m_Ptr + index, count);
154 void* operator new (size_t) throw()
159 typedef const CFX_ByteStringC& FX_BSTR;
160 #define FX_BSTRC(str) CFX_ByteStringC(str, sizeof str-1)
161 struct CFX_StringData {
165 FX_STRSIZE m_nDataLength;
167 FX_STRSIZE m_nAllocLength;
171 class CFX_ByteString : public CFX_Object
174 typedef FX_CHAR value_type;
181 CFX_ByteString(const CFX_ByteString& str);
183 CFX_ByteString(char ch);
185 CFX_ByteString(FX_LPCSTR ptr, FX_STRSIZE len = -1);
187 CFX_ByteString(FX_LPCBYTE ptr, FX_STRSIZE len);
189 CFX_ByteString(FX_BSTR bstrc);
191 CFX_ByteString(FX_BSTR bstrc1, FX_BSTR bstrc2);
195 static CFX_ByteString FromUnicode(FX_LPCWSTR ptr, FX_STRSIZE len = -1);
197 static CFX_ByteString FromUnicode(const CFX_WideString& str);
199 // Explicit conversion to raw string
200 FX_LPCSTR c_str() const
202 return m_pData ? m_pData->m_String : "";
205 // Implicit conversion to C-style string -- deprecated
206 operator FX_LPCSTR() const
208 return m_pData ? m_pData->m_String : "";
211 operator FX_LPCBYTE() const
213 return m_pData ? (FX_LPCBYTE)m_pData->m_String : NULL;
216 FX_STRSIZE GetLength() const
218 return m_pData ? m_pData->m_nDataLength : 0;
226 int Compare(FX_BSTR str) const;
229 bool Equal(FX_BSTR str) const;
232 bool EqualNoCase(FX_BSTR str) const;
234 bool operator == (FX_LPCSTR str) const
239 bool operator == (FX_BSTR str) const
244 bool operator == (const CFX_ByteString& str) const;
246 bool operator != (FX_LPCSTR str) const
251 bool operator != (FX_BSTR str) const
256 bool operator != (const CFX_ByteString& str) const
258 return !operator==(str);
263 const CFX_ByteString& operator = (FX_LPCSTR str);
265 const CFX_ByteString& operator = (FX_BSTR bstrc);
267 const CFX_ByteString& operator = (const CFX_ByteString& stringSrc);
269 const CFX_ByteString& operator = (const CFX_BinaryBuf& buf);
271 void Load(FX_LPCBYTE str, FX_STRSIZE len);
273 const CFX_ByteString& operator += (FX_CHAR ch);
275 const CFX_ByteString& operator += (FX_LPCSTR str);
277 const CFX_ByteString& operator += (const CFX_ByteString& str);
279 const CFX_ByteString& operator += (FX_BSTR bstrc);
281 FX_BYTE GetAt(FX_STRSIZE nIndex) const
283 return m_pData ? m_pData->m_String[nIndex] : 0;
286 FX_BYTE operator[](FX_STRSIZE nIndex) const
288 return m_pData ? m_pData->m_String[nIndex] : 0;
291 void SetAt(FX_STRSIZE nIndex, FX_CHAR ch);
293 FX_STRSIZE Insert(FX_STRSIZE index, FX_CHAR ch);
295 FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1);
298 void Format(FX_LPCSTR lpszFormat, ... );
300 void FormatV(FX_LPCSTR lpszFormat, va_list argList);
303 void Reserve(FX_STRSIZE len);
305 FX_LPSTR GetBuffer(FX_STRSIZE len);
307 FX_LPSTR LockBuffer();
309 void ReleaseBuffer(FX_STRSIZE len = -1);
311 CFX_ByteString Mid(FX_STRSIZE first) const;
313 CFX_ByteString Mid(FX_STRSIZE first, FX_STRSIZE count) const;
315 CFX_ByteString Left(FX_STRSIZE count) const;
317 CFX_ByteString Right(FX_STRSIZE count) const;
319 FX_STRSIZE Find(FX_BSTR lpszSub, FX_STRSIZE start = 0) const;
321 FX_STRSIZE Find(FX_CHAR ch, FX_STRSIZE start = 0) const;
323 FX_STRSIZE ReverseFind(FX_CHAR ch) const;
331 void TrimRight(FX_CHAR chTarget);
333 void TrimRight(FX_BSTR lpszTargets);
337 void TrimLeft(FX_CHAR chTarget);
339 void TrimLeft(FX_BSTR lpszTargets);
341 FX_STRSIZE Replace(FX_BSTR lpszOld, FX_BSTR lpszNew);
343 FX_STRSIZE Remove(FX_CHAR ch);
345 CFX_WideString UTF8Decode() const;
347 void ConvertFrom(const CFX_WideString& str, CFX_CharMap* pCharMap = NULL);
349 FX_DWORD GetID(FX_STRSIZE start_pos = 0) const;
351 #define FXFORMAT_SIGNED 1
352 #define FXFORMAT_HEX 2
353 #define FXFORMAT_CAPITAL 4
355 static CFX_ByteString FormatInteger(int i, FX_DWORD flags = 0);
357 static CFX_ByteString FormatFloat(FX_FLOAT f, int precision = 0);
360 struct CFX_StringData* m_pData;
361 void AllocCopy(CFX_ByteString& dest, FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex) const;
362 void AssignCopy(FX_STRSIZE nSrcLen, FX_LPCSTR lpszSrcData);
363 void ConcatCopy(FX_STRSIZE nSrc1Len, FX_LPCSTR lpszSrc1Data, FX_STRSIZE nSrc2Len, FX_LPCSTR lpszSrc2Data);
364 void ConcatInPlace(FX_STRSIZE nSrcLen, FX_LPCSTR lpszSrcData);
365 void CopyBeforeWrite();
366 void AllocBeforeWrite(FX_STRSIZE nLen);
368 inline CFX_ByteStringC::CFX_ByteStringC(const CFX_ByteString& src)
370 m_Ptr = (FX_LPCBYTE)src;
371 m_Length = src.GetLength();
373 inline CFX_ByteStringC& CFX_ByteStringC::operator = (const CFX_ByteString& src)
375 m_Ptr = (FX_LPCBYTE)src;
376 m_Length = src.GetLength();
380 inline CFX_ByteString operator + (FX_BSTR str1, FX_BSTR str2)
382 return CFX_ByteString(str1, str2);
384 inline CFX_ByteString operator + (FX_BSTR str1, FX_LPCSTR str2)
386 return CFX_ByteString(str1, str2);
388 inline CFX_ByteString operator + (FX_LPCSTR str1, FX_BSTR str2)
390 return CFX_ByteString(str1, str2);
392 inline CFX_ByteString operator + (FX_BSTR str1, FX_CHAR ch)
394 return CFX_ByteString(str1, CFX_ByteStringC(ch));
396 inline CFX_ByteString operator + (FX_CHAR ch, FX_BSTR str2)
398 return CFX_ByteString(ch, str2);
400 inline CFX_ByteString operator + (const CFX_ByteString& str1, const CFX_ByteString& str2)
402 return CFX_ByteString(str1, str2);
404 inline CFX_ByteString operator + (const CFX_ByteString& str1, FX_CHAR ch)
406 return CFX_ByteString(str1, CFX_ByteStringC(ch));
408 inline CFX_ByteString operator + (FX_CHAR ch, const CFX_ByteString& str2)
410 return CFX_ByteString(ch, str2);
412 inline CFX_ByteString operator + (const CFX_ByteString& str1, FX_LPCSTR str2)
414 return CFX_ByteString(str1, str2);
416 inline CFX_ByteString operator + (FX_LPCSTR str1, const CFX_ByteString& str2)
418 return CFX_ByteString(str1, str2);
420 inline CFX_ByteString operator + (const CFX_ByteString& str1, FX_BSTR str2)
422 return CFX_ByteString(str1, str2);
424 inline CFX_ByteString operator + (FX_BSTR str1, const CFX_ByteString& str2)
426 return CFX_ByteString(str1, str2);
428 class CFX_StringBufBase : public CFX_Object
432 CFX_StringBufBase(FX_STRSIZE limit)
438 FX_CHAR* GetPtr() const
440 return (FX_CHAR*)(this + 1);
443 FX_STRSIZE GetSize() const
453 void Copy(FX_BSTR str);
455 void Append(FX_BSTR str);
457 void Append(int i, FX_DWORD flags = 0);
459 CFX_ByteStringC GetStringC() const
461 return CFX_ByteStringC((FX_CHAR*)(this + 1), m_Size);
464 CFX_ByteString GetString() const
466 return CFX_ByteString((FX_CHAR*)(this + 1), m_Size);
474 template<FX_STRSIZE limit>
475 class CFX_StringBufTemplate : public CFX_StringBufBase
479 CFX_StringBufTemplate() : CFX_StringBufBase(limit) {}
481 FX_CHAR m_Buffer[limit];
483 typedef CFX_StringBufTemplate<256> CFX_StringBuf256;
484 class CFX_WideStringC : public CFX_Object
487 typedef FX_WCHAR value_type;
495 CFX_WideStringC(FX_LPCWSTR ptr)
498 m_Length = ptr ? (FX_STRSIZE)FXSYS_wcslen(ptr) : 0;
501 CFX_WideStringC(FX_WCHAR& ch)
507 CFX_WideStringC(FX_LPCWSTR ptr, FX_STRSIZE len)
511 m_Length = (FX_STRSIZE)FXSYS_wcslen(ptr);
517 CFX_WideStringC(const CFX_WideStringC& src)
520 m_Length = src.m_Length;
523 CFX_WideStringC(const CFX_WideString& src);
525 CFX_WideStringC& operator = (FX_LPCWSTR src)
528 m_Length = (FX_STRSIZE)FXSYS_wcslen(src);
532 CFX_WideStringC& operator = (const CFX_WideStringC& src)
535 m_Length = src.m_Length;
539 CFX_WideStringC& operator = (const CFX_WideString& src);
541 bool operator == (const CFX_WideStringC& str) const
543 return str.m_Length == m_Length && FXSYS_memcmp32(str.m_Ptr, m_Ptr, m_Length * sizeof(FX_WCHAR)) == 0;
546 bool operator != (const CFX_WideStringC& str) const
548 return str.m_Length != m_Length || FXSYS_memcmp32(str.m_Ptr, m_Ptr, m_Length * sizeof(FX_WCHAR)) != 0;
551 FX_LPCWSTR GetPtr() const
556 FX_STRSIZE GetLength() const
563 return m_Length == 0;
566 FX_WCHAR GetAt(FX_STRSIZE index) const
571 CFX_WideStringC Left(FX_STRSIZE count) const
574 return CFX_WideStringC();
576 if (count > m_Length) {
579 return CFX_WideStringC(m_Ptr, count);
582 CFX_WideStringC Mid(FX_STRSIZE index, FX_STRSIZE count = -1) const
587 if (index > m_Length) {
588 return CFX_WideStringC();
590 if (count < 0 || count > m_Length - index) {
591 count = m_Length - index;
593 return CFX_WideStringC(m_Ptr + index, count);
596 CFX_WideStringC Right(FX_STRSIZE count) const
599 return CFX_WideStringC();
601 if (count > m_Length) {
604 return CFX_WideStringC(m_Ptr + m_Length - count, count);
613 void* operator new (size_t) throw()
618 typedef const CFX_WideStringC& FX_WSTR;
619 #define FX_WSTRC(wstr) CFX_WideStringC(wstr, FX_ArraySize(wstr) - 1)
620 struct CFX_StringDataW {
624 FX_STRSIZE m_nDataLength;
626 FX_STRSIZE m_nAllocLength;
628 FX_WCHAR m_String[1];
630 class CFX_WideString : public CFX_Object
633 typedef FX_WCHAR value_type;
640 CFX_WideString(const CFX_WideString& str);
642 CFX_WideString(FX_LPCWSTR ptr, FX_STRSIZE len = -1)
647 CFX_WideString(FX_WCHAR ch);
649 CFX_WideString(const CFX_WideStringC& str);
651 CFX_WideString(const CFX_WideStringC& str1, const CFX_WideStringC& str2);
655 static CFX_WideString FromLocal(const char* str, FX_STRSIZE len = -1);
657 static CFX_WideString FromUTF8(const char* str, FX_STRSIZE len);
659 static CFX_WideString FromUTF16LE(const unsigned short* str, FX_STRSIZE len);
661 static FX_STRSIZE WStringLength(const unsigned short* str);
663 // Explicit conversion to raw string
664 FX_LPCWSTR c_str() const
666 return m_pData ? m_pData->m_String : L"";
669 // Implicit conversion to C-style wide string -- deprecated
670 operator FX_LPCWSTR() const
672 return m_pData ? m_pData->m_String : L"";
678 FX_BOOL IsEmpty() const
683 FX_STRSIZE GetLength() const
685 return m_pData ? m_pData->m_nDataLength : 0;
688 const CFX_WideString& operator = (FX_LPCWSTR str);
690 const CFX_WideString& operator =(const CFX_WideString& stringSrc);
692 const CFX_WideString& operator =(const CFX_WideStringC& stringSrc);
694 const CFX_WideString& operator += (FX_LPCWSTR str);
696 const CFX_WideString& operator += (FX_WCHAR ch);
698 const CFX_WideString& operator += (const CFX_WideString& str);
700 const CFX_WideString& operator += (const CFX_WideStringC& str);
702 FX_WCHAR GetAt(FX_STRSIZE nIndex) const
704 return m_pData ? m_pData->m_String[nIndex] : 0;
707 FX_WCHAR operator[](FX_STRSIZE nIndex) const
709 return m_pData ? m_pData->m_String[nIndex] : 0;
712 void SetAt(FX_STRSIZE nIndex, FX_WCHAR ch);
714 int Compare(FX_LPCWSTR str) const;
716 int Compare(const CFX_WideString& str) const;
718 int CompareNoCase(FX_LPCWSTR str) const;
720 bool Equal(const CFX_WideStringC& str) const;
722 CFX_WideString Mid(FX_STRSIZE first) const;
724 CFX_WideString Mid(FX_STRSIZE first, FX_STRSIZE count) const;
726 CFX_WideString Left(FX_STRSIZE count) const;
728 CFX_WideString Right(FX_STRSIZE count) const;
730 FX_STRSIZE Insert(FX_STRSIZE index, FX_WCHAR ch);
732 FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1);
734 void Format(FX_LPCWSTR lpszFormat, ... );
736 void FormatV(FX_LPCWSTR lpszFormat, va_list argList);
744 void TrimRight(FX_WCHAR chTarget);
746 void TrimRight(FX_LPCWSTR lpszTargets);
750 void TrimLeft(FX_WCHAR chTarget);
752 void TrimLeft(FX_LPCWSTR lpszTargets);
754 void Reserve(FX_STRSIZE len);
756 FX_LPWSTR GetBuffer(FX_STRSIZE len);
758 FX_LPWSTR LockBuffer();
760 void ReleaseBuffer(FX_STRSIZE len = -1);
762 int GetInteger() const;
764 FX_FLOAT GetFloat() const;
766 FX_STRSIZE Find(FX_LPCWSTR lpszSub, FX_STRSIZE start = 0) const;
768 FX_STRSIZE Find(FX_WCHAR ch, FX_STRSIZE start = 0) const;
770 FX_STRSIZE Replace(FX_LPCWSTR lpszOld, FX_LPCWSTR lpszNew);
772 FX_STRSIZE Remove(FX_WCHAR ch);
774 CFX_ByteString UTF8Encode() const;
776 CFX_ByteString UTF16LE_Encode() const;
778 void ConvertFrom(const CFX_ByteString& str, CFX_CharMap* pCharMap = NULL);
780 void InitStr(FX_LPCWSTR ptr, int len);
782 CFX_StringDataW* m_pData;
783 void CopyBeforeWrite();
784 void AllocBeforeWrite(FX_STRSIZE nLen);
785 void ConcatInPlace(FX_STRSIZE nSrcLen, FX_LPCWSTR lpszSrcData);
786 void ConcatCopy(FX_STRSIZE nSrc1Len, FX_LPCWSTR lpszSrc1Data, FX_STRSIZE nSrc2Len, FX_LPCWSTR lpszSrc2Data);
787 void AssignCopy(FX_STRSIZE nSrcLen, FX_LPCWSTR lpszSrcData);
788 void AllocCopy(CFX_WideString& dest, FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex) const;
790 inline CFX_WideStringC::CFX_WideStringC(const CFX_WideString& src)
793 m_Length = src.GetLength();
795 inline CFX_WideStringC& CFX_WideStringC::operator = (const CFX_WideString& src)
798 m_Length = src.GetLength();
802 inline CFX_WideString operator + (const CFX_WideStringC& str1, const CFX_WideStringC& str2)
804 return CFX_WideString(str1, str2);
806 inline CFX_WideString operator + (const CFX_WideStringC& str1, FX_LPCWSTR str2)
808 return CFX_WideString(str1, str2);
810 inline CFX_WideString operator + (FX_LPCWSTR str1, const CFX_WideStringC& str2)
812 return CFX_WideString(str1, str2);
814 inline CFX_WideString operator + (const CFX_WideStringC& str1, FX_WCHAR ch)
816 return CFX_WideString(str1, CFX_WideStringC(ch));
818 inline CFX_WideString operator + (FX_WCHAR ch, const CFX_WideStringC& str2)
820 return CFX_WideString(ch, str2);
822 inline CFX_WideString operator + (const CFX_WideString& str1, const CFX_WideString& str2)
824 return CFX_WideString(str1, str2);
826 inline CFX_WideString operator + (const CFX_WideString& str1, FX_WCHAR ch)
828 return CFX_WideString(str1, CFX_WideStringC(ch));
830 inline CFX_WideString operator + (FX_WCHAR ch, const CFX_WideString& str2)
832 return CFX_WideString(ch, str2);
834 inline CFX_WideString operator + (const CFX_WideString& str1, FX_LPCWSTR str2)
836 return CFX_WideString(str1, str2);
838 inline CFX_WideString operator + (FX_LPCWSTR str1, const CFX_WideString& str2)
840 return CFX_WideString(str1, str2);
842 inline CFX_WideString operator + (const CFX_WideString& str1, const CFX_WideStringC& str2)
844 return CFX_WideString(str1, str2);
846 inline CFX_WideString operator + (const CFX_WideStringC& str1, const CFX_WideString& str2)
848 return CFX_WideString(str1, str2);
851 bool operator==(const CFX_WideString& s1, const CFX_WideString& s2);
852 bool operator==(const CFX_WideString& s1, const CFX_WideStringC& s2);
853 bool operator==(const CFX_WideStringC& s1, const CFX_WideString& s2);
854 bool operator== (const CFX_WideString& s1, FX_LPCWSTR s2);
855 bool operator==(FX_LPCWSTR s1, const CFX_WideString& s2);
856 bool operator!=(const CFX_WideString& s1, const CFX_WideString& s2);
857 bool operator!=(const CFX_WideString& s1, const CFX_WideStringC& s2);
858 bool operator!=(const CFX_WideStringC& s1, const CFX_WideString& s2);
859 bool operator!= (const CFX_WideString& s1, FX_LPCWSTR s2);
860 bool operator!=(FX_LPCWSTR s1, const CFX_WideString& s2);
861 FX_FLOAT FX_atof(FX_BSTR str);
862 void FX_atonum(FX_BSTR str, FX_BOOL& bInteger, void* pData);
863 FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_LPSTR buf);
864 CFX_ByteString FX_UTF8Encode(FX_LPCWSTR pwsStr, FX_STRSIZE len);
865 inline CFX_ByteString FX_UTF8Encode(FX_WSTR wsStr)
867 return FX_UTF8Encode(wsStr.GetPtr(), wsStr.GetLength());
869 inline CFX_ByteString FX_UTF8Encode(const CFX_WideString &wsStr)
871 return FX_UTF8Encode(wsStr.c_str(), wsStr.GetLength());