7
7
* modify it under the terms of the GNU Lesser General Public License
8
8
* as published by the Free Software Foundation; either version 2
9
9
* of the License, or (at your option) any later version.
10
- *
10
+ *
11
11
* This program is distributed in the hope that it will be useful,
12
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
14
* GNU Lesser General Public License for more details.
15
- *
15
+ *
16
16
* You should have received a copy of the GNU Lesser General Public License
17
17
* along with this program; if not, write to the Free Software Foundation
18
18
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
19
*
20
20
* This class is to large extents copied from Saxon 2003-01-21 (version ?).
21
21
* See comment at the back about licensing for those parts.
22
- *
22
+ *
23
23
* $Id$
24
24
*/
25
25
package org .exist .util ;
26
26
27
+ import org .apache .commons .lang3 .StringUtils ;
28
+
27
29
import java .io .Serializable ;
28
30
import java .io .Writer ;
29
31
36
38
37
39
public final class FastStringBuffer implements CharSequence , Serializable {
38
40
39
- private static final long serialVersionUID = -504264698052799896L ;
41
+ private static final long serialVersionUID = -504264698052799896L ;
40
42
41
- private char [] array ;
43
+ private char [] array ;
42
44
private int used = 0 ;
43
45
44
- public FastStringBuffer (int initialSize ) {
46
+ /**
47
+ * Create a FastStringBuffer with a given initial capacity
48
+ * @param initialSize the initial capacity
49
+ */
50
+
51
+ public FastStringBuffer (final int initialSize ) {
45
52
array = new char [initialSize ];
46
53
}
47
54
@@ -50,8 +57,8 @@ public FastStringBuffer(int initialSize) {
50
57
* @param s the String to be appended
51
58
*/
52
59
53
- public void append (String s ) {
54
- int len = s .length ();
60
+ public void append (final String s ) {
61
+ final int len = s .length ();
55
62
ensureCapacity (len );
56
63
s .getChars (0 , len , array , used );
57
64
used += len ;
@@ -62,8 +69,8 @@ public void append(String s) {
62
69
* @param s the String to be appended
63
70
*/
64
71
65
- public void append (CharSlice s ) {
66
- int len = s .length ();
72
+ public void append (final CharSlice s ) {
73
+ final int len = s .length ();
67
74
ensureCapacity (len );
68
75
s .copyTo (array , used );
69
76
used += len ;
@@ -74,8 +81,8 @@ public void append(CharSlice s) {
74
81
* @param s the FastStringBuffer to be appended
75
82
*/
76
83
77
- public void append (FastStringBuffer s ) {
78
- int len = s .length ();
84
+ public void append (final FastStringBuffer s ) {
85
+ final int len = s .length ();
79
86
ensureCapacity (len );
80
87
s .getChars (0 , len , array , used );
81
88
used += len ;
@@ -86,8 +93,8 @@ public void append(FastStringBuffer s) {
86
93
* @param s the StringBuffer to be appended
87
94
*/
88
95
89
- public void append (StringBuffer s ) {
90
- int len = s .length ();
96
+ public void append (final StringBuilder s ) {
97
+ final int len = s .length ();
91
98
ensureCapacity (len );
92
99
s .getChars (0 , len , array , used );
93
100
used += len ;
@@ -98,7 +105,7 @@ public void append(StringBuffer s) {
98
105
* @param s the CharSequence to be appended
99
106
*/
100
107
101
- public void append (CharSequence s ) {
108
+ public void append (final CharSequence s ) {
102
109
// Although we provide variants of this method for different subtypes, Java decides which to use based
103
110
// on the static type of the operand. We want to use the right method based on the dynamic type, to avoid
104
111
// creating objects and copying strings unnecessarily. So we do a dynamic dispatch.
@@ -126,7 +133,7 @@ public void append(CharSequence s) {
126
133
* @param length the number of characters to be copied
127
134
*/
128
135
129
- public void append (char [] srcArray , int start , int length ) {
136
+ public void append (final char [] srcArray , final int start , final int length ) {
130
137
ensureCapacity (length );
131
138
System .arraycopy (srcArray , start , array , used , length );
132
139
used += length ;
@@ -137,7 +144,7 @@ public void append(char[] srcArray, int start, int length) {
137
144
* @param srcArray the array whose contents are to be added
138
145
*/
139
146
140
- public void append (char [] srcArray ) {
147
+ public void append (final char [] srcArray ) {
141
148
final int length = srcArray .length ;
142
149
ensureCapacity (length );
143
150
System .arraycopy (srcArray , 0 , array , used , length );
@@ -149,7 +156,7 @@ public void append(char[] srcArray) {
149
156
* @param ch the character to be added
150
157
*/
151
158
152
- public void append (char ch ) {
159
+ public void append (final char ch ) {
153
160
ensureCapacity (1 );
154
161
array [used ++] = ch ;
155
162
}
@@ -173,7 +180,7 @@ public void appendWideChar(final int ch) {
173
180
*
174
181
* @param ch the character
175
182
*/
176
- public void prependWideChar (int ch ) {
183
+ public void prependWideChar (final int ch ) {
177
184
if (ch > 0xffff ) {
178
185
insertCharAt (0 , XMLChar .lowSurrogate (ch ));
179
186
insertCharAt (0 , XMLChar .highSurrogate (ch ));
@@ -207,7 +214,7 @@ public int length() {
207
214
* @throws IndexOutOfBoundsException if the <code>index</code> argument is negative or not less than
208
215
* <code>length()</code>
209
216
*/
210
- public char charAt (int index ) {
217
+ public char charAt (final int index ) {
211
218
if (index >= used ) {
212
219
throw new IndexOutOfBoundsException ("" + index );
213
220
}
@@ -229,7 +236,7 @@ public char charAt(int index) {
229
236
* if <code>end</code> is greater than <code>length()</code>,
230
237
* or if <code>start</code> is greater than <code>end</code>
231
238
*/
232
- public CharSequence subSequence (int start , int end ) {
239
+ public CharSequence subSequence (final int start , final int end ) {
233
240
return new CharSlice (array , start , end - start );
234
241
}
235
242
@@ -263,7 +270,7 @@ public CharSequence subSequence(int start, int end) {
263
270
* <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than
264
271
* <code>dst.length</code></ul>
265
272
*/
266
- public void getChars (int srcBegin , int srcEnd , char dst [], int dstBegin ) {
273
+ public void getChars (final int srcBegin , final int srcEnd , final char [] dst , final int dstBegin ) {
267
274
if (srcBegin < 0 ) {
268
275
throw new StringIndexOutOfBoundsException (srcBegin );
269
276
}
@@ -281,7 +288,7 @@ public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
281
288
* @param ch the character to search for
282
289
* @return the position of the first occurrence, or -1 if not found
283
290
*/
284
- public int indexOf (char ch ) {
291
+ public int indexOf (final char ch ) {
285
292
for (int i =0 ; i <used ; i ++) {
286
293
if (array [i ] == ch ) {
287
294
return i ;
@@ -304,7 +311,7 @@ public String toString() {
304
311
* @param ch the new character to overwrite the existing character at that location
305
312
* @throws IndexOutOfBoundsException if {@code int < 0 || int >= length()}
306
313
*/
307
- public void setCharAt (int index , char ch ) {
314
+ public void setCharAt (final int index , final char ch ) {
308
315
if (index <0 || index >used ) {
309
316
throw new IndexOutOfBoundsException ("" +index );
310
317
}
@@ -317,14 +324,12 @@ public void setCharAt(int index, char ch) {
317
324
* @param ch the new character to insert at that location
318
325
* @throws IndexOutOfBoundsException if {@code int < 0 || int >= length()}
319
326
*/
320
- public void insertCharAt (int index , char ch ) {
327
+ public void insertCharAt (final int index , final char ch ) {
321
328
if (index <0 || index >used ) {
322
329
throw new IndexOutOfBoundsException ("" +index );
323
330
}
324
331
ensureCapacity (1 );
325
- for (int i =used ; i >index ; i --) {
326
- array [i ] = array [i -1 ];
327
- }
332
+ System .arraycopy (array , index , array , index + 1 , used - index );
328
333
used ++;
329
334
array [index ] = ch ;
330
335
}
@@ -334,14 +339,12 @@ public void insertCharAt(int index, char ch) {
334
339
* @param index the index of the character to be set
335
340
* @throws IndexOutOfBoundsException if {@code int < 0 || int >= length()}
336
341
*/
337
- public void removeCharAt (int index ) {
342
+ public void removeCharAt (final int index ) {
338
343
if (index <0 || index >used ) {
339
344
throw new IndexOutOfBoundsException ("" +index );
340
345
}
341
346
used --;
342
- for (int i =index ; i <used ; i ++) {
343
- array [i ] = array [i +1 ];
344
- }
347
+ System .arraycopy (array , index + 1 , array , index , used - index );
345
348
}
346
349
347
350
/**
@@ -351,7 +354,7 @@ public void removeCharAt(int index) {
351
354
*
352
355
* @param length the new length
353
356
*/
354
- public void setLength (int length ) {
357
+ public void setLength (final int length ) {
355
358
if (length < 0 || length > used ) {
356
359
return ;
357
360
}
@@ -363,13 +366,13 @@ public void setLength(int length) {
363
366
*
364
367
* @param extra the extra capacity needed.
365
368
*/
366
- public void ensureCapacity (int extra ) {
369
+ public void ensureCapacity (final int extra ) {
367
370
if (used + extra > array .length ) {
368
371
int newlen = array .length * 2 ;
369
372
if (newlen < used + extra ) {
370
373
newlen = used + extra *2 ;
371
374
}
372
- char [] array2 = new char [newlen ];
375
+ final char [] array2 = new char [newlen ];
373
376
System .arraycopy (array , 0 , array2 , 0 , used );
374
377
array = array2 ;
375
378
}
@@ -385,7 +388,7 @@ public void ensureCapacity(int extra) {
385
388
*/
386
389
public CharSequence condense () {
387
390
if (array .length - used > 256 || array .length > used * 2 ) {
388
- char [] array2 = new char [used ];
391
+ final char [] array2 = new char [used ];
389
392
System .arraycopy (array , 0 , array2 , 0 , used );
390
393
array = array2 ;
391
394
}
@@ -410,7 +413,7 @@ public void write(final Writer writer) throws java.io.IOException {
410
413
*
411
414
* @return the diagnostic print
412
415
*/
413
- public static String diagnosticPrint (CharSequence in ) {
416
+ public static String diagnosticPrint (final CharSequence in ) {
414
417
final FastStringBuffer buff = new FastStringBuffer (in .length ()*2 );
415
418
for (int i =0 ; i <in .length (); i ++) {
416
419
final char c = in .charAt (i );
@@ -425,9 +428,9 @@ public static String diagnosticPrint(CharSequence in) {
425
428
}
426
429
return buff .toString ();
427
430
}
428
-
431
+
429
432
//Quick copies from old eXist's FastStringBuffer
430
-
433
+
431
434
/**
432
435
* Manefest constant: Suppress leading whitespace. This should be used when
433
436
* normalize-to-SAX is called for the first chunk of a multi-chunk output,
@@ -451,32 +454,35 @@ public static String diagnosticPrint(CharSequence in) {
451
454
*
452
455
* see sendNormalizedSAXcharacters(char[],int,int,org.xml.sax.ContentHandler,int)
453
456
*/
454
- public final static int SUPPRESS_BOTH
455
- = SUPPRESS_LEADING_WS | SUPPRESS_TRAILING_WS ;
456
-
457
+ public final static int SUPPRESS_BOTH = SUPPRESS_LEADING_WS | SUPPRESS_TRAILING_WS ;
458
+
457
459
/**
458
460
* Gets the normalizedString attribute of the FastStringBuffer object
459
461
*
460
- *@param mode Description of the Parameter
462
+ *@param mode Trim mode
461
463
*@return The normalizedString value
462
464
*/
463
- public String getNormalizedString ( int mode ) {
464
- return getNormalizedString ( new StringBuffer (toString ()), mode ).toString ();
465
+ public String getNormalizedString (final int mode ) {
466
+
467
+ switch (mode ){
468
+ case SUPPRESS_BOTH :
469
+ return toString ().trim ();
470
+
471
+ case SUPPRESS_LEADING_WS :
472
+ return StringUtils .stripStart (toString (),null );
473
+
474
+ case SUPPRESS_TRAILING_WS :
475
+ return StringUtils .stripEnd (toString (),null );
476
+
477
+ default :
478
+ return toString ().trim ();
479
+ }
480
+
465
481
}
466
482
467
483
468
- /**
469
- * Gets the normalizedString attribute of the FastStringBuffer object
470
- *
471
- *@param sb Description of the Parameter
472
- *@param mode Description of the Parameter
473
- *@return The normalizedString value
474
- */
475
- public StringBuffer getNormalizedString (StringBuffer sb , int mode ) {
476
- //TODO : switch (mode)
477
- return new StringBuffer (toString ().trim ());
478
- }
479
-
484
+
485
+
480
486
}
481
487
482
488
//
0 commit comments