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 CORE_FXCRT_INCLUDE_FX_STRING_H_ | 7 #ifndef CORE_FXCRT_INCLUDE_FX_STRING_H_ |
8 #define CORE_FXCRT_INCLUDE_FX_STRING_H_ | 8 #define CORE_FXCRT_INCLUDE_FX_STRING_H_ |
9 | 9 |
10 #include <stdint.h> // For intptr_t. | 10 #include <stdint.h> // For intptr_t. |
11 #include <algorithm> | 11 #include <algorithm> |
12 | 12 |
13 #include "core/fxcrt/include/cfx_retain_ptr.h" | |
14 #include "core/fxcrt/include/fx_memory.h" | 13 #include "core/fxcrt/include/fx_memory.h" |
15 #include "core/fxcrt/include/fx_system.h" | 14 #include "core/fxcrt/include/fx_system.h" |
16 | 15 |
17 class CFX_BinaryBuf; | 16 class CFX_BinaryBuf; |
18 class CFX_ByteString; | 17 class CFX_ByteString; |
19 class CFX_WideString; | 18 class CFX_WideString; |
20 | 19 |
21 // An immutable string with caller-provided storage which must outlive the | 20 // An immutable string with caller-provided storage which must outlive the |
22 // string itself. | 21 // string itself. |
23 class CFX_ByteStringC { | 22 class CFX_ByteStringC { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 FXSYS_memcmp(other.m_Ptr, m_Ptr, m_Length) == 0; | 85 FXSYS_memcmp(other.m_Ptr, m_Ptr, m_Length) == 0; |
87 } | 86 } |
88 bool operator!=(const char* ptr) const { return !(*this == ptr); } | 87 bool operator!=(const char* ptr) const { return !(*this == ptr); } |
89 bool operator!=(const CFX_ByteStringC& other) const { | 88 bool operator!=(const CFX_ByteStringC& other) const { |
90 return !(*this == other); | 89 return !(*this == other); |
91 } | 90 } |
92 | 91 |
93 uint32_t GetID(FX_STRSIZE start_pos = 0) const; | 92 uint32_t GetID(FX_STRSIZE start_pos = 0) const; |
94 | 93 |
95 const uint8_t* GetPtr() const { return m_Ptr; } | 94 const uint8_t* GetPtr() const { return m_Ptr; } |
| 95 |
96 const FX_CHAR* GetCStr() const { return (const FX_CHAR*)m_Ptr; } | 96 const FX_CHAR* GetCStr() const { return (const FX_CHAR*)m_Ptr; } |
97 | 97 |
98 FX_STRSIZE GetLength() const { return m_Length; } | 98 FX_STRSIZE GetLength() const { return m_Length; } |
| 99 |
99 bool IsEmpty() const { return m_Length == 0; } | 100 bool IsEmpty() const { return m_Length == 0; } |
100 | 101 |
101 uint8_t GetAt(FX_STRSIZE index) const { return m_Ptr[index]; } | 102 uint8_t GetAt(FX_STRSIZE index) const { return m_Ptr[index]; } |
102 | 103 |
103 CFX_ByteStringC Mid(FX_STRSIZE index, FX_STRSIZE count = -1) const { | 104 CFX_ByteStringC Mid(FX_STRSIZE index, FX_STRSIZE count = -1) const { |
104 if (index < 0) { | 105 if (index < 0) { |
105 index = 0; | 106 index = 0; |
106 } | 107 } |
107 if (index > m_Length) { | 108 if (index > m_Length) { |
108 return CFX_ByteStringC(); | 109 return CFX_ByteStringC(); |
(...skipping 27 matching lines...) Expand all Loading... |
136 #define FXBSTR_ID(c1, c2, c3, c4) \ | 137 #define FXBSTR_ID(c1, c2, c3, c4) \ |
137 (((uint32_t)c1 << 24) | ((uint32_t)c2 << 16) | ((uint32_t)c3 << 8) | \ | 138 (((uint32_t)c1 << 24) | ((uint32_t)c2 << 16) | ((uint32_t)c3 << 8) | \ |
138 ((uint32_t)c4)) | 139 ((uint32_t)c4)) |
139 | 140 |
140 // A mutable string with shared buffers using copy-on-write semantics that | 141 // A mutable string with shared buffers using copy-on-write semantics that |
141 // avoids the cost of std::string's iterator stability guarantees. | 142 // avoids the cost of std::string's iterator stability guarantees. |
142 class CFX_ByteString { | 143 class CFX_ByteString { |
143 public: | 144 public: |
144 typedef FX_CHAR value_type; | 145 typedef FX_CHAR value_type; |
145 | 146 |
146 CFX_ByteString() {} | 147 CFX_ByteString() : m_pData(nullptr) {} |
147 CFX_ByteString(const CFX_ByteString& other) : m_pData(other.m_pData) {} | 148 |
148 CFX_ByteString(CFX_ByteString&& other) { m_pData.Swap(other.m_pData); } | 149 // Copy constructor. |
| 150 CFX_ByteString(const CFX_ByteString& str); |
| 151 |
| 152 // Move constructor. |
| 153 inline CFX_ByteString(CFX_ByteString&& other) { |
| 154 m_pData = other.m_pData; |
| 155 other.m_pData = nullptr; |
| 156 } |
149 | 157 |
150 CFX_ByteString(char ch); | 158 CFX_ByteString(char ch); |
151 CFX_ByteString(const FX_CHAR* ptr) | 159 CFX_ByteString(const FX_CHAR* ptr) |
152 : CFX_ByteString(ptr, ptr ? FXSYS_strlen(ptr) : 0) {} | 160 : CFX_ByteString(ptr, ptr ? FXSYS_strlen(ptr) : 0) {} |
153 | 161 |
154 CFX_ByteString(const FX_CHAR* ptr, FX_STRSIZE len); | 162 CFX_ByteString(const FX_CHAR* ptr, FX_STRSIZE len); |
155 CFX_ByteString(const uint8_t* ptr, FX_STRSIZE len); | 163 CFX_ByteString(const uint8_t* ptr, FX_STRSIZE len); |
156 | 164 |
157 CFX_ByteString(const CFX_ByteStringC& bstrc); | 165 CFX_ByteString(const CFX_ByteStringC& bstrc); |
158 CFX_ByteString(const CFX_ByteStringC& bstrc1, const CFX_ByteStringC& bstrc2); | 166 CFX_ByteString(const CFX_ByteStringC& bstrc1, const CFX_ByteStringC& bstrc2); |
159 | 167 |
160 ~CFX_ByteString(); | 168 ~CFX_ByteString(); |
161 | 169 |
162 // Deprecated -- use clear(). | 170 static CFX_ByteString FromUnicode(const FX_WCHAR* ptr, FX_STRSIZE len = -1); |
163 void Empty() { m_pData.Reset(); } | |
164 void clear() { m_pData.Reset(); } | |
165 | 171 |
166 static CFX_ByteString FromUnicode(const FX_WCHAR* ptr, FX_STRSIZE len = -1); | |
167 static CFX_ByteString FromUnicode(const CFX_WideString& str); | 172 static CFX_ByteString FromUnicode(const CFX_WideString& str); |
168 | 173 |
169 // Explicit conversion to C-style string. | 174 // Explicit conversion to C-style string. |
170 const FX_CHAR* c_str() const { return m_pData ? m_pData->m_String : ""; } | 175 const FX_CHAR* c_str() const { return m_pData ? m_pData->m_String : ""; } |
171 | 176 |
172 // Implicit conversion to C-style string -- deprecated. | 177 // Implicit conversion to C-style string -- deprecated. |
173 operator const FX_CHAR*() const { return m_pData ? m_pData->m_String : ""; } | 178 operator const FX_CHAR*() const { return m_pData ? m_pData->m_String : ""; } |
174 | 179 |
175 // Explicit conversion to uint8_t*. | 180 // Explicit conversion to uint8_t*. |
176 const uint8_t* raw_str() const { | 181 const uint8_t* raw_str() const { |
177 return m_pData ? reinterpret_cast<const uint8_t*>(m_pData->m_String) | 182 return m_pData ? reinterpret_cast<const uint8_t*>(m_pData->m_String) |
178 : nullptr; | 183 : nullptr; |
179 } | 184 } |
180 | 185 |
181 // Implicit conversiont to uint8_t* -- deprecated. | 186 // Implicit conversiont to uint8_t* -- deprecated. |
182 operator const uint8_t*() const { | 187 operator const uint8_t*() const { |
183 return m_pData ? reinterpret_cast<const uint8_t*>(m_pData->m_String) | 188 return m_pData ? reinterpret_cast<const uint8_t*>(m_pData->m_String) |
184 : nullptr; | 189 : nullptr; |
185 } | 190 } |
186 | 191 |
187 FX_STRSIZE GetLength() const { return m_pData ? m_pData->m_nDataLength : 0; } | 192 FX_STRSIZE GetLength() const { return m_pData ? m_pData->m_nDataLength : 0; } |
| 193 |
188 bool IsEmpty() const { return !GetLength(); } | 194 bool IsEmpty() const { return !GetLength(); } |
189 | 195 |
190 int Compare(const CFX_ByteStringC& str) const; | 196 int Compare(const CFX_ByteStringC& str) const; |
191 | 197 |
192 bool Equal(const char* ptr) const; | 198 bool Equal(const char* ptr) const; |
193 bool Equal(const CFX_ByteStringC& str) const; | 199 bool Equal(const CFX_ByteStringC& str) const; |
194 bool Equal(const CFX_ByteString& other) const; | 200 bool Equal(const CFX_ByteString& other) const; |
195 | 201 |
196 bool EqualNoCase(const CFX_ByteStringC& str) const; | 202 bool EqualNoCase(const CFX_ByteStringC& str) const; |
197 | 203 |
198 bool operator==(const char* ptr) const { return Equal(ptr); } | 204 bool operator==(const char* ptr) const { return Equal(ptr); } |
199 bool operator==(const CFX_ByteStringC& str) const { return Equal(str); } | 205 bool operator==(const CFX_ByteStringC& str) const { return Equal(str); } |
200 bool operator==(const CFX_ByteString& other) const { return Equal(other); } | 206 bool operator==(const CFX_ByteString& other) const { return Equal(other); } |
201 | 207 |
202 bool operator!=(const char* ptr) const { return !(*this == ptr); } | 208 bool operator!=(const char* ptr) const { return !(*this == ptr); } |
203 bool operator!=(const CFX_ByteStringC& str) const { return !(*this == str); } | 209 bool operator!=(const CFX_ByteStringC& str) const { return !(*this == str); } |
204 bool operator!=(const CFX_ByteString& other) const { | 210 bool operator!=(const CFX_ByteString& other) const { |
205 return !(*this == other); | 211 return !(*this == other); |
206 } | 212 } |
207 | 213 |
208 bool operator<(const CFX_ByteString& str) const { | 214 bool operator<(const CFX_ByteString& str) const { |
209 int result = FXSYS_memcmp(c_str(), str.c_str(), | 215 int result = FXSYS_memcmp(c_str(), str.c_str(), |
210 std::min(GetLength(), str.GetLength())); | 216 std::min(GetLength(), str.GetLength())); |
211 return result < 0 || (result == 0 && GetLength() < str.GetLength()); | 217 return result < 0 || (result == 0 && GetLength() < str.GetLength()); |
212 } | 218 } |
213 | 219 |
| 220 void Empty(); |
| 221 |
214 const CFX_ByteString& operator=(const FX_CHAR* str); | 222 const CFX_ByteString& operator=(const FX_CHAR* str); |
| 223 |
215 const CFX_ByteString& operator=(const CFX_ByteStringC& bstrc); | 224 const CFX_ByteString& operator=(const CFX_ByteStringC& bstrc); |
| 225 |
216 const CFX_ByteString& operator=(const CFX_ByteString& stringSrc); | 226 const CFX_ByteString& operator=(const CFX_ByteString& stringSrc); |
| 227 |
217 const CFX_ByteString& operator=(const CFX_BinaryBuf& buf); | 228 const CFX_ByteString& operator=(const CFX_BinaryBuf& buf); |
218 | 229 |
219 void Load(const uint8_t* str, FX_STRSIZE len); | 230 void Load(const uint8_t* str, FX_STRSIZE len); |
220 | 231 |
221 const CFX_ByteString& operator+=(FX_CHAR ch); | 232 const CFX_ByteString& operator+=(FX_CHAR ch); |
| 233 |
222 const CFX_ByteString& operator+=(const FX_CHAR* str); | 234 const CFX_ByteString& operator+=(const FX_CHAR* str); |
| 235 |
223 const CFX_ByteString& operator+=(const CFX_ByteString& str); | 236 const CFX_ByteString& operator+=(const CFX_ByteString& str); |
| 237 |
224 const CFX_ByteString& operator+=(const CFX_ByteStringC& bstrc); | 238 const CFX_ByteString& operator+=(const CFX_ByteStringC& bstrc); |
225 | 239 |
226 uint8_t GetAt(FX_STRSIZE nIndex) const { | 240 uint8_t GetAt(FX_STRSIZE nIndex) const { |
227 return m_pData ? m_pData->m_String[nIndex] : 0; | 241 return m_pData ? m_pData->m_String[nIndex] : 0; |
228 } | 242 } |
229 | 243 |
230 uint8_t operator[](FX_STRSIZE nIndex) const { | 244 uint8_t operator[](FX_STRSIZE nIndex) const { |
231 return m_pData ? m_pData->m_String[nIndex] : 0; | 245 return m_pData ? m_pData->m_String[nIndex] : 0; |
232 } | 246 } |
233 | 247 |
234 void SetAt(FX_STRSIZE nIndex, FX_CHAR ch); | 248 void SetAt(FX_STRSIZE nIndex, FX_CHAR ch); |
| 249 |
235 FX_STRSIZE Insert(FX_STRSIZE index, FX_CHAR ch); | 250 FX_STRSIZE Insert(FX_STRSIZE index, FX_CHAR ch); |
| 251 |
236 FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1); | 252 FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1); |
237 | 253 |
238 void Format(const FX_CHAR* lpszFormat, ...); | 254 void Format(const FX_CHAR* lpszFormat, ...); |
| 255 |
239 void FormatV(const FX_CHAR* lpszFormat, va_list argList); | 256 void FormatV(const FX_CHAR* lpszFormat, va_list argList); |
240 | 257 |
241 void Reserve(FX_STRSIZE len); | 258 void Reserve(FX_STRSIZE len); |
| 259 |
242 FX_CHAR* GetBuffer(FX_STRSIZE len); | 260 FX_CHAR* GetBuffer(FX_STRSIZE len); |
| 261 |
243 void ReleaseBuffer(FX_STRSIZE len = -1); | 262 void ReleaseBuffer(FX_STRSIZE len = -1); |
244 | 263 |
245 CFX_ByteString Mid(FX_STRSIZE first) const; | 264 CFX_ByteString Mid(FX_STRSIZE first) const; |
| 265 |
246 CFX_ByteString Mid(FX_STRSIZE first, FX_STRSIZE count) const; | 266 CFX_ByteString Mid(FX_STRSIZE first, FX_STRSIZE count) const; |
| 267 |
247 CFX_ByteString Left(FX_STRSIZE count) const; | 268 CFX_ByteString Left(FX_STRSIZE count) const; |
| 269 |
248 CFX_ByteString Right(FX_STRSIZE count) const; | 270 CFX_ByteString Right(FX_STRSIZE count) const; |
249 | 271 |
250 FX_STRSIZE Find(const CFX_ByteStringC& lpszSub, FX_STRSIZE start = 0) const; | 272 FX_STRSIZE Find(const CFX_ByteStringC& lpszSub, FX_STRSIZE start = 0) const; |
| 273 |
251 FX_STRSIZE Find(FX_CHAR ch, FX_STRSIZE start = 0) const; | 274 FX_STRSIZE Find(FX_CHAR ch, FX_STRSIZE start = 0) const; |
| 275 |
252 FX_STRSIZE ReverseFind(FX_CHAR ch) const; | 276 FX_STRSIZE ReverseFind(FX_CHAR ch) const; |
253 | 277 |
254 void MakeLower(); | 278 void MakeLower(); |
| 279 |
255 void MakeUpper(); | 280 void MakeUpper(); |
256 | 281 |
257 void TrimRight(); | 282 void TrimRight(); |
| 283 |
258 void TrimRight(FX_CHAR chTarget); | 284 void TrimRight(FX_CHAR chTarget); |
| 285 |
259 void TrimRight(const CFX_ByteStringC& lpszTargets); | 286 void TrimRight(const CFX_ByteStringC& lpszTargets); |
260 | 287 |
261 void TrimLeft(); | 288 void TrimLeft(); |
| 289 |
262 void TrimLeft(FX_CHAR chTarget); | 290 void TrimLeft(FX_CHAR chTarget); |
| 291 |
263 void TrimLeft(const CFX_ByteStringC& lpszTargets); | 292 void TrimLeft(const CFX_ByteStringC& lpszTargets); |
264 | 293 |
265 FX_STRSIZE Replace(const CFX_ByteStringC& lpszOld, | 294 FX_STRSIZE Replace(const CFX_ByteStringC& lpszOld, |
266 const CFX_ByteStringC& lpszNew); | 295 const CFX_ByteStringC& lpszNew); |
267 | 296 |
268 FX_STRSIZE Remove(FX_CHAR ch); | 297 FX_STRSIZE Remove(FX_CHAR ch); |
269 | 298 |
270 CFX_WideString UTF8Decode() const; | 299 CFX_WideString UTF8Decode() const; |
271 | 300 |
272 uint32_t GetID(FX_STRSIZE start_pos = 0) const; | 301 uint32_t GetID(FX_STRSIZE start_pos = 0) const; |
273 | 302 |
274 #define FXFORMAT_SIGNED 1 | 303 #define FXFORMAT_SIGNED 1 |
275 #define FXFORMAT_HEX 2 | 304 #define FXFORMAT_HEX 2 |
276 #define FXFORMAT_CAPITAL 4 | 305 #define FXFORMAT_CAPITAL 4 |
277 | 306 |
278 static CFX_ByteString FormatInteger(int i, uint32_t flags = 0); | 307 static CFX_ByteString FormatInteger(int i, uint32_t flags = 0); |
279 static CFX_ByteString FormatFloat(FX_FLOAT f, int precision = 0); | 308 static CFX_ByteString FormatFloat(FX_FLOAT f, int precision = 0); |
280 | 309 |
281 protected: | 310 protected: |
| 311 // To ensure ref counts do not overflow, consider the worst possible case: |
| 312 // the entire address space contains nothing but pointers to this object. |
| 313 // Since the count increments with each new pointer, the largest value is |
| 314 // the number of pointers that can fit into the address space. The size of |
| 315 // the address space itself is a good upper bound on it; we need not go |
| 316 // larger. |
282 class StringData { | 317 class StringData { |
283 public: | 318 public: |
284 static StringData* Create(FX_STRSIZE nLen); | 319 static StringData* Create(int nLen); |
285 static StringData* Create(const StringData& other); | |
286 static StringData* Create(const FX_CHAR* pStr, FX_STRSIZE nLen); | |
287 | |
288 void Retain() { ++m_nRefs; } | 320 void Retain() { ++m_nRefs; } |
289 void Release() { | 321 void Release() { |
290 if (--m_nRefs <= 0) | 322 if (--m_nRefs <= 0) |
291 FX_Free(this); | 323 FX_Free(this); |
292 } | 324 } |
293 | 325 |
294 bool CanOperateInPlace(FX_STRSIZE nTotalLen) const { | |
295 return m_nRefs <= 1 && nTotalLen <= m_nAllocLength; | |
296 } | |
297 | |
298 void CopyContents(const StringData& other); | |
299 void CopyContents(const FX_CHAR* pStr, FX_STRSIZE nLen); | |
300 void CopyContentsAt(FX_STRSIZE offset, | |
301 const FX_CHAR* pStr, | |
302 FX_STRSIZE nLen); | |
303 | |
304 // To ensure ref counts do not overflow, consider the worst possible case: | |
305 // the entire address space contains nothing but pointers to this object. | |
306 // Since the count increments with each new pointer, the largest value is | |
307 // the number of pointers that can fit into the address space. The size of | |
308 // the address space itself is a good upper bound on it. | |
309 intptr_t m_nRefs; // Would prefer ssize_t, but no windows support. | 326 intptr_t m_nRefs; // Would prefer ssize_t, but no windows support. |
310 | |
311 // |FX_STRSIZE| is currently typedef'd as |int|. | |
312 // TODO(palmer): It should be a |size_t|, or at least unsigned. | |
313 // These lengths do not include the terminating NUL, but the underlying | |
314 // buffer is sized to be capable of holding it. | |
315 FX_STRSIZE m_nDataLength; | 327 FX_STRSIZE m_nDataLength; |
316 FX_STRSIZE m_nAllocLength; | 328 FX_STRSIZE m_nAllocLength; |
317 | |
318 // Not really 1, variable size. | |
319 FX_CHAR m_String[1]; | 329 FX_CHAR m_String[1]; |
320 | 330 |
321 private: | 331 private: |
322 StringData(FX_STRSIZE dataLen, FX_STRSIZE allocLen); | 332 StringData(FX_STRSIZE dataLen, FX_STRSIZE allocLen) |
| 333 : m_nRefs(1), m_nDataLength(dataLen), m_nAllocLength(allocLen) { |
| 334 FXSYS_assert(dataLen >= 0); |
| 335 FXSYS_assert(allocLen >= 0); |
| 336 FXSYS_assert(dataLen <= allocLen); |
| 337 m_String[dataLen] = 0; |
| 338 } |
323 ~StringData() = delete; | 339 ~StringData() = delete; |
324 }; | 340 }; |
325 | 341 |
326 void CopyBeforeWrite(); | |
327 void AllocBeforeWrite(FX_STRSIZE nLen); | |
328 void AllocCopy(CFX_ByteString& dest, | 342 void AllocCopy(CFX_ByteString& dest, |
329 FX_STRSIZE nCopyLen, | 343 FX_STRSIZE nCopyLen, |
330 FX_STRSIZE nCopyIndex) const; | 344 FX_STRSIZE nCopyIndex) const; |
331 void AssignCopy(const FX_CHAR* pSrcData, FX_STRSIZE nSrcLen); | 345 void AssignCopy(FX_STRSIZE nSrcLen, const FX_CHAR* lpszSrcData); |
332 void Concat(const FX_CHAR* lpszSrcData, FX_STRSIZE nSrcLen); | 346 void ConcatCopy(FX_STRSIZE nSrc1Len, |
| 347 const FX_CHAR* lpszSrc1Data, |
| 348 FX_STRSIZE nSrc2Len, |
| 349 const FX_CHAR* lpszSrc2Data); |
| 350 void ConcatInPlace(FX_STRSIZE nSrcLen, const FX_CHAR* lpszSrcData); |
| 351 void CopyBeforeWrite(); |
| 352 void AllocBeforeWrite(FX_STRSIZE nLen); |
333 | 353 |
334 CFX_RetainPtr<StringData> m_pData; | 354 StringData* m_pData; |
335 friend class fxcrt_ByteStringConcat_Test; | 355 friend class fxcrt_ByteStringConcatInPlace_Test; |
336 }; | 356 }; |
337 | |
338 inline CFX_ByteStringC::CFX_ByteStringC(const CFX_ByteString& src) { | 357 inline CFX_ByteStringC::CFX_ByteStringC(const CFX_ByteString& src) { |
339 m_Ptr = (const uint8_t*)src; | 358 m_Ptr = (const uint8_t*)src; |
340 m_Length = src.GetLength(); | 359 m_Length = src.GetLength(); |
341 } | 360 } |
342 inline CFX_ByteStringC& CFX_ByteStringC::operator=(const CFX_ByteString& src) { | 361 inline CFX_ByteStringC& CFX_ByteStringC::operator=(const CFX_ByteString& src) { |
343 m_Ptr = (const uint8_t*)src; | 362 m_Ptr = (const uint8_t*)src; |
344 m_Length = src.GetLength(); | 363 m_Length = src.GetLength(); |
345 return *this; | 364 return *this; |
346 } | 365 } |
347 | 366 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 return CFX_ByteString(str1, str2); | 414 return CFX_ByteString(str1, str2); |
396 } | 415 } |
397 inline CFX_ByteString operator+(const CFX_ByteString& str1, | 416 inline CFX_ByteString operator+(const CFX_ByteString& str1, |
398 const CFX_ByteStringC& str2) { | 417 const CFX_ByteStringC& str2) { |
399 return CFX_ByteString(str1, str2); | 418 return CFX_ByteString(str1, str2); |
400 } | 419 } |
401 inline CFX_ByteString operator+(const CFX_ByteStringC& str1, | 420 inline CFX_ByteString operator+(const CFX_ByteStringC& str1, |
402 const CFX_ByteString& str2) { | 421 const CFX_ByteString& str2) { |
403 return CFX_ByteString(str1, str2); | 422 return CFX_ByteString(str1, str2); |
404 } | 423 } |
405 | |
406 class CFX_WideStringC { | 424 class CFX_WideStringC { |
407 public: | 425 public: |
408 typedef FX_WCHAR value_type; | 426 typedef FX_WCHAR value_type; |
409 | 427 |
410 CFX_WideStringC() { | 428 CFX_WideStringC() { |
411 m_Ptr = NULL; | 429 m_Ptr = NULL; |
412 m_Length = 0; | 430 m_Length = 0; |
413 } | 431 } |
414 | 432 |
415 CFX_WideStringC(const FX_WCHAR* ptr) { | 433 CFX_WideStringC(const FX_WCHAR* ptr) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 } | 471 } |
454 bool operator==(const CFX_WideStringC& str) const { | 472 bool operator==(const CFX_WideStringC& str) const { |
455 return str.m_Length == m_Length && wmemcmp(str.m_Ptr, m_Ptr, m_Length) == 0; | 473 return str.m_Length == m_Length && wmemcmp(str.m_Ptr, m_Ptr, m_Length) == 0; |
456 } | 474 } |
457 bool operator!=(const wchar_t* ptr) const { return !(*this == ptr); } | 475 bool operator!=(const wchar_t* ptr) const { return !(*this == ptr); } |
458 bool operator!=(const CFX_WideStringC& str) const { return !(*this == str); } | 476 bool operator!=(const CFX_WideStringC& str) const { return !(*this == str); } |
459 | 477 |
460 const FX_WCHAR* GetPtr() const { return m_Ptr; } | 478 const FX_WCHAR* GetPtr() const { return m_Ptr; } |
461 | 479 |
462 FX_STRSIZE GetLength() const { return m_Length; } | 480 FX_STRSIZE GetLength() const { return m_Length; } |
| 481 |
463 bool IsEmpty() const { return m_Length == 0; } | 482 bool IsEmpty() const { return m_Length == 0; } |
464 | 483 |
465 FX_WCHAR GetAt(FX_STRSIZE index) const { return m_Ptr[index]; } | 484 FX_WCHAR GetAt(FX_STRSIZE index) const { return m_Ptr[index]; } |
466 | 485 |
467 CFX_WideStringC Left(FX_STRSIZE count) const { | 486 CFX_WideStringC Left(FX_STRSIZE count) const { |
468 if (count < 1) { | 487 if (count < 1) { |
469 return CFX_WideStringC(); | 488 return CFX_WideStringC(); |
470 } | 489 } |
471 if (count > m_Length) { | 490 if (count > m_Length) { |
472 count = m_Length; | 491 count = m_Length; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
504 return result < 0 || (result == 0 && m_Length < that.m_Length); | 523 return result < 0 || (result == 0 && m_Length < that.m_Length); |
505 } | 524 } |
506 | 525 |
507 protected: | 526 protected: |
508 const FX_WCHAR* m_Ptr; | 527 const FX_WCHAR* m_Ptr; |
509 FX_STRSIZE m_Length; | 528 FX_STRSIZE m_Length; |
510 | 529 |
511 private: | 530 private: |
512 void* operator new(size_t) throw() { return NULL; } | 531 void* operator new(size_t) throw() { return NULL; } |
513 }; | 532 }; |
514 | |
515 inline bool operator==(const wchar_t* lhs, const CFX_WideStringC& rhs) { | 533 inline bool operator==(const wchar_t* lhs, const CFX_WideStringC& rhs) { |
516 return rhs == lhs; | 534 return rhs == lhs; |
517 } | 535 } |
518 inline bool operator!=(const wchar_t* lhs, const CFX_WideStringC& rhs) { | 536 inline bool operator!=(const wchar_t* lhs, const CFX_WideStringC& rhs) { |
519 return rhs != lhs; | 537 return rhs != lhs; |
520 } | 538 } |
521 #define FX_WSTRC(wstr) CFX_WideStringC(wstr, FX_ArraySize(wstr) - 1) | 539 #define FX_WSTRC(wstr) CFX_WideStringC(wstr, FX_ArraySize(wstr) - 1) |
522 | 540 |
523 // A mutable string with shared buffers using copy-on-write semantics that | 541 // A mutable string with shared buffers using copy-on-write semantics that |
524 // avoids the cost of std::string's iterator stability guarantees. | 542 // avoids the cost of std::string's iterator stability guarantees. |
525 class CFX_WideString { | 543 class CFX_WideString { |
526 public: | 544 public: |
527 typedef FX_WCHAR value_type; | 545 typedef FX_WCHAR value_type; |
528 | 546 |
529 CFX_WideString() : m_pData(nullptr) {} | 547 CFX_WideString() : m_pData(nullptr) {} |
530 | 548 |
531 // Copy constructor. | 549 // Copy constructor. |
532 CFX_WideString(const CFX_WideString& str); | 550 CFX_WideString(const CFX_WideString& str); |
533 | 551 |
534 // Move constructor. | 552 // Move constructor. |
535 CFX_WideString(CFX_WideString&& other) { | 553 inline CFX_WideString(CFX_WideString&& other) { |
536 m_pData = other.m_pData; | 554 m_pData = other.m_pData; |
537 other.m_pData = nullptr; | 555 other.m_pData = nullptr; |
538 } | 556 } |
539 | 557 |
540 CFX_WideString(const FX_WCHAR* ptr) | 558 CFX_WideString(const FX_WCHAR* ptr) |
541 : CFX_WideString(ptr, ptr ? FXSYS_wcslen(ptr) : 0) {} | 559 : CFX_WideString(ptr, ptr ? FXSYS_wcslen(ptr) : 0) {} |
542 | 560 |
543 CFX_WideString(const FX_WCHAR* ptr, FX_STRSIZE len); | 561 CFX_WideString(const FX_WCHAR* ptr, FX_STRSIZE len); |
| 562 |
544 CFX_WideString(FX_WCHAR ch); | 563 CFX_WideString(FX_WCHAR ch); |
545 | 564 |
546 CFX_WideString(const CFX_WideStringC& str); | 565 CFX_WideString(const CFX_WideStringC& str); |
| 566 |
547 CFX_WideString(const CFX_WideStringC& str1, const CFX_WideStringC& str2); | 567 CFX_WideString(const CFX_WideStringC& str1, const CFX_WideStringC& str2); |
548 | 568 |
549 ~CFX_WideString(); | 569 ~CFX_WideString(); |
550 | 570 |
551 static CFX_WideString FromLocal(const CFX_ByteString& str); | 571 static CFX_WideString FromLocal(const CFX_ByteString& str); |
| 572 |
552 static CFX_WideString FromCodePage(const CFX_ByteString& str, | 573 static CFX_WideString FromCodePage(const CFX_ByteString& str, |
553 uint16_t codepage); | 574 uint16_t codepage); |
554 | 575 |
555 static CFX_WideString FromUTF8(const char* str, FX_STRSIZE len); | 576 static CFX_WideString FromUTF8(const char* str, FX_STRSIZE len); |
| 577 |
556 static CFX_WideString FromUTF16LE(const unsigned short* str, FX_STRSIZE len); | 578 static CFX_WideString FromUTF16LE(const unsigned short* str, FX_STRSIZE len); |
557 | 579 |
558 static FX_STRSIZE WStringLength(const unsigned short* str); | 580 static FX_STRSIZE WStringLength(const unsigned short* str); |
559 | 581 |
560 // Explicit conversion to C-style wide string. | 582 // Explicit conversion to C-style wide string. |
561 const FX_WCHAR* c_str() const { return m_pData ? m_pData->m_String : L""; } | 583 const FX_WCHAR* c_str() const { return m_pData ? m_pData->m_String : L""; } |
562 | 584 |
563 // Implicit conversion to C-style wide string -- deprecated. | 585 // Implicit conversion to C-style wide string -- deprecated. |
564 operator const FX_WCHAR*() const { return m_pData ? m_pData->m_String : L""; } | 586 operator const FX_WCHAR*() const { return m_pData ? m_pData->m_String : L""; } |
565 | 587 |
566 void Empty(); | 588 void Empty(); |
567 | 589 |
568 bool IsEmpty() const { return !GetLength(); } | 590 bool IsEmpty() const { return !GetLength(); } |
| 591 |
569 FX_STRSIZE GetLength() const { return m_pData ? m_pData->m_nDataLength : 0; } | 592 FX_STRSIZE GetLength() const { return m_pData ? m_pData->m_nDataLength : 0; } |
570 | 593 |
571 const CFX_WideString& operator=(const FX_WCHAR* str); | 594 const CFX_WideString& operator=(const FX_WCHAR* str); |
| 595 |
572 const CFX_WideString& operator=(const CFX_WideString& stringSrc); | 596 const CFX_WideString& operator=(const CFX_WideString& stringSrc); |
| 597 |
573 const CFX_WideString& operator=(const CFX_WideStringC& stringSrc); | 598 const CFX_WideString& operator=(const CFX_WideStringC& stringSrc); |
574 | 599 |
575 const CFX_WideString& operator+=(const FX_WCHAR* str); | 600 const CFX_WideString& operator+=(const FX_WCHAR* str); |
| 601 |
576 const CFX_WideString& operator+=(FX_WCHAR ch); | 602 const CFX_WideString& operator+=(FX_WCHAR ch); |
| 603 |
577 const CFX_WideString& operator+=(const CFX_WideString& str); | 604 const CFX_WideString& operator+=(const CFX_WideString& str); |
| 605 |
578 const CFX_WideString& operator+=(const CFX_WideStringC& str); | 606 const CFX_WideString& operator+=(const CFX_WideStringC& str); |
579 | 607 |
580 bool operator==(const wchar_t* ptr) const { return Equal(ptr); } | 608 bool operator==(const wchar_t* ptr) const { return Equal(ptr); } |
581 bool operator==(const CFX_WideStringC& str) const { return Equal(str); } | 609 bool operator==(const CFX_WideStringC& str) const { return Equal(str); } |
582 bool operator==(const CFX_WideString& other) const { return Equal(other); } | 610 bool operator==(const CFX_WideString& other) const { return Equal(other); } |
583 | 611 |
584 bool operator!=(const wchar_t* ptr) const { return !(*this == ptr); } | 612 bool operator!=(const wchar_t* ptr) const { return !(*this == ptr); } |
585 bool operator!=(const CFX_WideStringC& str) const { return !(*this == str); } | 613 bool operator!=(const CFX_WideStringC& str) const { return !(*this == str); } |
586 bool operator!=(const CFX_WideString& other) const { | 614 bool operator!=(const CFX_WideString& other) const { |
587 return !(*this == other); | 615 return !(*this == other); |
588 } | 616 } |
589 | 617 |
590 bool operator<(const CFX_WideString& str) const { | 618 bool operator<(const CFX_WideString& str) const { |
591 int result = | 619 int result = |
592 wmemcmp(c_str(), str.c_str(), std::min(GetLength(), str.GetLength())); | 620 wmemcmp(c_str(), str.c_str(), std::min(GetLength(), str.GetLength())); |
593 return result < 0 || (result == 0 && GetLength() < str.GetLength()); | 621 return result < 0 || (result == 0 && GetLength() < str.GetLength()); |
594 } | 622 } |
595 | 623 |
596 FX_WCHAR GetAt(FX_STRSIZE nIndex) const { | 624 FX_WCHAR GetAt(FX_STRSIZE nIndex) const { |
597 return m_pData ? m_pData->m_String[nIndex] : 0; | 625 return m_pData ? m_pData->m_String[nIndex] : 0; |
598 } | 626 } |
599 | 627 |
600 FX_WCHAR operator[](FX_STRSIZE nIndex) const { | 628 FX_WCHAR operator[](FX_STRSIZE nIndex) const { |
601 return m_pData ? m_pData->m_String[nIndex] : 0; | 629 return m_pData ? m_pData->m_String[nIndex] : 0; |
602 } | 630 } |
603 | 631 |
604 void SetAt(FX_STRSIZE nIndex, FX_WCHAR ch); | 632 void SetAt(FX_STRSIZE nIndex, FX_WCHAR ch); |
605 | 633 |
606 int Compare(const FX_WCHAR* str) const; | 634 int Compare(const FX_WCHAR* str) const; |
| 635 |
607 int Compare(const CFX_WideString& str) const; | 636 int Compare(const CFX_WideString& str) const; |
| 637 |
608 int CompareNoCase(const FX_WCHAR* str) const; | 638 int CompareNoCase(const FX_WCHAR* str) const; |
609 | 639 |
610 bool Equal(const wchar_t* ptr) const; | 640 bool Equal(const wchar_t* ptr) const; |
611 bool Equal(const CFX_WideStringC& str) const; | 641 bool Equal(const CFX_WideStringC& str) const; |
612 bool Equal(const CFX_WideString& other) const; | 642 bool Equal(const CFX_WideString& other) const; |
613 | 643 |
614 CFX_WideString Mid(FX_STRSIZE first) const; | 644 CFX_WideString Mid(FX_STRSIZE first) const; |
| 645 |
615 CFX_WideString Mid(FX_STRSIZE first, FX_STRSIZE count) const; | 646 CFX_WideString Mid(FX_STRSIZE first, FX_STRSIZE count) const; |
| 647 |
616 CFX_WideString Left(FX_STRSIZE count) const; | 648 CFX_WideString Left(FX_STRSIZE count) const; |
| 649 |
617 CFX_WideString Right(FX_STRSIZE count) const; | 650 CFX_WideString Right(FX_STRSIZE count) const; |
618 | 651 |
619 FX_STRSIZE Insert(FX_STRSIZE index, FX_WCHAR ch); | 652 FX_STRSIZE Insert(FX_STRSIZE index, FX_WCHAR ch); |
| 653 |
620 FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1); | 654 FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1); |
621 | 655 |
622 void Format(const FX_WCHAR* lpszFormat, ...); | 656 void Format(const FX_WCHAR* lpszFormat, ...); |
| 657 |
623 void FormatV(const FX_WCHAR* lpszFormat, va_list argList); | 658 void FormatV(const FX_WCHAR* lpszFormat, va_list argList); |
624 | 659 |
625 void MakeLower(); | 660 void MakeLower(); |
| 661 |
626 void MakeUpper(); | 662 void MakeUpper(); |
627 | 663 |
628 void TrimRight(); | 664 void TrimRight(); |
| 665 |
629 void TrimRight(FX_WCHAR chTarget); | 666 void TrimRight(FX_WCHAR chTarget); |
| 667 |
630 void TrimRight(const FX_WCHAR* lpszTargets); | 668 void TrimRight(const FX_WCHAR* lpszTargets); |
631 | 669 |
632 void TrimLeft(); | 670 void TrimLeft(); |
| 671 |
633 void TrimLeft(FX_WCHAR chTarget); | 672 void TrimLeft(FX_WCHAR chTarget); |
| 673 |
634 void TrimLeft(const FX_WCHAR* lpszTargets); | 674 void TrimLeft(const FX_WCHAR* lpszTargets); |
635 | 675 |
636 void Reserve(FX_STRSIZE len); | 676 void Reserve(FX_STRSIZE len); |
| 677 |
637 FX_WCHAR* GetBuffer(FX_STRSIZE len); | 678 FX_WCHAR* GetBuffer(FX_STRSIZE len); |
| 679 |
638 void ReleaseBuffer(FX_STRSIZE len = -1); | 680 void ReleaseBuffer(FX_STRSIZE len = -1); |
639 | 681 |
640 int GetInteger() const; | 682 int GetInteger() const; |
| 683 |
641 FX_FLOAT GetFloat() const; | 684 FX_FLOAT GetFloat() const; |
642 | 685 |
643 FX_STRSIZE Find(const FX_WCHAR* lpszSub, FX_STRSIZE start = 0) const; | 686 FX_STRSIZE Find(const FX_WCHAR* lpszSub, FX_STRSIZE start = 0) const; |
| 687 |
644 FX_STRSIZE Find(FX_WCHAR ch, FX_STRSIZE start = 0) const; | 688 FX_STRSIZE Find(FX_WCHAR ch, FX_STRSIZE start = 0) const; |
| 689 |
645 FX_STRSIZE Replace(const FX_WCHAR* lpszOld, const FX_WCHAR* lpszNew); | 690 FX_STRSIZE Replace(const FX_WCHAR* lpszOld, const FX_WCHAR* lpszNew); |
| 691 |
646 FX_STRSIZE Remove(FX_WCHAR ch); | 692 FX_STRSIZE Remove(FX_WCHAR ch); |
647 | 693 |
648 CFX_ByteString UTF8Encode() const; | 694 CFX_ByteString UTF8Encode() const; |
| 695 |
649 CFX_ByteString UTF16LE_Encode() const; | 696 CFX_ByteString UTF16LE_Encode() const; |
650 | 697 |
651 protected: | 698 protected: |
652 class StringData { | 699 class StringData { |
653 public: | 700 public: |
654 static StringData* Create(int nLen); | 701 static StringData* Create(int nLen); |
655 void Retain() { ++m_nRefs; } | 702 void Retain() { ++m_nRefs; } |
656 void Release() { | 703 void Release() { |
657 if (--m_nRefs <= 0) | 704 if (--m_nRefs <= 0) |
658 FX_Free(this); | 705 FX_Free(this); |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
765 } | 812 } |
766 | 813 |
767 FX_FLOAT FX_atof(const CFX_ByteStringC& str); | 814 FX_FLOAT FX_atof(const CFX_ByteStringC& str); |
768 inline FX_FLOAT FX_atof(const CFX_WideStringC& wsStr) { | 815 inline FX_FLOAT FX_atof(const CFX_WideStringC& wsStr) { |
769 return FX_atof(FX_UTF8Encode(wsStr.GetPtr(), wsStr.GetLength())); | 816 return FX_atof(FX_UTF8Encode(wsStr.GetPtr(), wsStr.GetLength())); |
770 } | 817 } |
771 void FX_atonum(const CFX_ByteStringC& str, FX_BOOL& bInteger, void* pData); | 818 void FX_atonum(const CFX_ByteStringC& str, FX_BOOL& bInteger, void* pData); |
772 FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_CHAR* buf); | 819 FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_CHAR* buf); |
773 | 820 |
774 #endif // CORE_FXCRT_INCLUDE_FX_STRING_H_ | 821 #endif // CORE_FXCRT_INCLUDE_FX_STRING_H_ |
OLD | NEW |