OLD | NEW |
| (Empty) |
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. | |
4 | |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
6 | |
7 #ifndef CORE_INCLUDE_FXCRT_FX_STRING_H_ | |
8 #define CORE_INCLUDE_FXCRT_FX_STRING_H_ | |
9 | |
10 #include <stdint.h> // For intptr_t. | |
11 #include <algorithm> | |
12 | |
13 #include "core/include/fxcrt/fx_memory.h" | |
14 #include "core/include/fxcrt/fx_system.h" | |
15 | |
16 class CFX_BinaryBuf; | |
17 class CFX_ByteString; | |
18 class CFX_WideString; | |
19 | |
20 // An immutable string with caller-provided storage which must outlive the | |
21 // string itself. | |
22 class CFX_ByteStringC { | |
23 public: | |
24 typedef FX_CHAR value_type; | |
25 | |
26 CFX_ByteStringC() { | |
27 m_Ptr = NULL; | |
28 m_Length = 0; | |
29 } | |
30 | |
31 CFX_ByteStringC(const uint8_t* ptr, FX_STRSIZE size) { | |
32 m_Ptr = ptr; | |
33 m_Length = size; | |
34 } | |
35 | |
36 CFX_ByteStringC(const FX_CHAR* ptr) { | |
37 m_Ptr = (const uint8_t*)ptr; | |
38 m_Length = ptr ? FXSYS_strlen(ptr) : 0; | |
39 } | |
40 | |
41 // |ch| must be an lvalue that outlives the the CFX_ByteStringC. However, | |
42 // the use of char rvalues are not caught at compile time. They are | |
43 // implicitly promoted to CFX_ByteString (see below) and then the | |
44 // CFX_ByteStringC is constructed from the CFX_ByteString via the alternate | |
45 // constructor below. The CFX_ByteString then typically goes out of scope | |
46 // and |m_Ptr| may be left pointing to invalid memory. Beware. | |
47 // TODO(tsepez): Mark single-argument string constructors as explicit. | |
48 CFX_ByteStringC(FX_CHAR& ch) { | |
49 m_Ptr = (const uint8_t*)&ch; | |
50 m_Length = 1; | |
51 } | |
52 | |
53 CFX_ByteStringC(const FX_CHAR* ptr, FX_STRSIZE len) { | |
54 m_Ptr = (const uint8_t*)ptr; | |
55 m_Length = (len == -1) ? FXSYS_strlen(ptr) : len; | |
56 } | |
57 | |
58 CFX_ByteStringC(const CFX_ByteStringC& src) { | |
59 m_Ptr = src.m_Ptr; | |
60 m_Length = src.m_Length; | |
61 } | |
62 | |
63 CFX_ByteStringC(const CFX_ByteString& src); | |
64 | |
65 CFX_ByteStringC& operator=(const FX_CHAR* src) { | |
66 m_Ptr = (const uint8_t*)src; | |
67 m_Length = m_Ptr ? FXSYS_strlen(src) : 0; | |
68 return *this; | |
69 } | |
70 | |
71 CFX_ByteStringC& operator=(const CFX_ByteStringC& src) { | |
72 m_Ptr = src.m_Ptr; | |
73 m_Length = src.m_Length; | |
74 return *this; | |
75 } | |
76 | |
77 CFX_ByteStringC& operator=(const CFX_ByteString& src); | |
78 | |
79 bool operator==(const char* ptr) const { | |
80 return FXSYS_strlen(ptr) == m_Length && | |
81 FXSYS_memcmp(ptr, m_Ptr, m_Length) == 0; | |
82 } | |
83 bool operator==(const CFX_ByteStringC& other) const { | |
84 return other.m_Length == m_Length && | |
85 FXSYS_memcmp(other.m_Ptr, m_Ptr, m_Length) == 0; | |
86 } | |
87 bool operator!=(const char* ptr) const { return !(*this == ptr); } | |
88 bool operator!=(const CFX_ByteStringC& other) const { | |
89 return !(*this == other); | |
90 } | |
91 | |
92 FX_DWORD GetID(FX_STRSIZE start_pos = 0) const; | |
93 | |
94 const uint8_t* GetPtr() const { return m_Ptr; } | |
95 | |
96 const FX_CHAR* GetCStr() const { return (const FX_CHAR*)m_Ptr; } | |
97 | |
98 FX_STRSIZE GetLength() const { return m_Length; } | |
99 | |
100 bool IsEmpty() const { return m_Length == 0; } | |
101 | |
102 uint8_t GetAt(FX_STRSIZE index) const { return m_Ptr[index]; } | |
103 | |
104 CFX_ByteStringC Mid(FX_STRSIZE index, FX_STRSIZE count = -1) const { | |
105 if (index < 0) { | |
106 index = 0; | |
107 } | |
108 if (index > m_Length) { | |
109 return CFX_ByteStringC(); | |
110 } | |
111 if (count < 0 || count > m_Length - index) { | |
112 count = m_Length - index; | |
113 } | |
114 return CFX_ByteStringC(m_Ptr + index, count); | |
115 } | |
116 | |
117 const uint8_t& operator[](size_t index) const { return m_Ptr[index]; } | |
118 | |
119 bool operator<(const CFX_ByteStringC& that) const { | |
120 int result = memcmp(m_Ptr, that.m_Ptr, std::min(m_Length, that.m_Length)); | |
121 return result < 0 || (result == 0 && m_Length < that.m_Length); | |
122 } | |
123 | |
124 protected: | |
125 const uint8_t* m_Ptr; | |
126 FX_STRSIZE m_Length; | |
127 | |
128 private: | |
129 void* operator new(size_t) throw() { return NULL; } | |
130 }; | |
131 inline bool operator==(const char* lhs, const CFX_ByteStringC& rhs) { | |
132 return rhs == lhs; | |
133 } | |
134 inline bool operator!=(const char* lhs, const CFX_ByteStringC& rhs) { | |
135 return rhs != lhs; | |
136 } | |
137 #define FXBSTR_ID(c1, c2, c3, c4) ((c1 << 24) | (c2 << 16) | (c3 << 8) | (c4)) | |
138 | |
139 // A mutable string with shared buffers using copy-on-write semantics that | |
140 // avoids the cost of std::string's iterator stability guarantees. | |
141 class CFX_ByteString { | |
142 public: | |
143 typedef FX_CHAR value_type; | |
144 | |
145 CFX_ByteString() : m_pData(nullptr) {} | |
146 | |
147 // Copy constructor. | |
148 CFX_ByteString(const CFX_ByteString& str); | |
149 | |
150 // Move constructor. | |
151 inline CFX_ByteString(CFX_ByteString&& other) { | |
152 m_pData = other.m_pData; | |
153 other.m_pData = nullptr; | |
154 } | |
155 | |
156 CFX_ByteString(char ch); | |
157 CFX_ByteString(const FX_CHAR* ptr) | |
158 : CFX_ByteString(ptr, ptr ? FXSYS_strlen(ptr) : 0) {} | |
159 | |
160 CFX_ByteString(const FX_CHAR* ptr, FX_STRSIZE len); | |
161 CFX_ByteString(const uint8_t* ptr, FX_STRSIZE len); | |
162 | |
163 CFX_ByteString(const CFX_ByteStringC& bstrc); | |
164 CFX_ByteString(const CFX_ByteStringC& bstrc1, const CFX_ByteStringC& bstrc2); | |
165 | |
166 ~CFX_ByteString(); | |
167 | |
168 static CFX_ByteString FromUnicode(const FX_WCHAR* ptr, FX_STRSIZE len = -1); | |
169 | |
170 static CFX_ByteString FromUnicode(const CFX_WideString& str); | |
171 | |
172 // Explicit conversion to C-style string. | |
173 const FX_CHAR* c_str() const { return m_pData ? m_pData->m_String : ""; } | |
174 | |
175 // Implicit conversion to C-style string -- deprecated. | |
176 operator const FX_CHAR*() const { return m_pData ? m_pData->m_String : ""; } | |
177 | |
178 // Explicit conversion to uint8_t*. | |
179 const uint8_t* raw_str() const { | |
180 return m_pData ? reinterpret_cast<const uint8_t*>(m_pData->m_String) | |
181 : nullptr; | |
182 } | |
183 | |
184 // Implicit conversiont to uint8_t* -- deprecated. | |
185 operator const uint8_t*() const { | |
186 return m_pData ? reinterpret_cast<const uint8_t*>(m_pData->m_String) | |
187 : nullptr; | |
188 } | |
189 | |
190 FX_STRSIZE GetLength() const { return m_pData ? m_pData->m_nDataLength : 0; } | |
191 | |
192 bool IsEmpty() const { return !GetLength(); } | |
193 | |
194 int Compare(const CFX_ByteStringC& str) const; | |
195 | |
196 bool Equal(const char* ptr) const; | |
197 bool Equal(const CFX_ByteStringC& str) const; | |
198 bool Equal(const CFX_ByteString& other) const; | |
199 | |
200 bool EqualNoCase(const CFX_ByteStringC& str) const; | |
201 | |
202 bool operator==(const char* ptr) const { return Equal(ptr); } | |
203 bool operator==(const CFX_ByteStringC& str) const { return Equal(str); } | |
204 bool operator==(const CFX_ByteString& other) const { return Equal(other); } | |
205 | |
206 bool operator!=(const char* ptr) const { return !(*this == ptr); } | |
207 bool operator!=(const CFX_ByteStringC& str) const { return !(*this == str); } | |
208 bool operator!=(const CFX_ByteString& other) const { | |
209 return !(*this == other); | |
210 } | |
211 | |
212 bool operator<(const CFX_ByteString& str) const { | |
213 int result = FXSYS_memcmp(c_str(), str.c_str(), | |
214 std::min(GetLength(), str.GetLength())); | |
215 return result < 0 || (result == 0 && GetLength() < str.GetLength()); | |
216 } | |
217 | |
218 void Empty(); | |
219 | |
220 const CFX_ByteString& operator=(const FX_CHAR* str); | |
221 | |
222 const CFX_ByteString& operator=(const CFX_ByteStringC& bstrc); | |
223 | |
224 const CFX_ByteString& operator=(const CFX_ByteString& stringSrc); | |
225 | |
226 const CFX_ByteString& operator=(const CFX_BinaryBuf& buf); | |
227 | |
228 void Load(const uint8_t* str, FX_STRSIZE len); | |
229 | |
230 const CFX_ByteString& operator+=(FX_CHAR ch); | |
231 | |
232 const CFX_ByteString& operator+=(const FX_CHAR* str); | |
233 | |
234 const CFX_ByteString& operator+=(const CFX_ByteString& str); | |
235 | |
236 const CFX_ByteString& operator+=(const CFX_ByteStringC& bstrc); | |
237 | |
238 uint8_t GetAt(FX_STRSIZE nIndex) const { | |
239 return m_pData ? m_pData->m_String[nIndex] : 0; | |
240 } | |
241 | |
242 uint8_t operator[](FX_STRSIZE nIndex) const { | |
243 return m_pData ? m_pData->m_String[nIndex] : 0; | |
244 } | |
245 | |
246 void SetAt(FX_STRSIZE nIndex, FX_CHAR ch); | |
247 | |
248 FX_STRSIZE Insert(FX_STRSIZE index, FX_CHAR ch); | |
249 | |
250 FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1); | |
251 | |
252 void Format(const FX_CHAR* lpszFormat, ...); | |
253 | |
254 void FormatV(const FX_CHAR* lpszFormat, va_list argList); | |
255 | |
256 void Reserve(FX_STRSIZE len); | |
257 | |
258 FX_CHAR* GetBuffer(FX_STRSIZE len); | |
259 | |
260 void ReleaseBuffer(FX_STRSIZE len = -1); | |
261 | |
262 CFX_ByteString Mid(FX_STRSIZE first) const; | |
263 | |
264 CFX_ByteString Mid(FX_STRSIZE first, FX_STRSIZE count) const; | |
265 | |
266 CFX_ByteString Left(FX_STRSIZE count) const; | |
267 | |
268 CFX_ByteString Right(FX_STRSIZE count) const; | |
269 | |
270 FX_STRSIZE Find(const CFX_ByteStringC& lpszSub, FX_STRSIZE start = 0) const; | |
271 | |
272 FX_STRSIZE Find(FX_CHAR ch, FX_STRSIZE start = 0) const; | |
273 | |
274 FX_STRSIZE ReverseFind(FX_CHAR ch) const; | |
275 | |
276 void MakeLower(); | |
277 | |
278 void MakeUpper(); | |
279 | |
280 void TrimRight(); | |
281 | |
282 void TrimRight(FX_CHAR chTarget); | |
283 | |
284 void TrimRight(const CFX_ByteStringC& lpszTargets); | |
285 | |
286 void TrimLeft(); | |
287 | |
288 void TrimLeft(FX_CHAR chTarget); | |
289 | |
290 void TrimLeft(const CFX_ByteStringC& lpszTargets); | |
291 | |
292 FX_STRSIZE Replace(const CFX_ByteStringC& lpszOld, | |
293 const CFX_ByteStringC& lpszNew); | |
294 | |
295 FX_STRSIZE Remove(FX_CHAR ch); | |
296 | |
297 CFX_WideString UTF8Decode() const; | |
298 | |
299 FX_DWORD GetID(FX_STRSIZE start_pos = 0) const; | |
300 | |
301 #define FXFORMAT_SIGNED 1 | |
302 #define FXFORMAT_HEX 2 | |
303 #define FXFORMAT_CAPITAL 4 | |
304 | |
305 static CFX_ByteString FormatInteger(int i, FX_DWORD flags = 0); | |
306 static CFX_ByteString FormatFloat(FX_FLOAT f, int precision = 0); | |
307 | |
308 protected: | |
309 // To ensure ref counts do not overflow, consider the worst possible case: | |
310 // the entire address space contains nothing but pointers to this object. | |
311 // Since the count increments with each new pointer, the largest value is | |
312 // the number of pointers that can fit into the address space. The size of | |
313 // the address space itself is a good upper bound on it; we need not go | |
314 // larger. | |
315 class StringData { | |
316 public: | |
317 static StringData* Create(int nLen); | |
318 void Retain() { ++m_nRefs; } | |
319 void Release() { | |
320 if (--m_nRefs <= 0) | |
321 FX_Free(this); | |
322 } | |
323 | |
324 intptr_t m_nRefs; // Would prefer ssize_t, but no windows support. | |
325 FX_STRSIZE m_nDataLength; | |
326 FX_STRSIZE m_nAllocLength; | |
327 FX_CHAR m_String[1]; | |
328 | |
329 private: | |
330 StringData(FX_STRSIZE dataLen, FX_STRSIZE allocLen) | |
331 : m_nRefs(1), m_nDataLength(dataLen), m_nAllocLength(allocLen) { | |
332 FXSYS_assert(dataLen >= 0); | |
333 FXSYS_assert(allocLen >= 0); | |
334 FXSYS_assert(dataLen <= allocLen); | |
335 m_String[dataLen] = 0; | |
336 } | |
337 ~StringData() = delete; | |
338 }; | |
339 | |
340 void AllocCopy(CFX_ByteString& dest, | |
341 FX_STRSIZE nCopyLen, | |
342 FX_STRSIZE nCopyIndex) const; | |
343 void AssignCopy(FX_STRSIZE nSrcLen, const FX_CHAR* lpszSrcData); | |
344 void ConcatCopy(FX_STRSIZE nSrc1Len, | |
345 const FX_CHAR* lpszSrc1Data, | |
346 FX_STRSIZE nSrc2Len, | |
347 const FX_CHAR* lpszSrc2Data); | |
348 void ConcatInPlace(FX_STRSIZE nSrcLen, const FX_CHAR* lpszSrcData); | |
349 void CopyBeforeWrite(); | |
350 void AllocBeforeWrite(FX_STRSIZE nLen); | |
351 | |
352 StringData* m_pData; | |
353 friend class fxcrt_ByteStringConcatInPlace_Test; | |
354 }; | |
355 inline CFX_ByteStringC::CFX_ByteStringC(const CFX_ByteString& src) { | |
356 m_Ptr = (const uint8_t*)src; | |
357 m_Length = src.GetLength(); | |
358 } | |
359 inline CFX_ByteStringC& CFX_ByteStringC::operator=(const CFX_ByteString& src) { | |
360 m_Ptr = (const uint8_t*)src; | |
361 m_Length = src.GetLength(); | |
362 return *this; | |
363 } | |
364 | |
365 inline bool operator==(const char* lhs, const CFX_ByteString& rhs) { | |
366 return rhs == lhs; | |
367 } | |
368 inline bool operator==(const CFX_ByteStringC& lhs, const CFX_ByteString& rhs) { | |
369 return rhs == lhs; | |
370 } | |
371 inline bool operator!=(const char* lhs, const CFX_ByteString& rhs) { | |
372 return rhs != lhs; | |
373 } | |
374 inline bool operator!=(const CFX_ByteStringC& lhs, const CFX_ByteString& rhs) { | |
375 return rhs != lhs; | |
376 } | |
377 | |
378 inline CFX_ByteString operator+(const CFX_ByteStringC& str1, | |
379 const CFX_ByteStringC& str2) { | |
380 return CFX_ByteString(str1, str2); | |
381 } | |
382 inline CFX_ByteString operator+(const CFX_ByteStringC& str1, | |
383 const FX_CHAR* str2) { | |
384 return CFX_ByteString(str1, str2); | |
385 } | |
386 inline CFX_ByteString operator+(const FX_CHAR* str1, | |
387 const CFX_ByteStringC& str2) { | |
388 return CFX_ByteString(str1, str2); | |
389 } | |
390 inline CFX_ByteString operator+(const CFX_ByteStringC& str1, FX_CHAR ch) { | |
391 return CFX_ByteString(str1, CFX_ByteStringC(ch)); | |
392 } | |
393 inline CFX_ByteString operator+(FX_CHAR ch, const CFX_ByteStringC& str2) { | |
394 return CFX_ByteString(ch, str2); | |
395 } | |
396 inline CFX_ByteString operator+(const CFX_ByteString& str1, | |
397 const CFX_ByteString& str2) { | |
398 return CFX_ByteString(str1, str2); | |
399 } | |
400 inline CFX_ByteString operator+(const CFX_ByteString& str1, FX_CHAR ch) { | |
401 return CFX_ByteString(str1, CFX_ByteStringC(ch)); | |
402 } | |
403 inline CFX_ByteString operator+(FX_CHAR ch, const CFX_ByteString& str2) { | |
404 return CFX_ByteString(ch, str2); | |
405 } | |
406 inline CFX_ByteString operator+(const CFX_ByteString& str1, | |
407 const FX_CHAR* str2) { | |
408 return CFX_ByteString(str1, str2); | |
409 } | |
410 inline CFX_ByteString operator+(const FX_CHAR* str1, | |
411 const CFX_ByteString& str2) { | |
412 return CFX_ByteString(str1, str2); | |
413 } | |
414 inline CFX_ByteString operator+(const CFX_ByteString& str1, | |
415 const CFX_ByteStringC& str2) { | |
416 return CFX_ByteString(str1, str2); | |
417 } | |
418 inline CFX_ByteString operator+(const CFX_ByteStringC& str1, | |
419 const CFX_ByteString& str2) { | |
420 return CFX_ByteString(str1, str2); | |
421 } | |
422 class CFX_WideStringC { | |
423 public: | |
424 typedef FX_WCHAR value_type; | |
425 | |
426 CFX_WideStringC() { | |
427 m_Ptr = NULL; | |
428 m_Length = 0; | |
429 } | |
430 | |
431 CFX_WideStringC(const FX_WCHAR* ptr) { | |
432 m_Ptr = ptr; | |
433 m_Length = ptr ? FXSYS_wcslen(ptr) : 0; | |
434 } | |
435 | |
436 CFX_WideStringC(FX_WCHAR& ch) { | |
437 m_Ptr = &ch; | |
438 m_Length = 1; | |
439 } | |
440 | |
441 CFX_WideStringC(const FX_WCHAR* ptr, FX_STRSIZE len) { | |
442 m_Ptr = ptr; | |
443 m_Length = (len == -1) ? FXSYS_wcslen(ptr) : len; | |
444 } | |
445 | |
446 CFX_WideStringC(const CFX_WideStringC& src) { | |
447 m_Ptr = src.m_Ptr; | |
448 m_Length = src.m_Length; | |
449 } | |
450 | |
451 CFX_WideStringC(const CFX_WideString& src); | |
452 | |
453 CFX_WideStringC& operator=(const FX_WCHAR* src) { | |
454 m_Ptr = src; | |
455 m_Length = FXSYS_wcslen(src); | |
456 return *this; | |
457 } | |
458 | |
459 CFX_WideStringC& operator=(const CFX_WideStringC& src) { | |
460 m_Ptr = src.m_Ptr; | |
461 m_Length = src.m_Length; | |
462 return *this; | |
463 } | |
464 | |
465 CFX_WideStringC& operator=(const CFX_WideString& src); | |
466 | |
467 bool operator==(const wchar_t* ptr) const { | |
468 return FXSYS_wcslen(ptr) == m_Length && wmemcmp(ptr, m_Ptr, m_Length) == 0; | |
469 } | |
470 bool operator==(const CFX_WideStringC& str) const { | |
471 return str.m_Length == m_Length && wmemcmp(str.m_Ptr, m_Ptr, m_Length) == 0; | |
472 } | |
473 bool operator!=(const wchar_t* ptr) const { return !(*this == ptr); } | |
474 bool operator!=(const CFX_WideStringC& str) const { return !(*this == str); } | |
475 | |
476 const FX_WCHAR* GetPtr() const { return m_Ptr; } | |
477 | |
478 FX_STRSIZE GetLength() const { return m_Length; } | |
479 | |
480 bool IsEmpty() const { return m_Length == 0; } | |
481 | |
482 FX_WCHAR GetAt(FX_STRSIZE index) const { return m_Ptr[index]; } | |
483 | |
484 CFX_WideStringC Left(FX_STRSIZE count) const { | |
485 if (count < 1) { | |
486 return CFX_WideStringC(); | |
487 } | |
488 if (count > m_Length) { | |
489 count = m_Length; | |
490 } | |
491 return CFX_WideStringC(m_Ptr, count); | |
492 } | |
493 | |
494 CFX_WideStringC Mid(FX_STRSIZE index, FX_STRSIZE count = -1) const { | |
495 if (index < 0) { | |
496 index = 0; | |
497 } | |
498 if (index > m_Length) { | |
499 return CFX_WideStringC(); | |
500 } | |
501 if (count < 0 || count > m_Length - index) { | |
502 count = m_Length - index; | |
503 } | |
504 return CFX_WideStringC(m_Ptr + index, count); | |
505 } | |
506 | |
507 CFX_WideStringC Right(FX_STRSIZE count) const { | |
508 if (count < 1) { | |
509 return CFX_WideStringC(); | |
510 } | |
511 if (count > m_Length) { | |
512 count = m_Length; | |
513 } | |
514 return CFX_WideStringC(m_Ptr + m_Length - count, count); | |
515 } | |
516 | |
517 const FX_WCHAR& operator[](size_t index) const { return m_Ptr[index]; } | |
518 | |
519 bool operator<(const CFX_WideStringC& that) const { | |
520 int result = wmemcmp(m_Ptr, that.m_Ptr, std::min(m_Length, that.m_Length)); | |
521 return result < 0 || (result == 0 && m_Length < that.m_Length); | |
522 } | |
523 | |
524 protected: | |
525 const FX_WCHAR* m_Ptr; | |
526 FX_STRSIZE m_Length; | |
527 | |
528 private: | |
529 void* operator new(size_t) throw() { return NULL; } | |
530 }; | |
531 inline bool operator==(const wchar_t* lhs, const CFX_WideStringC& rhs) { | |
532 return rhs == lhs; | |
533 } | |
534 inline bool operator!=(const wchar_t* lhs, const CFX_WideStringC& rhs) { | |
535 return rhs != lhs; | |
536 } | |
537 #define FX_WSTRC(wstr) CFX_WideStringC(wstr, FX_ArraySize(wstr) - 1) | |
538 | |
539 // A mutable string with shared buffers using copy-on-write semantics that | |
540 // avoids the cost of std::string's iterator stability guarantees. | |
541 class CFX_WideString { | |
542 public: | |
543 typedef FX_WCHAR value_type; | |
544 | |
545 CFX_WideString() : m_pData(nullptr) {} | |
546 | |
547 // Copy constructor. | |
548 CFX_WideString(const CFX_WideString& str); | |
549 | |
550 // Move constructor. | |
551 inline CFX_WideString(CFX_WideString&& other) { | |
552 m_pData = other.m_pData; | |
553 other.m_pData = nullptr; | |
554 } | |
555 | |
556 CFX_WideString(const FX_WCHAR* ptr) | |
557 : CFX_WideString(ptr, ptr ? FXSYS_wcslen(ptr) : 0) {} | |
558 | |
559 CFX_WideString(const FX_WCHAR* ptr, FX_STRSIZE len); | |
560 | |
561 CFX_WideString(FX_WCHAR ch); | |
562 | |
563 CFX_WideString(const CFX_WideStringC& str); | |
564 | |
565 CFX_WideString(const CFX_WideStringC& str1, const CFX_WideStringC& str2); | |
566 | |
567 ~CFX_WideString(); | |
568 | |
569 static CFX_WideString FromLocal(const CFX_ByteString& str); | |
570 | |
571 static CFX_WideString FromCodePage(const CFX_ByteString& str, | |
572 uint16_t codepage); | |
573 | |
574 static CFX_WideString FromUTF8(const char* str, FX_STRSIZE len); | |
575 | |
576 static CFX_WideString FromUTF16LE(const unsigned short* str, FX_STRSIZE len); | |
577 | |
578 static FX_STRSIZE WStringLength(const unsigned short* str); | |
579 | |
580 // Explicit conversion to C-style wide string. | |
581 const FX_WCHAR* c_str() const { return m_pData ? m_pData->m_String : L""; } | |
582 | |
583 // Implicit conversion to C-style wide string -- deprecated. | |
584 operator const FX_WCHAR*() const { return m_pData ? m_pData->m_String : L""; } | |
585 | |
586 void Empty(); | |
587 | |
588 bool IsEmpty() const { return !GetLength(); } | |
589 | |
590 FX_STRSIZE GetLength() const { return m_pData ? m_pData->m_nDataLength : 0; } | |
591 | |
592 const CFX_WideString& operator=(const FX_WCHAR* str); | |
593 | |
594 const CFX_WideString& operator=(const CFX_WideString& stringSrc); | |
595 | |
596 const CFX_WideString& operator=(const CFX_WideStringC& stringSrc); | |
597 | |
598 const CFX_WideString& operator+=(const FX_WCHAR* str); | |
599 | |
600 const CFX_WideString& operator+=(FX_WCHAR ch); | |
601 | |
602 const CFX_WideString& operator+=(const CFX_WideString& str); | |
603 | |
604 const CFX_WideString& operator+=(const CFX_WideStringC& str); | |
605 | |
606 bool operator==(const wchar_t* ptr) const { return Equal(ptr); } | |
607 bool operator==(const CFX_WideStringC& str) const { return Equal(str); } | |
608 bool operator==(const CFX_WideString& other) const { return Equal(other); } | |
609 | |
610 bool operator!=(const wchar_t* ptr) const { return !(*this == ptr); } | |
611 bool operator!=(const CFX_WideStringC& str) const { return !(*this == str); } | |
612 bool operator!=(const CFX_WideString& other) const { | |
613 return !(*this == other); | |
614 } | |
615 | |
616 bool operator<(const CFX_WideString& str) const { | |
617 int result = | |
618 wmemcmp(c_str(), str.c_str(), std::min(GetLength(), str.GetLength())); | |
619 return result < 0 || (result == 0 && GetLength() < str.GetLength()); | |
620 } | |
621 | |
622 FX_WCHAR GetAt(FX_STRSIZE nIndex) const { | |
623 return m_pData ? m_pData->m_String[nIndex] : 0; | |
624 } | |
625 | |
626 FX_WCHAR operator[](FX_STRSIZE nIndex) const { | |
627 return m_pData ? m_pData->m_String[nIndex] : 0; | |
628 } | |
629 | |
630 void SetAt(FX_STRSIZE nIndex, FX_WCHAR ch); | |
631 | |
632 int Compare(const FX_WCHAR* str) const; | |
633 | |
634 int Compare(const CFX_WideString& str) const; | |
635 | |
636 int CompareNoCase(const FX_WCHAR* str) const; | |
637 | |
638 bool Equal(const wchar_t* ptr) const; | |
639 bool Equal(const CFX_WideStringC& str) const; | |
640 bool Equal(const CFX_WideString& other) const; | |
641 | |
642 CFX_WideString Mid(FX_STRSIZE first) const; | |
643 | |
644 CFX_WideString Mid(FX_STRSIZE first, FX_STRSIZE count) const; | |
645 | |
646 CFX_WideString Left(FX_STRSIZE count) const; | |
647 | |
648 CFX_WideString Right(FX_STRSIZE count) const; | |
649 | |
650 FX_STRSIZE Insert(FX_STRSIZE index, FX_WCHAR ch); | |
651 | |
652 FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1); | |
653 | |
654 void Format(const FX_WCHAR* lpszFormat, ...); | |
655 | |
656 void FormatV(const FX_WCHAR* lpszFormat, va_list argList); | |
657 | |
658 void MakeLower(); | |
659 | |
660 void MakeUpper(); | |
661 | |
662 void TrimRight(); | |
663 | |
664 void TrimRight(FX_WCHAR chTarget); | |
665 | |
666 void TrimRight(const FX_WCHAR* lpszTargets); | |
667 | |
668 void TrimLeft(); | |
669 | |
670 void TrimLeft(FX_WCHAR chTarget); | |
671 | |
672 void TrimLeft(const FX_WCHAR* lpszTargets); | |
673 | |
674 void Reserve(FX_STRSIZE len); | |
675 | |
676 FX_WCHAR* GetBuffer(FX_STRSIZE len); | |
677 | |
678 void ReleaseBuffer(FX_STRSIZE len = -1); | |
679 | |
680 int GetInteger() const; | |
681 | |
682 FX_FLOAT GetFloat() const; | |
683 | |
684 FX_STRSIZE Find(const FX_WCHAR* lpszSub, FX_STRSIZE start = 0) const; | |
685 | |
686 FX_STRSIZE Find(FX_WCHAR ch, FX_STRSIZE start = 0) const; | |
687 | |
688 FX_STRSIZE Replace(const FX_WCHAR* lpszOld, const FX_WCHAR* lpszNew); | |
689 | |
690 FX_STRSIZE Remove(FX_WCHAR ch); | |
691 | |
692 CFX_ByteString UTF8Encode() const; | |
693 | |
694 CFX_ByteString UTF16LE_Encode() const; | |
695 | |
696 protected: | |
697 class StringData { | |
698 public: | |
699 static StringData* Create(int nLen); | |
700 void Retain() { ++m_nRefs; } | |
701 void Release() { | |
702 if (--m_nRefs <= 0) | |
703 FX_Free(this); | |
704 } | |
705 | |
706 intptr_t m_nRefs; // Would prefer ssize_t, but no windows support. | |
707 FX_STRSIZE m_nDataLength; | |
708 FX_STRSIZE m_nAllocLength; | |
709 FX_WCHAR m_String[1]; | |
710 | |
711 private: | |
712 StringData(FX_STRSIZE dataLen, FX_STRSIZE allocLen) | |
713 : m_nRefs(1), m_nDataLength(dataLen), m_nAllocLength(allocLen) { | |
714 FXSYS_assert(dataLen >= 0); | |
715 FXSYS_assert(allocLen >= 0); | |
716 FXSYS_assert(dataLen <= allocLen); | |
717 m_String[dataLen] = 0; | |
718 } | |
719 ~StringData() = delete; | |
720 }; | |
721 | |
722 void CopyBeforeWrite(); | |
723 void AllocBeforeWrite(FX_STRSIZE nLen); | |
724 void ConcatInPlace(FX_STRSIZE nSrcLen, const FX_WCHAR* lpszSrcData); | |
725 void ConcatCopy(FX_STRSIZE nSrc1Len, | |
726 const FX_WCHAR* lpszSrc1Data, | |
727 FX_STRSIZE nSrc2Len, | |
728 const FX_WCHAR* lpszSrc2Data); | |
729 void AssignCopy(FX_STRSIZE nSrcLen, const FX_WCHAR* lpszSrcData); | |
730 void AllocCopy(CFX_WideString& dest, | |
731 FX_STRSIZE nCopyLen, | |
732 FX_STRSIZE nCopyIndex) const; | |
733 | |
734 StringData* m_pData; | |
735 friend class fxcrt_WideStringConcatInPlace_Test; | |
736 }; | |
737 inline CFX_WideStringC::CFX_WideStringC(const CFX_WideString& src) { | |
738 m_Ptr = src.c_str(); | |
739 m_Length = src.GetLength(); | |
740 } | |
741 inline CFX_WideStringC& CFX_WideStringC::operator=(const CFX_WideString& src) { | |
742 m_Ptr = src.c_str(); | |
743 m_Length = src.GetLength(); | |
744 return *this; | |
745 } | |
746 | |
747 inline CFX_WideString operator+(const CFX_WideStringC& str1, | |
748 const CFX_WideStringC& str2) { | |
749 return CFX_WideString(str1, str2); | |
750 } | |
751 inline CFX_WideString operator+(const CFX_WideStringC& str1, | |
752 const FX_WCHAR* str2) { | |
753 return CFX_WideString(str1, str2); | |
754 } | |
755 inline CFX_WideString operator+(const FX_WCHAR* str1, | |
756 const CFX_WideStringC& str2) { | |
757 return CFX_WideString(str1, str2); | |
758 } | |
759 inline CFX_WideString operator+(const CFX_WideStringC& str1, FX_WCHAR ch) { | |
760 return CFX_WideString(str1, CFX_WideStringC(ch)); | |
761 } | |
762 inline CFX_WideString operator+(FX_WCHAR ch, const CFX_WideStringC& str2) { | |
763 return CFX_WideString(ch, str2); | |
764 } | |
765 inline CFX_WideString operator+(const CFX_WideString& str1, | |
766 const CFX_WideString& str2) { | |
767 return CFX_WideString(str1, str2); | |
768 } | |
769 inline CFX_WideString operator+(const CFX_WideString& str1, FX_WCHAR ch) { | |
770 return CFX_WideString(str1, CFX_WideStringC(ch)); | |
771 } | |
772 inline CFX_WideString operator+(FX_WCHAR ch, const CFX_WideString& str2) { | |
773 return CFX_WideString(ch, str2); | |
774 } | |
775 inline CFX_WideString operator+(const CFX_WideString& str1, | |
776 const FX_WCHAR* str2) { | |
777 return CFX_WideString(str1, str2); | |
778 } | |
779 inline CFX_WideString operator+(const FX_WCHAR* str1, | |
780 const CFX_WideString& str2) { | |
781 return CFX_WideString(str1, str2); | |
782 } | |
783 inline CFX_WideString operator+(const CFX_WideString& str1, | |
784 const CFX_WideStringC& str2) { | |
785 return CFX_WideString(str1, str2); | |
786 } | |
787 inline CFX_WideString operator+(const CFX_WideStringC& str1, | |
788 const CFX_WideString& str2) { | |
789 return CFX_WideString(str1, str2); | |
790 } | |
791 inline bool operator==(const wchar_t* lhs, const CFX_WideString& rhs) { | |
792 return rhs == lhs; | |
793 } | |
794 inline bool operator==(const CFX_WideStringC& lhs, const CFX_WideString& rhs) { | |
795 return rhs == lhs; | |
796 } | |
797 inline bool operator!=(const wchar_t* lhs, const CFX_WideString& rhs) { | |
798 return rhs != lhs; | |
799 } | |
800 inline bool operator!=(const CFX_WideStringC& lhs, const CFX_WideString& rhs) { | |
801 return rhs != lhs; | |
802 } | |
803 | |
804 CFX_ByteString FX_UTF8Encode(const FX_WCHAR* pwsStr, FX_STRSIZE len); | |
805 inline CFX_ByteString FX_UTF8Encode(const CFX_WideStringC& wsStr) { | |
806 return FX_UTF8Encode(wsStr.GetPtr(), wsStr.GetLength()); | |
807 } | |
808 inline CFX_ByteString FX_UTF8Encode(const CFX_WideString& wsStr) { | |
809 return FX_UTF8Encode(wsStr.c_str(), wsStr.GetLength()); | |
810 } | |
811 | |
812 FX_FLOAT FX_atof(const CFX_ByteStringC& str); | |
813 inline FX_FLOAT FX_atof(const CFX_WideStringC& wsStr) { | |
814 return FX_atof(FX_UTF8Encode(wsStr.GetPtr(), wsStr.GetLength())); | |
815 } | |
816 void FX_atonum(const CFX_ByteStringC& str, FX_BOOL& bInteger, void* pData); | |
817 FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_CHAR* buf); | |
818 | |
819 #endif // CORE_INCLUDE_FXCRT_FX_STRING_H_ | |
OLD | NEW |