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 XFA_FDE_CFDE_TXTEDTBUF_H_
8 #define XFA_FDE_CFDE_TXTEDTBUF_H_
9 
10 #include <memory>
11 #include <tuple>
12 #include <vector>
13 
14 #include "core/fxcrt/fx_basic.h"
15 #include "core/fxcrt/fx_system.h"
16 #include "xfa/fde/ifx_chariter.h"
17 
18 class IFX_Pause;
19 
20 class CFDE_TxtEdtBuf {
21  public:
22   class Iterator : public IFX_CharIter {
23    public:
24     explicit Iterator(CFDE_TxtEdtBuf* pBuf, FX_WCHAR wcAlias = 0);
25     ~Iterator() override;
26 
27     bool Next(bool bPrev = false) override;
28     FX_WCHAR GetChar() override;
29 
30     void SetAt(int32_t nIndex) override;
31     int32_t GetAt() const override;
32 
33     bool IsEOF(bool bTail = true) const override;
34     IFX_CharIter* Clone() override;
35 
36    private:
37     CFDE_TxtEdtBuf* m_pBuf;
38     int32_t m_nCurChunk;
39     int32_t m_nCurIndex;
40     int32_t m_nIndex;
41     FX_WCHAR m_Alias;
42   };
43 
44   CFDE_TxtEdtBuf();
45   ~CFDE_TxtEdtBuf();
46 
47   int32_t GetChunkSize() const;
48   int32_t GetTextLength() const;
49 
50   void SetText(const CFX_WideString& wsText);
51   CFX_WideString GetText() const;
52 
53   FX_WCHAR GetCharByIndex(int32_t nIndex) const;
54   CFX_WideString GetRange(int32_t nBegin, int32_t nCount) const;
55 
56   void Insert(int32_t nPos, const FX_WCHAR* lpText, int32_t nLength);
57   void Delete(int32_t nIndex, int32_t nLength);
58   void Clear(bool bRelease);
59 
60  private:
61   friend class Iterator;
62   friend class CFDE_TxtEdtBufTest;
63 
64   class ChunkHeader {
65    public:
66     ChunkHeader();
67     ~ChunkHeader();
68 
69     int32_t nUsed;
70     std::unique_ptr<FX_WCHAR, FxFreeDeleter> wChars;
71   };
72 
73   void SetChunkSizeForTesting(size_t size);
74   std::tuple<int32_t, int32_t> Index2CP(int32_t nIndex) const;
75   std::unique_ptr<ChunkHeader> NewChunk();
76 
77   size_t m_chunkSize;
78   int32_t m_nTotal;
79   std::vector<std::unique_ptr<ChunkHeader>> m_chunks;
80 };
81 
82 #endif  // XFA_FDE_CFDE_TXTEDTBUF_H_
83