Coverage Report - org.apache.commons.lang3.text.StrBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
StrBuilder
97%
743/761
95%
510/532
3,243
StrBuilder$StrBuilderReader
100%
30/30
95%
23/24
3,243
StrBuilder$StrBuilderTokenizer
80%
8/10
50%
2/4
3,243
StrBuilder$StrBuilderWriter
100%
15/15
N/A
3,243
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  * 
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.lang3.text;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.io.Reader;
 21  
 import java.io.Serializable;
 22  
 import java.io.Writer;
 23  
 import java.nio.CharBuffer;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 
 27  
 import org.apache.commons.lang3.ArrayUtils;
 28  
 import org.apache.commons.lang3.ObjectUtils;
 29  
 import org.apache.commons.lang3.SystemUtils;
 30  
 import org.apache.commons.lang3.builder.Builder;
 31  
 
 32  
 /**
 33  
  * Builds a string from constituent parts providing a more flexible and powerful API
 34  
  * than StringBuffer.
 35  
  * <p>
 36  
  * The main differences from StringBuffer/StringBuilder are:
 37  
  * </p>
 38  
  * <ul>
 39  
  * <li>Not synchronized</li>
 40  
  * <li>Not final</li>
 41  
  * <li>Subclasses have direct access to character array</li>
 42  
  * <li>Additional methods
 43  
  *  <ul>
 44  
  *   <li>appendWithSeparators - adds an array of values, with a separator</li>
 45  
  *   <li>appendPadding - adds a length padding characters</li>
 46  
  *   <li>appendFixedLength - adds a fixed width field to the builder</li>
 47  
  *   <li>toCharArray/getChars - simpler ways to get a range of the character array</li>
 48  
  *   <li>delete - delete char or string</li>
 49  
  *   <li>replace - search and replace for a char or string</li>
 50  
  *   <li>leftString/rightString/midString - substring without exceptions</li>
 51  
  *   <li>contains - whether the builder contains a char or string</li>
 52  
  *   <li>size/clear/isEmpty - collections style API methods</li>
 53  
  *  </ul>
 54  
  * </li>
 55  
  * <li>Views
 56  
  *  <ul>
 57  
  *   <li>asTokenizer - uses the internal buffer as the source of a StrTokenizer</li>
 58  
  *   <li>asReader - uses the internal buffer as the source of a Reader</li>
 59  
  *   <li>asWriter - allows a Writer to write directly to the internal buffer</li>
 60  
  *  </ul>
 61  
  * </li>
 62  
  * </ul>
 63  
  * <p>
 64  
  * The aim has been to provide an API that mimics very closely what StringBuffer
 65  
  * provides, but with additional methods. It should be noted that some edge cases,
 66  
  * with invalid indices or null input, have been altered - see individual methods.
 67  
  * The biggest of these changes is that by default, null will not output the text
 68  
  * 'null'. This can be controlled by a property, {@link #setNullText(String)}.
 69  
  * <p>
 70  
  * Prior to 3.0, this class implemented Cloneable but did not implement the 
 71  
  * clone method so could not be used. From 3.0 onwards it no longer implements 
 72  
  * the interface. 
 73  
  *
 74  
  * @since 2.2
 75  
  */
 76  0
 public class StrBuilder implements CharSequence, Appendable, Serializable, Builder<String> {
 77  
 
 78  
     /**
 79  
      * The extra capacity for new builders.
 80  
      */
 81  
     static final int CAPACITY = 32;
 82  
 
 83  
     /**
 84  
      * Required for serialization support.
 85  
      * 
 86  
      * @see java.io.Serializable
 87  
      */
 88  
     private static final long serialVersionUID = 7628716375283629643L;
 89  
 
 90  
     /** Internal data storage. */
 91  
     protected char[] buffer; // TODO make private?
 92  
     /** Current size of the buffer. */
 93  
     protected int size; // TODO make private?
 94  
     /** The new line. */
 95  
     private String newLine;
 96  
     /** The null text. */
 97  
     private String nullText;
 98  
 
 99  
     //-----------------------------------------------------------------------
 100  
     /**
 101  
      * Constructor that creates an empty builder initial capacity 32 characters.
 102  
      */
 103  
     public StrBuilder() {
 104  496
         this(CAPACITY);
 105  496
     }
 106  
 
 107  
     /**
 108  
      * Constructor that creates an empty builder the specified initial capacity.
 109  
      *
 110  
      * @param initialCapacity  the initial capacity, zero or less will be converted to 32
 111  
      */
 112  
     public StrBuilder(int initialCapacity) {
 113  711
         super();
 114  711
         if (initialCapacity <= 0) {
 115  2
             initialCapacity = CAPACITY;
 116  
         }
 117  711
         buffer = new char[initialCapacity];
 118  711
     }
 119  
 
 120  
     /**
 121  
      * Constructor that creates a builder from the string, allocating
 122  
      * 32 extra characters for growth.
 123  
      *
 124  
      * @param str  the string to copy, null treated as blank string
 125  
      */
 126  
     public StrBuilder(final String str) {
 127  276
         super();
 128  276
         if (str == null) {
 129  1
             buffer = new char[CAPACITY];
 130  
         } else {
 131  275
             buffer = new char[str.length() + CAPACITY];
 132  275
             append(str);
 133  
         }
 134  276
     }
 135  
 
 136  
     //-----------------------------------------------------------------------
 137  
     /**
 138  
      * Gets the text to be appended when a new line is added.
 139  
      *
 140  
      * @return the new line text, null means use system default
 141  
      */
 142  
     public String getNewLineText() {
 143  4
         return newLine;
 144  
     }
 145  
 
 146  
     /**
 147  
      * Sets the text to be appended when a new line is added.
 148  
      *
 149  
      * @param newLine  the new line text, null means use system default
 150  
      * @return this, to enable chaining
 151  
      */
 152  
     public StrBuilder setNewLineText(final String newLine) {
 153  6
         this.newLine = newLine;
 154  6
         return this;
 155  
     }
 156  
 
 157  
     //-----------------------------------------------------------------------
 158  
     /**
 159  
      * Gets the text to be appended when null is added.
 160  
      *
 161  
      * @return the null text, null means no append
 162  
      */
 163  
     public String getNullText() {
 164  9
         return nullText;
 165  
     }
 166  
 
 167  
     /**
 168  
      * Sets the text to be appended when null is added.
 169  
      *
 170  
      * @param nullText  the null text, null means no append
 171  
      * @return this, to enable chaining
 172  
      */
 173  
     public StrBuilder setNullText(String nullText) {
 174  20
         if (nullText != null && nullText.isEmpty()) {
 175  1
             nullText = null;
 176  
         }
 177  20
         this.nullText = nullText;
 178  20
         return this;
 179  
     }
 180  
 
 181  
     //-----------------------------------------------------------------------
 182  
     /**
 183  
      * Gets the length of the string builder.
 184  
      *
 185  
      * @return the length
 186  
      */
 187  
     @Override
 188  
     public int length() {
 189  1383
         return size;
 190  
     }
 191  
 
 192  
     /**
 193  
      * Updates the length of the builder by either dropping the last characters
 194  
      * or adding filler of Unicode zero.
 195  
      *
 196  
      * @param length  the length to set to, must be zero or positive
 197  
      * @return this, to enable chaining
 198  
      * @throws IndexOutOfBoundsException if the length is negative
 199  
      */
 200  
     public StrBuilder setLength(final int length) {
 201  11
         if (length < 0) {
 202  2
             throw new StringIndexOutOfBoundsException(length);
 203  
         }
 204  9
         if (length < size) {
 205  4
             size = length;
 206  5
         } else if (length > size) {
 207  3
             ensureCapacity(length);
 208  3
             final int oldEnd = size;
 209  3
             final int newEnd = length;
 210  3
             size = length;
 211  21
             for (int i = oldEnd; i < newEnd; i++) {
 212  18
                 buffer[i] = '\0';
 213  
             }
 214  
         }
 215  9
         return this;
 216  
     }
 217  
 
 218  
     //-----------------------------------------------------------------------
 219  
     /**
 220  
      * Gets the current size of the internal character array buffer.
 221  
      *
 222  
      * @return the capacity
 223  
      */
 224  
     public int capacity() {
 225  27
         return buffer.length;
 226  
     }
 227  
 
 228  
     /**
 229  
      * Checks the capacity and ensures that it is at least the size specified.
 230  
      *
 231  
      * @param capacity  the capacity to ensure
 232  
      * @return this, to enable chaining
 233  
      */
 234  
     public StrBuilder ensureCapacity(final int capacity) {
 235  2727
         if (capacity > buffer.length) {
 236  528
             final char[] old = buffer;
 237  528
             buffer = new char[capacity * 2];
 238  528
             System.arraycopy(old, 0, buffer, 0, size);
 239  
         }
 240  2727
         return this;
 241  
     }
 242  
 
 243  
     /**
 244  
      * Minimizes the capacity to the actual length of the string.
 245  
      *
 246  
      * @return this, to enable chaining
 247  
      */
 248  
     public StrBuilder minimizeCapacity() {
 249  5
         if (buffer.length > length()) {
 250  5
             final char[] old = buffer;
 251  5
             buffer = new char[length()];
 252  5
             System.arraycopy(old, 0, buffer, 0, size);
 253  
         }
 254  5
         return this;
 255  
     }
 256  
 
 257  
     //-----------------------------------------------------------------------
 258  
     /**
 259  
      * Gets the length of the string builder.
 260  
      * <p>
 261  
      * This method is the same as {@link #length()} and is provided to match the
 262  
      * API of Collections.
 263  
      *
 264  
      * @return the length
 265  
      */
 266  
     public int size() {
 267  310
         return size;
 268  
     }
 269  
 
 270  
     /**
 271  
      * Checks is the string builder is empty (convenience Collections API style method).
 272  
      * <p>
 273  
      * This method is the same as checking {@link #length()} and is provided to match the
 274  
      * API of Collections.
 275  
      *
 276  
      * @return <code>true</code> if the size is <code>0</code>.
 277  
      */
 278  
     public boolean isEmpty() {
 279  23
         return size == 0;
 280  
     }
 281  
 
 282  
     /**
 283  
      * Clears the string builder (convenience Collections API style method).
 284  
      * <p>
 285  
      * This method does not reduce the size of the internal character buffer.
 286  
      * To do that, call <code>clear()</code> followed by {@link #minimizeCapacity()}.
 287  
      * <p>
 288  
      * This method is the same as {@link #setLength(int)} called with zero
 289  
      * and is provided to match the API of Collections.
 290  
      *
 291  
      * @return this, to enable chaining
 292  
      */
 293  
     public StrBuilder clear() {
 294  252
         size = 0;
 295  252
         return this;
 296  
     }
 297  
 
 298  
     //-----------------------------------------------------------------------
 299  
     /**
 300  
      * Gets the character at the specified index.
 301  
      *
 302  
      * @see #setCharAt(int, char)
 303  
      * @see #deleteCharAt(int)
 304  
      * @param index  the index to retrieve, must be valid
 305  
      * @return the character at the index
 306  
      * @throws IndexOutOfBoundsException if the index is invalid
 307  
      */
 308  
     @Override
 309  
     public char charAt(final int index) {
 310  17
         if (index < 0 || index >= length()) {
 311  4
             throw new StringIndexOutOfBoundsException(index);
 312  
         }
 313  13
         return buffer[index];
 314  
     }
 315  
 
 316  
     /**
 317  
      * Sets the character at the specified index.
 318  
      *
 319  
      * @see #charAt(int)
 320  
      * @see #deleteCharAt(int)
 321  
      * @param index  the index to set
 322  
      * @param ch  the new character
 323  
      * @return this, to enable chaining
 324  
      * @throws IndexOutOfBoundsException if the index is invalid
 325  
      */
 326  
     public StrBuilder setCharAt(final int index, final char ch) {
 327  7
         if (index < 0 || index >= length()) {
 328  3
             throw new StringIndexOutOfBoundsException(index);
 329  
         }
 330  4
         buffer[index] = ch;
 331  4
         return this;
 332  
     }
 333  
 
 334  
     /**
 335  
      * Deletes the character at the specified index.
 336  
      *
 337  
      * @see #charAt(int)
 338  
      * @see #setCharAt(int, char)
 339  
      * @param index  the index to delete
 340  
      * @return this, to enable chaining
 341  
      * @throws IndexOutOfBoundsException if the index is invalid
 342  
      */
 343  
     public StrBuilder deleteCharAt(final int index) {
 344  99
         if (index < 0 || index >= size) {
 345  1
             throw new StringIndexOutOfBoundsException(index);
 346  
         }
 347  98
         deleteImpl(index, index + 1, 1);
 348  98
         return this;
 349  
     }
 350  
 
 351  
     //-----------------------------------------------------------------------
 352  
     /**
 353  
      * Copies the builder's character array into a new character array.
 354  
      * 
 355  
      * @return a new array that represents the contents of the builder
 356  
      */
 357  
     public char[] toCharArray() {
 358  3
         if (size == 0) {
 359  2
             return ArrayUtils.EMPTY_CHAR_ARRAY;
 360  
         }
 361  1
         final char chars[] = new char[size];
 362  1
         System.arraycopy(buffer, 0, chars, 0, size);
 363  1
         return chars;
 364  
     }
 365  
 
 366  
     /**
 367  
      * Copies part of the builder's character array into a new character array.
 368  
      * 
 369  
      * @param startIndex  the start index, inclusive, must be valid
 370  
      * @param endIndex  the end index, exclusive, must be valid except that
 371  
      *  if too large it is treated as end of string
 372  
      * @return a new array that holds part of the contents of the builder
 373  
      * @throws IndexOutOfBoundsException if startIndex is invalid,
 374  
      *  or if endIndex is invalid (but endIndex greater than size is valid)
 375  
      */
 376  
     public char[] toCharArray(final int startIndex, int endIndex) {
 377  7
         endIndex = validateRange(startIndex, endIndex);
 378  5
         final int len = endIndex - startIndex;
 379  5
         if (len == 0) {
 380  1
             return ArrayUtils.EMPTY_CHAR_ARRAY;
 381  
         }
 382  4
         final char chars[] = new char[len];
 383  4
         System.arraycopy(buffer, startIndex, chars, 0, len);
 384  4
         return chars;
 385  
     }
 386  
 
 387  
     /**
 388  
      * Copies the character array into the specified array.
 389  
      * 
 390  
      * @param destination  the destination array, null will cause an array to be created
 391  
      * @return the input array, unless that was null or too small
 392  
      */
 393  
     public char[] getChars(char[] destination) {
 394  5
         final int len = length();
 395  5
         if (destination == null || destination.length < len) {
 396  2
             destination = new char[len];
 397  
         }
 398  5
         System.arraycopy(buffer, 0, destination, 0, len);
 399  5
         return destination;
 400  
     }
 401  
 
 402  
     /**
 403  
      * Copies the character array into the specified array.
 404  
      *
 405  
      * @param startIndex  first index to copy, inclusive, must be valid
 406  
      * @param endIndex  last index, exclusive, must be valid
 407  
      * @param destination  the destination array, must not be null or too small
 408  
      * @param destinationIndex  the index to start copying in destination
 409  
      * @throws NullPointerException if the array is null
 410  
      * @throws IndexOutOfBoundsException if any index is invalid
 411  
      */
 412  
     public void getChars(final int startIndex, final int endIndex, final char destination[], final int destinationIndex) {
 413  28
         if (startIndex < 0) {
 414  1
             throw new StringIndexOutOfBoundsException(startIndex);
 415  
         }
 416  27
         if (endIndex < 0 || endIndex > length()) {
 417  2
             throw new StringIndexOutOfBoundsException(endIndex);
 418  
         }
 419  25
         if (startIndex > endIndex) {
 420  1
             throw new StringIndexOutOfBoundsException("end < start");
 421  
         }
 422  24
         System.arraycopy(buffer, startIndex, destination, destinationIndex, endIndex - startIndex);
 423  24
     }
 424  
 
 425  
     //-----------------------------------------------------------------------
 426  
     /**
 427  
      * If possible, reads chars from the provided {@link Readable} directly into underlying
 428  
      * character buffer without making extra copies.
 429  
      *
 430  
      * @param readable  object to read from
 431  
      * @return the number of characters read
 432  
      * @throws IOException if an I/O error occurs
 433  
      *
 434  
      * @since 3.4
 435  
      * @see #appendTo(Appendable)
 436  
      */
 437  
     public int readFrom(final Readable readable) throws IOException {
 438  303
         final int oldSize = size;
 439  303
         if (readable instanceof Reader) {
 440  101
             final Reader r = (Reader) readable;
 441  101
             ensureCapacity(size + 1);
 442  
             int read;
 443  367
             while ((read = r.read(buffer, size, buffer.length - size)) != -1) {
 444  266
                 size += read;
 445  266
                 ensureCapacity(size + 1);
 446  
             }
 447  101
         } else if (readable instanceof CharBuffer) {
 448  101
             final CharBuffer cb = (CharBuffer) readable;
 449  101
             final int remaining = cb.remaining();
 450  101
             ensureCapacity(size + remaining);
 451  101
             cb.get(buffer, size, remaining);
 452  101
             size += remaining;
 453  101
         } else {
 454  
             while (true) {
 455  367
                 ensureCapacity(size + 1);
 456  367
                 final CharBuffer buf = CharBuffer.wrap(buffer, size, buffer.length - size);
 457  367
                 final int read = readable.read(buf);
 458  367
                 if (read == -1) {
 459  101
                     break;
 460  
                 }
 461  266
                 size += read;
 462  266
             }
 463  
         }
 464  303
         return size - oldSize;
 465  
     }
 466  
 
 467  
     //-----------------------------------------------------------------------
 468  
     /**
 469  
      * Appends the new line string to this string builder.
 470  
      * <p>
 471  
      * The new line string can be altered using {@link #setNewLineText(String)}.
 472  
      * This might be used to force the output to always use Unix line endings
 473  
      * even when on Windows.
 474  
      *
 475  
      * @return this, to enable chaining
 476  
      */
 477  
     public StrBuilder appendNewLine() {
 478  23
         if (newLine == null)  {
 479  22
             append(SystemUtils.LINE_SEPARATOR);
 480  22
             return this;
 481  
         }
 482  1
         return append(newLine);
 483  
     }
 484  
 
 485  
     /**
 486  
      * Appends the text representing <code>null</code> to this string builder.
 487  
      *
 488  
      * @return this, to enable chaining
 489  
      */
 490  
     public StrBuilder appendNull() {
 491  23
         if (nullText == null)  {
 492  7
             return this;
 493  
         }
 494  16
         return append(nullText);
 495  
     }
 496  
 
 497  
     /**
 498  
      * Appends an object to this string builder.
 499  
      * Appending null will call {@link #appendNull()}.
 500  
      *
 501  
      * @param obj  the object to append
 502  
      * @return this, to enable chaining
 503  
      */
 504  
     public StrBuilder append(final Object obj) {
 505  88
         if (obj == null) {
 506  8
             return appendNull();
 507  
         }
 508  80
         if (obj instanceof CharSequence) {
 509  59
             return append((CharSequence) obj);
 510  
         }
 511  21
         return append(obj.toString());        
 512  
     }
 513  
 
 514  
     /**
 515  
      * Appends a CharSequence to this string builder.
 516  
      * Appending null will call {@link #appendNull()}.
 517  
      *
 518  
      * @param seq  the CharSequence to append
 519  
      * @return this, to enable chaining
 520  
      * @since 3.0
 521  
      */
 522  
     @Override
 523  
     public StrBuilder append(final CharSequence seq) {
 524  60
         if (seq == null) {
 525  0
             return appendNull();
 526  
         }
 527  60
         if (seq instanceof StrBuilder) {
 528  0
             return append((StrBuilder) seq);
 529  
         }
 530  60
         if (seq instanceof StringBuilder) {
 531  0
             return append((StringBuilder) seq);
 532  
         }
 533  60
         if (seq instanceof StringBuffer) {
 534  0
             return append((StringBuffer) seq);
 535  
         }
 536  60
         if (seq instanceof CharBuffer) {
 537  0
             return append((CharBuffer) seq);
 538  
         }
 539  60
         return append(seq.toString());        
 540  
     }
 541  
 
 542  
     /**
 543  
      * Appends part of a CharSequence to this string builder.
 544  
      * Appending null will call {@link #appendNull()}.
 545  
      *
 546  
      * @param seq  the CharSequence to append
 547  
      * @param startIndex  the start index, inclusive, must be valid
 548  
      * @param length  the length to append, must be valid
 549  
      * @return this, to enable chaining
 550  
      * @since 3.0
 551  
      */
 552  
     @Override
 553  
     public StrBuilder append(final CharSequence seq, final int startIndex, final int length) {
 554  33
         if (seq == null) {
 555  0
             return appendNull();
 556  
         } 
 557  33
         return append(seq.toString(), startIndex, length);
 558  
     }
 559  
     
 560  
     /**
 561  
      * Appends a string to this string builder.
 562  
      * Appending null will call {@link #appendNull()}.
 563  
      *
 564  
      * @param str  the string to append
 565  
      * @return this, to enable chaining
 566  
      */
 567  
     public StrBuilder append(final String str) {
 568  527
         if (str == null) {
 569  3
             return appendNull();
 570  
         }
 571  524
         final int strLen = str.length();
 572  524
         if (strLen > 0) {
 573  504
             final int len = length();
 574  504
             ensureCapacity(len + strLen);
 575  504
             str.getChars(0, strLen, buffer, len);
 576  504
             size += strLen;
 577  
         }
 578  524
         return this;
 579  
     }
 580  
    
 581  
 
 582  
     /**
 583  
      * Appends part of a string to this string builder.
 584  
      * Appending null will call {@link #appendNull()}.
 585  
      *
 586  
      * @param str  the string to append
 587  
      * @param startIndex  the start index, inclusive, must be valid
 588  
      * @param length  the length to append, must be valid
 589  
      * @return this, to enable chaining
 590  
      */
 591  
     public StrBuilder append(final String str, final int startIndex, final int length) {
 592  62
         if (str == null) {
 593  2
             return appendNull();
 594  
         }
 595  60
         if (startIndex < 0 || startIndex > str.length()) {
 596  3
             throw new StringIndexOutOfBoundsException("startIndex must be valid");
 597  
         }
 598  57
         if (length < 0 || (startIndex + length) > str.length()) {
 599  3
             throw new StringIndexOutOfBoundsException("length must be valid");
 600  
         }
 601  54
         if (length > 0) {
 602  53
             final int len = length();
 603  53
             ensureCapacity(len + length);
 604  53
             str.getChars(startIndex, startIndex + length, buffer, len);
 605  53
             size += length;
 606  
         }
 607  54
         return this;
 608  
     }
 609  
 
 610  
     /**
 611  
      * Calls {@link String#format(String, Object...)} and appends the result.
 612  
      *
 613  
      * @param format the format string
 614  
      * @param objs the objects to use in the format string
 615  
      * @return {@code this} to enable chaining
 616  
      * @see String#format(String, Object...)
 617  
      * @since 3.2
 618  
      */
 619  
     public StrBuilder append(final String format, final Object... objs) {
 620  5
         return append(String.format(format, objs));
 621  
     }
 622  
 
 623  
     /**
 624  
      * Appends the contents of a char buffer to this string builder.
 625  
      * Appending null will call {@link #appendNull()}.
 626  
      *
 627  
      * @param buf  the char buffer to append
 628  
      * @return this, to enable chaining
 629  
      * @since 3.4
 630  
      */
 631  
     public StrBuilder append(final CharBuffer buf) {
 632  1
         if (buf == null) {
 633  0
             return appendNull();
 634  
         }
 635  1
         if (buf.hasArray()) {
 636  1
             final int length = buf.remaining();
 637  1
             final int len = length();
 638  1
             ensureCapacity(len + length);
 639  1
             System.arraycopy(buf.array(), buf.arrayOffset() + buf.position(), buffer, len, length);
 640  1
             size += length;
 641  1
         } else {
 642  0
             append(buf.toString());
 643  
         }
 644  1
         return this;
 645  
     }
 646  
 
 647  
     /**
 648  
      * Appends the contents of a char buffer to this string builder.
 649  
      * Appending null will call {@link #appendNull()}.
 650  
      *
 651  
      * @param buf  the char buffer to append
 652  
      * @param startIndex  the start index, inclusive, must be valid
 653  
      * @param length  the length to append, must be valid
 654  
      * @return this, to enable chaining
 655  
      * @since 3.4
 656  
      */
 657  
     public StrBuilder append(final CharBuffer buf, final int startIndex, final int length) {
 658  1
         if (buf == null) {
 659  0
             return appendNull();
 660  
         }
 661  1
         if (buf.hasArray()) {
 662  1
             final int totalLength = buf.remaining();
 663  1
             if (startIndex < 0 || startIndex > totalLength) {
 664  0
                 throw new StringIndexOutOfBoundsException("startIndex must be valid");
 665  
             }
 666  1
             if (length < 0 || (startIndex + length) > totalLength) {
 667  0
                 throw new StringIndexOutOfBoundsException("length must be valid");
 668  
             }
 669  1
             final int len = length();
 670  1
             ensureCapacity(len + length);
 671  1
             System.arraycopy(buf.array(), buf.arrayOffset() + buf.position() + startIndex, buffer, len, length);
 672  1
             size += length;
 673  1
         } else {
 674  0
             append(buf.toString(), startIndex, length);
 675  
         }
 676  1
         return this;
 677  
     }
 678  
 
 679  
     /**
 680  
      * Appends a string buffer to this string builder.
 681  
      * Appending null will call {@link #appendNull()}.
 682  
      *
 683  
      * @param str  the string buffer to append
 684  
      * @return this, to enable chaining
 685  
      */
 686  
     public StrBuilder append(final StringBuffer str) {
 687  26
         if (str == null) {
 688  3
             return appendNull();
 689  
         }
 690  23
         final int strLen = str.length();
 691  23
         if (strLen > 0) {
 692  22
             final int len = length();
 693  22
             ensureCapacity(len + strLen);
 694  22
             str.getChars(0, strLen, buffer, len);
 695  22
             size += strLen;
 696  
         }
 697  23
         return this;
 698  
     }
 699  
 
 700  
     /**
 701  
      * Appends part of a string buffer to this string builder.
 702  
      * Appending null will call {@link #appendNull()}.
 703  
      *
 704  
      * @param str  the string to append
 705  
      * @param startIndex  the start index, inclusive, must be valid
 706  
      * @param length  the length to append, must be valid
 707  
      * @return this, to enable chaining
 708  
      */
 709  
     public StrBuilder append(final StringBuffer str, final int startIndex, final int length) {
 710  58
         if (str == null) {
 711  1
             return appendNull();
 712  
         }
 713  57
         if (startIndex < 0 || startIndex > str.length()) {
 714  3
             throw new StringIndexOutOfBoundsException("startIndex must be valid");
 715  
         }
 716  54
         if (length < 0 || (startIndex + length) > str.length()) {
 717  3
             throw new StringIndexOutOfBoundsException("length must be valid");
 718  
         }
 719  51
         if (length > 0) {
 720  50
             final int len = length();
 721  50
             ensureCapacity(len + length);
 722  50
             str.getChars(startIndex, startIndex + length, buffer, len);
 723  50
             size += length;
 724  
         }
 725  51
         return this;
 726  
     }
 727  
 
 728  
     /**
 729  
      * Appends a StringBuilder to this string builder.
 730  
      * Appending null will call {@link #appendNull()}.
 731  
      *
 732  
      * @param str the StringBuilder to append
 733  
      * @return this, to enable chaining
 734  
      * @since 3.2
 735  
      */
 736  
     public StrBuilder append(final StringBuilder str) {
 737  5
         if (str == null) {
 738  0
             return appendNull();
 739  
         }
 740  5
         final int strLen = str.length();
 741  5
         if (strLen > 0) {
 742  4
             final int len = length();
 743  4
             ensureCapacity(len + strLen);
 744  4
             str.getChars(0, strLen, buffer, len);
 745  4
             size += strLen;
 746  
         }
 747  5
         return this;
 748  
     }
 749  
     
 750  
     /**
 751  
      * Appends part of a StringBuilder to this string builder.
 752  
      * Appending null will call {@link #appendNull()}.
 753  
      *
 754  
      * @param str the StringBuilder to append
 755  
      * @param startIndex the start index, inclusive, must be valid
 756  
      * @param length the length to append, must be valid
 757  
      * @return this, to enable chaining
 758  
      * @since 3.2
 759  
      */
 760  
     public StrBuilder append(final StringBuilder str, final int startIndex, final int length) {
 761  43
         if (str == null) {
 762  0
             return appendNull();
 763  
         }
 764  43
         if (startIndex < 0 || startIndex > str.length()) {
 765  3
             throw new StringIndexOutOfBoundsException("startIndex must be valid");
 766  
         }
 767  40
         if (length < 0 || (startIndex + length) > str.length()) {
 768  3
             throw new StringIndexOutOfBoundsException("length must be valid");
 769  
         }
 770  37
         if (length > 0) {
 771  36
             final int len = length();
 772  36
             ensureCapacity(len + length);
 773  36
             str.getChars(startIndex, startIndex + length, buffer, len);
 774  36
             size += length;
 775  
         }
 776  37
         return this;
 777  
     }
 778  
 
 779  
     /**
 780  
      * Appends another string builder to this string builder.
 781  
      * Appending null will call {@link #appendNull()}.
 782  
      *
 783  
      * @param str  the string builder to append
 784  
      * @return this, to enable chaining
 785  
      */
 786  
     public StrBuilder append(final StrBuilder str) {
 787  23
         if (str == null) {
 788  1
             return appendNull();
 789  
         }
 790  22
         final int strLen = str.length();
 791  22
         if (strLen > 0) {
 792  21
             final int len = length();
 793  21
             ensureCapacity(len + strLen);
 794  21
             System.arraycopy(str.buffer, 0, buffer, len, strLen);
 795  21
             size += strLen;
 796  
         }
 797  22
         return this;
 798  
     }
 799  
 
 800  
     /**
 801  
      * Appends part of a string builder to this string builder.
 802  
      * Appending null will call {@link #appendNull()}.
 803  
      *
 804  
      * @param str  the string to append
 805  
      * @param startIndex  the start index, inclusive, must be valid
 806  
      * @param length  the length to append, must be valid
 807  
      * @return this, to enable chaining
 808  
      */
 809  
     public StrBuilder append(final StrBuilder str, final int startIndex, final int length) {
 810  26
         if (str == null) {
 811  1
             return appendNull();
 812  
         }
 813  25
         if (startIndex < 0 || startIndex > str.length()) {
 814  3
             throw new StringIndexOutOfBoundsException("startIndex must be valid");
 815  
         }
 816  22
         if (length < 0 || (startIndex + length) > str.length()) {
 817  3
             throw new StringIndexOutOfBoundsException("length must be valid");
 818  
         }
 819  19
         if (length > 0) {
 820  18
             final int len = length();
 821  18
             ensureCapacity(len + length);
 822  18
             str.getChars(startIndex, startIndex + length, buffer, len);
 823  18
             size += length;
 824  
         }
 825  19
         return this;
 826  
     }
 827  
 
 828  
     /**
 829  
      * Appends a char array to the string builder.
 830  
      * Appending null will call {@link #appendNull()}.
 831  
      *
 832  
      * @param chars  the char array to append
 833  
      * @return this, to enable chaining
 834  
      */
 835  
     public StrBuilder append(final char[] chars) {
 836  22
         if (chars == null) {
 837  1
             return appendNull();
 838  
         }
 839  21
         final int strLen = chars.length;
 840  21
         if (strLen > 0) {
 841  20
             final int len = length();
 842  20
             ensureCapacity(len + strLen);
 843  20
             System.arraycopy(chars, 0, buffer, len, strLen);
 844  20
             size += strLen;
 845  
         }
 846  21
         return this;
 847  
     }
 848  
 
 849  
     /**
 850  
      * Appends a char array to the string builder.
 851  
      * Appending null will call {@link #appendNull()}.
 852  
      *
 853  
      * @param chars  the char array to append
 854  
      * @param startIndex  the start index, inclusive, must be valid
 855  
      * @param length  the length to append, must be valid
 856  
      * @return this, to enable chaining
 857  
      */
 858  
     public StrBuilder append(final char[] chars, final int startIndex, final int length) {
 859  51
         if (chars == null) {
 860  1
             return appendNull();
 861  
         }
 862  50
         if (startIndex < 0 || startIndex > chars.length) {
 863  3
             throw new StringIndexOutOfBoundsException("Invalid startIndex: " + length);
 864  
         }
 865  47
         if (length < 0 || (startIndex + length) > chars.length) {
 866  3
             throw new StringIndexOutOfBoundsException("Invalid length: " + length);
 867  
         }
 868  44
         if (length > 0) {
 869  43
             final int len = length();
 870  43
             ensureCapacity(len + length);
 871  43
             System.arraycopy(chars, startIndex, buffer, len, length);
 872  43
             size += length;
 873  
         }
 874  44
         return this;
 875  
     }
 876  
 
 877  
     /**
 878  
      * Appends a boolean value to the string builder.
 879  
      *
 880  
      * @param value  the value to append
 881  
      * @return this, to enable chaining
 882  
      */
 883  
     public StrBuilder append(final boolean value) {
 884  5
         if (value) {
 885  3
             ensureCapacity(size + 4);
 886  3
             buffer[size++] = 't';
 887  3
             buffer[size++] = 'r';
 888  3
             buffer[size++] = 'u';
 889  3
             buffer[size++] = 'e';
 890  
         } else {
 891  2
             ensureCapacity(size + 5);
 892  2
             buffer[size++] = 'f';
 893  2
             buffer[size++] = 'a';
 894  2
             buffer[size++] = 'l';
 895  2
             buffer[size++] = 's';
 896  2
             buffer[size++] = 'e';
 897  
         }
 898  5
         return this;
 899  
     }
 900  
 
 901  
     /**
 902  
      * Appends a char value to the string builder.
 903  
      *
 904  
      * @param ch  the value to append
 905  
      * @return this, to enable chaining
 906  
      * @since 3.0
 907  
      */
 908  
     @Override
 909  
     public StrBuilder append(final char ch) {
 910  260
         final int len = length();
 911  260
         ensureCapacity(len + 1);
 912  260
         buffer[size++] = ch;
 913  260
         return this;
 914  
     }
 915  
 
 916  
     /**
 917  
      * Appends an int value to the string builder using <code>String.valueOf</code>.
 918  
      *
 919  
      * @param value  the value to append
 920  
      * @return this, to enable chaining
 921  
      */
 922  
     public StrBuilder append(final int value) {
 923  2
         return append(String.valueOf(value));
 924  
     }
 925  
 
 926  
     /**
 927  
      * Appends a long value to the string builder using <code>String.valueOf</code>.
 928  
      *
 929  
      * @param value  the value to append
 930  
      * @return this, to enable chaining
 931  
      */
 932  
     public StrBuilder append(final long value) {
 933  2
         return append(String.valueOf(value));
 934  
     }
 935  
 
 936  
     /**
 937  
      * Appends a float value to the string builder using <code>String.valueOf</code>.
 938  
      *
 939  
      * @param value  the value to append
 940  
      * @return this, to enable chaining
 941  
      */
 942  
     public StrBuilder append(final float value) {
 943  2
         return append(String.valueOf(value));
 944  
     }
 945  
 
 946  
     /**
 947  
      * Appends a double value to the string builder using <code>String.valueOf</code>.
 948  
      *
 949  
      * @param value  the value to append
 950  
      * @return this, to enable chaining
 951  
      */
 952  
     public StrBuilder append(final double value) {
 953  2
         return append(String.valueOf(value));
 954  
     }
 955  
 
 956  
     //-----------------------------------------------------------------------
 957  
     /**
 958  
      * Appends an object followed by a new line to this string builder.
 959  
      * Appending null will call {@link #appendNull()}.
 960  
      *
 961  
      * @param obj  the object to append
 962  
      * @return this, to enable chaining
 963  
      * @since 2.3
 964  
      */
 965  
     public StrBuilder appendln(final Object obj) {
 966  3
         return append(obj).appendNewLine();
 967  
     }
 968  
 
 969  
     /**
 970  
      * Appends a string followed by a new line to this string builder.
 971  
      * Appending null will call {@link #appendNull()}.
 972  
      *
 973  
      * @param str  the string to append
 974  
      * @return this, to enable chaining
 975  
      * @since 2.3
 976  
      */
 977  
     public StrBuilder appendln(final String str) {
 978  1
         return append(str).appendNewLine();
 979  
     }
 980  
 
 981  
     /**
 982  
      * Appends part of a string followed by a new line to this string builder.
 983  
      * Appending null will call {@link #appendNull()}.
 984  
      *
 985  
      * @param str  the string to append
 986  
      * @param startIndex  the start index, inclusive, must be valid
 987  
      * @param length  the length to append, must be valid
 988  
      * @return this, to enable chaining
 989  
      * @since 2.3
 990  
      */
 991  
     public StrBuilder appendln(final String str, final int startIndex, final int length) {
 992  1
         return append(str, startIndex, length).appendNewLine();
 993  
     }
 994  
 
 995  
     /**
 996  
      * Calls {@link String#format(String, Object...)} and appends the result.
 997  
      *
 998  
      * @param format the format string
 999  
      * @param objs the objects to use in the format string
 1000  
      * @return {@code this} to enable chaining
 1001  
      * @see String#format(String, Object...)
 1002  
      * @since 3.2
 1003  
      */
 1004  
     public StrBuilder appendln(final String format, final Object... objs) {
 1005  1
         return append(format, objs).appendNewLine();
 1006  
     }
 1007  
 
 1008  
     /**
 1009  
      * Appends a string buffer followed by a new line to this string builder.
 1010  
      * Appending null will call {@link #appendNull()}.
 1011  
      *
 1012  
      * @param str  the string buffer to append
 1013  
      * @return this, to enable chaining
 1014  
      * @since 2.3
 1015  
      */
 1016  
     public StrBuilder appendln(final StringBuffer str) {
 1017  1
         return append(str).appendNewLine();
 1018  
     }
 1019  
 
 1020  
     /**
 1021  
      * Appends a string builder followed by a new line to this string builder.
 1022  
      * Appending null will call {@link #appendNull()}.
 1023  
      *
 1024  
      * @param str  the string builder to append
 1025  
      * @return this, to enable chaining
 1026  
      * @since 3.2
 1027  
      */
 1028  
     public StrBuilder appendln(final StringBuilder str) {
 1029  1
         return append(str).appendNewLine();
 1030  
     }
 1031  
 
 1032  
     /**
 1033  
      * Appends part of a string builder followed by a new line to this string builder.
 1034  
      * Appending null will call {@link #appendNull()}.
 1035  
      *
 1036  
      * @param str  the string builder to append
 1037  
      * @param startIndex  the start index, inclusive, must be valid
 1038  
      * @param length  the length to append, must be valid
 1039  
      * @return this, to enable chaining
 1040  
      * @since 3.2
 1041  
      */
 1042  
     public StrBuilder appendln(final StringBuilder str, final int startIndex, final int length) {
 1043  1
         return append(str, startIndex, length).appendNewLine();
 1044  
     }
 1045  
 
 1046  
     /**
 1047  
      * Appends part of a string buffer followed by a new line to this string builder.
 1048  
      * Appending null will call {@link #appendNull()}.
 1049  
      *
 1050  
      * @param str  the string to append
 1051  
      * @param startIndex  the start index, inclusive, must be valid
 1052  
      * @param length  the length to append, must be valid
 1053  
      * @return this, to enable chaining
 1054  
      * @since 2.3
 1055  
      */
 1056  
     public StrBuilder appendln(final StringBuffer str, final int startIndex, final int length) {
 1057  1
         return append(str, startIndex, length).appendNewLine();
 1058  
     }
 1059  
 
 1060  
     /**
 1061  
      * Appends another string builder followed by a new line to this string builder.
 1062  
      * Appending null will call {@link #appendNull()}.
 1063  
      *
 1064  
      * @param str  the string builder to append
 1065  
      * @return this, to enable chaining
 1066  
      * @since 2.3
 1067  
      */
 1068  
     public StrBuilder appendln(final StrBuilder str) {
 1069  1
         return append(str).appendNewLine();
 1070  
     }
 1071  
 
 1072  
     /**
 1073  
      * Appends part of a string builder followed by a new line to this string builder.
 1074  
      * Appending null will call {@link #appendNull()}.
 1075  
      *
 1076  
      * @param str  the string to append
 1077  
      * @param startIndex  the start index, inclusive, must be valid
 1078  
      * @param length  the length to append, must be valid
 1079  
      * @return this, to enable chaining
 1080  
      * @since 2.3
 1081  
      */
 1082  
     public StrBuilder appendln(final StrBuilder str, final int startIndex, final int length) {
 1083  1
         return append(str, startIndex, length).appendNewLine();
 1084  
     }
 1085  
 
 1086  
     /**
 1087  
      * Appends a char array followed by a new line to the string builder.
 1088  
      * Appending null will call {@link #appendNull()}.
 1089  
      *
 1090  
      * @param chars  the char array to append
 1091  
      * @return this, to enable chaining
 1092  
      * @since 2.3
 1093  
      */
 1094  
     public StrBuilder appendln(final char[] chars) {
 1095  1
         return append(chars).appendNewLine();
 1096  
     }
 1097  
 
 1098  
     /**
 1099  
      * Appends a char array followed by a new line to the string builder.
 1100  
      * Appending null will call {@link #appendNull()}.
 1101  
      *
 1102  
      * @param chars  the char array to append
 1103  
      * @param startIndex  the start index, inclusive, must be valid
 1104  
      * @param length  the length to append, must be valid
 1105  
      * @return this, to enable chaining
 1106  
      * @since 2.3
 1107  
      */
 1108  
     public StrBuilder appendln(final char[] chars, final int startIndex, final int length) {
 1109  1
         return append(chars, startIndex, length).appendNewLine();
 1110  
     }
 1111  
 
 1112  
     /**
 1113  
      * Appends a boolean value followed by a new line to the string builder.
 1114  
      *
 1115  
      * @param value  the value to append
 1116  
      * @return this, to enable chaining
 1117  
      * @since 2.3
 1118  
      */
 1119  
     public StrBuilder appendln(final boolean value) {
 1120  2
         return append(value).appendNewLine();
 1121  
     }
 1122  
 
 1123  
     /**
 1124  
      * Appends a char value followed by a new line to the string builder.
 1125  
      *
 1126  
      * @param ch  the value to append
 1127  
      * @return this, to enable chaining
 1128  
      * @since 2.3
 1129  
      */
 1130  
     public StrBuilder appendln(final char ch) {
 1131  0
         return append(ch).appendNewLine();
 1132  
     }
 1133  
 
 1134  
     /**
 1135  
      * Appends an int value followed by a new line to the string builder using <code>String.valueOf</code>.
 1136  
      *
 1137  
      * @param value  the value to append
 1138  
      * @return this, to enable chaining
 1139  
      * @since 2.3
 1140  
      */
 1141  
     public StrBuilder appendln(final int value) {
 1142  1
         return append(value).appendNewLine();
 1143  
     }
 1144  
 
 1145  
     /**
 1146  
      * Appends a long value followed by a new line to the string builder using <code>String.valueOf</code>.
 1147  
      *
 1148  
      * @param value  the value to append
 1149  
      * @return this, to enable chaining
 1150  
      * @since 2.3
 1151  
      */
 1152  
     public StrBuilder appendln(final long value) {
 1153  1
         return append(value).appendNewLine();
 1154  
     }
 1155  
 
 1156  
     /**
 1157  
      * Appends a float value followed by a new line to the string builder using <code>String.valueOf</code>.
 1158  
      *
 1159  
      * @param value  the value to append
 1160  
      * @return this, to enable chaining
 1161  
      * @since 2.3
 1162  
      */
 1163  
     public StrBuilder appendln(final float value) {
 1164  1
         return append(value).appendNewLine();
 1165  
     }
 1166  
 
 1167  
     /**
 1168  
      * Appends a double value followed by a new line to the string builder using <code>String.valueOf</code>.
 1169  
      *
 1170  
      * @param value  the value to append
 1171  
      * @return this, to enable chaining
 1172  
      * @since 2.3
 1173  
      */
 1174  
     public StrBuilder appendln(final double value) {
 1175  1
         return append(value).appendNewLine();
 1176  
     }
 1177  
 
 1178  
     //-----------------------------------------------------------------------
 1179  
     /**
 1180  
      * Appends each item in an array to the builder without any separators.
 1181  
      * Appending a null array will have no effect.
 1182  
      * Each object is appended using {@link #append(Object)}.
 1183  
      *
 1184  
      * @param <T>  the element type
 1185  
      * @param array  the array to append
 1186  
      * @return this, to enable chaining
 1187  
      * @since 2.3
 1188  
      */
 1189  
     public <T> StrBuilder appendAll(final T... array) {
 1190  5
         if (array != null && array.length > 0) {
 1191  14
             for (final Object element : array) {
 1192  11
                 append(element);
 1193  
             }
 1194  
         }
 1195  5
         return this;
 1196  
     }
 1197  
 
 1198  
     /**
 1199  
      * Appends each item in a iterable to the builder without any separators.
 1200  
      * Appending a null iterable will have no effect.
 1201  
      * Each object is appended using {@link #append(Object)}.
 1202  
      *
 1203  
      * @param iterable  the iterable to append
 1204  
      * @return this, to enable chaining
 1205  
      * @since 2.3
 1206  
      */
 1207  
     public StrBuilder appendAll(final Iterable<?> iterable) {
 1208  3
         if (iterable != null) {
 1209  2
             for (final Object o : iterable) {
 1210  3
                 append(o);
 1211  3
             }
 1212  
         }
 1213  3
         return this;
 1214  
     }
 1215  
 
 1216  
     /**
 1217  
      * Appends each item in an iterator to the builder without any separators.
 1218  
      * Appending a null iterator will have no effect.
 1219  
      * Each object is appended using {@link #append(Object)}.
 1220  
      *
 1221  
      * @param it  the iterator to append
 1222  
      * @return this, to enable chaining
 1223  
      * @since 2.3
 1224  
      */
 1225  
     public StrBuilder appendAll(final Iterator<?> it) {
 1226  3
         if (it != null) {
 1227  5
             while (it.hasNext()) {
 1228  3
                 append(it.next());
 1229  
             }
 1230  
         }
 1231  3
         return this;
 1232  
     }
 1233  
 
 1234  
     //-----------------------------------------------------------------------
 1235  
     /**
 1236  
      * Appends an array placing separators between each value, but
 1237  
      * not before the first or after the last.
 1238  
      * Appending a null array will have no effect.
 1239  
      * Each object is appended using {@link #append(Object)}.
 1240  
      *
 1241  
      * @param array  the array to append
 1242  
      * @param separator  the separator to use, null means no separator
 1243  
      * @return this, to enable chaining
 1244  
      */
 1245  
     public StrBuilder appendWithSeparators(final Object[] array, final String separator) {
 1246  6
         if (array != null && array.length > 0) {
 1247  
             @SuppressWarnings( "deprecation" ) // ObjectUtils.toString(Object) has been deprecated in 3.2
 1248  4
             final String sep = ObjectUtils.toString(separator);
 1249  4
             append(array[0]);
 1250  12
             for (int i = 1; i < array.length; i++) {
 1251  8
                 append(sep);
 1252  8
                 append(array[i]);
 1253  
             }
 1254  
         }
 1255  6
         return this;
 1256  
     }
 1257  
 
 1258  
     /**
 1259  
      * Appends a iterable placing separators between each value, but
 1260  
      * not before the first or after the last.
 1261  
      * Appending a null iterable will have no effect.
 1262  
      * Each object is appended using {@link #append(Object)}.
 1263  
      *
 1264  
      * @param iterable  the iterable to append
 1265  
      * @param separator  the separator to use, null means no separator
 1266  
      * @return this, to enable chaining
 1267  
      */
 1268  
     public StrBuilder appendWithSeparators(final Iterable<?> iterable, final String separator) {
 1269  8
         if (iterable != null) {
 1270  
             @SuppressWarnings( "deprecation" ) // ObjectUtils.toString(Object) has been deprecated in 3.2
 1271  7
             final String sep = ObjectUtils.toString(separator);
 1272  7
             final Iterator<?> it = iterable.iterator();
 1273  25
             while (it.hasNext()) {
 1274  18
                 append(it.next());
 1275  18
                 if (it.hasNext()) {
 1276  12
                     append(sep);
 1277  
                 }
 1278  
             }
 1279  
         }
 1280  8
         return this;
 1281  
     }
 1282  
 
 1283  
     /**
 1284  
      * Appends an iterator placing separators between each value, but
 1285  
      * not before the first or after the last.
 1286  
      * Appending a null iterator will have no effect.
 1287  
      * Each object is appended using {@link #append(Object)}.
 1288  
      *
 1289  
      * @param it  the iterator to append
 1290  
      * @param separator  the separator to use, null means no separator
 1291  
      * @return this, to enable chaining
 1292  
      */
 1293  
     public StrBuilder appendWithSeparators(final Iterator<?> it, final String separator) {
 1294  5
         if (it != null) {
 1295  
             @SuppressWarnings( "deprecation" ) // ObjectUtils.toString(Object) has been deprecated in 3.2
 1296  4
             final String sep = ObjectUtils.toString(separator);
 1297  13
             while (it.hasNext()) {
 1298  9
                 append(it.next());
 1299  9
                 if (it.hasNext()) {
 1300  6
                     append(sep);
 1301  
                 }
 1302  
             }
 1303  
         }
 1304  5
         return this;
 1305  
     }
 1306  
 
 1307  
     //-----------------------------------------------------------------------
 1308  
     /**
 1309  
      * Appends a separator if the builder is currently non-empty.
 1310  
      * Appending a null separator will have no effect.
 1311  
      * The separator is appended using {@link #append(String)}.
 1312  
      * <p>
 1313  
      * This method is useful for adding a separator each time around the
 1314  
      * loop except the first.
 1315  
      * <pre>
 1316  
      * for (Iterator it = list.iterator(); it.hasNext(); ) {
 1317  
      *   appendSeparator(",");
 1318  
      *   append(it.next());
 1319  
      * }
 1320  
      * </pre>
 1321  
      * Note that for this simple example, you should use
 1322  
      * {@link #appendWithSeparators(Iterable, String)}.
 1323  
      * 
 1324  
      * @param separator  the separator to use, null means no separator
 1325  
      * @return this, to enable chaining
 1326  
      * @since 2.3
 1327  
      */
 1328  
     public StrBuilder appendSeparator(final String separator) {
 1329  2
         return appendSeparator(separator, null);
 1330  
     }
 1331  
 
 1332  
     /**
 1333  
      * Appends one of both separators to the StrBuilder.
 1334  
      * If the builder is currently empty it will append the defaultIfEmpty-separator
 1335  
      * Otherwise it will append the standard-separator
 1336  
      * 
 1337  
      * Appending a null separator will have no effect.
 1338  
      * The separator is appended using {@link #append(String)}.
 1339  
      * <p>
 1340  
      * This method is for example useful for constructing queries
 1341  
      * <pre>
 1342  
      * StrBuilder whereClause = new StrBuilder();
 1343  
      * if(searchCommand.getPriority() != null) {
 1344  
      *  whereClause.appendSeparator(" and", " where");
 1345  
      *  whereClause.append(" priority = ?")
 1346  
      * }
 1347  
      * if(searchCommand.getComponent() != null) {
 1348  
      *  whereClause.appendSeparator(" and", " where");
 1349  
      *  whereClause.append(" component = ?")
 1350  
      * }
 1351  
      * selectClause.append(whereClause)
 1352  
      * </pre>
 1353  
      * 
 1354  
      * @param standard the separator if builder is not empty, null means no separator
 1355  
      * @param defaultIfEmpty the separator if builder is empty, null means no separator
 1356  
      * @return this, to enable chaining
 1357  
      * @since 2.5
 1358  
      */
 1359  
     public StrBuilder appendSeparator(final String standard, final String defaultIfEmpty) {
 1360  8
         final String str = isEmpty() ? defaultIfEmpty : standard;
 1361  8
         if (str != null) {
 1362  3
             append(str);
 1363  
         }
 1364  8
         return this;
 1365  
     }
 1366  
 
 1367  
     /**
 1368  
      * Appends a separator if the builder is currently non-empty.
 1369  
      * The separator is appended using {@link #append(char)}.
 1370  
      * <p>
 1371  
      * This method is useful for adding a separator each time around the
 1372  
      * loop except the first.
 1373  
      * <pre>
 1374  
      * for (Iterator it = list.iterator(); it.hasNext(); ) {
 1375  
      *   appendSeparator(',');
 1376  
      *   append(it.next());
 1377  
      * }
 1378  
      * </pre>
 1379  
      * Note that for this simple example, you should use
 1380  
      * {@link #appendWithSeparators(Iterable, String)}.
 1381  
      * 
 1382  
      * @param separator  the separator to use
 1383  
      * @return this, to enable chaining
 1384  
      * @since 2.3
 1385  
      */
 1386  
     public StrBuilder appendSeparator(final char separator) {
 1387  2
         if (size() > 0) {
 1388  1
             append(separator);
 1389  
         }
 1390  2
         return this;
 1391  
     }
 1392  
 
 1393  
     /**
 1394  
      * Append one of both separators to the builder
 1395  
      * If the builder is currently empty it will append the defaultIfEmpty-separator
 1396  
      * Otherwise it will append the standard-separator
 1397  
      *
 1398  
      * The separator is appended using {@link #append(char)}.
 1399  
      * @param standard the separator if builder is not empty
 1400  
      * @param defaultIfEmpty the separator if builder is empty
 1401  
      * @return this, to enable chaining
 1402  
      * @since 2.5
 1403  
      */
 1404  
     public StrBuilder appendSeparator(final char standard, final char defaultIfEmpty) {
 1405  2
         if (size() > 0) {
 1406  1
             append(standard);
 1407  
         } else {
 1408  1
             append(defaultIfEmpty);
 1409  
         }
 1410  2
         return this;
 1411  
     }
 1412  
     /**
 1413  
      * Appends a separator to the builder if the loop index is greater than zero.
 1414  
      * Appending a null separator will have no effect.
 1415  
      * The separator is appended using {@link #append(String)}.
 1416  
      * <p>
 1417  
      * This method is useful for adding a separator each time around the
 1418  
      * loop except the first.
 1419  
      * </p>
 1420  
      * <pre>
 1421  
      * for (int i = 0; i &lt; list.size(); i++) {
 1422  
      *   appendSeparator(",", i);
 1423  
      *   append(list.get(i));
 1424  
      * }
 1425  
      * </pre>
 1426  
      * Note that for this simple example, you should use
 1427  
      * {@link #appendWithSeparators(Iterable, String)}.
 1428  
      * 
 1429  
      * @param separator  the separator to use, null means no separator
 1430  
      * @param loopIndex  the loop index
 1431  
      * @return this, to enable chaining
 1432  
      * @since 2.3
 1433  
      */
 1434  
     public StrBuilder appendSeparator(final String separator, final int loopIndex) {
 1435  3
         if (separator != null && loopIndex > 0) {
 1436  1
             append(separator);
 1437  
         }
 1438  3
         return this;
 1439  
     }
 1440  
 
 1441  
     /**
 1442  
      * Appends a separator to the builder if the loop index is greater than zero.
 1443  
      * The separator is appended using {@link #append(char)}.
 1444  
      * <p>
 1445  
      * This method is useful for adding a separator each time around the
 1446  
      * loop except the first.
 1447  
      * </p>
 1448  
      * <pre>
 1449  
      * for (int i = 0; i &lt; list.size(); i++) {
 1450  
      *   appendSeparator(",", i);
 1451  
      *   append(list.get(i));
 1452  
      * }
 1453  
      * </pre>
 1454  
      * Note that for this simple example, you should use
 1455  
      * {@link #appendWithSeparators(Iterable, String)}.
 1456  
      * 
 1457  
      * @param separator  the separator to use
 1458  
      * @param loopIndex  the loop index
 1459  
      * @return this, to enable chaining
 1460  
      * @since 2.3
 1461  
      */
 1462  
     public StrBuilder appendSeparator(final char separator, final int loopIndex) {
 1463  3
         if (loopIndex > 0) {
 1464  1
             append(separator);
 1465  
         }
 1466  3
         return this;
 1467  
     }
 1468  
 
 1469  
     //-----------------------------------------------------------------------
 1470  
     /**
 1471  
      * Appends the pad character to the builder the specified number of times.
 1472  
      * 
 1473  
      * @param length  the length to append, negative means no append
 1474  
      * @param padChar  the character to append
 1475  
      * @return this, to enable chaining
 1476  
      */
 1477  
     public StrBuilder appendPadding(final int length, final char padChar) {
 1478  4
         if (length >= 0) {
 1479  3
             ensureCapacity(size + length);
 1480  20
             for (int i = 0; i < length; i++) {
 1481  17
                 buffer[size++] = padChar;
 1482  
             }
 1483  
         }
 1484  4
         return this;
 1485  
     }
 1486  
 
 1487  
     //-----------------------------------------------------------------------
 1488  
     /**
 1489  
      * Appends an object to the builder padding on the left to a fixed width.
 1490  
      * The <code>toString</code> of the object is used.
 1491  
      * If the object is larger than the length, the left hand side is lost.
 1492  
      * If the object is null, the null text value is used.
 1493  
      * 
 1494  
      * @param obj  the object to append, null uses null text
 1495  
      * @param width  the fixed field width, zero or negative has no effect
 1496  
      * @param padChar  the pad character to use
 1497  
      * @return this, to enable chaining
 1498  
      */
 1499  
     public StrBuilder appendFixedWidthPadLeft(final Object obj, final int width, final char padChar) {
 1500  16
         if (width > 0) {
 1501  12
             ensureCapacity(size + width);
 1502  12
             String str = (obj == null ? getNullText() : obj.toString());
 1503  12
             if (str == null) {
 1504  1
                 str = "";
 1505  
             }
 1506  12
             final int strLen = str.length();
 1507  12
             if (strLen >= width) {
 1508  6
                 str.getChars(strLen - width, strLen, buffer, size);
 1509  
             } else {
 1510  6
                 final int padLen = width - strLen;
 1511  33
                 for (int i = 0; i < padLen; i++) {
 1512  27
                     buffer[size + i] = padChar;
 1513  
                 }
 1514  6
                 str.getChars(0, strLen, buffer, size + padLen);
 1515  
             }
 1516  12
             size += width;
 1517  
         }
 1518  16
         return this;
 1519  
     }
 1520  
 
 1521  
     /**
 1522  
      * Appends an object to the builder padding on the left to a fixed width.
 1523  
      * The <code>String.valueOf</code> of the <code>int</code> value is used.
 1524  
      * If the formatted value is larger than the length, the left hand side is lost.
 1525  
      * 
 1526  
      * @param value  the value to append
 1527  
      * @param width  the fixed field width, zero or negative has no effect
 1528  
      * @param padChar  the pad character to use
 1529  
      * @return this, to enable chaining
 1530  
      */
 1531  
     public StrBuilder appendFixedWidthPadLeft(final int value, final int width, final char padChar) {
 1532  7
         return appendFixedWidthPadLeft(String.valueOf(value), width, padChar);
 1533  
     }
 1534  
 
 1535  
     /**
 1536  
      * Appends an object to the builder padding on the right to a fixed length.
 1537  
      * The <code>toString</code> of the object is used.
 1538  
      * If the object is larger than the length, the right hand side is lost.
 1539  
      * If the object is null, null text value is used.
 1540  
      * 
 1541  
      * @param obj  the object to append, null uses null text
 1542  
      * @param width  the fixed field width, zero or negative has no effect
 1543  
      * @param padChar  the pad character to use
 1544  
      * @return this, to enable chaining
 1545  
      */
 1546  
     public StrBuilder appendFixedWidthPadRight(final Object obj, final int width, final char padChar) {
 1547  17
         if (width > 0) {
 1548  13
             ensureCapacity(size + width);
 1549  13
             String str = (obj == null ? getNullText() : obj.toString());
 1550  13
             if (str == null) {
 1551  1
                 str = "";
 1552  
             }
 1553  13
             final int strLen = str.length();
 1554  13
             if (strLen >= width) {
 1555  7
                 str.getChars(0, width, buffer, size);
 1556  
             } else {
 1557  6
                 final int padLen = width - strLen;
 1558  6
                 str.getChars(0, strLen, buffer, size);
 1559  33
                 for (int i = 0; i < padLen; i++) {
 1560  27
                     buffer[size + strLen + i] = padChar;
 1561  
                 }
 1562  
             }
 1563  13
             size += width;
 1564  
         }
 1565  17
         return this;
 1566  
     }
 1567  
 
 1568  
     /**
 1569  
      * Appends an object to the builder padding on the right to a fixed length.
 1570  
      * The <code>String.valueOf</code> of the <code>int</code> value is used.
 1571  
      * If the object is larger than the length, the right hand side is lost.
 1572  
      * 
 1573  
      * @param value  the value to append
 1574  
      * @param width  the fixed field width, zero or negative has no effect
 1575  
      * @param padChar  the pad character to use
 1576  
      * @return this, to enable chaining
 1577  
      */
 1578  
     public StrBuilder appendFixedWidthPadRight(final int value, final int width, final char padChar) {
 1579  7
         return appendFixedWidthPadRight(String.valueOf(value), width, padChar);
 1580  
     }
 1581  
 
 1582  
     //-----------------------------------------------------------------------
 1583  
     /**
 1584  
      * Inserts the string representation of an object into this builder.
 1585  
      * Inserting null will use the stored null text value.
 1586  
      *
 1587  
      * @param index  the index to add at, must be valid
 1588  
      * @param obj  the object to insert
 1589  
      * @return this, to enable chaining
 1590  
      * @throws IndexOutOfBoundsException if the index is invalid
 1591  
      */
 1592  
     public StrBuilder insert(final int index, final Object obj) {
 1593  8
         if (obj == null) {
 1594  2
             return insert(index, nullText);
 1595  
         }
 1596  6
         return insert(index, obj.toString());
 1597  
     }
 1598  
 
 1599  
     /**
 1600  
      * Inserts the string into this builder.
 1601  
      * Inserting null will use the stored null text value.
 1602  
      *
 1603  
      * @param index  the index to add at, must be valid
 1604  
      * @param str  the string to insert
 1605  
      * @return this, to enable chaining
 1606  
      * @throws IndexOutOfBoundsException if the index is invalid
 1607  
      */
 1608  
     public StrBuilder insert(final int index, String str) {
 1609  31
         validateIndex(index);
 1610  15
         if (str == null) {
 1611  5
             str = nullText;
 1612  
         }
 1613  15
         if (str != null) {
 1614  11
             final int strLen = str.length();
 1615  11
             if (strLen > 0) {
 1616  11
                 final int newSize = size + strLen;
 1617  11
                 ensureCapacity(newSize);
 1618  11
                 System.arraycopy(buffer, index, buffer, index + strLen, size - index);
 1619  11
                 size = newSize;
 1620  11
                 str.getChars(0, strLen, buffer, index);
 1621  
             }
 1622  
         }
 1623  15
         return this;
 1624  
     }
 1625  
 
 1626  
     /**
 1627  
      * Inserts the character array into this builder.
 1628  
      * Inserting null will use the stored null text value.
 1629  
      *
 1630  
      * @param index  the index to add at, must be valid
 1631  
      * @param chars  the char array to insert
 1632  
      * @return this, to enable chaining
 1633  
      * @throws IndexOutOfBoundsException if the index is invalid
 1634  
      */
 1635  
     public StrBuilder insert(final int index, final char chars[]) {
 1636  6
         validateIndex(index);
 1637  4
         if (chars == null) {
 1638  2
             return insert(index, nullText);
 1639  
         }
 1640  2
         final int len = chars.length;
 1641  2
         if (len > 0) {
 1642  1
             ensureCapacity(size + len);
 1643  1
             System.arraycopy(buffer, index, buffer, index + len, size - index);
 1644  1
             System.arraycopy(chars, 0, buffer, index, len);
 1645  1
             size += len;
 1646  
         }
 1647  2
         return this;
 1648  
     }
 1649  
 
 1650  
     /**
 1651  
      * Inserts part of the character array into this builder.
 1652  
      * Inserting null will use the stored null text value.
 1653  
      *
 1654  
      * @param index  the index to add at, must be valid
 1655  
      * @param chars  the char array to insert
 1656  
      * @param offset  the offset into the character array to start at, must be valid
 1657  
      * @param length  the length of the character array part to copy, must be positive
 1658  
      * @return this, to enable chaining
 1659  
      * @throws IndexOutOfBoundsException if any index is invalid
 1660  
      */
 1661  
     public StrBuilder insert(final int index, final char chars[], final int offset, final int length) {
 1662  11
         validateIndex(index);
 1663  9
         if (chars == null) {
 1664  2
             return insert(index, nullText);
 1665  
         }
 1666  7
         if (offset < 0 || offset > chars.length) {
 1667  2
             throw new StringIndexOutOfBoundsException("Invalid offset: " + offset);
 1668  
         }
 1669  5
         if (length < 0 || offset + length > chars.length) {
 1670  2
             throw new StringIndexOutOfBoundsException("Invalid length: " + length);
 1671  
         }
 1672  3
         if (length > 0) {
 1673  1
             ensureCapacity(size + length);
 1674  1
             System.arraycopy(buffer, index, buffer, index + length, size - index);
 1675  1
             System.arraycopy(chars, offset, buffer, index, length);
 1676  1
             size += length;
 1677  
         }
 1678  3
         return this;
 1679  
     }
 1680  
 
 1681  
     /**
 1682  
      * Inserts the value into this builder.
 1683  
      *
 1684  
      * @param index  the index to add at, must be valid
 1685  
      * @param value  the value to insert
 1686  
      * @return this, to enable chaining
 1687  
      * @throws IndexOutOfBoundsException if the index is invalid
 1688  
      */
 1689  
     public StrBuilder insert(int index, final boolean value) {
 1690  4
         validateIndex(index);
 1691  2
         if (value) {
 1692  1
             ensureCapacity(size + 4);
 1693  1
             System.arraycopy(buffer, index, buffer, index + 4, size - index);
 1694  1
             buffer[index++] = 't';
 1695  1
             buffer[index++] = 'r';
 1696  1
             buffer[index++] = 'u';
 1697  1
             buffer[index] = 'e';
 1698  1
             size += 4;
 1699  
         } else {
 1700  1
             ensureCapacity(size + 5);
 1701  1
             System.arraycopy(buffer, index, buffer, index + 5, size - index);
 1702  1
             buffer[index++] = 'f';
 1703  1
             buffer[index++] = 'a';
 1704  1
             buffer[index++] = 'l';
 1705  1
             buffer[index++] = 's';
 1706  1
             buffer[index] = 'e';
 1707  1
             size += 5;
 1708  
         }
 1709  2
         return this;
 1710  
     }
 1711  
 
 1712  
     /**
 1713  
      * Inserts the value into this builder.
 1714  
      *
 1715  
      * @param index  the index to add at, must be valid
 1716  
      * @param value  the value to insert
 1717  
      * @return this, to enable chaining
 1718  
      * @throws IndexOutOfBoundsException if the index is invalid
 1719  
      */
 1720  
     public StrBuilder insert(final int index, final char value) {
 1721  4
         validateIndex(index);
 1722  2
         ensureCapacity(size + 1);
 1723  2
         System.arraycopy(buffer, index, buffer, index + 1, size - index);
 1724  2
         buffer[index] = value;
 1725  2
         size++;
 1726  2
         return this;
 1727  
     }
 1728  
 
 1729  
     /**
 1730  
      * Inserts the value into this builder.
 1731  
      *
 1732  
      * @param index  the index to add at, must be valid
 1733  
      * @param value  the value to insert
 1734  
      * @return this, to enable chaining
 1735  
      * @throws IndexOutOfBoundsException if the index is invalid
 1736  
      */
 1737  
     public StrBuilder insert(final int index, final int value) {
 1738  2
         return insert(index, String.valueOf(value));
 1739  
     }
 1740  
 
 1741  
     /**
 1742  
      * Inserts the value into this builder.
 1743  
      *
 1744  
      * @param index  the index to add at, must be valid
 1745  
      * @param value  the value to insert
 1746  
      * @return this, to enable chaining
 1747  
      * @throws IndexOutOfBoundsException if the index is invalid
 1748  
      */
 1749  
     public StrBuilder insert(final int index, final long value) {
 1750  3
         return insert(index, String.valueOf(value));
 1751  
     }
 1752  
 
 1753  
     /**
 1754  
      * Inserts the value into this builder.
 1755  
      *
 1756  
      * @param index  the index to add at, must be valid
 1757  
      * @param value  the value to insert
 1758  
      * @return this, to enable chaining
 1759  
      * @throws IndexOutOfBoundsException if the index is invalid
 1760  
      */
 1761  
     public StrBuilder insert(final int index, final float value) {
 1762  3
         return insert(index, String.valueOf(value));
 1763  
     }
 1764  
 
 1765  
     /**
 1766  
      * Inserts the value into this builder.
 1767  
      *
 1768  
      * @param index  the index to add at, must be valid
 1769  
      * @param value  the value to insert
 1770  
      * @return this, to enable chaining
 1771  
      * @throws IndexOutOfBoundsException if the index is invalid
 1772  
      */
 1773  
     public StrBuilder insert(final int index, final double value) {
 1774  3
         return insert(index, String.valueOf(value));
 1775  
     }
 1776  
 
 1777  
     //-----------------------------------------------------------------------
 1778  
     /**
 1779  
      * Internal method to delete a range without validation.
 1780  
      *
 1781  
      * @param startIndex  the start index, must be valid
 1782  
      * @param endIndex  the end index (exclusive), must be valid
 1783  
      * @param len  the length, must be valid
 1784  
      * @throws IndexOutOfBoundsException if any index is invalid
 1785  
      */
 1786  
     private void deleteImpl(final int startIndex, final int endIndex, final int len) {
 1787  132
         System.arraycopy(buffer, endIndex, buffer, startIndex, size - endIndex);
 1788  132
         size -= len;
 1789  132
     }
 1790  
 
 1791  
     /**
 1792  
      * Deletes the characters between the two specified indices.
 1793  
      *
 1794  
      * @param startIndex  the start index, inclusive, must be valid
 1795  
      * @param endIndex  the end index, exclusive, must be valid except
 1796  
      *  that if too large it is treated as end of string
 1797  
      * @return this, to enable chaining
 1798  
      * @throws IndexOutOfBoundsException if the index is invalid
 1799  
      */
 1800  
     public StrBuilder delete(final int startIndex, int endIndex) {
 1801  12
         endIndex = validateRange(startIndex, endIndex);
 1802  9
         final int len = endIndex - startIndex;
 1803  9
         if (len > 0) {
 1804  8
             deleteImpl(startIndex, endIndex, len);
 1805  
         }
 1806  9
         return this;
 1807  
     }
 1808  
 
 1809  
     //-----------------------------------------------------------------------
 1810  
     /**
 1811  
      * Deletes the character wherever it occurs in the builder.
 1812  
      *
 1813  
      * @param ch  the character to delete
 1814  
      * @return this, to enable chaining
 1815  
      */
 1816  
     public StrBuilder deleteAll(final char ch) {
 1817  24
         for (int i = 0; i < size; i++) {
 1818  19
             if (buffer[i] == ch) {
 1819  5
                 final int start = i;
 1820  8
                 while (++i < size) {
 1821  6
                     if (buffer[i] != ch) {
 1822  3
                         break;
 1823  
                     }
 1824  
                 }
 1825  5
                 final int len = i - start;
 1826  5
                 deleteImpl(start, i, len);
 1827  5
                 i -= len;
 1828  
             }
 1829  
         }
 1830  5
         return this;
 1831  
     }
 1832  
 
 1833  
     /**
 1834  
      * Deletes the character wherever it occurs in the builder.
 1835  
      *
 1836  
      * @param ch  the character to delete
 1837  
      * @return this, to enable chaining
 1838  
      */
 1839  
     public StrBuilder deleteFirst(final char ch) {
 1840  11
         for (int i = 0; i < size; i++) {
 1841  9
             if (buffer[i] == ch) {
 1842  3
                 deleteImpl(i, i + 1, 1);
 1843  3
                 break;
 1844  
             }
 1845  
         }
 1846  5
         return this;
 1847  
     }
 1848  
 
 1849  
     //-----------------------------------------------------------------------
 1850  
     /**
 1851  
      * Deletes the string wherever it occurs in the builder.
 1852  
      *
 1853  
      * @param str  the string to delete, null causes no action
 1854  
      * @return this, to enable chaining
 1855  
      */
 1856  
     public StrBuilder deleteAll(final String str) {
 1857  9
         final int len = (str == null ? 0 : str.length());
 1858  9
         if (len > 0) {
 1859  7
             int index = indexOf(str, 0);
 1860  19
             while (index >= 0) {
 1861  12
                 deleteImpl(index, index + len, len);
 1862  12
                 index = indexOf(str, index);
 1863  
             }
 1864  
         }
 1865  9
         return this;
 1866  
     }
 1867  
 
 1868  
     /**
 1869  
      * Deletes the string wherever it occurs in the builder.
 1870  
      *
 1871  
      * @param str  the string to delete, null causes no action
 1872  
      * @return this, to enable chaining
 1873  
      */
 1874  
     public StrBuilder deleteFirst(final String str) {
 1875  10
         final int len = (str == null ? 0 : str.length());
 1876  10
         if (len > 0) {
 1877  8
             final int index = indexOf(str, 0);
 1878  8
             if (index >= 0) {
 1879  6
                 deleteImpl(index, index + len, len);
 1880  
             }
 1881  
         }
 1882  10
         return this;
 1883  
     }
 1884  
 
 1885  
     //-----------------------------------------------------------------------
 1886  
     /**
 1887  
      * Deletes all parts of the builder that the matcher matches.
 1888  
      * <p>
 1889  
      * Matchers can be used to perform advanced deletion behaviour.
 1890  
      * For example you could write a matcher to delete all occurrences
 1891  
      * where the character 'a' is followed by a number.
 1892  
      *
 1893  
      * @param matcher  the matcher to use to find the deletion, null causes no action
 1894  
      * @return this, to enable chaining
 1895  
      */
 1896  
     public StrBuilder deleteAll(final StrMatcher matcher) {
 1897  4
         return replace(matcher, null, 0, size, -1);
 1898  
     }
 1899  
 
 1900  
     /**
 1901  
      * Deletes the first match within the builder using the specified matcher.
 1902  
      * <p>
 1903  
      * Matchers can be used to perform advanced deletion behaviour.
 1904  
      * For example you could write a matcher to delete
 1905  
      * where the character 'a' is followed by a number.
 1906  
      *
 1907  
      * @param matcher  the matcher to use to find the deletion, null causes no action
 1908  
      * @return this, to enable chaining
 1909  
      */
 1910  
     public StrBuilder deleteFirst(final StrMatcher matcher) {
 1911  4
         return replace(matcher, null, 0, size, 1);
 1912  
     }
 1913  
 
 1914  
     //-----------------------------------------------------------------------
 1915  
     /**
 1916  
      * Internal method to delete a range without validation.
 1917  
      *
 1918  
      * @param startIndex  the start index, must be valid
 1919  
      * @param endIndex  the end index (exclusive), must be valid
 1920  
      * @param removeLen  the length to remove (endIndex - startIndex), must be valid
 1921  
      * @param insertStr  the string to replace with, null means delete range
 1922  
      * @param insertLen  the length of the insert string, must be valid
 1923  
      * @throws IndexOutOfBoundsException if any index is invalid
 1924  
      */
 1925  
     private void replaceImpl(final int startIndex, final int endIndex, final int removeLen, final String insertStr, final int insertLen) {
 1926  814
         final int newSize = size - removeLen + insertLen;
 1927  814
         if (insertLen != removeLen) {
 1928  800
             ensureCapacity(newSize);
 1929  800
             System.arraycopy(buffer, endIndex, buffer, startIndex + insertLen, size - endIndex);
 1930  800
             size = newSize;
 1931  
         }
 1932  814
         if (insertLen > 0) {
 1933  797
             insertStr.getChars(0, insertLen, buffer, startIndex);
 1934  
         }
 1935  814
     }
 1936  
 
 1937  
     /**
 1938  
      * Replaces a portion of the string builder with another string.
 1939  
      * The length of the inserted string does not have to match the removed length.
 1940  
      *
 1941  
      * @param startIndex  the start index, inclusive, must be valid
 1942  
      * @param endIndex  the end index, exclusive, must be valid except
 1943  
      *  that if too large it is treated as end of string
 1944  
      * @param replaceStr  the string to replace with, null means delete range
 1945  
      * @return this, to enable chaining
 1946  
      * @throws IndexOutOfBoundsException if the index is invalid
 1947  
      */
 1948  
     public StrBuilder replace(final int startIndex, int endIndex, final String replaceStr) {
 1949  697
         endIndex = validateRange(startIndex, endIndex);
 1950  694
         final int insertLen = (replaceStr == null ? 0 : replaceStr.length());
 1951  694
         replaceImpl(startIndex, endIndex, endIndex - startIndex, replaceStr, insertLen);
 1952  694
         return this;
 1953  
     }
 1954  
 
 1955  
     //-----------------------------------------------------------------------
 1956  
     /**
 1957  
      * Replaces the search character with the replace character
 1958  
      * throughout the builder.
 1959  
      *
 1960  
      * @param search  the search character
 1961  
      * @param replace  the replace character
 1962  
      * @return this, to enable chaining
 1963  
      */
 1964  
     public StrBuilder replaceAll(final char search, final char replace) {
 1965  5
         if (search != replace) {
 1966  36
             for (int i = 0; i < size; i++) {
 1967  32
                 if (buffer[i] == search) {
 1968  8
                     buffer[i] = replace;
 1969  
                 }
 1970  
             }
 1971  
         }
 1972  5
         return this;
 1973  
     }
 1974  
 
 1975  
     /**
 1976  
      * Replaces the first instance of the search character with the
 1977  
      * replace character in the builder.
 1978  
      *
 1979  
      * @param search  the search character
 1980  
      * @param replace  the replace character
 1981  
      * @return this, to enable chaining
 1982  
      */
 1983  
     public StrBuilder replaceFirst(final char search, final char replace) {
 1984  5
         if (search != replace) {
 1985  15
             for (int i = 0; i < size; i++) {
 1986  14
                 if (buffer[i] == search) {
 1987  3
                     buffer[i] = replace;
 1988  3
                     break;
 1989  
                 }
 1990  
             }
 1991  
         }
 1992  5
         return this;
 1993  
     }
 1994  
 
 1995  
     //-----------------------------------------------------------------------
 1996  
     /**
 1997  
      * Replaces the search string with the replace string throughout the builder.
 1998  
      *
 1999  
      * @param searchStr  the search string, null causes no action to occur
 2000  
      * @param replaceStr  the replace string, null is equivalent to an empty string
 2001  
      * @return this, to enable chaining
 2002  
      */
 2003  
     public StrBuilder replaceAll(final String searchStr, final String replaceStr) {
 2004  10
         final int searchLen = (searchStr == null ? 0 : searchStr.length());
 2005  10
         if (searchLen > 0) {
 2006  6
             final int replaceLen = (replaceStr == null ? 0 : replaceStr.length());
 2007  6
             int index = indexOf(searchStr, 0);
 2008  16
             while (index >= 0) {
 2009  10
                 replaceImpl(index, index + searchLen, searchLen, replaceStr, replaceLen);
 2010  10
                 index = indexOf(searchStr, index + replaceLen);
 2011  
             }
 2012  
         }
 2013  10
         return this;
 2014  
     }
 2015  
 
 2016  
     /**
 2017  
      * Replaces the first instance of the search string with the replace string.
 2018  
      *
 2019  
      * @param searchStr  the search string, null causes no action to occur
 2020  
      * @param replaceStr  the replace string, null is equivalent to an empty string
 2021  
      * @return this, to enable chaining
 2022  
      */
 2023  
     public StrBuilder replaceFirst(final String searchStr, final String replaceStr) {
 2024  10
         final int searchLen = (searchStr == null ? 0 : searchStr.length());
 2025  10
         if (searchLen > 0) {
 2026  6
             final int index = indexOf(searchStr, 0);
 2027  6
             if (index >= 0) {
 2028  5
                 final int replaceLen = (replaceStr == null ? 0 : replaceStr.length());
 2029  5
                 replaceImpl(index, index + searchLen, searchLen, replaceStr, replaceLen);
 2030  
             }
 2031  
         }
 2032  10
         return this;
 2033  
     }
 2034  
 
 2035  
     //-----------------------------------------------------------------------
 2036  
     /**
 2037  
      * Replaces all matches within the builder with the replace string.
 2038  
      * <p>
 2039  
      * Matchers can be used to perform advanced replace behaviour.
 2040  
      * For example you could write a matcher to replace all occurrences
 2041  
      * where the character 'a' is followed by a number.
 2042  
      *
 2043  
      * @param matcher  the matcher to use to find the deletion, null causes no action
 2044  
      * @param replaceStr  the replace string, null is equivalent to an empty string
 2045  
      * @return this, to enable chaining
 2046  
      */
 2047  
     public StrBuilder replaceAll(final StrMatcher matcher, final String replaceStr) {
 2048  11
         return replace(matcher, replaceStr, 0, size, -1);
 2049  
     }
 2050  
 
 2051  
     /**
 2052  
      * Replaces the first match within the builder with the replace string.
 2053  
      * <p>
 2054  
      * Matchers can be used to perform advanced replace behaviour.
 2055  
      * For example you could write a matcher to replace
 2056  
      * where the character 'a' is followed by a number.
 2057  
      *
 2058  
      * @param matcher  the matcher to use to find the deletion, null causes no action
 2059  
      * @param replaceStr  the replace string, null is equivalent to an empty string
 2060  
      * @return this, to enable chaining
 2061  
      */
 2062  
     public StrBuilder replaceFirst(final StrMatcher matcher, final String replaceStr) {
 2063  11
         return replace(matcher, replaceStr, 0, size, 1);
 2064  
     }
 2065  
 
 2066  
     // -----------------------------------------------------------------------
 2067  
     /**
 2068  
      * Advanced search and replaces within the builder using a matcher.
 2069  
      * <p>
 2070  
      * Matchers can be used to perform advanced behaviour.
 2071  
      * For example you could write a matcher to delete all occurrences
 2072  
      * where the character 'a' is followed by a number.
 2073  
      *
 2074  
      * @param matcher  the matcher to use to find the deletion, null causes no action
 2075  
      * @param replaceStr  the string to replace the match with, null is a delete
 2076  
      * @param startIndex  the start index, inclusive, must be valid
 2077  
      * @param endIndex  the end index, exclusive, must be valid except
 2078  
      *  that if too large it is treated as end of string
 2079  
      * @param replaceCount  the number of times to replace, -1 for replace all
 2080  
      * @return this, to enable chaining
 2081  
      * @throws IndexOutOfBoundsException if start index is invalid
 2082  
      */
 2083  
     public StrBuilder replace(
 2084  
             final StrMatcher matcher, final String replaceStr,
 2085  
             final int startIndex, int endIndex, final int replaceCount) {
 2086  72
         endIndex = validateRange(startIndex, endIndex);
 2087  69
         return replaceImpl(matcher, replaceStr, startIndex, endIndex, replaceCount);
 2088  
     }
 2089  
 
 2090  
     /**
 2091  
      * Replaces within the builder using a matcher.
 2092  
      * <p>
 2093  
      * Matchers can be used to perform advanced behaviour.
 2094  
      * For example you could write a matcher to delete all occurrences
 2095  
      * where the character 'a' is followed by a number.
 2096  
      *
 2097  
      * @param matcher  the matcher to use to find the deletion, null causes no action
 2098  
      * @param replaceStr  the string to replace the match with, null is a delete
 2099  
      * @param from  the start index, must be valid
 2100  
      * @param to  the end index (exclusive), must be valid
 2101  
      * @param replaceCount  the number of times to replace, -1 for replace all
 2102  
      * @return this, to enable chaining
 2103  
      * @throws IndexOutOfBoundsException if any index is invalid
 2104  
      */
 2105  
     private StrBuilder replaceImpl(
 2106  
             final StrMatcher matcher, final String replaceStr,
 2107  
             final int from, int to, int replaceCount) {
 2108  69
         if (matcher == null || size == 0) {
 2109  10
             return this;
 2110  
         }
 2111  59
         final int replaceLen = (replaceStr == null ? 0 : replaceStr.length());
 2112  59
         final char[] buf = buffer;
 2113  312
         for (int i = from; i < to && replaceCount != 0; i++) {
 2114  253
             final int removeLen = matcher.isMatch(buf, i, from, to);
 2115  253
             if (removeLen > 0) {
 2116  105
                 replaceImpl(i, i + removeLen, removeLen, replaceStr, replaceLen);
 2117  105
                 to = to - removeLen + replaceLen;
 2118  105
                 i = i + replaceLen - 1;
 2119  105
                 if (replaceCount > 0) {
 2120  21
                     replaceCount--;
 2121  
                 }
 2122  
             }
 2123  
         }
 2124  59
         return this;
 2125  
     }
 2126  
 
 2127  
     //-----------------------------------------------------------------------
 2128  
     /**
 2129  
      * Reverses the string builder placing each character in the opposite index.
 2130  
      * 
 2131  
      * @return this, to enable chaining
 2132  
      */
 2133  
     public StrBuilder reverse() {
 2134  5
         if (size == 0) {
 2135  3
             return this;
 2136  
         }
 2137  
         
 2138  2
         final int half = size / 2;
 2139  2
         final char[] buf = buffer;
 2140  6
         for (int leftIdx = 0, rightIdx = size - 1; leftIdx < half; leftIdx++,rightIdx--) {
 2141  4
             final char swap = buf[leftIdx];
 2142  4
             buf[leftIdx] = buf[rightIdx];
 2143  4
             buf[rightIdx] = swap;
 2144  
         }
 2145  2
         return this;
 2146  
     }
 2147  
 
 2148  
     //-----------------------------------------------------------------------
 2149  
     /**
 2150  
      * Trims the builder by removing characters less than or equal to a space
 2151  
      * from the beginning and end.
 2152  
      *
 2153  
      * @return this, to enable chaining
 2154  
      */
 2155  
     public StrBuilder trim() {
 2156  6
         if (size == 0) {
 2157  1
             return this;
 2158  
         }
 2159  5
         int len = size;
 2160  5
         final char[] buf = buffer;
 2161  5
         int pos = 0;
 2162  14
         while (pos < len && buf[pos] <= ' ') {
 2163  9
             pos++;
 2164  
         }
 2165  11
         while (pos < len && buf[len - 1] <= ' ') {
 2166  6
             len--;
 2167  
         }
 2168  5
         if (len < size) {
 2169  2
             delete(len, size);
 2170  
         }
 2171  5
         if (pos > 0) {
 2172  3
             delete(0, pos);
 2173  
         }
 2174  5
         return this;
 2175  
     }
 2176  
 
 2177  
     //-----------------------------------------------------------------------
 2178  
     /**
 2179  
      * Checks whether this builder starts with the specified string.
 2180  
      * <p>
 2181  
      * Note that this method handles null input quietly, unlike String.
 2182  
      * 
 2183  
      * @param str  the string to search for, null returns false
 2184  
      * @return true if the builder starts with the string
 2185  
      */
 2186  
     public boolean startsWith(final String str) {
 2187  7
         if (str == null) {
 2188  1
             return false;
 2189  
         }
 2190  6
         final int len = str.length();
 2191  6
         if (len == 0) {
 2192  1
             return true;
 2193  
         }
 2194  5
         if (len > size) {
 2195  1
             return false;
 2196  
         }
 2197  10
         for (int i = 0; i < len; i++) {
 2198  7
             if (buffer[i] != str.charAt(i)) {
 2199  1
                 return false;
 2200  
             }
 2201  
         }
 2202  3
         return true;
 2203  
     }
 2204  
 
 2205  
     /**
 2206  
      * Checks whether this builder ends with the specified string.
 2207  
      * <p>
 2208  
      * Note that this method handles null input quietly, unlike String.
 2209  
      * 
 2210  
      * @param str  the string to search for, null returns false
 2211  
      * @return true if the builder ends with the string
 2212  
      */
 2213  
     public boolean endsWith(final String str) {
 2214  11
         if (str == null) {
 2215  1
             return false;
 2216  
         }
 2217  10
         final int len = str.length();
 2218  10
         if (len == 0) {
 2219  1
             return true;
 2220  
         }
 2221  9
         if (len > size) {
 2222  5
             return false;
 2223  
         }
 2224  4
         int pos = size - len;
 2225  10
         for (int i = 0; i < len; i++,pos++) {
 2226  7
             if (buffer[pos] != str.charAt(i)) {
 2227  1
                 return false;
 2228  
             }
 2229  
         }
 2230  3
         return true;
 2231  
     }
 2232  
 
 2233  
     //-----------------------------------------------------------------------
 2234  
     /**
 2235  
      * {@inheritDoc}
 2236  
      * @since 3.0
 2237  
      */
 2238  
     @Override
 2239  
     public CharSequence subSequence(final int startIndex, final int endIndex) {
 2240  8
       if (startIndex < 0) {
 2241  1
           throw new StringIndexOutOfBoundsException(startIndex);
 2242  
       }
 2243  7
       if (endIndex > size) {
 2244  1
           throw new StringIndexOutOfBoundsException(endIndex);
 2245  
       }
 2246  6
       if (startIndex > endIndex) {
 2247  2
           throw new StringIndexOutOfBoundsException(endIndex - startIndex);
 2248  
       }
 2249  4
       return substring(startIndex, endIndex);
 2250  
     }
 2251  
 
 2252  
     /**
 2253  
      * Extracts a portion of this string builder as a string.
 2254  
      * 
 2255  
      * @param start  the start index, inclusive, must be valid
 2256  
      * @return the new string
 2257  
      * @throws IndexOutOfBoundsException if the index is invalid
 2258  
      */
 2259  
     public String substring(final int start) {
 2260  6
         return substring(start, size);
 2261  
     }
 2262  
 
 2263  
     /**
 2264  
      * Extracts a portion of this string builder as a string.
 2265  
      * <p>
 2266  
      * Note: This method treats an endIndex greater than the length of the
 2267  
      * builder as equal to the length of the builder, and continues
 2268  
      * without error, unlike StringBuffer or String.
 2269  
      * 
 2270  
      * @param startIndex  the start index, inclusive, must be valid
 2271  
      * @param endIndex  the end index, exclusive, must be valid except
 2272  
      *  that if too large it is treated as end of string
 2273  
      * @return the new string
 2274  
      * @throws IndexOutOfBoundsException if the index is invalid
 2275  
      */
 2276  
     public String substring(final int startIndex, int endIndex) {
 2277  196
         endIndex = validateRange(startIndex, endIndex);
 2278  192
         return new String(buffer, startIndex, endIndex - startIndex);
 2279  
     }
 2280  
 
 2281  
     /**
 2282  
      * Extracts the leftmost characters from the string builder without
 2283  
      * throwing an exception.
 2284  
      * <p>
 2285  
      * This method extracts the left <code>length</code> characters from
 2286  
      * the builder. If this many characters are not available, the whole
 2287  
      * builder is returned. Thus the returned string may be shorter than the
 2288  
      * length requested.
 2289  
      * 
 2290  
      * @param length  the number of characters to extract, negative returns empty string
 2291  
      * @return the new string
 2292  
      */
 2293  
     public String leftString(final int length) {
 2294  4
         if (length <= 0) {
 2295  2
             return "";
 2296  2
         } else if (length >= size) {
 2297  1
             return new String(buffer, 0, size);
 2298  
         } else {
 2299  1
             return new String(buffer, 0, length);
 2300  
         }
 2301  
     }
 2302  
 
 2303  
     /**
 2304  
      * Extracts the rightmost characters from the string builder without
 2305  
      * throwing an exception.
 2306  
      * <p>
 2307  
      * This method extracts the right <code>length</code> characters from
 2308  
      * the builder. If this many characters are not available, the whole
 2309  
      * builder is returned. Thus the returned string may be shorter than the
 2310  
      * length requested.
 2311  
      * 
 2312  
      * @param length  the number of characters to extract, negative returns empty string
 2313  
      * @return the new string
 2314  
      */
 2315  
     public String rightString(final int length) {
 2316  4
         if (length <= 0) {
 2317  2
             return "";
 2318  2
         } else if (length >= size) {
 2319  1
             return new String(buffer, 0, size);
 2320  
         } else {
 2321  1
             return new String(buffer, size - length, length);
 2322  
         }
 2323  
     }
 2324  
 
 2325  
     /**
 2326  
      * Extracts some characters from the middle of the string builder without
 2327  
      * throwing an exception.
 2328  
      * <p>
 2329  
      * This method extracts <code>length</code> characters from the builder
 2330  
      * at the specified index.
 2331  
      * If the index is negative it is treated as zero.
 2332  
      * If the index is greater than the builder size, it is treated as the builder size.
 2333  
      * If the length is negative, the empty string is returned.
 2334  
      * If insufficient characters are available in the builder, as much as possible is returned.
 2335  
      * Thus the returned string may be shorter than the length requested.
 2336  
      * 
 2337  
      * @param index  the index to start at, negative means zero
 2338  
      * @param length  the number of characters to extract, negative returns empty string
 2339  
      * @return the new string
 2340  
      */
 2341  
     public String midString(int index, final int length) {
 2342  6
         if (index < 0) {
 2343  1
             index = 0;
 2344  
         }
 2345  6
         if (length <= 0 || index >= size) {
 2346  2
             return "";
 2347  
         }
 2348  4
         if (size <= index + length) {
 2349  1
             return new String(buffer, index, size - index);
 2350  
         }
 2351  3
         return new String(buffer, index, length);
 2352  
     }
 2353  
 
 2354  
     //-----------------------------------------------------------------------
 2355  
     /**
 2356  
      * Checks if the string builder contains the specified char.
 2357  
      *
 2358  
      * @param ch  the character to find
 2359  
      * @return true if the builder contains the character
 2360  
      */
 2361  
     public boolean contains(final char ch) {
 2362  5
         final char[] thisBuf = buffer;
 2363  76
         for (int i = 0; i < this.size; i++) {
 2364  74
             if (thisBuf[i] == ch) {
 2365  3
                 return true;
 2366  
             }
 2367  
         }
 2368  2
         return false;
 2369  
     }
 2370  
 
 2371  
     /**
 2372  
      * Checks if the string builder contains the specified string.
 2373  
      *
 2374  
      * @param str  the string to find
 2375  
      * @return true if the builder contains the string
 2376  
      */
 2377  
     public boolean contains(final String str) {
 2378  5
         return indexOf(str, 0) >= 0;
 2379  
     }
 2380  
 
 2381  
     /**
 2382  
      * Checks if the string builder contains a string matched using the
 2383  
      * specified matcher.
 2384  
      * <p>
 2385  
      * Matchers can be used to perform advanced searching behaviour.
 2386  
      * For example you could write a matcher to search for the character
 2387  
      * 'a' followed by a number.
 2388  
      *
 2389  
      * @param matcher  the matcher to use, null returns -1
 2390  
      * @return true if the matcher finds a match in the builder
 2391  
      */
 2392  
     public boolean contains(final StrMatcher matcher) {
 2393  7
         return indexOf(matcher, 0) >= 0;
 2394  
     }
 2395  
 
 2396  
     //-----------------------------------------------------------------------
 2397  
     /**
 2398  
      * Searches the string builder to find the first reference to the specified char.
 2399  
      * 
 2400  
      * @param ch  the character to find
 2401  
      * @return the first index of the character, or -1 if not found
 2402  
      */
 2403  
     public int indexOf(final char ch) {
 2404  6
         return indexOf(ch, 0);
 2405  
     }
 2406  
 
 2407  
     /**
 2408  
      * Searches the string builder to find the first reference to the specified char.
 2409  
      * 
 2410  
      * @param ch  the character to find
 2411  
      * @param startIndex  the index to start at, invalid index rounded to edge
 2412  
      * @return the first index of the character, or -1 if not found
 2413  
      */
 2414  
     public int indexOf(final char ch, int startIndex) {
 2415  62
         startIndex = (startIndex < 0 ? 0 : startIndex);
 2416  62
         if (startIndex >= size) {
 2417  2
             return -1;
 2418  
         }
 2419  60
         final char[] thisBuf = buffer;
 2420  179
         for (int i = startIndex; i < size; i++) {
 2421  166
             if (thisBuf[i] == ch) {
 2422  47
                 return i;
 2423  
             }
 2424  
         }
 2425  13
         return -1;
 2426  
     }
 2427  
 
 2428  
     /**
 2429  
      * Searches the string builder to find the first reference to the specified string.
 2430  
      * <p>
 2431  
      * Note that a null input string will return -1, whereas the JDK throws an exception.
 2432  
      * 
 2433  
      * @param str  the string to find, null returns -1
 2434  
      * @return the first index of the string, or -1 if not found
 2435  
      */
 2436  
     public int indexOf(final String str) {
 2437  11
         return indexOf(str, 0);
 2438  
     }
 2439  
 
 2440  
     /**
 2441  
      * Searches the string builder to find the first reference to the specified
 2442  
      * string starting searching from the given index.
 2443  
      * <p>
 2444  
      * Note that a null input string will return -1, whereas the JDK throws an exception.
 2445  
      * 
 2446  
      * @param str  the string to find, null returns -1
 2447  
      * @param startIndex  the index to start at, invalid index rounded to edge
 2448  
      * @return the first index of the string, or -1 if not found
 2449  
      */
 2450  
     public int indexOf(final String str, int startIndex) {
 2451  86
         startIndex = (startIndex < 0 ? 0 : startIndex);
 2452  86
         if (str == null || startIndex >= size) {
 2453  13
             return -1;
 2454  
         }
 2455  73
         final int strLen = str.length();
 2456  73
         if (strLen == 1) {
 2457  45
             return indexOf(str.charAt(0), startIndex);
 2458  
         }
 2459  28
         if (strLen == 0) {
 2460  2
             return startIndex;
 2461  
         }
 2462  26
         if (strLen > size) {
 2463  1
             return -1;
 2464  
         }
 2465  25
         final char[] thisBuf = buffer;
 2466  25
         final int len = size - strLen + 1;
 2467  
         outer:
 2468  124
         for (int i = startIndex; i < len; i++) {
 2469  178
             for (int j = 0; j < strLen; j++) {
 2470  159
                 if (str.charAt(j) != thisBuf[i + j]) {
 2471  99
                     continue outer;
 2472  
                 }
 2473  
             }
 2474  19
             return i;
 2475  
         }
 2476  6
         return -1;
 2477  
     }
 2478  
 
 2479  
     /**
 2480  
      * Searches the string builder using the matcher to find the first match.
 2481  
      * <p>
 2482  
      * Matchers can be used to perform advanced searching behaviour.
 2483  
      * For example you could write a matcher to find the character 'a'
 2484  
      * followed by a number.
 2485  
      *
 2486  
      * @param matcher  the matcher to use, null returns -1
 2487  
      * @return the first index matched, or -1 if not found
 2488  
      */
 2489  
     public int indexOf(final StrMatcher matcher) {
 2490  9
         return indexOf(matcher, 0);
 2491  
     }
 2492  
 
 2493  
     /**
 2494  
      * Searches the string builder using the matcher to find the first
 2495  
      * match searching from the given index.
 2496  
      * <p>
 2497  
      * Matchers can be used to perform advanced searching behaviour.
 2498  
      * For example you could write a matcher to find the character 'a'
 2499  
      * followed by a number.
 2500  
      *
 2501  
      * @param matcher  the matcher to use, null returns -1
 2502  
      * @param startIndex  the index to start at, invalid index rounded to edge
 2503  
      * @return the first index matched, or -1 if not found
 2504  
      */
 2505  
     public int indexOf(final StrMatcher matcher, int startIndex) {
 2506  44
         startIndex = (startIndex < 0 ? 0 : startIndex);
 2507  44
         if (matcher == null || startIndex >= size) {
 2508  13
             return -1;
 2509  
         }
 2510  31
         final int len = size;
 2511  31
         final char[] buf = buffer;
 2512  153
         for (int i = startIndex; i < len; i++) {
 2513  146
             if (matcher.isMatch(buf, i, startIndex, len) > 0) {
 2514  24
                 return i;
 2515  
             }
 2516  
         }
 2517  7
         return -1;
 2518  
     }
 2519  
 
 2520  
     //-----------------------------------------------------------------------
 2521  
     /**
 2522  
      * Searches the string builder to find the last reference to the specified char.
 2523  
      * 
 2524  
      * @param ch  the character to find
 2525  
      * @return the last index of the character, or -1 if not found
 2526  
      */
 2527  
     public int lastIndexOf(final char ch) {
 2528  5
         return lastIndexOf(ch, size - 1);
 2529  
     }
 2530  
 
 2531  
     /**
 2532  
      * Searches the string builder to find the last reference to the specified char.
 2533  
      * 
 2534  
      * @param ch  the character to find
 2535  
      * @param startIndex  the index to start at, invalid index rounded to edge
 2536  
      * @return the last index of the character, or -1 if not found
 2537  
      */
 2538  
     public int lastIndexOf(final char ch, int startIndex) {
 2539  29
         startIndex = (startIndex >= size ? size - 1 : startIndex);
 2540  29
         if (startIndex < 0) {
 2541  1
             return -1;
 2542  
         }
 2543  62
         for (int i = startIndex; i >= 0; i--) {
 2544  57
             if (buffer[i] == ch) {
 2545  23
                 return i;
 2546  
             }
 2547  
         }
 2548  5
         return -1;
 2549  
     }
 2550  
 
 2551  
     /**
 2552  
      * Searches the string builder to find the last reference to the specified string.
 2553  
      * <p>
 2554  
      * Note that a null input string will return -1, whereas the JDK throws an exception.
 2555  
      * 
 2556  
      * @param str  the string to find, null returns -1
 2557  
      * @return the last index of the string, or -1 if not found
 2558  
      */
 2559  
     public int lastIndexOf(final String str) {
 2560  10
         return lastIndexOf(str, size - 1);
 2561  
     }
 2562  
 
 2563  
     /**
 2564  
      * Searches the string builder to find the last reference to the specified
 2565  
      * string starting searching from the given index.
 2566  
      * <p>
 2567  
      * Note that a null input string will return -1, whereas the JDK throws an exception.
 2568  
      * 
 2569  
      * @param str  the string to find, null returns -1
 2570  
      * @param startIndex  the index to start at, invalid index rounded to edge
 2571  
      * @return the last index of the string, or -1 if not found
 2572  
      */
 2573  
     public int lastIndexOf(final String str, int startIndex) {
 2574  31
         startIndex = (startIndex >= size ? size - 1 : startIndex);
 2575  31
         if (str == null || startIndex < 0) {
 2576  3
             return -1;
 2577  
         }
 2578  28
         final int strLen = str.length();
 2579  28
         if (strLen > 0 && strLen <= size) {
 2580  25
             if (strLen == 1) {
 2581  15
                 return lastIndexOf(str.charAt(0), startIndex);
 2582  
             }
 2583  
 
 2584  
             outer:
 2585  15
             for (int i = startIndex - strLen + 1; i >= 0; i--) {
 2586  32
                 for (int j = 0; j < strLen; j++) {
 2587  23
                     if (str.charAt(j) != buffer[i + j]) {
 2588  5
                         continue outer;
 2589  
                     }
 2590  
                 }
 2591  9
                 return i;
 2592  
             }
 2593  
             
 2594  3
         } else if (strLen == 0) {
 2595  2
             return startIndex;
 2596  
         }
 2597  2
         return -1;
 2598  
     }
 2599  
 
 2600  
     /**
 2601  
      * Searches the string builder using the matcher to find the last match.
 2602  
      * <p>
 2603  
      * Matchers can be used to perform advanced searching behaviour.
 2604  
      * For example you could write a matcher to find the character 'a'
 2605  
      * followed by a number.
 2606  
      *
 2607  
      * @param matcher  the matcher to use, null returns -1
 2608  
      * @return the last index matched, or -1 if not found
 2609  
      */
 2610  
     public int lastIndexOf(final StrMatcher matcher) {
 2611  9
         return lastIndexOf(matcher, size);
 2612  
     }
 2613  
 
 2614  
     /**
 2615  
      * Searches the string builder using the matcher to find the last
 2616  
      * match searching from the given index.
 2617  
      * <p>
 2618  
      * Matchers can be used to perform advanced searching behaviour.
 2619  
      * For example you could write a matcher to find the character 'a'
 2620  
      * followed by a number.
 2621  
      *
 2622  
      * @param matcher  the matcher to use, null returns -1
 2623  
      * @param startIndex  the index to start at, invalid index rounded to edge
 2624  
      * @return the last index matched, or -1 if not found
 2625  
      */
 2626  
     public int lastIndexOf(final StrMatcher matcher, int startIndex) {
 2627  38
         startIndex = (startIndex >= size ? size - 1 : startIndex);
 2628  38
         if (matcher == null || startIndex < 0) {
 2629  11
             return -1;
 2630  
         }
 2631  27
         final char[] buf = buffer;
 2632  27
         final int endIndex = startIndex + 1;
 2633  114
         for (int i = startIndex; i >= 0; i--) {
 2634  108
             if (matcher.isMatch(buf, i, 0, endIndex) > 0) {
 2635  21
                 return i;
 2636  
             }
 2637  
         }
 2638  6
         return -1;
 2639  
     }
 2640  
 
 2641  
     //-----------------------------------------------------------------------
 2642  
     /**
 2643  
      * Creates a tokenizer that can tokenize the contents of this builder.
 2644  
      * <p>
 2645  
      * This method allows the contents of this builder to be tokenized.
 2646  
      * The tokenizer will be setup by default to tokenize on space, tab,
 2647  
      * newline and formfeed (as per StringTokenizer). These values can be
 2648  
      * changed on the tokenizer class, before retrieving the tokens.
 2649  
      * <p>
 2650  
      * The returned tokenizer is linked to this builder. You may intermix
 2651  
      * calls to the buider and tokenizer within certain limits, however
 2652  
      * there is no synchronization. Once the tokenizer has been used once,
 2653  
      * it must be {@link StrTokenizer#reset() reset} to pickup the latest
 2654  
      * changes in the builder. For example:
 2655  
      * <pre>
 2656  
      * StrBuilder b = new StrBuilder();
 2657  
      * b.append("a b ");
 2658  
      * StrTokenizer t = b.asTokenizer();
 2659  
      * String[] tokens1 = t.getTokenArray();  // returns a,b
 2660  
      * b.append("c d ");
 2661  
      * String[] tokens2 = t.getTokenArray();  // returns a,b (c and d ignored)
 2662  
      * t.reset();              // reset causes builder changes to be picked up
 2663  
      * String[] tokens3 = t.getTokenArray();  // returns a,b,c,d
 2664  
      * </pre>
 2665  
      * In addition to simply intermixing appends and tokenization, you can also
 2666  
      * call the set methods on the tokenizer to alter how it tokenizes. Just
 2667  
      * remember to call reset when you want to pickup builder changes.
 2668  
      * <p>
 2669  
      * Calling {@link StrTokenizer#reset(String)} or {@link StrTokenizer#reset(char[])}
 2670  
      * with a non-null value will break the link with the builder.
 2671  
      *
 2672  
      * @return a tokenizer that is linked to this builder
 2673  
      */
 2674  
     public StrTokenizer asTokenizer() {
 2675  1
         return new StrBuilderTokenizer();
 2676  
     }
 2677  
 
 2678  
     //-----------------------------------------------------------------------
 2679  
     /**
 2680  
      * Gets the contents of this builder as a Reader.
 2681  
      * <p>
 2682  
      * This method allows the contents of the builder to be read
 2683  
      * using any standard method that expects a Reader.
 2684  
      * <p>
 2685  
      * To use, simply create a <code>StrBuilder</code>, populate it with
 2686  
      * data, call <code>asReader</code>, and then read away.
 2687  
      * <p>
 2688  
      * The internal character array is shared between the builder and the reader.
 2689  
      * This allows you to append to the builder after creating the reader,
 2690  
      * and the changes will be picked up.
 2691  
      * Note however, that no synchronization occurs, so you must perform
 2692  
      * all operations with the builder and the reader in one thread.
 2693  
      * <p>
 2694  
      * The returned reader supports marking, and ignores the flush method.
 2695  
      *
 2696  
      * @return a reader that reads from this builder
 2697  
      */
 2698  
     public Reader asReader() {
 2699  3
         return new StrBuilderReader();
 2700  
     }
 2701  
 
 2702  
     //-----------------------------------------------------------------------
 2703  
     /**
 2704  
      * Gets this builder as a Writer that can be written to.
 2705  
      * <p>
 2706  
      * This method allows you to populate the contents of the builder
 2707  
      * using any standard method that takes a Writer.
 2708  
      * <p>
 2709  
      * To use, simply create a <code>StrBuilder</code>,
 2710  
      * call <code>asWriter</code>, and populate away. The data is available
 2711  
      * at any time using the methods of the <code>StrBuilder</code>.
 2712  
      * <p>
 2713  
      * The internal character array is shared between the builder and the writer.
 2714  
      * This allows you to intermix calls that append to the builder and
 2715  
      * write using the writer and the changes will be occur correctly.
 2716  
      * Note however, that no synchronization occurs, so you must perform
 2717  
      * all operations with the builder and the writer in one thread.
 2718  
      * <p>
 2719  
      * The returned writer ignores the close and flush methods.
 2720  
      *
 2721  
      * @return a writer that populates this builder
 2722  
      */
 2723  
     public Writer asWriter() {
 2724  1
         return new StrBuilderWriter();
 2725  
     }
 2726  
 
 2727  
     /**
 2728  
      * Appends current contents of this <code>StrBuilder</code> to the
 2729  
      * provided {@link Appendable}.
 2730  
      * <p>
 2731  
      * This method tries to avoid doing any extra copies of contents.
 2732  
      *
 2733  
      * @param appendable  the appendable to append data to
 2734  
      * @throws IOException  if an I/O error occurs
 2735  
      *
 2736  
      * @since 3.4
 2737  
      * @see #readFrom(Readable)
 2738  
      */
 2739  
     public void appendTo(final Appendable appendable) throws IOException {
 2740  4
         if (appendable instanceof Writer) {
 2741  1
             ((Writer) appendable).write(buffer, 0, size);
 2742  3
         } else if (appendable instanceof StringBuilder) {
 2743  1
             ((StringBuilder) appendable).append(buffer, 0, size);
 2744  2
         } else if (appendable instanceof StringBuffer) {
 2745  1
             ((StringBuffer) appendable).append(buffer, 0, size);
 2746  1
         } else if (appendable instanceof CharBuffer) {
 2747  1
             ((CharBuffer) appendable).put(buffer, 0, size);
 2748  
         } else {
 2749  0
             appendable.append(this);
 2750  
         }
 2751  4
     }
 2752  
 
 2753  
     //-----------------------------------------------------------------------
 2754  
 //    /**
 2755  
 //     * Gets a String version of the string builder by calling the internal
 2756  
 //     * constructor of String by reflection.
 2757  
 //     * <p>
 2758  
 //     * WARNING: You must not use the StrBuilder after calling this method
 2759  
 //     * as the buffer is now shared with the String object. To ensure this,
 2760  
 //     * the internal character array is set to null, so you will get
 2761  
 //     * NullPointerExceptions on all method calls.
 2762  
 //     *
 2763  
 //     * @return the builder as a String
 2764  
 //     */
 2765  
 //    public String toSharedString() {
 2766  
 //        try {
 2767  
 //            Constructor con = String.class.getDeclaredConstructor(
 2768  
 //                new Class[] {int.class, int.class, char[].class});
 2769  
 //            con.setAccessible(true);
 2770  
 //            char[] buffer = buf;
 2771  
 //            buf = null;
 2772  
 //            size = -1;
 2773  
 //            nullText = null;
 2774  
 //            return (String) con.newInstance(
 2775  
 //                new Object[] {Integer.valueOf(0), Integer.valueOf(size), buffer});
 2776  
 //            
 2777  
 //        } catch (Exception ex) {
 2778  
 //            ex.printStackTrace();
 2779  
 //            throw new UnsupportedOperationException("StrBuilder.toSharedString is unsupported: " + ex.getMessage());
 2780  
 //        }
 2781  
 //    }
 2782  
 
 2783  
     //-----------------------------------------------------------------------
 2784  
     /**
 2785  
      * Checks the contents of this builder against another to see if they
 2786  
      * contain the same character content ignoring case.
 2787  
      *
 2788  
      * @param other  the object to check, null returns false
 2789  
      * @return true if the builders contain the same characters in the same order
 2790  
      */
 2791  
     public boolean equalsIgnoreCase(final StrBuilder other) {
 2792  9
         if (this == other) {
 2793  4
             return true;
 2794  
         }
 2795  5
         if (this.size != other.size) {
 2796  1
             return false;
 2797  
         }
 2798  4
         final char thisBuf[] = this.buffer;
 2799  4
         final char otherBuf[] = other.buffer;
 2800  13
         for (int i = size - 1; i >= 0; i--) {
 2801  9
             final char c1 = thisBuf[i];
 2802  9
             final char c2 = otherBuf[i];
 2803  9
             if (c1 != c2 && Character.toUpperCase(c1) != Character.toUpperCase(c2)) {
 2804  0
                 return false;
 2805  
             }
 2806  
         }
 2807  4
         return true;
 2808  
     }
 2809  
 
 2810  
     /**
 2811  
      * Checks the contents of this builder against another to see if they
 2812  
      * contain the same character content.
 2813  
      *
 2814  
      * @param other  the object to check, null returns false
 2815  
      * @return true if the builders contain the same characters in the same order
 2816  
      */
 2817  
     public boolean equals(final StrBuilder other) {
 2818  11
         if (this == other) {
 2819  2
             return true;
 2820  
         }
 2821  9
         if (other == null) {
 2822  1
             return false;
 2823  
         }
 2824  8
         if (this.size != other.size) {
 2825  2
             return false;
 2826  
         }
 2827  6
         final char thisBuf[] = this.buffer;
 2828  6
         final char otherBuf[] = other.buffer;
 2829  12
         for (int i = size - 1; i >= 0; i--) {
 2830  8
             if (thisBuf[i] != otherBuf[i]) {
 2831  2
                 return false;
 2832  
             }
 2833  
         }
 2834  4
         return true;
 2835  
     }
 2836  
 
 2837  
     /**
 2838  
      * Checks the contents of this builder against another to see if they
 2839  
      * contain the same character content.
 2840  
      *
 2841  
      * @param obj  the object to check, null returns false
 2842  
      * @return true if the builders contain the same characters in the same order
 2843  
      */
 2844  
     @Override
 2845  
     public boolean equals(final Object obj) {
 2846  6
         return obj instanceof StrBuilder && equals((StrBuilder) obj);
 2847  
     }
 2848  
 
 2849  
     /**
 2850  
      * Gets a suitable hash code for this builder.
 2851  
      *
 2852  
      * @return a hash code
 2853  
      */
 2854  
     @Override
 2855  
     public int hashCode() {
 2856  4
         final char buf[] = buffer;
 2857  4
         int hash = 0;
 2858  10
         for (int i = size - 1; i >= 0; i--) {
 2859  6
             hash = 31 * hash + buf[i];
 2860  
         }
 2861  4
         return hash;
 2862  
     }
 2863  
 
 2864  
     //-----------------------------------------------------------------------
 2865  
     /**
 2866  
      * Gets a String version of the string builder, creating a new instance
 2867  
      * each time the method is called.
 2868  
      * <p>
 2869  
      * Note that unlike StringBuffer, the string version returned is
 2870  
      * independent of the string builder.
 2871  
      *
 2872  
      * @return the builder as a String
 2873  
      */
 2874  
     @Override
 2875  
     public String toString() {
 2876  1031
         return new String(buffer, 0, size);
 2877  
     }
 2878  
 
 2879  
     /**
 2880  
      * Gets a StringBuffer version of the string builder, creating a
 2881  
      * new instance each time the method is called.
 2882  
      *
 2883  
      * @return the builder as a StringBuffer
 2884  
      */
 2885  
     public StringBuffer toStringBuffer() {
 2886  2
         return new StringBuffer(size).append(buffer, 0, size);
 2887  
     }
 2888  
 
 2889  
     /**
 2890  
      * Gets a StringBuilder version of the string builder, creating a
 2891  
      * new instance each time the method is called.
 2892  
      *
 2893  
      * @return the builder as a StringBuilder
 2894  
      * @since 3.2
 2895  
      */
 2896  
     public StringBuilder toStringBuilder() {
 2897  2
         return new StringBuilder(size).append(buffer, 0, size);
 2898  
     }
 2899  
 
 2900  
     /**
 2901  
      * Implement the {@link Builder} interface.
 2902  
      * @return the builder as a String
 2903  
      * @since 3.2
 2904  
      * @see #toString()
 2905  
      */
 2906  
     @Override
 2907  
     public String build() {
 2908  1
         return toString();
 2909  
     }
 2910  
 
 2911  
     //-----------------------------------------------------------------------
 2912  
     /**
 2913  
      * Validates parameters defining a range of the builder.
 2914  
      * 
 2915  
      * @param startIndex  the start index, inclusive, must be valid
 2916  
      * @param endIndex  the end index, exclusive, must be valid except
 2917  
      *  that if too large it is treated as end of string
 2918  
      * @return the new string
 2919  
      * @throws IndexOutOfBoundsException if the index is invalid
 2920  
      */
 2921  
     protected int validateRange(final int startIndex, int endIndex) {
 2922  984
         if (startIndex < 0) {
 2923  6
             throw new StringIndexOutOfBoundsException(startIndex);
 2924  
         }
 2925  978
         if (endIndex > size) {
 2926  9
             endIndex = size;
 2927  
         }
 2928  978
         if (startIndex > endIndex) {
 2929  9
             throw new StringIndexOutOfBoundsException("end < start");
 2930  
         }
 2931  969
         return endIndex;
 2932  
     }
 2933  
 
 2934  
     /**
 2935  
      * Validates parameters defining a single index in the builder.
 2936  
      * 
 2937  
      * @param index  the index, must be valid
 2938  
      * @throws IndexOutOfBoundsException if the index is invalid
 2939  
      */
 2940  
     protected void validateIndex(final int index) {
 2941  56
         if (index < 0 || index > size) {
 2942  24
             throw new StringIndexOutOfBoundsException(index);
 2943  
         }
 2944  32
     }
 2945  
 
 2946  
     //-----------------------------------------------------------------------
 2947  
     /**
 2948  
      * Inner class to allow StrBuilder to operate as a tokenizer.
 2949  
      */
 2950  
     class StrBuilderTokenizer extends StrTokenizer {
 2951  
 
 2952  
         /**
 2953  
          * Default constructor.
 2954  
          */
 2955  1
         StrBuilderTokenizer() {
 2956  1
             super();
 2957  1
         }
 2958  
 
 2959  
         /** {@inheritDoc} */
 2960  
         @Override
 2961  
         protected List<String> tokenize(final char[] chars, final int offset, final int count) {
 2962  2
             if (chars == null) {
 2963  2
                 return super.tokenize(StrBuilder.this.buffer, 0, StrBuilder.this.size());
 2964  
             }
 2965  0
             return super.tokenize(chars, offset, count);
 2966  
         }
 2967  
 
 2968  
         /** {@inheritDoc} */
 2969  
         @Override
 2970  
         public String getContent() {
 2971  1
             final String str = super.getContent();
 2972  1
             if (str == null) {
 2973  1
                 return StrBuilder.this.toString();
 2974  
             }
 2975  0
             return str;
 2976  
         }
 2977  
     }
 2978  
 
 2979  
     //-----------------------------------------------------------------------
 2980  
     /**
 2981  
      * Inner class to allow StrBuilder to operate as a reader.
 2982  
      */
 2983  
     class StrBuilderReader extends Reader {
 2984  
         /** The current stream position. */
 2985  
         private int pos;
 2986  
         /** The last mark position. */
 2987  
         private int mark;
 2988  
 
 2989  
         /**
 2990  
          * Default constructor.
 2991  
          */
 2992  3
         StrBuilderReader() {
 2993  3
             super();
 2994  3
         }
 2995  
 
 2996  
         /** {@inheritDoc} */
 2997  
         @Override
 2998  
         public void close() {
 2999  
             // do nothing
 3000  1
         }
 3001  
 
 3002  
         /** {@inheritDoc} */
 3003  
         @Override
 3004  
         public int read() {
 3005  3
             if (ready() == false) {
 3006  1
                 return -1;
 3007  
             }
 3008  2
             return StrBuilder.this.charAt(pos++);
 3009  
         }
 3010  
 
 3011  
         /** {@inheritDoc} */
 3012  
         @Override
 3013  
         public int read(final char b[], final int off, int len) {
 3014  11
             if (off < 0 || len < 0 || off > b.length ||
 3015  
                     (off + len) > b.length || (off + len) < 0) {
 3016  5
                 throw new IndexOutOfBoundsException();
 3017  
             }
 3018  6
             if (len == 0) {
 3019  1
                 return 0;
 3020  
             }
 3021  5
             if (pos >= StrBuilder.this.size()) {
 3022  1
                 return -1;
 3023  
             }
 3024  4
             if (pos + len > size()) {
 3025  2
                 len = StrBuilder.this.size() - pos;
 3026  
             }
 3027  4
             StrBuilder.this.getChars(pos, pos + len, b, off);
 3028  4
             pos += len;
 3029  4
             return len;
 3030  
         }
 3031  
 
 3032  
         /** {@inheritDoc} */
 3033  
         @Override
 3034  
         public long skip(long n) {
 3035  4
             if (pos + n > StrBuilder.this.size()) {
 3036  1
                 n = StrBuilder.this.size() - pos;
 3037  
             }
 3038  4
             if (n < 0) {
 3039  1
                 return 0;
 3040  
             }
 3041  3
             pos += n;
 3042  3
             return n;
 3043  
         }
 3044  
 
 3045  
         /** {@inheritDoc} */
 3046  
         @Override
 3047  
         public boolean ready() {
 3048  7
             return pos < StrBuilder.this.size();
 3049  
         }
 3050  
 
 3051  
         /** {@inheritDoc} */
 3052  
         @Override
 3053  
         public boolean markSupported() {
 3054  1
             return true;
 3055  
         }
 3056  
 
 3057  
         /** {@inheritDoc} */
 3058  
         @Override
 3059  
         public void mark(final int readAheadLimit) {
 3060  1
             mark = pos;
 3061  1
         }
 3062  
 
 3063  
         /** {@inheritDoc} */
 3064  
         @Override
 3065  
         public void reset() {
 3066  2
             pos = mark;
 3067  2
         }
 3068  
     }
 3069  
 
 3070  
     //-----------------------------------------------------------------------
 3071  
     /**
 3072  
      * Inner class to allow StrBuilder to operate as a writer.
 3073  
      */
 3074  
     class StrBuilderWriter extends Writer {
 3075  
 
 3076  
         /**
 3077  
          * Default constructor.
 3078  
          */
 3079  1
         StrBuilderWriter() {
 3080  1
             super();
 3081  1
         }
 3082  
 
 3083  
         /** {@inheritDoc} */
 3084  
         @Override
 3085  
         public void close() {
 3086  
             // do nothing
 3087  1
         }
 3088  
 
 3089  
         /** {@inheritDoc} */
 3090  
         @Override
 3091  
         public void flush() {
 3092  
             // do nothing
 3093  1
         }
 3094  
 
 3095  
         /** {@inheritDoc} */
 3096  
         @Override
 3097  
         public void write(final int c) {
 3098  2
             StrBuilder.this.append((char) c);
 3099  2
         }
 3100  
 
 3101  
         /** {@inheritDoc} */
 3102  
         @Override
 3103  
         public void write(final char[] cbuf) {
 3104  1
             StrBuilder.this.append(cbuf);
 3105  1
         }
 3106  
 
 3107  
         /** {@inheritDoc} */
 3108  
         @Override
 3109  
         public void write(final char[] cbuf, final int off, final int len) {
 3110  1
             StrBuilder.this.append(cbuf, off, len);
 3111  1
         }
 3112  
 
 3113  
         /** {@inheritDoc} */
 3114  
         @Override
 3115  
         public void write(final String str) {
 3116  2
             StrBuilder.this.append(str);
 3117  2
         }
 3118  
 
 3119  
         /** {@inheritDoc} */
 3120  
         @Override
 3121  
         public void write(final String str, final int off, final int len) {
 3122  1
             StrBuilder.this.append(str, off, len);
 3123  1
         }
 3124  
     }
 3125  
 
 3126  
 }