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;
18 class CFX_ByteStringC : public CFX_Object
28 CFX_ByteStringC(FX_LPCBYTE ptr, FX_STRSIZE size)
34 CFX_ByteStringC(FX_LPCSTR ptr)
36 m_Ptr = (FX_LPCBYTE)ptr;
37 m_Length = ptr ? (FX_STRSIZE)FXSYS_strlen(ptr) : 0;
40 CFX_ByteStringC(FX_CHAR& ch)
42 m_Ptr = (FX_LPCBYTE)&ch;
46 CFX_ByteStringC(FX_LPCSTR ptr, FX_STRSIZE len)
48 m_Ptr = (FX_LPCBYTE)ptr;
50 m_Length = (FX_STRSIZE)FXSYS_strlen(ptr);
56 CFX_ByteStringC(const CFX_ByteStringC& src)
59 m_Length = src.m_Length;
62 CFX_ByteStringC(const CFX_ByteString& src);
64 CFX_ByteStringC& operator = (FX_LPCSTR src)
66 m_Ptr = (FX_LPCBYTE)src;
67 m_Length = (FX_STRSIZE)FXSYS_strlen(src);
71 CFX_ByteStringC& operator = (const CFX_ByteStringC& src)
74 m_Length = src.m_Length;
78 CFX_ByteStringC& operator = (const CFX_ByteString& src);
80 bool operator == (const CFX_ByteStringC& str) const
82 return str.m_Length == m_Length && FXSYS_memcmp32(str.m_Ptr, m_Ptr, m_Length) == 0;
85 bool operator != (const CFX_ByteStringC& str) const
87 return str.m_Length != m_Length || FXSYS_memcmp32(str.m_Ptr, m_Ptr, m_Length) != 0;
89 #define FXBSTR_ID(c1, c2, c3, c4) ((c1 << 24) | (c2 << 16) | (c3 << 8) | (c4))
91 FX_DWORD GetID(FX_STRSIZE start_pos = 0) const;
93 FX_LPCBYTE GetPtr() const
98 FX_LPCSTR GetCStr() const
100 return (FX_LPCSTR)m_Ptr;
103 FX_STRSIZE GetLength() const
110 return m_Length == 0;
113 operator FX_LPCBYTE() const
118 FX_BYTE GetAt(FX_STRSIZE index) const
123 CFX_ByteStringC Mid(FX_STRSIZE index, FX_STRSIZE count = -1) const
128 if (index > m_Length) {
129 return CFX_ByteStringC();
131 if (count < 0 || count > m_Length - index) {
132 count = m_Length - index;
134 return CFX_ByteStringC(m_Ptr + index, count);
143 void* operator new (size_t) throw()
148 typedef const CFX_ByteStringC& FX_BSTR;
149 #define FX_BSTRC(str) CFX_ByteStringC(str, sizeof str-1)
150 struct CFX_StringData {
154 FX_STRSIZE m_nDataLength;
156 FX_STRSIZE m_nAllocLength;
160 class CFX_ByteString : public CFX_Object
169 CFX_ByteString(const CFX_ByteString& str);
171 CFX_ByteString(char ch);
173 CFX_ByteString(FX_LPCSTR ptr, FX_STRSIZE len = -1);
175 CFX_ByteString(FX_LPCBYTE ptr, FX_STRSIZE len);
177 CFX_ByteString(FX_BSTR bstrc);
179 CFX_ByteString(FX_BSTR bstrc1, FX_BSTR bstrc2);
183 static CFX_ByteString FromUnicode(FX_LPCWSTR ptr, FX_STRSIZE len = -1);
185 static CFX_ByteString FromUnicode(const CFX_WideString& str);
187 operator FX_LPCSTR() const
189 return m_pData ? m_pData->m_String : "";
192 operator FX_LPCBYTE() const
194 return m_pData ? (FX_LPCBYTE)m_pData->m_String : NULL;
197 FX_STRSIZE GetLength() const
199 return m_pData ? m_pData->m_nDataLength : 0;
207 int Compare(FX_BSTR str) const;
210 bool Equal(FX_BSTR str) const;
213 bool EqualNoCase(FX_BSTR str) const;
215 bool operator == (FX_LPCSTR str) const
220 bool operator == (FX_BSTR str) const
225 bool operator == (const CFX_ByteString& str) const;
227 bool operator != (FX_LPCSTR str) const
232 bool operator != (FX_BSTR str) const
237 bool operator != (const CFX_ByteString& str) const
239 return !operator==(str);
244 const CFX_ByteString& operator = (FX_LPCSTR str);
246 const CFX_ByteString& operator = (FX_BSTR bstrc);
248 const CFX_ByteString& operator = (const CFX_ByteString& stringSrc);
250 const CFX_ByteString& operator = (const CFX_BinaryBuf& buf);
252 void Load(FX_LPCBYTE str, FX_STRSIZE len);
254 const CFX_ByteString& operator += (FX_CHAR ch);
256 const CFX_ByteString& operator += (FX_LPCSTR str);
258 const CFX_ByteString& operator += (const CFX_ByteString& str);
260 const CFX_ByteString& operator += (FX_BSTR bstrc);
262 FX_BYTE GetAt(FX_STRSIZE nIndex) const
264 return m_pData ? m_pData->m_String[nIndex] : 0;
267 FX_BYTE operator[](FX_STRSIZE nIndex) const
269 return m_pData ? m_pData->m_String[nIndex] : 0;
272 void SetAt(FX_STRSIZE nIndex, FX_CHAR ch);
274 FX_STRSIZE Insert(FX_STRSIZE index, FX_CHAR ch);
276 FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1);
279 void Format(FX_LPCSTR lpszFormat, ... );
281 void FormatV(FX_LPCSTR lpszFormat, va_list argList);
284 void Reserve(FX_STRSIZE len);
286 FX_LPSTR GetBuffer(FX_STRSIZE len);
288 FX_LPSTR LockBuffer();
290 void ReleaseBuffer(FX_STRSIZE len = -1);
292 CFX_ByteString Mid(FX_STRSIZE first) const;
294 CFX_ByteString Mid(FX_STRSIZE first, FX_STRSIZE count) const;
296 CFX_ByteString Left(FX_STRSIZE count) const;
298 CFX_ByteString Right(FX_STRSIZE count) const;
300 FX_STRSIZE Find(FX_BSTR lpszSub, FX_STRSIZE start = 0) const;
302 FX_STRSIZE Find(FX_CHAR ch, FX_STRSIZE start = 0) const;
304 FX_STRSIZE ReverseFind(FX_CHAR ch) const;
312 void TrimRight(FX_CHAR chTarget);
314 void TrimRight(FX_BSTR lpszTargets);
318 void TrimLeft(FX_CHAR chTarget);
320 void TrimLeft(FX_BSTR lpszTargets);
322 FX_STRSIZE Replace(FX_BSTR lpszOld, FX_BSTR lpszNew);
324 FX_STRSIZE Remove(FX_CHAR ch);
326 CFX_WideString UTF8Decode() const;
328 void ConvertFrom(const CFX_WideString& str, CFX_CharMap* pCharMap = NULL);
330 FX_DWORD GetID(FX_STRSIZE start_pos = 0) const;
332 static CFX_ByteString LoadFromFile(FX_BSTR file_path);
333 #define FXFORMAT_SIGNED 1
334 #define FXFORMAT_HEX 2
335 #define FXFORMAT_CAPITAL 4
337 static CFX_ByteString FormatInteger(int i, FX_DWORD flags = 0);
339 static CFX_ByteString FormatFloat(FX_FLOAT f, int precision = 0);
342 struct CFX_StringData* m_pData;
343 void AllocCopy(CFX_ByteString& dest, FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex) const;
344 void AssignCopy(FX_STRSIZE nSrcLen, FX_LPCSTR lpszSrcData);
345 void ConcatCopy(FX_STRSIZE nSrc1Len, FX_LPCSTR lpszSrc1Data, FX_STRSIZE nSrc2Len, FX_LPCSTR lpszSrc2Data);
346 void ConcatInPlace(FX_STRSIZE nSrcLen, FX_LPCSTR lpszSrcData);
347 void CopyBeforeWrite();
348 void AllocBeforeWrite(FX_STRSIZE nLen);
350 inline CFX_ByteStringC::CFX_ByteStringC(const CFX_ByteString& src)
352 m_Ptr = (FX_LPCBYTE)src;
353 m_Length = src.GetLength();
355 inline CFX_ByteStringC& CFX_ByteStringC::operator = (const CFX_ByteString& src)
357 m_Ptr = (FX_LPCBYTE)src;
358 m_Length = src.GetLength();
362 inline CFX_ByteString operator + (FX_BSTR str1, FX_BSTR str2)
364 return CFX_ByteString(str1, str2);
366 inline CFX_ByteString operator + (FX_BSTR str1, FX_LPCSTR str2)
368 return CFX_ByteString(str1, str2);
370 inline CFX_ByteString operator + (FX_LPCSTR str1, FX_BSTR str2)
372 return CFX_ByteString(str1, str2);
374 inline CFX_ByteString operator + (FX_BSTR str1, FX_CHAR ch)
376 return CFX_ByteString(str1, CFX_ByteStringC(ch));
378 inline CFX_ByteString operator + (FX_CHAR ch, FX_BSTR str2)
380 return CFX_ByteString(ch, str2);
382 inline CFX_ByteString operator + (const CFX_ByteString& str1, const CFX_ByteString& str2)
384 return CFX_ByteString(str1, str2);
386 inline CFX_ByteString operator + (const CFX_ByteString& str1, FX_CHAR ch)
388 return CFX_ByteString(str1, CFX_ByteStringC(ch));
390 inline CFX_ByteString operator + (FX_CHAR ch, const CFX_ByteString& str2)
392 return CFX_ByteString(ch, str2);
394 inline CFX_ByteString operator + (const CFX_ByteString& str1, FX_LPCSTR str2)
396 return CFX_ByteString(str1, str2);
398 inline CFX_ByteString operator + (FX_LPCSTR str1, const CFX_ByteString& str2)
400 return CFX_ByteString(str1, str2);
402 inline CFX_ByteString operator + (const CFX_ByteString& str1, FX_BSTR str2)
404 return CFX_ByteString(str1, str2);
406 inline CFX_ByteString operator + (FX_BSTR str1, const CFX_ByteString& str2)
408 return CFX_ByteString(str1, str2);
410 class CFX_StringBufBase : public CFX_Object
414 CFX_StringBufBase(FX_STRSIZE limit)
420 FX_CHAR* GetPtr() const
422 return (FX_CHAR*)(this + 1);
425 FX_STRSIZE GetSize() const
435 void Copy(FX_BSTR str);
437 void Append(FX_BSTR str);
439 void Append(int i, FX_DWORD flags = 0);
441 CFX_ByteStringC GetStringC() const
443 return CFX_ByteStringC((FX_CHAR*)(this + 1), m_Size);
446 CFX_ByteString GetString() const
448 return CFX_ByteString((FX_CHAR*)(this + 1), m_Size);
456 template<FX_STRSIZE limit>
457 class CFX_StringBufTemplate : public CFX_StringBufBase
461 CFX_StringBufTemplate() : CFX_StringBufBase(limit) {}
463 FX_CHAR m_Buffer[limit];
465 typedef CFX_StringBufTemplate<256> CFX_StringBuf256;
466 class CFX_WideStringC : public CFX_Object
476 CFX_WideStringC(FX_LPCWSTR ptr)
479 m_Length = ptr ? (FX_STRSIZE)FXSYS_wcslen(ptr) : 0;
482 CFX_WideStringC(FX_WCHAR& ch)
488 CFX_WideStringC(FX_LPCWSTR ptr, FX_STRSIZE len)
492 m_Length = (FX_STRSIZE)FXSYS_wcslen(ptr);
498 CFX_WideStringC(const CFX_WideStringC& src)
501 m_Length = src.m_Length;
504 CFX_WideStringC(const CFX_WideString& src);
506 CFX_WideStringC& operator = (FX_LPCWSTR src)
509 m_Length = (FX_STRSIZE)FXSYS_wcslen(src);
513 CFX_WideStringC& operator = (const CFX_WideStringC& src)
516 m_Length = src.m_Length;
520 CFX_WideStringC& operator = (const CFX_WideString& src);
522 bool operator == (const CFX_WideStringC& str) const
524 return str.m_Length == m_Length && FXSYS_memcmp32(str.m_Ptr, m_Ptr, m_Length * sizeof(FX_WCHAR)) == 0;
527 bool operator != (const CFX_WideStringC& str) const
529 return str.m_Length != m_Length || FXSYS_memcmp32(str.m_Ptr, m_Ptr, m_Length * sizeof(FX_WCHAR)) != 0;
532 FX_LPCWSTR GetPtr() const
537 FX_STRSIZE GetLength() const
544 return m_Length == 0;
547 FX_WCHAR GetAt(FX_STRSIZE index) const
552 CFX_WideStringC Left(FX_STRSIZE count) const
555 return CFX_WideStringC();
557 if (count > m_Length) {
560 return CFX_WideStringC(m_Ptr, count);
563 CFX_WideStringC Mid(FX_STRSIZE index, FX_STRSIZE count = -1) const
568 if (index > m_Length) {
569 return CFX_WideStringC();
571 if (count < 0 || count > m_Length - index) {
572 count = m_Length - index;
574 return CFX_WideStringC(m_Ptr + index, count);
577 CFX_WideStringC Right(FX_STRSIZE count) const
580 return CFX_WideStringC();
582 if (count > m_Length) {
585 return CFX_WideStringC(m_Ptr + m_Length - count, count);
594 void* operator new (size_t) throw()
599 typedef const CFX_WideStringC& FX_WSTR;
600 #define FX_WSTRC(wstr) CFX_WideStringC((FX_LPCWSTR)wstr, sizeof(wstr) / sizeof(FX_WCHAR) - 1)
601 struct CFX_StringDataW {
605 FX_STRSIZE m_nDataLength;
607 FX_STRSIZE m_nAllocLength;
609 FX_WCHAR m_String[1];
611 class CFX_WideString : public CFX_Object
620 CFX_WideString(const CFX_WideString& str);
622 CFX_WideString(FX_LPCWSTR ptr, FX_STRSIZE len = -1)
627 CFX_WideString(FX_WCHAR ch);
629 CFX_WideString(const CFX_WideStringC& str);
631 CFX_WideString(const CFX_WideStringC& str1, const CFX_WideStringC& str2);
635 static CFX_WideString FromLocal(const char* str, FX_STRSIZE len = -1);
637 static CFX_WideString FromUTF8(const char* str, FX_STRSIZE len);
639 static CFX_WideString FromUTF16LE(const unsigned short* str, FX_STRSIZE len);
641 static FX_STRSIZE WStringLength(const unsigned short* str);
643 operator FX_LPCWSTR() const
645 return m_pData ? m_pData->m_String : (FX_WCHAR*)L"";
651 FX_BOOL IsEmpty() const
656 FX_STRSIZE GetLength() const
658 return m_pData ? m_pData->m_nDataLength : 0;
661 const CFX_WideString& operator = (FX_LPCWSTR str);
663 const CFX_WideString& operator =(const CFX_WideString& stringSrc);
665 const CFX_WideString& operator =(const CFX_WideStringC& stringSrc);
667 const CFX_WideString& operator += (FX_LPCWSTR str);
669 const CFX_WideString& operator += (FX_WCHAR ch);
671 const CFX_WideString& operator += (const CFX_WideString& str);
673 const CFX_WideString& operator += (const CFX_WideStringC& str);
675 FX_WCHAR GetAt(FX_STRSIZE nIndex) const
677 return m_pData ? m_pData->m_String[nIndex] : 0;
680 FX_WCHAR operator[](FX_STRSIZE nIndex) const
682 return m_pData ? m_pData->m_String[nIndex] : 0;
685 void SetAt(FX_STRSIZE nIndex, FX_WCHAR ch);
687 int Compare(FX_LPCWSTR str) const;
689 int Compare(const CFX_WideString& str) const;
691 int CompareNoCase(FX_LPCWSTR str) const;
693 bool Equal(const CFX_WideStringC& str) const;
695 CFX_WideString Mid(FX_STRSIZE first) const;
697 CFX_WideString Mid(FX_STRSIZE first, FX_STRSIZE count) const;
699 CFX_WideString Left(FX_STRSIZE count) const;
701 CFX_WideString Right(FX_STRSIZE count) const;
703 FX_STRSIZE Insert(FX_STRSIZE index, FX_WCHAR ch);
705 FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1);
707 void Format(FX_LPCWSTR lpszFormat, ... );
709 void FormatV(FX_LPCWSTR lpszFormat, va_list argList);
717 void TrimRight(FX_WCHAR chTarget);
719 void TrimRight(FX_LPCWSTR lpszTargets);
723 void TrimLeft(FX_WCHAR chTarget);
725 void TrimLeft(FX_LPCWSTR lpszTargets);
727 void Reserve(FX_STRSIZE len);
729 FX_LPWSTR GetBuffer(FX_STRSIZE len);
731 FX_LPWSTR LockBuffer();
733 void ReleaseBuffer(FX_STRSIZE len = -1);
735 int GetInteger() const;
737 FX_FLOAT GetFloat() const;
739 FX_STRSIZE Find(FX_LPCWSTR lpszSub, FX_STRSIZE start = 0) const;
741 FX_STRSIZE Find(FX_WCHAR ch, FX_STRSIZE start = 0) const;
743 FX_STRSIZE Replace(FX_LPCWSTR lpszOld, FX_LPCWSTR lpszNew);
745 FX_STRSIZE Remove(FX_WCHAR ch);
747 CFX_ByteString UTF8Encode() const;
749 CFX_ByteString UTF16LE_Encode(FX_BOOL bTerminate = TRUE) const;
751 void ConvertFrom(const CFX_ByteString& str, CFX_CharMap* pCharMap = NULL);
753 void InitStr(FX_LPCWSTR ptr, int len);
755 CFX_StringDataW* m_pData;
756 void CopyBeforeWrite();
757 void AllocBeforeWrite(FX_STRSIZE nLen);
758 void ConcatInPlace(FX_STRSIZE nSrcLen, FX_LPCWSTR lpszSrcData);
759 void ConcatCopy(FX_STRSIZE nSrc1Len, FX_LPCWSTR lpszSrc1Data, FX_STRSIZE nSrc2Len, FX_LPCWSTR lpszSrc2Data);
760 void AssignCopy(FX_STRSIZE nSrcLen, FX_LPCWSTR lpszSrcData);
761 void AllocCopy(CFX_WideString& dest, FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex) const;
763 inline CFX_WideStringC::CFX_WideStringC(const CFX_WideString& src)
765 m_Ptr = (FX_LPCWSTR)src;
766 m_Length = src.GetLength();
768 inline CFX_WideStringC& CFX_WideStringC::operator = (const CFX_WideString& src)
770 m_Ptr = (FX_LPCWSTR)src;
771 m_Length = src.GetLength();
775 inline CFX_WideString operator + (const CFX_WideStringC& str1, const CFX_WideStringC& str2)
777 return CFX_WideString(str1, str2);
779 inline CFX_WideString operator + (const CFX_WideStringC& str1, FX_LPCWSTR str2)
781 return CFX_WideString(str1, str2);
783 inline CFX_WideString operator + (FX_LPCWSTR str1, const CFX_WideStringC& str2)
785 return CFX_WideString(str1, str2);
787 inline CFX_WideString operator + (const CFX_WideStringC& str1, FX_WCHAR ch)
789 return CFX_WideString(str1, CFX_WideStringC(ch));
791 inline CFX_WideString operator + (FX_WCHAR ch, const CFX_WideStringC& str2)
793 return CFX_WideString(ch, str2);
795 inline CFX_WideString operator + (const CFX_WideString& str1, const CFX_WideString& str2)
797 return CFX_WideString(str1, str2);
799 inline CFX_WideString operator + (const CFX_WideString& str1, FX_WCHAR ch)
801 return CFX_WideString(str1, CFX_WideStringC(ch));
803 inline CFX_WideString operator + (FX_WCHAR ch, const CFX_WideString& str2)
805 return CFX_WideString(ch, str2);
807 inline CFX_WideString operator + (const CFX_WideString& str1, FX_LPCWSTR str2)
809 return CFX_WideString(str1, str2);
811 inline CFX_WideString operator + (FX_LPCWSTR str1, const CFX_WideString& str2)
813 return CFX_WideString(str1, str2);
815 inline CFX_WideString operator + (const CFX_WideString& str1, const CFX_WideStringC& str2)
817 return CFX_WideString(str1, str2);
819 inline CFX_WideString operator + (const CFX_WideStringC& str1, const CFX_WideString& str2)
821 return CFX_WideString(str1, str2);
824 bool operator==(const CFX_WideString& s1, const CFX_WideString& s2);
825 bool operator==(const CFX_WideString& s1, const CFX_WideStringC& s2);
826 bool operator==(const CFX_WideStringC& s1, const CFX_WideString& s2);
827 bool operator== (const CFX_WideString& s1, FX_LPCWSTR s2);
828 bool operator==(FX_LPCWSTR s1, const CFX_WideString& s2);
829 bool operator!=(const CFX_WideString& s1, const CFX_WideString& s2);
830 bool operator!=(const CFX_WideString& s1, const CFX_WideStringC& s2);
831 bool operator!=(const CFX_WideStringC& s1, const CFX_WideString& s2);
832 bool operator!= (const CFX_WideString& s1, FX_LPCWSTR s2);
833 bool operator!=(FX_LPCWSTR s1, const CFX_WideString& s2);
834 FX_FLOAT FX_atof(FX_BSTR str);
835 void FX_atonum(FX_BSTR str, FX_BOOL& bInteger, void* pData);
836 FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_LPSTR buf);
837 CFX_ByteString FX_UTF8Encode(FX_LPCWSTR pwsStr, FX_STRSIZE len);
838 inline CFX_ByteString FX_UTF8Encode(FX_WSTR wsStr)
840 return FX_UTF8Encode(wsStr.GetPtr(), wsStr.GetLength());
842 inline CFX_ByteString FX_UTF8Encode(const CFX_WideString &wsStr)
844 return FX_UTF8Encode((FX_LPCWSTR)wsStr, wsStr.GetLength());