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
12 #include "fx_memory.h"
14 class CFX_ByteStringC;
16 class CFX_WideStringC;
20 typedef int FX_STRSIZE;
21 class CFX_ByteStringL;
22 class CFX_WideStringL;
24 // An immutable string with caller-provided storage which must outlive the
29 typedef FX_CHAR value_type;
37 CFX_ByteStringC(FX_LPCBYTE ptr, FX_STRSIZE size)
43 CFX_ByteStringC(FX_LPCSTR ptr)
45 m_Ptr = (FX_LPCBYTE)ptr;
46 m_Length = ptr ? (FX_STRSIZE)FXSYS_strlen(ptr) : 0;
49 // |ch| must be an lvalue that outlives the the CFX_ByteStringC. However,
50 // the use of char rvalues are not caught at compile time. They are
51 // implicitly promoted to CFX_ByteString (see below) and then the
52 // CFX_ByteStringC is constructed from the CFX_ByteString via the alternate
53 // constructor below. The CFX_ByteString then typically goes out of scope
54 // and |m_Ptr| may be left pointing to invalid memory. Beware.
55 // TODO(tsepez): Mark single-argument string constructors as explicit.
56 CFX_ByteStringC(FX_CHAR& ch)
58 m_Ptr = (FX_LPCBYTE)&ch;
62 CFX_ByteStringC(FX_LPCSTR ptr, FX_STRSIZE len)
64 m_Ptr = (FX_LPCBYTE)ptr;
66 m_Length = (FX_STRSIZE)FXSYS_strlen(ptr);
72 CFX_ByteStringC(const CFX_ByteStringC& src)
75 m_Length = src.m_Length;
78 CFX_ByteStringC(const CFX_ByteString& src);
80 CFX_ByteStringC& operator = (FX_LPCSTR src)
82 m_Ptr = (FX_LPCBYTE)src;
83 m_Length = m_Ptr ? (FX_STRSIZE)FXSYS_strlen(src) : 0;
87 CFX_ByteStringC& operator = (const CFX_ByteStringC& src)
90 m_Length = src.m_Length;
94 CFX_ByteStringC& operator = (const CFX_ByteString& src);
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;
101 bool operator != (const CFX_ByteStringC& str) const
103 return str.m_Length != m_Length || FXSYS_memcmp32(str.m_Ptr, m_Ptr, m_Length) != 0;
106 FX_DWORD GetID(FX_STRSIZE start_pos = 0) const;
108 FX_LPCBYTE GetPtr() const
113 FX_LPCSTR GetCStr() const
115 return (FX_LPCSTR)m_Ptr;
118 FX_STRSIZE GetLength() const
125 return m_Length == 0;
128 FX_BYTE GetAt(FX_STRSIZE index) const
133 CFX_ByteStringC Mid(FX_STRSIZE index, FX_STRSIZE count = -1) const
138 if (index > m_Length) {
139 return CFX_ByteStringC();
141 if (count < 0 || count > m_Length - index) {
142 count = m_Length - index;
144 return CFX_ByteStringC(m_Ptr + index, count);
147 const FX_BYTE& operator[] (size_t index) const
152 bool operator< (const CFX_ByteStringC& that) const
154 int result = memcmp(m_Ptr, that.m_Ptr, std::min(m_Length, that.m_Length));
155 return result < 0 || (result == 0 && m_Length < that.m_Length);
163 void* operator new (size_t) throw()
168 typedef const CFX_ByteStringC& FX_BSTR;
169 #define FX_BSTRC(str) CFX_ByteStringC(str, sizeof str-1)
170 #define FXBSTR_ID(c1, c2, c3, c4) ((c1 << 24) | (c2 << 16) | (c3 << 8) | (c4))
171 struct CFX_StringData {
175 FX_STRSIZE m_nDataLength;
177 FX_STRSIZE m_nAllocLength;
184 typedef FX_CHAR value_type;
191 CFX_ByteString(const CFX_ByteString& str);
193 CFX_ByteString(char ch);
195 CFX_ByteString(FX_LPCSTR ptr, FX_STRSIZE len = -1);
197 CFX_ByteString(FX_LPCBYTE ptr, FX_STRSIZE len);
199 CFX_ByteString(FX_BSTR bstrc);
201 CFX_ByteString(FX_BSTR bstrc1, FX_BSTR bstrc2);
205 static CFX_ByteString FromUnicode(FX_LPCWSTR ptr, FX_STRSIZE len = -1);
207 static CFX_ByteString FromUnicode(const CFX_WideString& str);
209 // Explicit conversion to raw string
210 FX_LPCSTR c_str() const
212 return m_pData ? m_pData->m_String : "";
215 // Implicit conversion to C-style string -- deprecated
216 operator FX_LPCSTR() const
218 return m_pData ? m_pData->m_String : "";
221 operator FX_LPCBYTE() const
223 return m_pData ? (FX_LPCBYTE)m_pData->m_String : NULL;
226 FX_STRSIZE GetLength() const
228 return m_pData ? m_pData->m_nDataLength : 0;
236 int Compare(FX_BSTR str) const;
239 bool Equal(FX_BSTR str) const;
242 bool EqualNoCase(FX_BSTR str) const;
244 bool operator == (FX_LPCSTR str) const
249 bool operator == (FX_BSTR str) const
254 bool operator == (const CFX_ByteString& str) const;
256 bool operator != (FX_LPCSTR str) const
261 bool operator != (FX_BSTR str) const
266 bool operator != (const CFX_ByteString& str) const
268 return !operator==(str);
271 bool operator< (const CFX_ByteString& str) const
273 int result = FXSYS_memcmp32(c_str(), str.c_str(), std::min(GetLength(), str.GetLength()));
274 return result < 0 || (result == 0 && GetLength() < str.GetLength());
279 const CFX_ByteString& operator = (FX_LPCSTR str);
281 const CFX_ByteString& operator = (FX_BSTR bstrc);
283 const CFX_ByteString& operator = (const CFX_ByteString& stringSrc);
285 const CFX_ByteString& operator = (const CFX_BinaryBuf& buf);
287 void Load(FX_LPCBYTE str, FX_STRSIZE len);
289 const CFX_ByteString& operator += (FX_CHAR ch);
291 const CFX_ByteString& operator += (FX_LPCSTR str);
293 const CFX_ByteString& operator += (const CFX_ByteString& str);
295 const CFX_ByteString& operator += (FX_BSTR bstrc);
297 FX_BYTE GetAt(FX_STRSIZE nIndex) const
299 return m_pData ? m_pData->m_String[nIndex] : 0;
302 FX_BYTE operator[](FX_STRSIZE nIndex) const
304 return m_pData ? m_pData->m_String[nIndex] : 0;
307 void SetAt(FX_STRSIZE nIndex, FX_CHAR ch);
309 FX_STRSIZE Insert(FX_STRSIZE index, FX_CHAR ch);
311 FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1);
314 void Format(FX_LPCSTR lpszFormat, ... );
316 void FormatV(FX_LPCSTR lpszFormat, va_list argList);
319 void Reserve(FX_STRSIZE len);
321 FX_LPSTR GetBuffer(FX_STRSIZE len);
323 void ReleaseBuffer(FX_STRSIZE len = -1);
325 CFX_ByteString Mid(FX_STRSIZE first) const;
327 CFX_ByteString Mid(FX_STRSIZE first, FX_STRSIZE count) const;
329 CFX_ByteString Left(FX_STRSIZE count) const;
331 CFX_ByteString Right(FX_STRSIZE count) const;
333 FX_STRSIZE Find(FX_BSTR lpszSub, FX_STRSIZE start = 0) const;
335 FX_STRSIZE Find(FX_CHAR ch, FX_STRSIZE start = 0) const;
337 FX_STRSIZE ReverseFind(FX_CHAR ch) const;
345 void TrimRight(FX_CHAR chTarget);
347 void TrimRight(FX_BSTR lpszTargets);
351 void TrimLeft(FX_CHAR chTarget);
353 void TrimLeft(FX_BSTR lpszTargets);
355 FX_STRSIZE Replace(FX_BSTR lpszOld, FX_BSTR lpszNew);
357 FX_STRSIZE Remove(FX_CHAR ch);
359 CFX_WideString UTF8Decode() const;
361 void ConvertFrom(const CFX_WideString& str, CFX_CharMap* pCharMap = NULL);
363 FX_DWORD GetID(FX_STRSIZE start_pos = 0) const;
365 #define FXFORMAT_SIGNED 1
366 #define FXFORMAT_HEX 2
367 #define FXFORMAT_CAPITAL 4
369 static CFX_ByteString FormatInteger(int i, FX_DWORD flags = 0);
371 static CFX_ByteString FormatFloat(FX_FLOAT f, int precision = 0);
374 struct CFX_StringData* m_pData;
375 void AllocCopy(CFX_ByteString& dest, FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex) const;
376 void AssignCopy(FX_STRSIZE nSrcLen, FX_LPCSTR lpszSrcData);
377 void ConcatCopy(FX_STRSIZE nSrc1Len, FX_LPCSTR lpszSrc1Data, FX_STRSIZE nSrc2Len, FX_LPCSTR lpszSrc2Data);
378 void ConcatInPlace(FX_STRSIZE nSrcLen, FX_LPCSTR lpszSrcData);
379 void CopyBeforeWrite();
380 void AllocBeforeWrite(FX_STRSIZE nLen);
382 inline CFX_ByteStringC::CFX_ByteStringC(const CFX_ByteString& src)
384 m_Ptr = (FX_LPCBYTE)src;
385 m_Length = src.GetLength();
387 inline CFX_ByteStringC& CFX_ByteStringC::operator = (const CFX_ByteString& src)
389 m_Ptr = (FX_LPCBYTE)src;
390 m_Length = src.GetLength();
394 inline CFX_ByteString operator + (FX_BSTR str1, FX_BSTR str2)
396 return CFX_ByteString(str1, str2);
398 inline CFX_ByteString operator + (FX_BSTR str1, FX_LPCSTR str2)
400 return CFX_ByteString(str1, str2);
402 inline CFX_ByteString operator + (FX_LPCSTR str1, FX_BSTR str2)
404 return CFX_ByteString(str1, str2);
406 inline CFX_ByteString operator + (FX_BSTR str1, FX_CHAR ch)
408 return CFX_ByteString(str1, CFX_ByteStringC(ch));
410 inline CFX_ByteString operator + (FX_CHAR ch, FX_BSTR str2)
412 return CFX_ByteString(ch, str2);
414 inline CFX_ByteString operator + (const CFX_ByteString& str1, const CFX_ByteString& str2)
416 return CFX_ByteString(str1, str2);
418 inline CFX_ByteString operator + (const CFX_ByteString& str1, FX_CHAR ch)
420 return CFX_ByteString(str1, CFX_ByteStringC(ch));
422 inline CFX_ByteString operator + (FX_CHAR ch, const CFX_ByteString& str2)
424 return CFX_ByteString(ch, str2);
426 inline CFX_ByteString operator + (const CFX_ByteString& str1, FX_LPCSTR str2)
428 return CFX_ByteString(str1, str2);
430 inline CFX_ByteString operator + (FX_LPCSTR str1, const CFX_ByteString& str2)
432 return CFX_ByteString(str1, str2);
434 inline CFX_ByteString operator + (const CFX_ByteString& str1, FX_BSTR str2)
436 return CFX_ByteString(str1, str2);
438 inline CFX_ByteString operator + (FX_BSTR str1, const CFX_ByteString& str2)
440 return CFX_ByteString(str1, str2);
442 class CFX_WideStringC
445 typedef FX_WCHAR value_type;
453 CFX_WideStringC(FX_LPCWSTR ptr)
456 m_Length = ptr ? (FX_STRSIZE)FXSYS_wcslen(ptr) : 0;
459 CFX_WideStringC(FX_WCHAR& ch)
465 CFX_WideStringC(FX_LPCWSTR ptr, FX_STRSIZE len)
469 m_Length = (FX_STRSIZE)FXSYS_wcslen(ptr);
475 CFX_WideStringC(const CFX_WideStringC& src)
478 m_Length = src.m_Length;
481 CFX_WideStringC(const CFX_WideString& src);
483 CFX_WideStringC& operator = (FX_LPCWSTR src)
486 m_Length = (FX_STRSIZE)FXSYS_wcslen(src);
490 CFX_WideStringC& operator = (const CFX_WideStringC& src)
493 m_Length = src.m_Length;
497 CFX_WideStringC& operator = (const CFX_WideString& src);
499 bool operator == (const CFX_WideStringC& str) const
501 return str.m_Length == m_Length && FXSYS_memcmp32(str.m_Ptr, m_Ptr, m_Length * sizeof(FX_WCHAR)) == 0;
504 bool operator != (const CFX_WideStringC& str) const
506 return str.m_Length != m_Length || FXSYS_memcmp32(str.m_Ptr, m_Ptr, m_Length * sizeof(FX_WCHAR)) != 0;
509 FX_LPCWSTR GetPtr() const
514 FX_STRSIZE GetLength() const
521 return m_Length == 0;
524 FX_WCHAR GetAt(FX_STRSIZE index) const
529 CFX_WideStringC Left(FX_STRSIZE count) const
532 return CFX_WideStringC();
534 if (count > m_Length) {
537 return CFX_WideStringC(m_Ptr, count);
540 CFX_WideStringC Mid(FX_STRSIZE index, FX_STRSIZE count = -1) const
545 if (index > m_Length) {
546 return CFX_WideStringC();
548 if (count < 0 || count > m_Length - index) {
549 count = m_Length - index;
551 return CFX_WideStringC(m_Ptr + index, count);
554 CFX_WideStringC Right(FX_STRSIZE count) const
557 return CFX_WideStringC();
559 if (count > m_Length) {
562 return CFX_WideStringC(m_Ptr + m_Length - count, count);
565 const FX_WCHAR& operator[] (size_t index) const
570 bool operator< (const CFX_WideStringC& that) const
572 int result = wmemcmp(m_Ptr, that.m_Ptr, std::min(m_Length, that.m_Length));
573 return result < 0 || (result == 0 && m_Length < that.m_Length);
581 void* operator new (size_t) throw()
586 typedef const CFX_WideStringC& FX_WSTR;
587 #define FX_WSTRC(wstr) CFX_WideStringC(wstr, FX_ArraySize(wstr) - 1)
588 struct CFX_StringDataW {
592 FX_STRSIZE m_nDataLength;
594 FX_STRSIZE m_nAllocLength;
596 FX_WCHAR m_String[1];
601 typedef FX_WCHAR value_type;
608 CFX_WideString(const CFX_WideString& str);
610 CFX_WideString(FX_LPCWSTR ptr, FX_STRSIZE len = -1)
615 CFX_WideString(FX_WCHAR ch);
617 CFX_WideString(const CFX_WideStringC& str);
619 CFX_WideString(const CFX_WideStringC& str1, const CFX_WideStringC& str2);
623 static CFX_WideString FromLocal(const char* str, FX_STRSIZE len = -1);
625 static CFX_WideString FromUTF8(const char* str, FX_STRSIZE len);
627 static CFX_WideString FromUTF16LE(const unsigned short* str, FX_STRSIZE len);
629 static FX_STRSIZE WStringLength(const unsigned short* str);
631 // Explicit conversion to raw string
632 FX_LPCWSTR c_str() const
634 return m_pData ? m_pData->m_String : L"";
637 // Implicit conversion to C-style wide string -- deprecated
638 operator FX_LPCWSTR() const
640 return m_pData ? m_pData->m_String : L"";
646 FX_BOOL IsEmpty() const
651 FX_STRSIZE GetLength() const
653 return m_pData ? m_pData->m_nDataLength : 0;
656 const CFX_WideString& operator = (FX_LPCWSTR str);
658 const CFX_WideString& operator =(const CFX_WideString& stringSrc);
660 const CFX_WideString& operator =(const CFX_WideStringC& stringSrc);
662 const CFX_WideString& operator += (FX_LPCWSTR str);
664 const CFX_WideString& operator += (FX_WCHAR ch);
666 const CFX_WideString& operator += (const CFX_WideString& str);
668 const CFX_WideString& operator += (const CFX_WideStringC& str);
670 bool operator< (const CFX_WideString& str) const {
671 int result = wmemcmp(c_str(), str.c_str(), std::min(GetLength(), str.GetLength()));
672 return result < 0 || (result == 0 && GetLength() < str.GetLength());
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 void ReleaseBuffer(FX_STRSIZE len = -1);
733 int GetInteger() const;
735 FX_FLOAT GetFloat() const;
737 FX_STRSIZE Find(FX_LPCWSTR lpszSub, FX_STRSIZE start = 0) const;
739 FX_STRSIZE Find(FX_WCHAR ch, FX_STRSIZE start = 0) const;
741 FX_STRSIZE Replace(FX_LPCWSTR lpszOld, FX_LPCWSTR lpszNew);
743 FX_STRSIZE Remove(FX_WCHAR ch);
745 CFX_ByteString UTF8Encode() const;
747 CFX_ByteString UTF16LE_Encode() const;
749 void ConvertFrom(const CFX_ByteString& str, CFX_CharMap* pCharMap = NULL);
751 void InitStr(FX_LPCWSTR ptr, int len);
753 CFX_StringDataW* m_pData;
754 void CopyBeforeWrite();
755 void AllocBeforeWrite(FX_STRSIZE nLen);
756 void ConcatInPlace(FX_STRSIZE nSrcLen, FX_LPCWSTR lpszSrcData);
757 void ConcatCopy(FX_STRSIZE nSrc1Len, FX_LPCWSTR lpszSrc1Data, FX_STRSIZE nSrc2Len, FX_LPCWSTR lpszSrc2Data);
758 void AssignCopy(FX_STRSIZE nSrcLen, FX_LPCWSTR lpszSrcData);
759 void AllocCopy(CFX_WideString& dest, FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex) const;
761 inline CFX_WideStringC::CFX_WideStringC(const CFX_WideString& src)
764 m_Length = src.GetLength();
766 inline CFX_WideStringC& CFX_WideStringC::operator = (const CFX_WideString& src)
769 m_Length = src.GetLength();
773 inline CFX_WideString operator + (const CFX_WideStringC& str1, const CFX_WideStringC& str2)
775 return CFX_WideString(str1, str2);
777 inline CFX_WideString operator + (const CFX_WideStringC& str1, FX_LPCWSTR str2)
779 return CFX_WideString(str1, str2);
781 inline CFX_WideString operator + (FX_LPCWSTR str1, const CFX_WideStringC& str2)
783 return CFX_WideString(str1, str2);
785 inline CFX_WideString operator + (const CFX_WideStringC& str1, FX_WCHAR ch)
787 return CFX_WideString(str1, CFX_WideStringC(ch));
789 inline CFX_WideString operator + (FX_WCHAR ch, const CFX_WideStringC& str2)
791 return CFX_WideString(ch, str2);
793 inline CFX_WideString operator + (const CFX_WideString& str1, const CFX_WideString& str2)
795 return CFX_WideString(str1, str2);
797 inline CFX_WideString operator + (const CFX_WideString& str1, FX_WCHAR ch)
799 return CFX_WideString(str1, CFX_WideStringC(ch));
801 inline CFX_WideString operator + (FX_WCHAR ch, const CFX_WideString& str2)
803 return CFX_WideString(ch, str2);
805 inline CFX_WideString operator + (const CFX_WideString& str1, FX_LPCWSTR str2)
807 return CFX_WideString(str1, str2);
809 inline CFX_WideString operator + (FX_LPCWSTR str1, const CFX_WideString& str2)
811 return CFX_WideString(str1, str2);
813 inline CFX_WideString operator + (const CFX_WideString& str1, const CFX_WideStringC& str2)
815 return CFX_WideString(str1, str2);
817 inline CFX_WideString operator + (const CFX_WideStringC& str1, const CFX_WideString& str2)
819 return CFX_WideString(str1, str2);
822 bool operator==(const CFX_WideString& s1, const CFX_WideString& s2);
823 bool operator==(const CFX_WideString& s1, const CFX_WideStringC& s2);
824 bool operator==(const CFX_WideStringC& s1, const CFX_WideString& s2);
825 bool operator== (const CFX_WideString& s1, FX_LPCWSTR s2);
826 bool operator==(FX_LPCWSTR s1, const CFX_WideString& s2);
827 bool operator!=(const CFX_WideString& s1, const CFX_WideString& s2);
828 bool operator!=(const CFX_WideString& s1, const CFX_WideStringC& s2);
829 bool operator!=(const CFX_WideStringC& s1, const CFX_WideString& s2);
830 bool operator!= (const CFX_WideString& s1, FX_LPCWSTR s2);
831 bool operator!=(FX_LPCWSTR s1, const CFX_WideString& s2);
832 FX_FLOAT FX_atof(FX_BSTR str);
833 void FX_atonum(FX_BSTR str, FX_BOOL& bInteger, void* pData);
834 FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_LPSTR buf);
835 CFX_ByteString FX_UTF8Encode(FX_LPCWSTR pwsStr, FX_STRSIZE len);
836 inline CFX_ByteString FX_UTF8Encode(FX_WSTR wsStr)
838 return FX_UTF8Encode(wsStr.GetPtr(), wsStr.GetLength());
840 inline CFX_ByteString FX_UTF8Encode(const CFX_WideString &wsStr)
842 return FX_UTF8Encode(wsStr.c_str(), wsStr.GetLength());