129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.text.GapTextStore;
|
|
14
|
131
|
15 import dwtx.jface.text.IDocumentPartitioningListener; // packageimport
|
|
16 import dwtx.jface.text.DefaultTextHover; // packageimport
|
|
17 import dwtx.jface.text.AbstractInformationControl; // packageimport
|
|
18 import dwtx.jface.text.TextUtilities; // packageimport
|
|
19 import dwtx.jface.text.IInformationControlCreatorExtension; // packageimport
|
|
20 import dwtx.jface.text.AbstractInformationControlManager; // packageimport
|
|
21 import dwtx.jface.text.ITextViewerExtension2; // packageimport
|
|
22 import dwtx.jface.text.IDocumentPartitioner; // packageimport
|
|
23 import dwtx.jface.text.DefaultIndentLineAutoEditStrategy; // packageimport
|
|
24 import dwtx.jface.text.ITextSelection; // packageimport
|
|
25 import dwtx.jface.text.Document; // packageimport
|
|
26 import dwtx.jface.text.FindReplaceDocumentAdapterContentProposalProvider; // packageimport
|
|
27 import dwtx.jface.text.ITextListener; // packageimport
|
|
28 import dwtx.jface.text.BadPartitioningException; // packageimport
|
|
29 import dwtx.jface.text.ITextViewerExtension5; // packageimport
|
|
30 import dwtx.jface.text.IDocumentPartitionerExtension3; // packageimport
|
|
31 import dwtx.jface.text.IUndoManager; // packageimport
|
|
32 import dwtx.jface.text.ITextHoverExtension2; // packageimport
|
|
33 import dwtx.jface.text.IRepairableDocument; // packageimport
|
|
34 import dwtx.jface.text.IRewriteTarget; // packageimport
|
|
35 import dwtx.jface.text.DefaultPositionUpdater; // packageimport
|
|
36 import dwtx.jface.text.RewriteSessionEditProcessor; // packageimport
|
|
37 import dwtx.jface.text.TextViewerHoverManager; // packageimport
|
|
38 import dwtx.jface.text.DocumentRewriteSession; // packageimport
|
|
39 import dwtx.jface.text.TextViewer; // packageimport
|
|
40 import dwtx.jface.text.ITextViewerExtension8; // packageimport
|
|
41 import dwtx.jface.text.RegExMessages; // packageimport
|
|
42 import dwtx.jface.text.IDelayedInputChangeProvider; // packageimport
|
|
43 import dwtx.jface.text.ITextOperationTargetExtension; // packageimport
|
|
44 import dwtx.jface.text.IWidgetTokenOwner; // packageimport
|
|
45 import dwtx.jface.text.IViewportListener; // packageimport
|
|
46 import dwtx.jface.text.MarkSelection; // packageimport
|
|
47 import dwtx.jface.text.IDocumentPartitioningListenerExtension; // packageimport
|
|
48 import dwtx.jface.text.IDocumentAdapterExtension; // packageimport
|
|
49 import dwtx.jface.text.IInformationControlExtension; // packageimport
|
|
50 import dwtx.jface.text.IDocumentPartitioningListenerExtension2; // packageimport
|
|
51 import dwtx.jface.text.DefaultDocumentAdapter; // packageimport
|
|
52 import dwtx.jface.text.ITextViewerExtension3; // packageimport
|
|
53 import dwtx.jface.text.IInformationControlCreator; // packageimport
|
|
54 import dwtx.jface.text.TypedRegion; // packageimport
|
|
55 import dwtx.jface.text.ISynchronizable; // packageimport
|
|
56 import dwtx.jface.text.IMarkRegionTarget; // packageimport
|
|
57 import dwtx.jface.text.TextViewerUndoManager; // packageimport
|
|
58 import dwtx.jface.text.IRegion; // packageimport
|
|
59 import dwtx.jface.text.IInformationControlExtension2; // packageimport
|
|
60 import dwtx.jface.text.IDocumentExtension4; // packageimport
|
|
61 import dwtx.jface.text.IDocumentExtension2; // packageimport
|
|
62 import dwtx.jface.text.IDocumentPartitionerExtension2; // packageimport
|
|
63 import dwtx.jface.text.Assert; // packageimport
|
|
64 import dwtx.jface.text.DefaultInformationControl; // packageimport
|
|
65 import dwtx.jface.text.IWidgetTokenOwnerExtension; // packageimport
|
|
66 import dwtx.jface.text.DocumentClone; // packageimport
|
|
67 import dwtx.jface.text.DefaultUndoManager; // packageimport
|
|
68 import dwtx.jface.text.IFindReplaceTarget; // packageimport
|
|
69 import dwtx.jface.text.IAutoEditStrategy; // packageimport
|
|
70 import dwtx.jface.text.ILineTrackerExtension; // packageimport
|
|
71 import dwtx.jface.text.IUndoManagerExtension; // packageimport
|
|
72 import dwtx.jface.text.TextSelection; // packageimport
|
|
73 import dwtx.jface.text.DefaultAutoIndentStrategy; // packageimport
|
|
74 import dwtx.jface.text.IAutoIndentStrategy; // packageimport
|
|
75 import dwtx.jface.text.IPainter; // packageimport
|
|
76 import dwtx.jface.text.IInformationControl; // packageimport
|
|
77 import dwtx.jface.text.IInformationControlExtension3; // packageimport
|
|
78 import dwtx.jface.text.ITextViewerExtension6; // packageimport
|
|
79 import dwtx.jface.text.IInformationControlExtension4; // packageimport
|
|
80 import dwtx.jface.text.DefaultLineTracker; // packageimport
|
|
81 import dwtx.jface.text.IDocumentInformationMappingExtension; // packageimport
|
|
82 import dwtx.jface.text.IRepairableDocumentExtension; // packageimport
|
|
83 import dwtx.jface.text.ITextHover; // packageimport
|
|
84 import dwtx.jface.text.FindReplaceDocumentAdapter; // packageimport
|
|
85 import dwtx.jface.text.ILineTracker; // packageimport
|
|
86 import dwtx.jface.text.Line; // packageimport
|
|
87 import dwtx.jface.text.ITextViewerExtension; // packageimport
|
|
88 import dwtx.jface.text.IDocumentAdapter; // packageimport
|
|
89 import dwtx.jface.text.TextEvent; // packageimport
|
|
90 import dwtx.jface.text.BadLocationException; // packageimport
|
|
91 import dwtx.jface.text.AbstractDocument; // packageimport
|
|
92 import dwtx.jface.text.AbstractLineTracker; // packageimport
|
|
93 import dwtx.jface.text.TreeLineTracker; // packageimport
|
|
94 import dwtx.jface.text.ITextPresentationListener; // packageimport
|
|
95 import dwtx.jface.text.Region; // packageimport
|
|
96 import dwtx.jface.text.ITextViewer; // packageimport
|
|
97 import dwtx.jface.text.IDocumentInformationMapping; // packageimport
|
|
98 import dwtx.jface.text.MarginPainter; // packageimport
|
|
99 import dwtx.jface.text.IPaintPositionManager; // packageimport
|
|
100 import dwtx.jface.text.TextPresentation; // packageimport
|
|
101 import dwtx.jface.text.IFindReplaceTargetExtension; // packageimport
|
|
102 import dwtx.jface.text.ISlaveDocumentManagerExtension; // packageimport
|
|
103 import dwtx.jface.text.ISelectionValidator; // packageimport
|
|
104 import dwtx.jface.text.IDocumentExtension; // packageimport
|
|
105 import dwtx.jface.text.PropagatingFontFieldEditor; // packageimport
|
|
106 import dwtx.jface.text.ConfigurableLineTracker; // packageimport
|
|
107 import dwtx.jface.text.SlaveDocumentEvent; // packageimport
|
|
108 import dwtx.jface.text.IDocumentListener; // packageimport
|
|
109 import dwtx.jface.text.PaintManager; // packageimport
|
|
110 import dwtx.jface.text.IFindReplaceTargetExtension3; // packageimport
|
|
111 import dwtx.jface.text.ITextDoubleClickStrategy; // packageimport
|
|
112 import dwtx.jface.text.IDocumentExtension3; // packageimport
|
|
113 import dwtx.jface.text.Position; // packageimport
|
|
114 import dwtx.jface.text.TextMessages; // packageimport
|
|
115 import dwtx.jface.text.CopyOnWriteTextStore; // packageimport
|
|
116 import dwtx.jface.text.WhitespaceCharacterPainter; // packageimport
|
|
117 import dwtx.jface.text.IPositionUpdater; // packageimport
|
|
118 import dwtx.jface.text.DefaultTextDoubleClickStrategy; // packageimport
|
|
119 import dwtx.jface.text.ListLineTracker; // packageimport
|
|
120 import dwtx.jface.text.ITextInputListener; // packageimport
|
|
121 import dwtx.jface.text.BadPositionCategoryException; // packageimport
|
|
122 import dwtx.jface.text.IWidgetTokenKeeperExtension; // packageimport
|
|
123 import dwtx.jface.text.IInputChangedListener; // packageimport
|
|
124 import dwtx.jface.text.ITextOperationTarget; // packageimport
|
|
125 import dwtx.jface.text.IDocumentInformationMappingExtension2; // packageimport
|
|
126 import dwtx.jface.text.ITextViewerExtension7; // packageimport
|
|
127 import dwtx.jface.text.IInformationControlExtension5; // packageimport
|
|
128 import dwtx.jface.text.IDocumentRewriteSessionListener; // packageimport
|
|
129 import dwtx.jface.text.JFaceTextUtil; // packageimport
|
|
130 import dwtx.jface.text.AbstractReusableInformationControlCreator; // packageimport
|
|
131 import dwtx.jface.text.TabsToSpacesConverter; // packageimport
|
|
132 import dwtx.jface.text.CursorLinePainter; // packageimport
|
|
133 import dwtx.jface.text.ITextHoverExtension; // packageimport
|
|
134 import dwtx.jface.text.IEventConsumer; // packageimport
|
|
135 import dwtx.jface.text.IDocument; // packageimport
|
|
136 import dwtx.jface.text.IWidgetTokenKeeper; // packageimport
|
|
137 import dwtx.jface.text.DocumentCommand; // packageimport
|
|
138 import dwtx.jface.text.TypedPosition; // packageimport
|
|
139 import dwtx.jface.text.IEditingSupportRegistry; // packageimport
|
|
140 import dwtx.jface.text.IDocumentPartitionerExtension; // packageimport
|
|
141 import dwtx.jface.text.AbstractHoverInformationControlManager; // packageimport
|
|
142 import dwtx.jface.text.IEditingSupport; // packageimport
|
|
143 import dwtx.jface.text.IMarkSelection; // packageimport
|
|
144 import dwtx.jface.text.ISlaveDocumentManager; // packageimport
|
|
145 import dwtx.jface.text.DocumentEvent; // packageimport
|
|
146 import dwtx.jface.text.DocumentPartitioningChangedEvent; // packageimport
|
|
147 import dwtx.jface.text.ITextStore; // packageimport
|
|
148 import dwtx.jface.text.JFaceTextMessages; // packageimport
|
|
149 import dwtx.jface.text.DocumentRewriteSessionEvent; // packageimport
|
|
150 import dwtx.jface.text.SequentialRewriteTextStore; // packageimport
|
|
151 import dwtx.jface.text.DocumentRewriteSessionType; // packageimport
|
|
152 import dwtx.jface.text.TextAttribute; // packageimport
|
|
153 import dwtx.jface.text.ITextViewerExtension4; // packageimport
|
|
154 import dwtx.jface.text.ITypedRegion; // packageimport
|
|
155
|
|
156
|
129
|
157 import dwt.dwthelper.utils;
|
|
158
|
|
159 import dwtx.core.runtime.Assert;
|
|
160
|
|
161
|
|
162 /**
|
|
163 * Implements a gap managing text store. The gap text store relies on the assumption that
|
|
164 * consecutive changes to a document are co-located. The start of the gap is always moved to the
|
|
165 * location of the last change.
|
|
166 * <p>
|
|
167 * <strong>Performance:</strong> Typing-style changes perform in constant time unless re-allocation
|
|
168 * becomes necessary. Generally, a change that does not cause re-allocation will cause at most one
|
|
169 * {@linkplain System#arraycopy(Object, int, Object, int, int) arraycopy} operation of a length of
|
|
170 * about <var>d</var>, where <var>d</var> is the distance from the previous change. Let <var>a(x)</var>
|
|
171 * be the algorithmic performance of an <code>arraycopy</code> operation of the length <var>x</var>,
|
|
172 * then such a change then performs in <i>O(a(x))</i>,
|
|
173 * {@linkplain #get(int, int) get(int, <var>length</var>)} performs in <i>O(a(length))</i>,
|
|
174 * {@link #get(int)} in <i>O(1)</i>.
|
|
175 * <p>
|
|
176 * How frequently the array needs re-allocation is controlled by the constructor parameters.
|
|
177 * </p>
|
|
178 * <p>
|
|
179 * This class is not intended to be subclassed.
|
|
180 * </p>
|
|
181 *
|
|
182 * @see CopyOnWriteTextStore for a copy-on-write text store wrapper
|
|
183 * @noextend This class is not intended to be subclassed by clients.
|
|
184 */
|
|
185 public class GapTextStore : ITextStore {
|
|
186 /**
|
|
187 * The minimum gap size allocated when re-allocation occurs.
|
|
188 * @since 3.3
|
|
189 */
|
|
190 private final int fMinGapSize;
|
|
191 /**
|
|
192 * The maximum gap size allocated when re-allocation occurs.
|
|
193 * @since 3.3
|
|
194 */
|
|
195 private final int fMaxGapSize;
|
|
196 /**
|
|
197 * The multiplier to compute the array size from the content length
|
|
198 * (1 <= fSizeMultiplier <= 2).
|
|
199 *
|
|
200 * @since 3.3
|
|
201 */
|
|
202 private final float fSizeMultiplier;
|
|
203
|
|
204 /** The store's content */
|
|
205 private char[] fContent= new char[0];
|
|
206 /** Starting index of the gap */
|
|
207 private int fGapStart= 0;
|
|
208 /** End index of the gap */
|
|
209 private int fGapEnd= 0;
|
|
210 /**
|
|
211 * The current high water mark. If a change would cause the gap to grow larger than this, the
|
|
212 * array is re-allocated.
|
|
213 * @since 3.3
|
|
214 */
|
|
215 private int fThreshold= 0;
|
|
216
|
|
217 /**
|
|
218 * Creates a new empty text store using the specified low and high watermarks.
|
|
219 *
|
|
220 * @param lowWatermark unused - at the lower bound, the array is only resized when the content
|
|
221 * does not fit
|
|
222 * @param highWatermark if the gap is ever larger than this, it will automatically be shrunken
|
|
223 * (>= 0)
|
|
224 * @deprecated use {@link GapTextStore#GapTextStore(int, int, float)} instead
|
|
225 */
|
|
226 public GapTextStore(int lowWatermark, int highWatermark) {
|
|
227 /*
|
|
228 * Legacy constructor. The API contract states that highWatermark is the upper bound for the
|
|
229 * gap size. Albeit this contract was not previously adhered to, it is now: The allocated
|
|
230 * gap size is fixed at half the highWatermark. Since the threshold is always twice the
|
|
231 * allocated gap size, the gap will never grow larger than highWatermark. Previously, the
|
|
232 * gap size was initialized to highWatermark, causing re-allocation if the content length
|
|
233 * shrunk right after allocation. The fixed gap size is now only half of the previous value,
|
|
234 * circumventing that problem (there was no API contract specifying the initial gap size).
|
|
235 *
|
|
236 * The previous implementation did not allow the gap size to become smaller than
|
|
237 * lowWatermark, which doesn't make any sense: that area of the gap was simply never ever
|
|
238 * used.
|
|
239 */
|
|
240 this(highWatermark / 2, highWatermark / 2, 0f);
|
|
241 }
|
|
242
|
|
243 /**
|
|
244 * Equivalent to
|
|
245 * {@linkplain GapTextStore#GapTextStore(int, int, float) new GapTextStore(256, 4096, 0.1f)}.
|
|
246 *
|
|
247 * @since 3.3
|
|
248 */
|
|
249 public GapTextStore() {
|
|
250 this(256, 4096, 0.1f);
|
|
251 }
|
|
252
|
|
253 /**
|
|
254 * Creates an empty text store that uses re-allocation thresholds relative to the content
|
|
255 * length. Re-allocation is controlled by the <em>gap factor</em>, which is the quotient of
|
|
256 * the gap size and the array size. Re-allocation occurs if a change causes the gap factor to go
|
|
257 * outside <code>[0, maxGapFactor]</code>. When re-allocation occurs, the array is sized
|
|
258 * such that the gap factor is <code>0.5 * maxGapFactor</code>. The gap size computed in this
|
|
259 * manner is bounded by the <code>minSize</code> and <code>maxSize</code> parameters.
|
|
260 * <p>
|
|
261 * A <code>maxGapFactor</code> of <code>0</code> creates a text store that never has a gap
|
|
262 * at all (if <code>minSize</code> is 0); a <code>maxGapFactor</code> of <code>1</code>
|
|
263 * creates a text store that doubles its size with every re-allocation and that never shrinks.
|
|
264 * </p>
|
|
265 * <p>
|
|
266 * The <code>minSize</code> and <code>maxSize</code> parameters are absolute bounds to the
|
|
267 * allocated gap size. Use <code>minSize</code> to avoid frequent re-allocation for small
|
|
268 * documents. Use <code>maxSize</code> to avoid a huge gap being allocated for large
|
|
269 * documents.
|
|
270 * </p>
|
|
271 *
|
|
272 * @param minSize the minimum gap size to allocate (>= 0; use 0 for no minimum)
|
|
273 * @param maxSize the maximum gap size to allocate (>= minSize; use
|
|
274 * {@link Integer#MAX_VALUE} for no maximum)
|
|
275 * @param maxGapFactor is the maximum fraction of the array that is occupied by the gap (<code>0 <= maxGapFactor <= 1</code>)
|
|
276 * @since 3.3
|
|
277 */
|
|
278 public GapTextStore(int minSize, int maxSize, float maxGapFactor) {
|
|
279 Assert.isLegal(0f <= maxGapFactor && maxGapFactor <= 1f);
|
|
280 Assert.isLegal(0 <= minSize && minSize <= maxSize);
|
|
281 fMinGapSize= minSize;
|
|
282 fMaxGapSize= maxSize;
|
|
283 fSizeMultiplier= 1 / (1 - maxGapFactor / 2);
|
|
284 }
|
|
285
|
|
286 /*
|
|
287 * @see dwtx.jface.text.ITextStore#get(int)
|
|
288 */
|
|
289 public final char get(int offset) {
|
|
290 if (offset < fGapStart)
|
|
291 return fContent[offset];
|
|
292
|
|
293 return fContent[offset + gapSize()];
|
|
294 }
|
|
295
|
|
296 /*
|
|
297 * @see dwtx.jface.text.ITextStore#get(int, int)
|
|
298 */
|
|
299 public final String get(int offset, int length) {
|
|
300 if (fGapStart <= offset)
|
|
301 return new String(fContent, offset + gapSize() , length);
|
|
302
|
|
303 final int end= offset + length;
|
|
304
|
|
305 if (end <= fGapStart)
|
|
306 return new String(fContent, offset, length);
|
|
307
|
|
308 StringBuffer buf= new StringBuffer(length);
|
|
309 buf.append(fContent, offset, fGapStart - offset);
|
|
310 buf.append(fContent, fGapEnd, end - fGapStart);
|
|
311 return buf.toString();
|
|
312 }
|
|
313
|
|
314 /*
|
|
315 * @see dwtx.jface.text.ITextStore#getLength()
|
|
316 */
|
|
317 public final int getLength() {
|
|
318 return fContent.length - gapSize();
|
|
319 }
|
|
320
|
|
321 /*
|
|
322 * @see dwtx.jface.text.ITextStore#set(java.lang.String)
|
|
323 */
|
|
324 public final void set(String text) {
|
|
325 /*
|
|
326 * Moves the gap to the end of the content. There is no sensible prediction of where the
|
|
327 * next change will occur, but at least the next change will not trigger re-allocation. This
|
|
328 * is especially important when using the GapTextStore within a CopyOnWriteTextStore, where
|
|
329 * the GTS is only initialized right before a modification.
|
|
330 */
|
|
331 replace(0, getLength(), text);
|
|
332 }
|
|
333
|
|
334 /*
|
|
335 * @see dwtx.jface.text.ITextStore#replace(int, int, java.lang.String)
|
|
336 */
|
|
337 public final void replace(int offset, int length, String text) {
|
|
338 if (text is null) {
|
|
339 adjustGap(offset, length, 0);
|
|
340 } else {
|
|
341 int textLength= text.length();
|
|
342 adjustGap(offset, length, textLength);
|
|
343 if (textLength !is 0)
|
|
344 text.getChars(0, textLength, fContent, offset);
|
|
345 }
|
|
346 }
|
|
347
|
|
348 /**
|
|
349 * Moves the gap to <code>offset + add</code>, moving any content after
|
|
350 * <code>offset + remove</code> behind the gap. The gap size is kept between 0 and
|
|
351 * {@link #fThreshold}, leading to re-allocation if needed. The content between
|
|
352 * <code>offset</code> and <code>offset + add</code> is undefined after this operation.
|
|
353 *
|
|
354 * @param offset the offset at which a change happens
|
|
355 * @param remove the number of character which are removed or overwritten at <code>offset</code>
|
|
356 * @param add the number of character which are inserted or overwriting at <code>offset</code>
|
|
357 */
|
|
358 private void adjustGap(int offset, int remove, int add) {
|
|
359 final int oldGapSize= gapSize();
|
|
360 final int newGapSize= oldGapSize - add + remove;
|
|
361 final bool reuseArray= 0 <= newGapSize && newGapSize <= fThreshold;
|
|
362
|
|
363 final int newGapStart= offset + add;
|
|
364 final int newGapEnd;
|
|
365
|
|
366 if (reuseArray)
|
|
367 newGapEnd= moveGap(offset, remove, oldGapSize, newGapSize, newGapStart);
|
|
368 else
|
|
369 newGapEnd= reallocate(offset, remove, oldGapSize, newGapSize, newGapStart);
|
|
370
|
|
371 fGapStart= newGapStart;
|
|
372 fGapEnd= newGapEnd;
|
|
373 }
|
|
374
|
|
375 /**
|
|
376 * Moves the gap to <code>newGapStart</code>.
|
|
377 *
|
|
378 * @param offset the change offset
|
|
379 * @param remove the number of removed / overwritten characters
|
|
380 * @param oldGapSize the old gap size
|
|
381 * @param newGapSize the gap size after the change
|
|
382 * @param newGapStart the offset in the array to move the gap to
|
|
383 * @return the new gap end
|
|
384 * @since 3.3
|
|
385 */
|
|
386 private int moveGap(int offset, int remove, int oldGapSize, int newGapSize, int newGapStart) {
|
|
387 /*
|
|
388 * No re-allocation necessary. The area between the change offset and gap can be copied
|
|
389 * in at most one operation. Don't copy parts that will be overwritten anyway.
|
|
390 */
|
|
391 final int newGapEnd= newGapStart + newGapSize;
|
|
392 if (offset < fGapStart) {
|
|
393 int afterRemove= offset + remove;
|
|
394 if (afterRemove < fGapStart) {
|
|
395 final int betweenSize= fGapStart - afterRemove;
|
|
396 arrayCopy(afterRemove, fContent, newGapEnd, betweenSize);
|
|
397 }
|
|
398 // otherwise, only the gap gets enlarged
|
|
399 } else {
|
|
400 final int offsetShifted= offset + oldGapSize;
|
|
401 final int betweenSize= offsetShifted - fGapEnd; // in the typing case, betweenSize is 0
|
|
402 arrayCopy(fGapEnd, fContent, fGapStart, betweenSize);
|
|
403 }
|
|
404 return newGapEnd;
|
|
405 }
|
|
406
|
|
407 /**
|
|
408 * Reallocates a new array and copies the data from the previous one.
|
|
409 *
|
|
410 * @param offset the change offset
|
|
411 * @param remove the number of removed / overwritten characters
|
|
412 * @param oldGapSize the old gap size
|
|
413 * @param newGapSize the gap size after the change if no re-allocation would occur (can be negative)
|
|
414 * @param newGapStart the offset in the array to move the gap to
|
|
415 * @return the new gap end
|
|
416 * @since 3.3
|
|
417 */
|
|
418 private int reallocate(int offset, int remove, final int oldGapSize, int newGapSize, final int newGapStart) {
|
|
419 // the new content length (without any gap)
|
|
420 final int newLength= fContent.length - newGapSize;
|
|
421 // the new array size based on the gap factor
|
|
422 int newArraySize= (int) (newLength * fSizeMultiplier);
|
|
423 newGapSize= newArraySize - newLength;
|
|
424
|
|
425 // bound the gap size within min/max
|
|
426 if (newGapSize < fMinGapSize) {
|
|
427 newGapSize= fMinGapSize;
|
|
428 newArraySize= newLength + newGapSize;
|
|
429 } else if (newGapSize > fMaxGapSize) {
|
|
430 newGapSize= fMaxGapSize;
|
|
431 newArraySize= newLength + newGapSize;
|
|
432 }
|
|
433
|
|
434 // the upper threshold is always twice the gapsize
|
|
435 fThreshold= newGapSize * 2;
|
|
436 final char[] newContent= allocate(newArraySize);
|
|
437 final int newGapEnd= newGapStart + newGapSize;
|
|
438
|
|
439 /*
|
|
440 * Re-allocation: The old content can be copied in at most 3 operations to the newly allocated
|
|
441 * array. Either one of change offset and the gap may come first.
|
|
442 * - unchanged area before the change offset / gap
|
|
443 * - area between the change offset and the gap (either one may be first)
|
|
444 * - rest area after the change offset / after the gap
|
|
445 */
|
|
446 if (offset < fGapStart) {
|
|
447 // change comes before gap
|
|
448 arrayCopy(0, newContent, 0, offset);
|
|
449 int afterRemove= offset + remove;
|
|
450 if (afterRemove < fGapStart) {
|
|
451 // removal is completely before the gap
|
|
452 final int betweenSize= fGapStart - afterRemove;
|
|
453 arrayCopy(afterRemove, newContent, newGapEnd, betweenSize);
|
|
454 final int restSize= fContent.length - fGapEnd;
|
|
455 arrayCopy(fGapEnd, newContent, newGapEnd + betweenSize, restSize);
|
|
456 } else {
|
|
457 // removal encompasses the gap
|
|
458 afterRemove += oldGapSize;
|
|
459 final int restSize= fContent.length - afterRemove;
|
|
460 arrayCopy(afterRemove, newContent, newGapEnd, restSize);
|
|
461 }
|
|
462 } else {
|
|
463 // gap comes before change
|
|
464 arrayCopy(0, newContent, 0, fGapStart);
|
|
465 final int offsetShifted= offset + oldGapSize;
|
|
466 final int betweenSize= offsetShifted - fGapEnd;
|
|
467 arrayCopy(fGapEnd, newContent, fGapStart, betweenSize);
|
|
468 final int afterRemove= offsetShifted + remove;
|
|
469 final int restSize= fContent.length - afterRemove;
|
|
470 arrayCopy(afterRemove, newContent, newGapEnd, restSize);
|
|
471 }
|
|
472
|
|
473 fContent= newContent;
|
|
474 return newGapEnd;
|
|
475 }
|
|
476
|
|
477 /**
|
|
478 * Allocates a new <code>char[size]</code>.
|
|
479 *
|
|
480 * @param size the length of the new array.
|
|
481 * @return a newly allocated char array
|
|
482 * @since 3.3
|
|
483 */
|
|
484 private char[] allocate(int size) {
|
|
485 return new char[size];
|
|
486 }
|
|
487
|
|
488 /*
|
|
489 * Executes System.arraycopy if length !is 0. A length < 0 cannot happen -> don't hide coding
|
|
490 * errors by checking for negative lengths.
|
|
491 * @since 3.3
|
|
492 */
|
|
493 private void arrayCopy(int srcPos, char[] dest, int destPos, int length) {
|
|
494 if (length !is 0)
|
|
495 System.arraycopy(fContent, srcPos, dest, destPos, length);
|
|
496 }
|
|
497
|
|
498 /**
|
|
499 * Returns the gap size.
|
|
500 *
|
|
501 * @return the gap size
|
|
502 * @since 3.3
|
|
503 */
|
|
504 private int gapSize() {
|
|
505 return fGapEnd - fGapStart;
|
|
506 }
|
|
507
|
|
508 /**
|
|
509 * Returns a copy of the content of this text store.
|
|
510 * For internal use only.
|
|
511 *
|
|
512 * @return a copy of the content of this text store
|
|
513 */
|
|
514 protected String getContentAsString() {
|
|
515 return new String(fContent);
|
|
516 }
|
|
517
|
|
518 /**
|
|
519 * Returns the start index of the gap managed by this text store.
|
|
520 * For internal use only.
|
|
521 *
|
|
522 * @return the start index of the gap managed by this text store
|
|
523 */
|
|
524 protected int getGapStartIndex() {
|
|
525 return fGapStart;
|
|
526 }
|
|
527
|
|
528 /**
|
|
529 * Returns the end index of the gap managed by this text store.
|
|
530 * For internal use only.
|
|
531 *
|
|
532 * @return the end index of the gap managed by this text store
|
|
533 */
|
|
534 protected int getGapEndIndex() {
|
|
535 return fGapEnd;
|
|
536 }
|
|
537 }
|