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 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <cctype> | 10 #include <cctype> |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 m_pData.Reset(StringData::Create(pStr, nLen)); | 87 m_pData.Reset(StringData::Create(pStr, nLen)); |
88 } | 88 } |
89 | 89 |
90 CFX_ByteString::CFX_ByteString(const uint8_t* pStr, FX_STRSIZE nLen) { | 90 CFX_ByteString::CFX_ByteString(const uint8_t* pStr, FX_STRSIZE nLen) { |
91 if (nLen > 0) { | 91 if (nLen > 0) { |
92 m_pData.Reset( | 92 m_pData.Reset( |
93 StringData::Create(reinterpret_cast<const FX_CHAR*>(pStr), nLen)); | 93 StringData::Create(reinterpret_cast<const FX_CHAR*>(pStr), nLen)); |
94 } | 94 } |
95 } | 95 } |
96 | 96 |
| 97 CFX_ByteString::CFX_ByteString() {} |
| 98 |
| 99 CFX_ByteString::CFX_ByteString(const CFX_ByteString& other) |
| 100 : m_pData(other.m_pData) {} |
| 101 |
| 102 CFX_ByteString::CFX_ByteString(CFX_ByteString&& other) { |
| 103 m_pData.Swap(other.m_pData); |
| 104 } |
| 105 |
97 CFX_ByteString::CFX_ByteString(char ch) { | 106 CFX_ByteString::CFX_ByteString(char ch) { |
98 m_pData.Reset(StringData::Create(1)); | 107 m_pData.Reset(StringData::Create(1)); |
99 m_pData->m_String[0] = ch; | 108 m_pData->m_String[0] = ch; |
100 } | 109 } |
101 | 110 |
| 111 CFX_ByteString::CFX_ByteString(const FX_CHAR* ptr) |
| 112 : CFX_ByteString(ptr, ptr ? FXSYS_strlen(ptr) : 0) {} |
| 113 |
102 CFX_ByteString::CFX_ByteString(const CFX_ByteStringC& stringSrc) { | 114 CFX_ByteString::CFX_ByteString(const CFX_ByteStringC& stringSrc) { |
103 if (!stringSrc.IsEmpty()) | 115 if (!stringSrc.IsEmpty()) |
104 m_pData.Reset(StringData::Create(stringSrc.c_str(), stringSrc.GetLength())); | 116 m_pData.Reset(StringData::Create(stringSrc.c_str(), stringSrc.GetLength())); |
105 } | 117 } |
106 | 118 |
107 CFX_ByteString::CFX_ByteString(const CFX_ByteStringC& str1, | 119 CFX_ByteString::CFX_ByteString(const CFX_ByteStringC& str1, |
108 const CFX_ByteStringC& str2) { | 120 const CFX_ByteStringC& str2) { |
109 int nNewLen = str1.GetLength() + str2.GetLength(); | 121 int nNewLen = str1.GetLength() + str2.GetLength(); |
110 if (nNewLen == 0) | 122 if (nNewLen == 0) |
111 return; | 123 return; |
112 | 124 |
113 m_pData.Reset(StringData::Create(nNewLen)); | 125 m_pData.Reset(StringData::Create(nNewLen)); |
114 m_pData->CopyContents(str1.c_str(), str1.GetLength()); | 126 m_pData->CopyContents(str1.c_str(), str1.GetLength()); |
115 m_pData->CopyContentsAt(str1.GetLength(), str2.c_str(), str2.GetLength()); | 127 m_pData->CopyContentsAt(str1.GetLength(), str2.c_str(), str2.GetLength()); |
116 } | 128 } |
117 | 129 |
118 CFX_ByteString::~CFX_ByteString() {} | 130 CFX_ByteString::~CFX_ByteString() {} |
119 | 131 |
| 132 void CFX_ByteString::clear() { |
| 133 m_pData.Reset(); |
| 134 } |
| 135 |
120 const CFX_ByteString& CFX_ByteString::operator=(const FX_CHAR* pStr) { | 136 const CFX_ByteString& CFX_ByteString::operator=(const FX_CHAR* pStr) { |
121 if (!pStr || !pStr[0]) | 137 if (!pStr || !pStr[0]) |
122 clear(); | 138 clear(); |
123 else | 139 else |
124 AssignCopy(pStr, FXSYS_strlen(pStr)); | 140 AssignCopy(pStr, FXSYS_strlen(pStr)); |
125 | 141 |
126 return *this; | 142 return *this; |
127 } | 143 } |
128 | 144 |
129 const CFX_ByteString& CFX_ByteString::operator=( | 145 const CFX_ByteString& CFX_ByteString::operator=( |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 return other.IsEmpty(); | 210 return other.IsEmpty(); |
195 | 211 |
196 if (other.IsEmpty()) | 212 if (other.IsEmpty()) |
197 return false; | 213 return false; |
198 | 214 |
199 return other.m_pData->m_nDataLength == m_pData->m_nDataLength && | 215 return other.m_pData->m_nDataLength == m_pData->m_nDataLength && |
200 FXSYS_memcmp(other.m_pData->m_String, m_pData->m_String, | 216 FXSYS_memcmp(other.m_pData->m_String, m_pData->m_String, |
201 m_pData->m_nDataLength) == 0; | 217 m_pData->m_nDataLength) == 0; |
202 } | 218 } |
203 | 219 |
| 220 bool CFX_ByteString::operator!=(const char* ptr) const { |
| 221 return !(*this == ptr); |
| 222 } |
| 223 |
| 224 bool CFX_ByteString::operator!=(const CFX_ByteStringC& str) const { |
| 225 return !(*this == str); |
| 226 } |
| 227 |
| 228 bool CFX_ByteString::operator!=(const CFX_ByteString& other) const { |
| 229 return !(*this == other); |
| 230 } |
| 231 |
| 232 bool CFX_ByteString::operator<(const CFX_ByteString& str) const { |
| 233 int result = FXSYS_memcmp(c_str(), str.c_str(), |
| 234 std::min(GetLength(), str.GetLength())); |
| 235 return result < 0 || (result == 0 && GetLength() < str.GetLength()); |
| 236 } |
| 237 |
| 238 uint8_t CFX_ByteString::GetAt(FX_STRSIZE nIndex) const { |
| 239 return m_pData ? m_pData->m_String[nIndex] : 0; |
| 240 } |
| 241 |
| 242 uint8_t CFX_ByteString::operator[](FX_STRSIZE nIndex) const { |
| 243 return m_pData ? m_pData->m_String[nIndex] : 0; |
| 244 } |
| 245 |
204 bool CFX_ByteString::EqualNoCase(const CFX_ByteStringC& str) const { | 246 bool CFX_ByteString::EqualNoCase(const CFX_ByteStringC& str) const { |
205 if (!m_pData) | 247 if (!m_pData) |
206 return str.IsEmpty(); | 248 return str.IsEmpty(); |
207 | 249 |
208 FX_STRSIZE len = str.GetLength(); | 250 FX_STRSIZE len = str.GetLength(); |
209 if (m_pData->m_nDataLength != len) | 251 if (m_pData->m_nDataLength != len) |
210 return false; | 252 return false; |
211 | 253 |
212 const uint8_t* pThis = (const uint8_t*)m_pData->m_String; | 254 const uint8_t* pThis = (const uint8_t*)m_pData->m_String; |
213 const uint8_t* pThat = str.raw_str(); | 255 const uint8_t* pThat = str.raw_str(); |
(...skipping 614 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
828 FX_STRSIZE len) { | 870 FX_STRSIZE len) { |
829 FX_STRSIZE str_len = len >= 0 ? len : FXSYS_wcslen(str); | 871 FX_STRSIZE str_len = len >= 0 ? len : FXSYS_wcslen(str); |
830 return FromUnicode(CFX_WideString(str, str_len)); | 872 return FromUnicode(CFX_WideString(str, str_len)); |
831 } | 873 } |
832 | 874 |
833 // static | 875 // static |
834 CFX_ByteString CFX_ByteString::FromUnicode(const CFX_WideString& str) { | 876 CFX_ByteString CFX_ByteString::FromUnicode(const CFX_WideString& str) { |
835 return CFX_CharMap::GetByteString(0, str.AsStringC()); | 877 return CFX_CharMap::GetByteString(0, str.AsStringC()); |
836 } | 878 } |
837 | 879 |
| 880 const FX_CHAR* CFX_ByteString::c_str() const { |
| 881 return m_pData ? m_pData->m_String : ""; |
| 882 } |
| 883 |
| 884 const uint8_t* CFX_ByteString::raw_str() const { |
| 885 return m_pData ? reinterpret_cast<const uint8_t*>(m_pData->m_String) |
| 886 : nullptr; |
| 887 } |
| 888 |
| 889 CFX_ByteStringC CFX_ByteString::AsStringC() const { |
| 890 return CFX_ByteStringC(raw_str(), GetLength()); |
| 891 } |
| 892 |
| 893 FX_STRSIZE CFX_ByteString::GetLength() const { |
| 894 return m_pData ? m_pData->m_nDataLength : 0; |
| 895 } |
| 896 |
| 897 bool CFX_ByteString::IsEmpty() const { |
| 898 return !GetLength(); |
| 899 } |
| 900 |
838 int CFX_ByteString::Compare(const CFX_ByteStringC& str) const { | 901 int CFX_ByteString::Compare(const CFX_ByteStringC& str) const { |
839 if (!m_pData) { | 902 if (!m_pData) { |
840 return str.IsEmpty() ? 0 : -1; | 903 return str.IsEmpty() ? 0 : -1; |
841 } | 904 } |
842 int this_len = m_pData->m_nDataLength; | 905 int this_len = m_pData->m_nDataLength; |
843 int that_len = str.GetLength(); | 906 int that_len = str.GetLength(); |
844 int min_len = this_len < that_len ? this_len : that_len; | 907 int min_len = this_len < that_len ? this_len : that_len; |
845 for (int i = 0; i < min_len; i++) { | 908 for (int i = 0; i < min_len; i++) { |
846 if ((uint8_t)m_pData->m_String[i] < str.GetAt(i)) { | 909 if ((uint8_t)m_pData->m_String[i] < str.GetAt(i)) { |
847 return -1; | 910 return -1; |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
976 fraction %= scale; | 1039 fraction %= scale; |
977 scale /= 10; | 1040 scale /= 10; |
978 } | 1041 } |
979 return buf_size; | 1042 return buf_size; |
980 } | 1043 } |
981 CFX_ByteString CFX_ByteString::FormatFloat(FX_FLOAT d, int precision) { | 1044 CFX_ByteString CFX_ByteString::FormatFloat(FX_FLOAT d, int precision) { |
982 FX_CHAR buf[32]; | 1045 FX_CHAR buf[32]; |
983 FX_STRSIZE len = FX_ftoa(d, buf); | 1046 FX_STRSIZE len = FX_ftoa(d, buf); |
984 return CFX_ByteString(buf, len); | 1047 return CFX_ByteString(buf, len); |
985 } | 1048 } |
OLD | NEW |