Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(369)

Side by Side Diff: core/fxcrt/include/fx_string.h

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

Powered by Google App Engine
This is Rietveld 408576698