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

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: Re-rebase 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.cpp ('k') | pdfium.gyp » ('j') | 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/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
17 class CFX_BinaryBuf; 18 class CFX_BinaryBuf;
18 class CFX_ByteString; 19 class CFX_ByteString;
19 class CFX_WideString; 20 class CFX_WideString;
20 21
21 // An immutable string with caller-provided storage which must outlive the 22 // An immutable string with caller-provided storage which must outlive the
22 // string itself. 23 // string itself.
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 uint32_t GetID(FX_STRSIZE start_pos = 0) const; 276 uint32_t GetID(FX_STRSIZE start_pos = 0) const;
276 277
277 #define FXFORMAT_SIGNED 1 278 #define FXFORMAT_SIGNED 1
278 #define FXFORMAT_HEX 2 279 #define FXFORMAT_HEX 2
279 #define FXFORMAT_CAPITAL 4 280 #define FXFORMAT_CAPITAL 4
280 281
281 static CFX_ByteString FormatInteger(int i, uint32_t flags = 0); 282 static CFX_ByteString FormatInteger(int i, uint32_t flags = 0);
282 static CFX_ByteString FormatFloat(FX_FLOAT f, int precision = 0); 283 static CFX_ByteString FormatFloat(FX_FLOAT f, int precision = 0);
283 284
284 protected: 285 protected:
285 class StringData { 286 using StringData = CFX_StringDataTemplate<FX_CHAR>;
286 public:
287 static StringData* Create(FX_STRSIZE nLen);
288 static StringData* Create(const StringData& other);
289 static StringData* Create(const FX_CHAR* pStr, FX_STRSIZE nLen);
290
291 void Retain() { ++m_nRefs; }
292 void Release() {
293 if (--m_nRefs <= 0)
294 FX_Free(this);
295 }
296
297 bool CanOperateInPlace(FX_STRSIZE nTotalLen) const {
298 return m_nRefs <= 1 && nTotalLen <= m_nAllocLength;
299 }
300
301 void CopyContents(const StringData& other);
302 void CopyContents(const FX_CHAR* pStr, FX_STRSIZE nLen);
303 void CopyContentsAt(FX_STRSIZE offset,
304 const FX_CHAR* pStr,
305 FX_STRSIZE nLen);
306
307 // To ensure ref counts do not overflow, consider the worst possible case:
308 // the entire address space contains nothing but pointers to this object.
309 // Since the count increments with each new pointer, the largest value is
310 // the number of pointers that can fit into the address space. The size of
311 // the address space itself is a good upper bound on it.
312 intptr_t m_nRefs; // Would prefer ssize_t, but no windows support.
313
314 // |FX_STRSIZE| is currently typedef'd as |int|.
315 // TODO(palmer): It should be a |size_t|, or at least unsigned.
316 // These lengths do not include the terminating NUL, but the underlying
317 // buffer is sized to be capable of holding it.
318 FX_STRSIZE m_nDataLength;
319 FX_STRSIZE m_nAllocLength;
320
321 // Not really 1, variable size.
322 FX_CHAR m_String[1];
323
324 private:
325 StringData(FX_STRSIZE dataLen, FX_STRSIZE allocLen);
326 ~StringData() = delete;
327 };
328 287
329 void ReallocBeforeWrite(FX_STRSIZE nNewLen); 288 void ReallocBeforeWrite(FX_STRSIZE nNewLen);
330 void AllocBeforeWrite(FX_STRSIZE nNewLen); 289 void AllocBeforeWrite(FX_STRSIZE nNewLen);
331 void AllocCopy(CFX_ByteString& dest, 290 void AllocCopy(CFX_ByteString& dest,
332 FX_STRSIZE nCopyLen, 291 FX_STRSIZE nCopyLen,
333 FX_STRSIZE nCopyIndex) const; 292 FX_STRSIZE nCopyIndex) const;
334 void AssignCopy(const FX_CHAR* pSrcData, FX_STRSIZE nSrcLen); 293 void AssignCopy(const FX_CHAR* pSrcData, FX_STRSIZE nSrcLen);
335 void Concat(const FX_CHAR* lpszSrcData, FX_STRSIZE nSrcLen); 294 void Concat(const FX_CHAR* lpszSrcData, FX_STRSIZE nSrcLen);
336 295
337 CFX_RetainPtr<StringData> m_pData; 296 CFX_RetainPtr<StringData> m_pData;
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after
763 } 722 }
764 723
765 FX_FLOAT FX_atof(const CFX_ByteStringC& str); 724 FX_FLOAT FX_atof(const CFX_ByteStringC& str);
766 inline FX_FLOAT FX_atof(const CFX_WideStringC& wsStr) { 725 inline FX_FLOAT FX_atof(const CFX_WideStringC& wsStr) {
767 return FX_atof(FX_UTF8Encode(wsStr.raw_str(), wsStr.GetLength()).c_str()); 726 return FX_atof(FX_UTF8Encode(wsStr.raw_str(), wsStr.GetLength()).c_str());
768 } 727 }
769 void FX_atonum(const CFX_ByteStringC& str, FX_BOOL& bInteger, void* pData); 728 void FX_atonum(const CFX_ByteStringC& str, FX_BOOL& bInteger, void* pData);
770 FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_CHAR* buf); 729 FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_CHAR* buf);
771 730
772 #endif // CORE_FXCRT_INCLUDE_FX_STRING_H_ 731 #endif // CORE_FXCRT_INCLUDE_FX_STRING_H_
OLDNEW
« no previous file with comments | « core/fxcrt/fx_basic_bstring.cpp ('k') | pdfium.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698