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

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

Issue 1844073003: Make StringData a templated class. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: 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
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/cfx_string_data_template.h"
13 #include "core/fxcrt/include/cfx_retain_ptr.h" 14 #include "core/fxcrt/include/cfx_retain_ptr.h"
14 #include "core/fxcrt/include/fx_memory.h" 15 #include "core/fxcrt/include/fx_memory.h"
15 #include "core/fxcrt/include/fx_system.h" 16 #include "core/fxcrt/include/fx_system.h"
16 17
18 extern template class CFX_StringDataTemplate<FX_CHAR>;
19 extern template class CFX_StringDataTemplate<FX_WCHAR>;
20
17 class CFX_BinaryBuf; 21 class CFX_BinaryBuf;
18 class CFX_ByteString; 22 class CFX_ByteString;
19 class CFX_WideString; 23 class CFX_WideString;
20 24
21 // An immutable string with caller-provided storage which must outlive the 25 // An immutable string with caller-provided storage which must outlive the
22 // string itself. 26 // string itself.
23 class CFX_ByteStringC { 27 class CFX_ByteStringC {
24 public: 28 public:
25 typedef FX_CHAR value_type; 29 typedef FX_CHAR value_type;
26 30
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 uint32_t GetID(FX_STRSIZE start_pos = 0) const; 276 uint32_t GetID(FX_STRSIZE start_pos = 0) const;
273 277
274 #define FXFORMAT_SIGNED 1 278 #define FXFORMAT_SIGNED 1
275 #define FXFORMAT_HEX 2 279 #define FXFORMAT_HEX 2
276 #define FXFORMAT_CAPITAL 4 280 #define FXFORMAT_CAPITAL 4
277 281
278 static CFX_ByteString FormatInteger(int i, uint32_t flags = 0); 282 static CFX_ByteString FormatInteger(int i, uint32_t flags = 0);
279 static CFX_ByteString FormatFloat(FX_FLOAT f, int precision = 0); 283 static CFX_ByteString FormatFloat(FX_FLOAT f, int precision = 0);
280 284
281 protected: 285 protected:
282 class StringData { 286 using StringData = CFX_StringDataTemplate<FX_CHAR>;
283 public:
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
288 void Retain() { ++m_nRefs; }
289 void Release() {
290 if (--m_nRefs <= 0)
291 FX_Free(this);
292 }
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.
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.
315 FX_STRSIZE m_nDataLength;
316 FX_STRSIZE m_nAllocLength;
317
318 // Not really 1, variable size.
319 FX_CHAR m_String[1];
320
321 private:
322 StringData(FX_STRSIZE dataLen, FX_STRSIZE allocLen);
323 ~StringData() = delete;
324 };
325 287
326 void CopyBeforeWrite(); 288 void CopyBeforeWrite();
327 void AllocBeforeWrite(FX_STRSIZE nLen); 289 void AllocBeforeWrite(FX_STRSIZE nLen);
328 void AllocCopy(CFX_ByteString& dest, 290 void AllocCopy(CFX_ByteString& dest,
329 FX_STRSIZE nCopyLen, 291 FX_STRSIZE nCopyLen,
330 FX_STRSIZE nCopyIndex) const; 292 FX_STRSIZE nCopyIndex) const;
331 void AssignCopy(const FX_CHAR* pSrcData, FX_STRSIZE nSrcLen); 293 void AssignCopy(const FX_CHAR* pSrcData, FX_STRSIZE nSrcLen);
332 void Concat(const FX_CHAR* lpszSrcData, FX_STRSIZE nSrcLen); 294 void Concat(const FX_CHAR* lpszSrcData, FX_STRSIZE nSrcLen);
333 295
334 CFX_RetainPtr<StringData> m_pData; 296 CFX_RetainPtr<StringData> m_pData;
(...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after
765 } 727 }
766 728
767 FX_FLOAT FX_atof(const CFX_ByteStringC& str); 729 FX_FLOAT FX_atof(const CFX_ByteStringC& str);
768 inline FX_FLOAT FX_atof(const CFX_WideStringC& wsStr) { 730 inline FX_FLOAT FX_atof(const CFX_WideStringC& wsStr) {
769 return FX_atof(FX_UTF8Encode(wsStr.GetPtr(), wsStr.GetLength())); 731 return FX_atof(FX_UTF8Encode(wsStr.GetPtr(), wsStr.GetLength()));
770 } 732 }
771 void FX_atonum(const CFX_ByteStringC& str, FX_BOOL& bInteger, void* pData); 733 void FX_atonum(const CFX_ByteStringC& str, FX_BOOL& bInteger, void* pData);
772 FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_CHAR* buf); 734 FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_CHAR* buf);
773 735
774 #endif // CORE_FXCRT_INCLUDE_FX_STRING_H_ 736 #endif // CORE_FXCRT_INCLUDE_FX_STRING_H_
OLDNEW
« core/fxcrt/fx_basic_bstring.cpp ('K') | « core/fxcrt/fx_basic_bstring.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698