1 //===- RewriteRope.h - Rope specialized for rewriter ------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines the RewriteRope class, which is a powerful string class.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_REWRITE_CORE_REWRITEROPE_H
14 #define LLVM_CLANG_REWRITE_CORE_REWRITEROPE_H
16 #include "llvm/ADT/IntrusiveRefCntPtr.h"
17 #include "llvm/ADT/StringRef.h"
25 //===--------------------------------------------------------------------===//
26 // RopeRefCountString Class
27 //===--------------------------------------------------------------------===//
29 /// RopeRefCountString - This struct is allocated with 'new char[]' from the
30 /// heap, and represents a reference counted chunk of string data. When its
31 /// ref count drops to zero, it is delete[]'d. This is primarily managed
32 /// through the RopePiece class below.
33 struct RopeRefCountString {
35 char Data[1]; // Variable sized.
37 void Retain() { ++RefCount; }
40 assert(RefCount > 0 && "Reference count is already zero.");
42 delete [] (char*)this;
46 //===--------------------------------------------------------------------===//
48 //===--------------------------------------------------------------------===//
50 /// RopePiece - This class represents a view into a RopeRefCountString object.
51 /// This allows references to string data to be efficiently chopped up and
52 /// moved around without having to push around the string data itself.
54 /// For example, we could have a 1M RopePiece and want to insert something
55 /// into the middle of it. To do this, we split it into two RopePiece objects
56 /// that both refer to the same underlying RopeRefCountString (just with
57 /// different offsets) which is a nice constant time operation.
59 llvm::IntrusiveRefCntPtr<RopeRefCountString> StrData;
60 unsigned StartOffs = 0;
63 RopePiece() = default;
64 RopePiece(llvm::IntrusiveRefCntPtr<RopeRefCountString> Str, unsigned Start,
66 : StrData(std::move(Str)), StartOffs(Start), EndOffs(End) {}
68 const char &operator[](unsigned Offset) const {
69 return StrData->Data[Offset+StartOffs];
71 char &operator[](unsigned Offset) {
72 return StrData->Data[Offset+StartOffs];
75 unsigned size() const { return EndOffs-StartOffs; }
78 //===--------------------------------------------------------------------===//
79 // RopePieceBTreeIterator Class
80 //===--------------------------------------------------------------------===//
82 /// RopePieceBTreeIterator - This class provides read-only forward iteration
83 /// over bytes that are in a RopePieceBTree. This first iterates over bytes
84 /// in a RopePiece, then iterates over RopePiece's in a RopePieceBTreeLeaf,
85 /// then iterates over RopePieceBTreeLeaf's in a RopePieceBTree.
86 class RopePieceBTreeIterator :
87 public std::iterator<std::forward_iterator_tag, const char, ptrdiff_t> {
88 /// CurNode - The current B+Tree node that we are inspecting.
89 const void /*RopePieceBTreeLeaf*/ *CurNode = nullptr;
91 /// CurPiece - The current RopePiece in the B+Tree node that we're
93 const RopePiece *CurPiece = nullptr;
95 /// CurChar - The current byte in the RopePiece we are pointing to.
99 RopePieceBTreeIterator() = default;
100 RopePieceBTreeIterator(const void /*RopePieceBTreeNode*/ *N);
102 char operator*() const {
103 return (*CurPiece)[CurChar];
106 bool operator==(const RopePieceBTreeIterator &RHS) const {
107 return CurPiece == RHS.CurPiece && CurChar == RHS.CurChar;
109 bool operator!=(const RopePieceBTreeIterator &RHS) const {
110 return !operator==(RHS);
113 RopePieceBTreeIterator& operator++() { // Preincrement
114 if (CurChar+1 < CurPiece->size())
121 RopePieceBTreeIterator operator++(int) { // Postincrement
122 RopePieceBTreeIterator tmp = *this; ++*this; return tmp;
125 llvm::StringRef piece() const {
126 return llvm::StringRef(&(*CurPiece)[0], CurPiece->size());
129 void MoveToNextPiece();
132 //===--------------------------------------------------------------------===//
133 // RopePieceBTree Class
134 //===--------------------------------------------------------------------===//
136 class RopePieceBTree {
137 void /*RopePieceBTreeNode*/ *Root;
141 RopePieceBTree(const RopePieceBTree &RHS);
142 RopePieceBTree &operator=(const RopePieceBTree &) = delete;
145 using iterator = RopePieceBTreeIterator;
147 iterator begin() const { return iterator(Root); }
148 iterator end() const { return iterator(); }
149 unsigned size() const;
150 unsigned empty() const { return size() == 0; }
154 void insert(unsigned Offset, const RopePiece &R);
156 void erase(unsigned Offset, unsigned NumBytes);
159 //===--------------------------------------------------------------------===//
161 //===--------------------------------------------------------------------===//
163 /// RewriteRope - A powerful string class. This class supports extremely
164 /// efficient insertions and deletions into the middle of it, even for
165 /// ridiculously long strings.
167 RopePieceBTree Chunks;
169 /// We allocate space for string data out of a buffer of size AllocChunkSize.
170 /// This keeps track of how much space is left.
171 llvm::IntrusiveRefCntPtr<RopeRefCountString> AllocBuffer;
172 enum { AllocChunkSize = 4080 };
173 unsigned AllocOffs = AllocChunkSize;
176 RewriteRope() = default;
177 RewriteRope(const RewriteRope &RHS) : Chunks(RHS.Chunks) {}
179 using iterator = RopePieceBTree::iterator;
180 using const_iterator = RopePieceBTree::iterator;
182 iterator begin() const { return Chunks.begin(); }
183 iterator end() const { return Chunks.end(); }
184 unsigned size() const { return Chunks.size(); }
190 void assign(const char *Start, const char *End) {
193 Chunks.insert(0, MakeRopeString(Start, End));
196 void insert(unsigned Offset, const char *Start, const char *End) {
197 assert(Offset <= size() && "Invalid position to insert!");
198 if (Start == End) return;
199 Chunks.insert(Offset, MakeRopeString(Start, End));
202 void erase(unsigned Offset, unsigned NumBytes) {
203 assert(Offset+NumBytes <= size() && "Invalid region to erase!");
204 if (NumBytes == 0) return;
205 Chunks.erase(Offset, NumBytes);
209 RopePiece MakeRopeString(const char *Start, const char *End);
214 #endif // LLVM_CLANG_REWRITE_CORE_REWRITEROPE_H