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