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. |
(...skipping 16 matching lines...) Expand all Loading... |
27 ((uint32_t)c4)) | 27 ((uint32_t)c4)) |
28 | 28 |
29 #define FX_WSTRC(wstr) CFX_WideStringC(wstr, FX_ArraySize(wstr) - 1) | 29 #define FX_WSTRC(wstr) CFX_WideStringC(wstr, FX_ArraySize(wstr) - 1) |
30 | 30 |
31 // A mutable string with shared buffers using copy-on-write semantics that | 31 // A mutable string with shared buffers using copy-on-write semantics that |
32 // avoids the cost of std::string's iterator stability guarantees. | 32 // avoids the cost of std::string's iterator stability guarantees. |
33 class CFX_ByteString { | 33 class CFX_ByteString { |
34 public: | 34 public: |
35 using CharType = FX_CHAR; | 35 using CharType = FX_CHAR; |
36 | 36 |
37 CFX_ByteString() {} | 37 CFX_ByteString(); |
38 CFX_ByteString(const CFX_ByteString& other) : m_pData(other.m_pData) {} | 38 CFX_ByteString(const CFX_ByteString& other); |
39 CFX_ByteString(CFX_ByteString&& other) { m_pData.Swap(other.m_pData); } | 39 CFX_ByteString(CFX_ByteString&& other); |
40 | 40 |
41 // Deliberately implicit to avoid calling on every string literal. | 41 // Deliberately implicit to avoid calling on every string literal. |
42 CFX_ByteString(char ch); | 42 CFX_ByteString(char ch); |
43 CFX_ByteString(const FX_CHAR* ptr) | 43 CFX_ByteString(const FX_CHAR* ptr); |
44 : CFX_ByteString(ptr, ptr ? FXSYS_strlen(ptr) : 0) {} | |
45 | 44 |
46 CFX_ByteString(const FX_CHAR* ptr, FX_STRSIZE len); | 45 CFX_ByteString(const FX_CHAR* ptr, FX_STRSIZE len); |
47 CFX_ByteString(const uint8_t* ptr, FX_STRSIZE len); | 46 CFX_ByteString(const uint8_t* ptr, FX_STRSIZE len); |
48 | 47 |
49 explicit CFX_ByteString(const CFX_ByteStringC& bstrc); | 48 explicit CFX_ByteString(const CFX_ByteStringC& bstrc); |
50 CFX_ByteString(const CFX_ByteStringC& bstrc1, const CFX_ByteStringC& bstrc2); | 49 CFX_ByteString(const CFX_ByteStringC& bstrc1, const CFX_ByteStringC& bstrc2); |
51 | 50 |
52 ~CFX_ByteString(); | 51 ~CFX_ByteString(); |
53 | 52 |
54 void clear() { m_pData.Reset(); } | 53 void clear(); |
55 | 54 |
56 static CFX_ByteString FromUnicode(const FX_WCHAR* ptr, FX_STRSIZE len = -1); | 55 static CFX_ByteString FromUnicode(const FX_WCHAR* ptr, FX_STRSIZE len = -1); |
57 static CFX_ByteString FromUnicode(const CFX_WideString& str); | 56 static CFX_ByteString FromUnicode(const CFX_WideString& str); |
58 | 57 |
59 // Explicit conversion to C-style string. | 58 // Explicit conversion to C-style string. |
60 // Note: Any subsequent modification of |this| will invalidate the result. | 59 // Note: Any subsequent modification of |this| will invalidate the result. |
61 const FX_CHAR* c_str() const { return m_pData ? m_pData->m_String : ""; } | 60 const FX_CHAR* c_str() const; |
62 | 61 |
63 // Explicit conversion to uint8_t*. | 62 // Explicit conversion to uint8_t*. |
64 // Note: Any subsequent modification of |this| will invalidate the result. | 63 // Note: Any subsequent modification of |this| will invalidate the result. |
65 const uint8_t* raw_str() const { | 64 const uint8_t* raw_str() const; |
66 return m_pData ? reinterpret_cast<const uint8_t*>(m_pData->m_String) | |
67 : nullptr; | |
68 } | |
69 | 65 |
70 // Explicit conversion to CFX_ByteStringC. | 66 // Explicit conversion to CFX_ByteStringC. |
71 // Note: Any subsequent modification of |this| will invalidate the result. | 67 // Note: Any subsequent modification of |this| will invalidate the result. |
72 CFX_ByteStringC AsStringC() const { | 68 CFX_ByteStringC AsStringC() const; |
73 return CFX_ByteStringC(raw_str(), GetLength()); | |
74 } | |
75 | 69 |
76 FX_STRSIZE GetLength() const { return m_pData ? m_pData->m_nDataLength : 0; } | 70 FX_STRSIZE GetLength() const; |
77 bool IsEmpty() const { return !GetLength(); } | 71 bool IsEmpty() const; |
78 | 72 |
79 int Compare(const CFX_ByteStringC& str) const; | 73 int Compare(const CFX_ByteStringC& str) const; |
80 bool EqualNoCase(const CFX_ByteStringC& str) const; | 74 bool EqualNoCase(const CFX_ByteStringC& str) const; |
81 | 75 |
82 bool operator==(const char* ptr) const; | 76 bool operator==(const char* ptr) const; |
83 bool operator==(const CFX_ByteStringC& str) const; | 77 bool operator==(const CFX_ByteStringC& str) const; |
84 bool operator==(const CFX_ByteString& other) const; | 78 bool operator==(const CFX_ByteString& other) const; |
85 | 79 |
86 bool operator!=(const char* ptr) const { return !(*this == ptr); } | 80 bool operator!=(const char* ptr) const; |
87 bool operator!=(const CFX_ByteStringC& str) const { return !(*this == str); } | 81 bool operator!=(const CFX_ByteStringC& str) const; |
88 bool operator!=(const CFX_ByteString& other) const { | 82 bool operator!=(const CFX_ByteString& other) const; |
89 return !(*this == other); | |
90 } | |
91 | 83 |
92 bool operator<(const CFX_ByteString& str) const { | 84 bool operator<(const CFX_ByteString& str) const; |
93 int result = FXSYS_memcmp(c_str(), str.c_str(), | |
94 std::min(GetLength(), str.GetLength())); | |
95 return result < 0 || (result == 0 && GetLength() < str.GetLength()); | |
96 } | |
97 | 85 |
98 const CFX_ByteString& operator=(const FX_CHAR* str); | 86 const CFX_ByteString& operator=(const FX_CHAR* str); |
99 const CFX_ByteString& operator=(const CFX_ByteStringC& bstrc); | 87 const CFX_ByteString& operator=(const CFX_ByteStringC& bstrc); |
100 const CFX_ByteString& operator=(const CFX_ByteString& stringSrc); | 88 const CFX_ByteString& operator=(const CFX_ByteString& stringSrc); |
101 | 89 |
102 const CFX_ByteString& operator+=(FX_CHAR ch); | 90 const CFX_ByteString& operator+=(FX_CHAR ch); |
103 const CFX_ByteString& operator+=(const FX_CHAR* str); | 91 const CFX_ByteString& operator+=(const FX_CHAR* str); |
104 const CFX_ByteString& operator+=(const CFX_ByteString& str); | 92 const CFX_ByteString& operator+=(const CFX_ByteString& str); |
105 const CFX_ByteString& operator+=(const CFX_ByteStringC& bstrc); | 93 const CFX_ByteString& operator+=(const CFX_ByteStringC& bstrc); |
106 | 94 |
107 uint8_t GetAt(FX_STRSIZE nIndex) const { | 95 uint8_t GetAt(FX_STRSIZE nIndex) const; |
108 return m_pData ? m_pData->m_String[nIndex] : 0; | |
109 } | |
110 | 96 |
111 uint8_t operator[](FX_STRSIZE nIndex) const { | 97 uint8_t operator[](FX_STRSIZE nIndex) const; |
112 return m_pData ? m_pData->m_String[nIndex] : 0; | |
113 } | |
114 | 98 |
115 void SetAt(FX_STRSIZE nIndex, FX_CHAR ch); | 99 void SetAt(FX_STRSIZE nIndex, FX_CHAR ch); |
116 FX_STRSIZE Insert(FX_STRSIZE index, FX_CHAR ch); | 100 FX_STRSIZE Insert(FX_STRSIZE index, FX_CHAR ch); |
117 FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1); | 101 FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1); |
118 | 102 |
119 void Format(const FX_CHAR* lpszFormat, ...); | 103 void Format(const FX_CHAR* lpszFormat, ...); |
120 void FormatV(const FX_CHAR* lpszFormat, va_list argList); | 104 void FormatV(const FX_CHAR* lpszFormat, va_list argList); |
121 | 105 |
122 void Reserve(FX_STRSIZE len); | 106 void Reserve(FX_STRSIZE len); |
123 FX_CHAR* GetBuffer(FX_STRSIZE len); | 107 FX_CHAR* GetBuffer(FX_STRSIZE len); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 const CFX_ByteString& str2) { | 215 const CFX_ByteString& str2) { |
232 return CFX_ByteString(str1, str2.AsStringC()); | 216 return CFX_ByteString(str1, str2.AsStringC()); |
233 } | 217 } |
234 | 218 |
235 // A mutable string with shared buffers using copy-on-write semantics that | 219 // A mutable string with shared buffers using copy-on-write semantics that |
236 // avoids the cost of std::string's iterator stability guarantees. | 220 // avoids the cost of std::string's iterator stability guarantees. |
237 class CFX_WideString { | 221 class CFX_WideString { |
238 public: | 222 public: |
239 using CharType = FX_WCHAR; | 223 using CharType = FX_WCHAR; |
240 | 224 |
241 CFX_WideString() {} | 225 CFX_WideString(); |
242 CFX_WideString(const CFX_WideString& other) : m_pData(other.m_pData) {} | 226 CFX_WideString(const CFX_WideString& other); |
243 CFX_WideString(CFX_WideString&& other) { m_pData.Swap(other.m_pData); } | 227 CFX_WideString(CFX_WideString&& other); |
244 | 228 |
245 // Deliberately implicit to avoid calling on every string literal. | 229 // Deliberately implicit to avoid calling on every string literal. |
246 CFX_WideString(FX_WCHAR ch); | 230 CFX_WideString(FX_WCHAR ch); |
247 CFX_WideString(const FX_WCHAR* ptr) | 231 CFX_WideString(const FX_WCHAR* ptr); |
248 : CFX_WideString(ptr, ptr ? FXSYS_wcslen(ptr) : 0) {} | |
249 | 232 |
250 CFX_WideString(const FX_WCHAR* ptr, FX_STRSIZE len); | 233 CFX_WideString(const FX_WCHAR* ptr, FX_STRSIZE len); |
251 | 234 |
252 explicit CFX_WideString(const CFX_WideStringC& str); | 235 explicit CFX_WideString(const CFX_WideStringC& str); |
253 CFX_WideString(const CFX_WideStringC& str1, const CFX_WideStringC& str2); | 236 CFX_WideString(const CFX_WideStringC& str1, const CFX_WideStringC& str2); |
254 | 237 |
255 ~CFX_WideString(); | 238 ~CFX_WideString(); |
256 | 239 |
257 static CFX_WideString FromLocal(const CFX_ByteStringC& str); | 240 static CFX_WideString FromLocal(const CFX_ByteStringC& str); |
258 static CFX_WideString FromCodePage(const CFX_ByteStringC& str, | 241 static CFX_WideString FromCodePage(const CFX_ByteStringC& str, |
259 uint16_t codepage); | 242 uint16_t codepage); |
260 | 243 |
261 static CFX_WideString FromUTF8(const CFX_ByteStringC& str); | 244 static CFX_WideString FromUTF8(const CFX_ByteStringC& str); |
262 static CFX_WideString FromUTF16LE(const unsigned short* str, FX_STRSIZE len); | 245 static CFX_WideString FromUTF16LE(const unsigned short* str, FX_STRSIZE len); |
263 | 246 |
264 static FX_STRSIZE WStringLength(const unsigned short* str); | 247 static FX_STRSIZE WStringLength(const unsigned short* str); |
265 | 248 |
266 // Explicit conversion to C-style wide string. | 249 // Explicit conversion to C-style wide string. |
267 // Note: Any subsequent modification of |this| will invalidate the result. | 250 // Note: Any subsequent modification of |this| will invalidate the result. |
268 const FX_WCHAR* c_str() const { return m_pData ? m_pData->m_String : L""; } | 251 const FX_WCHAR* c_str() const; |
269 | 252 |
270 // Explicit conversion to CFX_WideStringC. | 253 // Explicit conversion to CFX_WideStringC. |
271 // Note: Any subsequent modification of |this| will invalidate the result. | 254 // Note: Any subsequent modification of |this| will invalidate the result. |
272 CFX_WideStringC AsStringC() const { | 255 CFX_WideStringC AsStringC() const; |
273 return CFX_WideStringC(c_str(), GetLength()); | |
274 } | |
275 | 256 |
276 void clear() { m_pData.Reset(); } | 257 void clear(); |
277 | 258 |
278 FX_STRSIZE GetLength() const { return m_pData ? m_pData->m_nDataLength : 0; } | 259 FX_STRSIZE GetLength() const; |
279 bool IsEmpty() const { return !GetLength(); } | 260 bool IsEmpty() const; |
280 | 261 |
281 const CFX_WideString& operator=(const FX_WCHAR* str); | 262 const CFX_WideString& operator=(const FX_WCHAR* str); |
282 const CFX_WideString& operator=(const CFX_WideString& stringSrc); | 263 const CFX_WideString& operator=(const CFX_WideString& stringSrc); |
283 const CFX_WideString& operator=(const CFX_WideStringC& stringSrc); | 264 const CFX_WideString& operator=(const CFX_WideStringC& stringSrc); |
284 | 265 |
285 const CFX_WideString& operator+=(const FX_WCHAR* str); | 266 const CFX_WideString& operator+=(const FX_WCHAR* str); |
286 const CFX_WideString& operator+=(FX_WCHAR ch); | 267 const CFX_WideString& operator+=(FX_WCHAR ch); |
287 const CFX_WideString& operator+=(const CFX_WideString& str); | 268 const CFX_WideString& operator+=(const CFX_WideString& str); |
288 const CFX_WideString& operator+=(const CFX_WideStringC& str); | 269 const CFX_WideString& operator+=(const CFX_WideStringC& str); |
289 | 270 |
290 bool operator==(const wchar_t* ptr) const; | 271 bool operator==(const wchar_t* ptr) const; |
291 bool operator==(const CFX_WideStringC& str) const; | 272 bool operator==(const CFX_WideStringC& str) const; |
292 bool operator==(const CFX_WideString& other) const; | 273 bool operator==(const CFX_WideString& other) const; |
293 | 274 |
294 bool operator!=(const wchar_t* ptr) const { return !(*this == ptr); } | 275 bool operator!=(const wchar_t* ptr) const; |
295 bool operator!=(const CFX_WideStringC& str) const { return !(*this == str); } | 276 bool operator!=(const CFX_WideStringC& str) const; |
296 bool operator!=(const CFX_WideString& other) const { | 277 bool operator!=(const CFX_WideString& other) const; |
297 return !(*this == other); | |
298 } | |
299 | 278 |
300 bool operator<(const CFX_WideString& str) const { | 279 bool operator<(const CFX_WideString& str) const; |
301 int result = | |
302 wmemcmp(c_str(), str.c_str(), std::min(GetLength(), str.GetLength())); | |
303 return result < 0 || (result == 0 && GetLength() < str.GetLength()); | |
304 } | |
305 | 280 |
306 FX_WCHAR GetAt(FX_STRSIZE nIndex) const { | 281 FX_WCHAR GetAt(FX_STRSIZE nIndex) const; |
307 return m_pData ? m_pData->m_String[nIndex] : 0; | |
308 } | |
309 | 282 |
310 FX_WCHAR operator[](FX_STRSIZE nIndex) const { | 283 FX_WCHAR operator[](FX_STRSIZE nIndex) const; |
311 return m_pData ? m_pData->m_String[nIndex] : 0; | |
312 } | |
313 | 284 |
314 void SetAt(FX_STRSIZE nIndex, FX_WCHAR ch); | 285 void SetAt(FX_STRSIZE nIndex, FX_WCHAR ch); |
315 | 286 |
316 int Compare(const FX_WCHAR* str) const; | 287 int Compare(const FX_WCHAR* str) const; |
317 int Compare(const CFX_WideString& str) const; | 288 int Compare(const CFX_WideString& str) const; |
318 int CompareNoCase(const FX_WCHAR* str) const; | 289 int CompareNoCase(const FX_WCHAR* str) const; |
319 | 290 |
320 | 291 |
321 CFX_WideString Mid(FX_STRSIZE first) const; | 292 CFX_WideString Mid(FX_STRSIZE first) const; |
322 CFX_WideString Mid(FX_STRSIZE first, FX_STRSIZE count) const; | 293 CFX_WideString Mid(FX_STRSIZE first, FX_STRSIZE count) const; |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 } | 407 } |
437 | 408 |
438 FX_FLOAT FX_atof(const CFX_ByteStringC& str); | 409 FX_FLOAT FX_atof(const CFX_ByteStringC& str); |
439 inline FX_FLOAT FX_atof(const CFX_WideStringC& wsStr) { | 410 inline FX_FLOAT FX_atof(const CFX_WideStringC& wsStr) { |
440 return FX_atof(FX_UTF8Encode(wsStr.c_str(), wsStr.GetLength()).c_str()); | 411 return FX_atof(FX_UTF8Encode(wsStr.c_str(), wsStr.GetLength()).c_str()); |
441 } | 412 } |
442 void FX_atonum(const CFX_ByteStringC& str, FX_BOOL& bInteger, void* pData); | 413 void FX_atonum(const CFX_ByteStringC& str, FX_BOOL& bInteger, void* pData); |
443 FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_CHAR* buf); | 414 FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_CHAR* buf); |
444 | 415 |
445 #endif // CORE_FXCRT_INCLUDE_FX_STRING_H_ | 416 #endif // CORE_FXCRT_INCLUDE_FX_STRING_H_ |
OLD | NEW |