1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
//===--- RewriteRope.h - Rope specialized for rewriter ----------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the RewriteRope class, which is a powerful string class.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_REWRITEROPE_H
#define LLVM_CLANG_REWRITEROPE_H
#include "llvm/ADT/iterator"
#include <list>
#include <cstring>
namespace clang {
struct RopeRefCountString {
unsigned RefCount;
char Data[1]; // Variable sized.
};
struct RopePiece {
RopeRefCountString *StrData;
unsigned StartOffs;
unsigned EndOffs;
RopePiece(RopeRefCountString *Str, unsigned Start, unsigned End)
: StrData(Str), StartOffs(Start), EndOffs(End) {
++StrData->RefCount;
}
RopePiece(const RopePiece &RP)
: StrData(RP.StrData), StartOffs(RP.StartOffs), EndOffs(RP.EndOffs) {
++StrData->RefCount;
}
~RopePiece() {
if (--StrData->RefCount == 0)
delete [] (char*)StrData;
}
const char &operator[](unsigned Offset) const {
return StrData->Data[Offset+StartOffs];
}
char &operator[](unsigned Offset) {
return StrData->Data[Offset+StartOffs];
}
unsigned size() const { return EndOffs-StartOffs; }
};
class RewriteRope;
template <typename CharType, typename PieceIterType>
class RewriteRopeIterator :
public bidirectional_iterator<CharType, ptrdiff_t> {
PieceIterType CurPiece;
unsigned CurChar;
friend class RewriteRope;
public:
RewriteRopeIterator(const PieceIterType &curPiece, unsigned curChar)
: CurPiece(curPiece), CurChar(curChar) {}
CharType &operator*() const {
return (*CurPiece)[CurChar];
}
bool operator==(const RewriteRopeIterator &RHS) const {
return CurPiece == RHS.CurPiece && CurChar == RHS.CurChar;
}
bool operator!=(const RewriteRopeIterator &RHS) const {
return !operator==(RHS);
}
inline RewriteRopeIterator& operator++() { // Preincrement
if (CurChar+1 < CurPiece->size())
++CurChar;
else {
CurChar = 0;
++CurPiece;
}
return *this;
}
RewriteRopeIterator operator+(int Offset) const {
assert(Offset >= 0 && "FIXME: Only handle forward case so far!");
PieceIterType Piece = CurPiece;
unsigned Char = CurChar;
while (Char+Offset >= Piece->size()) {
Offset -= Piece->size()-Char;
++Piece;
Char = 0;
}
Char += Offset;
return RewriteRopeIterator(Piece, Char);
}
inline RewriteRopeIterator operator++(int) { // Postincrement
RewriteRopeIterator tmp = *this; ++*this; return tmp;
}
};
/// RewriteRope - A powerful string class, todo generalize this.
class RewriteRope {
// FIXME: This could be significantly faster by using a balanced binary tree
// instead of a list.
std::list<RopePiece> Chunks;
unsigned CurSize;
/// We allocate space for string data out of a buffer of size AllocChunkSize.
/// This keeps track of how much space is left.
RopeRefCountString *AllocBuffer;
unsigned AllocOffs;
enum { AllocChunkSize = 4080 };
public:
RewriteRope() : CurSize(0), AllocBuffer(0), AllocOffs(AllocChunkSize) {}
~RewriteRope() { clear(); }
typedef RewriteRopeIterator<char, std::list<RopePiece>::iterator> iterator;
typedef RewriteRopeIterator<const char,
std::list<RopePiece>::const_iterator> const_iterator;
iterator begin() { return iterator(Chunks.begin(), 0); }
iterator end() { return iterator(Chunks.end(), 0); }
const_iterator begin() const { return const_iterator(Chunks.begin(), 0); }
const_iterator end() const { return const_iterator(Chunks.end(), 0); }
unsigned size() const { return CurSize; }
void clear() {
Chunks.clear();
CurSize = 0;
}
void assign(const char *Start, const char *End) {
clear();
Chunks.push_back(MakeRopeString(Start, End));
CurSize = End-Start;
}
iterator getAtOffset(unsigned Offset) {
assert(Offset <= CurSize && "Offset out of range!");
if (Offset == CurSize) return iterator(Chunks.end(), 0);
std::list<RopePiece>::iterator Piece = Chunks.begin();
while (Offset >= Piece->size()) {
Offset -= Piece->size();
++Piece;
}
return iterator(Piece, Offset);
}
const_iterator getAtOffset(unsigned Offset) const {
assert(Offset <= CurSize && "Offset out of range!");
if (Offset == CurSize) return const_iterator(Chunks.end(), 0);
std::list<RopePiece>::const_iterator Piece = Chunks.begin();
while (Offset >= Piece->size()) {
Offset -= Piece->size();
++Piece;
}
return const_iterator(Piece, Offset);
}
void insert(iterator Loc, const char *Start, const char *End) {
if (Start == End) return;
Chunks.insert(SplitAt(Loc), MakeRopeString(Start, End));
CurSize += End-Start;
}
void erase(iterator Start, iterator End) {
if (Start == End) return;
// If erase is localized within the same chunk, this is a degenerate case.
if (Start.CurPiece == End.CurPiece) {
RopePiece &Chunk = *Start.CurPiece;
unsigned NumDel = End.CurChar-Start.CurChar;
CurSize -= NumDel;
// If deleting from start of chunk, just adjust range.
if (Start.CurChar == 0) {
if (Chunk.EndOffs != End.CurChar)
Chunk.StartOffs += NumDel;
else // Deleting entire chunk.
Chunks.erase(End.CurPiece);
return;
}
// If deleting to the end of chunk, just adjust range.
if (End.CurChar == Chunk.size()) {
Chunk.EndOffs -= NumDel;
return;
}
// If deleting the middle of a chunk, split this chunk and adjust the end
// piece.
SplitAt(Start)->StartOffs += NumDel;
return;
}
// Otherwise, the start chunk and the end chunk are different.
std::list<RopePiece>::iterator CurPiece = Start.CurPiece;
// Delete the end of the start chunk. If it is the whole thing, remove it.
{
RopePiece &StartChunk = *CurPiece;
unsigned NumDel = StartChunk.size()-Start.CurChar;
CurSize -= NumDel;
if (Start.CurChar == 0) {
// Delete the whole chunk.
Chunks.erase(CurPiece++);
} else {
// Otherwise, just move the end of chunk marker up.
StartChunk.EndOffs -= NumDel;
++CurPiece;
}
}
// If deleting a span of chunks, nuke them all now.
while (CurPiece != End.CurPiece) {
CurSize -= CurPiece->size();
Chunks.erase(CurPiece++);
}
// Finally, erase the start of the end chunk if appropriate.
if (End.CurChar != 0) {
End.CurPiece->StartOffs += End.CurChar;
CurSize -= End.CurChar;
}
}
private:
RopePiece MakeRopeString(const char *Start, const char *End) {
unsigned Len = End-Start;
// If we have space for this string in the current alloc buffer, use it.
if (AllocOffs+Len <= AllocChunkSize) {
memcpy(AllocBuffer->Data+AllocOffs, Start, Len);
AllocOffs += Len;
return RopePiece(AllocBuffer, AllocOffs-Len, AllocOffs);
|