Package com.utils

Source Code of com.utils.StringBuilderOpt

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.utils;

import com.GestDB.general.Trackbug;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CoderResult;
import java.util.Arrays;
import sun.misc.FloatingDecimal;

/**
*
* @author seni
*/
public class StringBuilderOpt implements Appendable {
    char value[];
    int count;

    public StringBuilderOpt(int capacity) {
        value = new char[capacity];
    }

    /**
     * Returns the length (character count).
     *
     * @return  the length of the sequence of characters currently
     *          represented by this object
     */
    public int length() {
  return count;
    }

    /**
     * Returns the current capacity. The capacity is the amount of storage
     * available for newly inserted characters, beyond which an allocation
     * will occur.
     *
     * @return  the current capacity
     */
    public int capacity() {
  return value.length;
    }

    /**
     * Ensures that the capacity is at least equal to the specified minimum.
     * If the current capacity is less than the argument, then a new internal
     * array is allocated with greater capacity. The new capacity is the
     * larger of:
     * <ul>
     * <li>The <code>minimumCapacity</code> argument.
     * <li>Twice the old capacity, plus <code>2</code>.
     * </ul>
     * If the <code>minimumCapacity</code> argument is nonpositive, this
     * method takes no action and simply returns.
     *
     * @param   minimumCapacity   the minimum desired capacity.
     */
    public void ensureCapacity(int minimumCapacity) {
  if (minimumCapacity > value.length) {
      expandCapacity(minimumCapacity);
  }
    }

    public void clear()
    {
        count = 0;
    }
   
    public StringBuilderOpt init(String text)
    {
        clear();
        append(text);
        return this;
    }
   
    /**
     * This implements the expansion semantics of ensureCapacity with no
     * size check or synchronization.
     */
    void expandCapacity(int minimumCapacity) {
  int newCapacity = (value.length + 1) * 2;
        if (newCapacity < 0) {
            newCapacity = Integer.MAX_VALUE;
        } else if (minimumCapacity > newCapacity) {
      newCapacity = minimumCapacity;
  }
       
        Trackbug.info("Se amplia la capacidad de " + value.length + " a " + newCapacity);
        value = Arrays.copyOf(value, newCapacity);
    }

    /**
     * Sets the length of the character sequence.
     * The sequence is changed to a new character sequence
     * whose length is specified by the argument. For every nonnegative
     * index <i>k</i> less than <code>newLength</code>, the character at
     * index <i>k</i> in the new character sequence is the same as the
     * character at index <i>k</i> in the old sequence if <i>k</i> is less
     * than the length of the old character sequence; otherwise, it is the
     * null character <code>'&#92;u0000'</code>.
     * 
     * In other words, if the <code>newLength</code> argument is less than
     * the current length, the length is changed to the specified length.
     * <p>
     * If the <code>newLength</code> argument is greater than or equal
     * to the current length, sufficient null characters
     * (<code>'&#92;u0000'</code>) are appended so that
     * length becomes the <code>newLength</code> argument.
     * <p>
     * The <code>newLength</code> argument must be greater than or equal
     * to <code>0</code>.
     *
     * @param      newLength   the new length
     * @throws     IndexOutOfBoundsException  if the
     *               <code>newLength</code> argument is negative.
     */
    public void setLength(int newLength) {
  if (newLength < 0)
      throw new StringIndexOutOfBoundsException(newLength);
  if (newLength > value.length)
      expandCapacity(newLength);

  if (count < newLength) {
      for (; count < newLength; count++)
    value[count] = '\0';
  } else {
            count = newLength;
        }
    }

    /**
     * Returns the <code>char</code> value in this sequence at the specified index.
     * The first <code>char</code> value is at index <code>0</code>, the next at index
     * <code>1</code>, and so on, as in array indexing.
     * <p>
     * The index argument must be greater than or equal to
     * <code>0</code>, and less than the length of this sequence.
     *
     * <p>If the <code>char</code> value specified by the index is a
     * <a href="Character.html#unicode">surrogate</a>, the surrogate
     * value is returned.
     *
     * @param      index   the index of the desired <code>char</code> value.
     * @return     the <code>char</code> value at the specified index.
     * @throws     IndexOutOfBoundsException  if <code>index</code> is
     *             negative or greater than or equal to <code>length()</code>.
     */
    public char charAt(int index) {
  if ((index < 0) || (index >= count))
      throw new StringIndexOutOfBoundsException(index);
  return value[index];
    }

    /**
     * Characters are copied from this sequence into the
     * destination character array <code>dst</code>. The first character to
     * be copied is at index <code>srcBegin</code>; the last character to
     * be copied is at index <code>srcEnd-1</code>. The total number of
     * characters to be copied is <code>srcEnd-srcBegin</code>. The
     * characters are copied into the subarray of <code>dst</code> starting
     * at index <code>dstBegin</code> and ending at index:
     * <p><blockquote><pre>
     * dstbegin + (srcEnd-srcBegin) - 1
     * </pre></blockquote>
     *
     * @param      srcBegin   start copying at this offset.
     * @param      srcEnd     stop copying at this offset.
     * @param      dst        the array to copy the data into.
     * @param      dstBegin   offset into <code>dst</code>.
     * @throws     NullPointerException if <code>dst</code> is
     *             <code>null</code>.
     * @throws     IndexOutOfBoundsException  if any of the following is true:
     *             <ul>
     *             <li><code>srcBegin</code> is negative
     *             <li><code>dstBegin</code> is negative
     *             <li>the <code>srcBegin</code> argument is greater than
     *             the <code>srcEnd</code> argument.
     *             <li><code>srcEnd</code> is greater than
     *             <code>this.length()</code>.
     *             <li><code>dstBegin+srcEnd-srcBegin</code> is greater than
     *             <code>dst.length</code>
     *             </ul>
     */
    public void getChars(int srcBegin, int srcEnd, char dst[],
                                      int dstBegin)
    {
  if (srcBegin < 0)
      throw new StringIndexOutOfBoundsException(srcBegin);
  if ((srcEnd < 0) || (srcEnd > count))
      throw new StringIndexOutOfBoundsException(srcEnd);
        if (srcBegin > srcEnd)
            throw new StringIndexOutOfBoundsException("srcBegin > srcEnd");
  System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
    }

    /**
     * The character at the specified index is set to <code>ch</code>. This
     * sequence is altered to represent a new character sequence that is
     * identical to the old character sequence, except that it contains the
     * character <code>ch</code> at position <code>index</code>.
     * <p>
     * The index argument must be greater than or equal to
     * <code>0</code>, and less than the length of this sequence.
     *
     * @param      index   the index of the character to modify.
     * @param      ch      the new character.
     * @throws     IndexOutOfBoundsException  if <code>index</code> is
     *             negative or greater than or equal to <code>length()</code>.
     */
    public void setCharAt(int index, char ch) {
  if ((index < 0) || (index >= count))
      throw new StringIndexOutOfBoundsException(index);
  value[index] = ch;
    }

    /**
     * Appends the string representation of the <code>Object</code>
     * argument.
     * <p>
     * The argument is converted to a string as if by the method
     * <code>String.valueOf</code>, and the characters of that
     * string are then appended to this sequence.
     *
     * @param   obj   an <code>Object</code>.
     * @return  a reference to this object.
     */
    public StringBuilderOpt append(Object obj) {
        if(obj instanceof String)
            return append((String)obj);
        else if(obj instanceof char[])
            return append((char [])obj);
        if (obj instanceof StringBuffer)
            return append((StringBuffer)obj);
        else
            return append(String.valueOf(obj));
    }

    /**
     * Appends the specified string to this character sequence.
     * <p>
     * The characters of the <code>String</code> argument are appended, in
     * order, increasing the length of this sequence by the length of the
     * argument. If <code>str</code> is <code>null</code>, then the four
     * characters <code>"null"</code> are appended.
     * <p>
     * Let <i>n</i> be the length of this character sequence just prior to
     * execution of the <code>append</code> method. Then the character at
     * index <i>k</i> in the new character sequence is equal to the character
     * at index <i>k</i> in the old character sequence, if <i>k</i> is less
     * than <i>n</i>; otherwise, it is equal to the character at index
     * <i>k-n</i> in the argument <code>str</code>.
     *
     * @param   str   a string.
     * @return  a reference to this object.
     */
    public StringBuilderOpt append(String str) {
  if (str == null) str = "";
        int len = str.length();
  if (len == 0) return this;
  int newCount = count + len;
  if (newCount > value.length)
      expandCapacity(newCount);
  str.getChars(0, len, value, count);
  count = newCount;
  return this;
    }

    // Documentation in subclasses because of synchro difference
    public StringBuilderOpt append(StringBuffer sb) {
  if (sb == null)
            return append("null");
  int len = sb.length();
  int newCount = count + len;
  if (newCount > value.length)
      expandCapacity(newCount);
  sb.getChars(0, len, value, count);
  count = newCount;
  return this;
    }

    // Documentation in subclasses because of synchro difference
    public StringBuilderOpt append(CharSequence s) {
        if (s == null)
            s = "";
        if (s instanceof String)
            return this.append((String)s);
        if (s instanceof StringBuffer)
            return this.append((StringBuffer)s);
        return this.append(s, 0, s.length());
    }

    /**
     * Appends a subsequence of the specified <code>CharSequence</code> to this
     * sequence.
     * <p>
     * Characters of the argument <code>s</code>, starting at
     * index <code>start</code>, are appended, in order, to the contents of
     * this sequence up to the (exclusive) index <code>end</code>. The length
     * of this sequence is increased by the value of <code>end - start</code>.
     * <p>
     * Let <i>n</i> be the length of this character sequence just prior to
     * execution of the <code>append</code> method. Then the character at
     * index <i>k</i> in this character sequence becomes equal to the
     * character at index <i>k</i> in this sequence, if <i>k</i> is less than
     * <i>n</i>; otherwise, it is equal to the character at index
     * <i>k+start-n</i> in the argument <code>s</code>.
     * <p>
     * If <code>s</code> is <code>null</code>, then this method appends
     * characters as if the s parameter was a sequence containing the four
     * characters <code>"null"</code>.
     *
     * @param   s the sequence to append.
     * @param   start   the starting index of the subsequence to be appended.
     * @param   end     the end index of the subsequence to be appended.
     * @return  a reference to this object.
     * @throws     IndexOutOfBoundsException if
     *                  <code>start</code> or <code>end</code> are negative, or
     *             <code>start</code> is greater than <code>end</code> or
     *             <code>end</code> is greater than <code>s.length()</code>
     */
    public StringBuilderOpt append(CharSequence s, int start, int end) {
        if (s == null)
            s = "null";
  if ((start < 0) || (end < 0) || (start > end) || (end > s.length()))
      throw new IndexOutOfBoundsException(
                "start " + start + ", end " + end + ", s.length() "
                + s.length());
  int len = end - start;
  if (len == 0)
            return this;
  int newCount = count + len;
  if (newCount > value.length)
      expandCapacity(newCount);
        for (int i=start; i<end; i++)
            value[count++] = s.charAt(i);
        count = newCount;
  return this;
    }

    /**
     * Appends the string representation of the <code>char</code> array
     * argument to this sequence.
     * <p>
     * The characters of the array argument are appended, in order, to
     * the contents of this sequence. The length of this sequence
     * increases by the length of the argument.
     * <p>
     * The overall effect is exactly as if the argument were converted to
     * a string by the method {@link String#valueOf(char[])} and the
     * characters of that string were then {@link #append(String) appended}
     * to this character sequence.
     *
     * @param   str   the characters to be appended.
     * @return  a reference to this object.
     */
    public StringBuilderOpt append(char str[]) {
  int newCount = count + str.length;
  if (newCount > value.length)
      expandCapacity(newCount);
        System.arraycopy(str, 0, value, count, str.length);
        count = newCount;
        return this;
    }

    /**
     * Appends the string representation of a subarray of the
     * <code>char</code> array argument to this sequence.
     * <p>
     * Characters of the <code>char</code> array <code>str</code>, starting at
     * index <code>offset</code>, are appended, in order, to the contents
     * of this sequence. The length of this sequence increases
     * by the value of <code>len</code>.
     * <p>
     * The overall effect is exactly as if the arguments were converted to
     * a string by the method {@link String#valueOf(char[],int,int)} and the
     * characters of that string were then {@link #append(String) appended}
     * to this character sequence.
     *
     * @param   str      the characters to be appended.
     * @param   offset   the index of the first <code>char</code> to append.
     * @param   len      the number of <code>char</code>s to append.
     * @return  a reference to this object.
     */
    public StringBuilderOpt append(char str[], int offset, int len) {
        int newCount = count + len;
  if (newCount > value.length)
      expandCapacity(newCount);
  System.arraycopy(str, offset, value, count, len);
  count = newCount;
  return this;
    }

    public StringBuilderOpt append(byte str[], int offset, int len) {
        int newCount = count + len;
  if (newCount > value.length)
      expandCapacity(newCount);
        for(int i=0; i < len; i++)
            value[count + i] = (char)str[i];
  count = newCount;
  return this;
    }
   
    public static String bytesToStringUTFCustom(byte[] bytes, int offset, int len)
    {
        char[] buffer = new char[bytes.length >> 1];
        for(int i = 0; i < buffer.length; i++) {
            int bpos = i << 1;
            char c = (char)(((bytes[bpos]&0x00FF)<<8) + (bytes[bpos+1]&0x00FF));
            buffer[i] = c;
        }
        return new String(buffer);
    }
   
   
    /**
     * Appends the string representation of the <code>boolean</code>
     * argument to the sequence.
     * <p>
     * The argument is converted to a string as if by the method
     * <code>String.valueOf</code>, and the characters of that
     * string are then appended to this sequence.
     *
     * @param   b   a <code>boolean</code>.
     * @return  a reference to this object.
     */
    public StringBuilderOpt append(boolean b) {
        if (b) {
            int newCount = count + 4;
            if (newCount > value.length)
                expandCapacity(newCount);
            value[count++] = 't';
            value[count++] = 'r';
            value[count++] = 'u';
            value[count++] = 'e';
        } else {
            int newCount = count + 5;
            if (newCount > value.length)
                expandCapacity(newCount);
            value[count++] = 'f';
            value[count++] = 'a';
            value[count++] = 'l';
            value[count++] = 's';
            value[count++] = 'e';
        }
  return this;
    }

    /**
     * Appends the string representation of the <code>char</code>
     * argument to this sequence.
     * <p>
     * The argument is appended to the contents of this sequence.
     * The length of this sequence increases by <code>1</code>.
     * <p>
     * The overall effect is exactly as if the argument were converted to
     * a string by the method {@link String#valueOf(char)} and the character
     * in that string were then {@link #append(String) appended} to this
     * character sequence.
     *
     * @param   c   a <code>char</code>.
     * @return  a reference to this object.
     */
    public StringBuilderOpt append(char c) {
        int newCount = count + 1;
  if (newCount > value.length)
      expandCapacity(newCount);
  value[count++] = c;
  return this;
    }

    /**
     * Appends the string representation of the <code>int</code>
     * argument to this sequence.
     * <p>
     * The argument is converted to a string as if by the method
     * <code>String.valueOf</code>, and the characters of that
     * string are then appended to this sequence.
     *
     * @param   i   an <code>int</code>.
     * @return  a reference to this object.
     */
    public StringBuilderOpt append(int i) {
        if (i == Integer.MIN_VALUE) {
            append("-2147483648");
            return this;
        }
        int appendedLength = (i < 0) ? stringSizeOfInt(-i) + 1
                                     : stringSizeOfInt(i);
        int spaceNeeded = count + appendedLength;
        if (spaceNeeded > value.length)
            expandCapacity(spaceNeeded);
  getCharsInteger(i, spaceNeeded, value);
        count = spaceNeeded;
        return this;
    }

    final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
                                     99999999, 999999999, Integer.MAX_VALUE };

    // Requires positive x
    static int stringSizeOfInt(int x) {
        for (int i=0; ; i++)
            if (x <= sizeTable[i])
                return i+1;
    }

    /**
     * Appends the string representation of the <code>long</code>
     * argument to this sequence.
     * <p>
     * The argument is converted to a string as if by the method
     * <code>String.valueOf</code>, and the characters of that
     * string are then appended to this sequence.
     *
     * @param   l   a <code>long</code>.
     * @return  a reference to this object.
     */
    public StringBuilderOpt append(long l) {
        if (l == Long.MIN_VALUE) {
            append("-9223372036854775808");
            return this;
        }
        int appendedLength = (l < 0) ? stringSizeOfLong(-l) + 1
                                     : stringSizeOfLong(l);
        int spaceNeeded = count + appendedLength;
        if (spaceNeeded > value.length)
            expandCapacity(spaceNeeded);
  getCharsLong(l, spaceNeeded, value);
        count = spaceNeeded;
        return this;
    }

    final static char[] digits = {
  '0' , '1' , '2' , '3' , '4' , '5' ,
  '6' , '7' , '8' , '9' , 'a' , 'b' ,
  'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
  'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
  'o' , 'p' , 'q' , 'r' , 's' , 't' ,
  'u' , 'v' , 'w' , 'x' , 'y' , 'z'
    };
   
    final static char [] DigitTens = {
  '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
  '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
  '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
  '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
  '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
  '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
  '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
  '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
  '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
  '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
  } ;

    final static char [] DigitOnes = {
  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  } ;

    /**
     * Places characters representing the integer i into the
     * character array buf. The characters are placed into
     * the buffer backwards starting with the least significant
     * digit at the specified index (exclusive), and working
     * backwards from there.
     *
     * Will fail if i == Long.MIN_VALUE
     */
    static void getCharsLong(long i, int index, char[] buf) {
        long q;
        int r;
        int charPos = index;
        char sign = 0;

        if (i < 0) {
            sign = '-';
            i = -i;
        }

        // Get 2 digits/iteration using longs until quotient fits into an int
        while (i > Integer.MAX_VALUE) {
            q = i / 100;
            // really: r = i - (q * 100);
            r = (int)(i - ((q << 6) + (q << 5) + (q << 2)));
            i = q;
            buf[--charPos] = DigitOnes[r];
            buf[--charPos] = DigitTens[r];
        }

        // Get 2 digits/iteration using ints
        int q2;
        int i2 = (int)i;
        while (i2 >= 65536) {
            q2 = i2 / 100;
            // really: r = i2 - (q * 100);
            r = i2 - ((q2 << 6) + (q2 << 5) + (q2 << 2));
            i2 = q2;
            buf[--charPos] = DigitOnes[r];
            buf[--charPos] = DigitTens[r];
        }

        // Fall thru to fast mode for smaller numbers
        // assert(i2 <= 65536, i2);
        for (;;) {
            q2 = (i2 * 52429) >>> (16+3);
            r = i2 - ((q2 << 3) + (q2 << 1))// r = i2-(q2*10) ...
            buf[--charPos] = digits[r];
            i2 = q2;
            if (i2 == 0) break;
        }
        if (sign != 0) {
            buf[--charPos] = sign;
        }
    }
   
    /**
     * Places characters representing the integer i into the
     * character array buf. The characters are placed into
     * the buffer backwards starting with the least significant
     * digit at the specified index (exclusive), and working
     * backwards from there.
     *
     * Will fail if i == Integer.MIN_VALUE
     */
    static void getCharsInteger(int i, int index, char[] buf) {
        int q, r;
        int charPos = index;
        char sign = 0;

        if (i < 0) {
            sign = '-';
            i = -i;
        }

        // Generate two digits per iteration
        while (i >= 65536) {
            q = i / 100;
        // really: r = i - (q * 100);
            r = i - ((q << 6) + (q << 5) + (q << 2));
            i = q;
            buf [--charPos] = DigitOnes[r];
            buf [--charPos] = DigitTens[r];
        }

        // Fall thru to fast mode for smaller numbers
        // assert(i <= 65536, i);
        for (;;) {
            q = (i * 52429) >>> (16+3);
            r = i - ((q << 3) + (q << 1))// r = i-(q*10) ...
            buf [--charPos] = digits [r];
            i = q;
            if (i == 0) break;
        }
        if (sign != 0) {
            buf [--charPos] = sign;
        }
    }

   
    // Requires positive x
    static int stringSizeOfLong(long x) {
        long p = 10;
        for (int i=1; i<19; i++) {
            if (x < p)
                return i;
            p = 10*p;
        }
        return 19;
    }

    /**
     * Appends the string representation of the <code>float</code>
     * argument to this sequence.
     * <p>
     * The argument is converted to a string as if by the method
     * <code>String.valueOf</code>, and the characters of that
     * string are then appended to this string sequence.
     *
     * @param   f   a <code>float</code>.
     * @return  a reference to this object.
     */
    public StringBuilderOpt append(float f) {
  new FloatingDecimal(f).appendTo(this);
   return this;
    }

    /**
     * Appends the string representation of the <code>double</code>
     * argument to this sequence.
     * <p>
     * The argument is converted to a string as if by the method
     * <code>String.valueOf</code>, and the characters of that
     * string are then appended to this sequence.
     *
     * @param   d   a <code>double</code>.
     * @return  a reference to this object.
     */
    public StringBuilderOpt append(double d) {
  new FloatingDecimal(d).appendTo(this);
   return this;
    }

    /**
     * Removes the characters in a substring of this sequence.
     * The substring begins at the specified <code>start</code> and extends to
     * the character at index <code>end - 1</code> or to the end of the
     * sequence if no such character exists. If
     * <code>start</code> is equal to <code>end</code>, no changes are made.
     *
     * @param      start  The beginning index, inclusive.
     * @param      end    The ending index, exclusive.
     * @return     This object.
     * @throws     StringIndexOutOfBoundsException  if <code>start</code>
     *             is negative, greater than <code>length()</code>, or
     *       greater than <code>end</code>.
     */
    public StringBuilderOpt delete(int start, int end) {
  if (start < 0)
      throw new StringIndexOutOfBoundsException(start);
  if (end > count)
      end = count;
  if (start > end)
      throw new StringIndexOutOfBoundsException();
        int len = end - start;
        if (len > 0) {
            System.arraycopy(value, start+len, value, start, count-end);
            count -= len;
        }
        return this;
    }

    /**
     * Removes the <code>char</code> at the specified position in this
     * sequence. This sequence is shortened by one <code>char</code>.
     *
     * <p>Note: If the character at the given index is a supplementary
     * character, this method does not remove the entire character. If
     * correct handling of supplementary characters is required,
     * determine the number of <code>char</code>s to remove by calling
     * <code>Character.charCount(thisSequence.codePointAt(index))</code>,
     * where <code>thisSequence</code> is this sequence.
     *
     * @param       index  Index of <code>char</code> to remove
     * @return      This object.
     * @throws      StringIndexOutOfBoundsException  if the <code>index</code>
     *        is negative or greater than or equal to
     *        <code>length()</code>.
     */
    public StringBuilderOpt deleteCharAt(int index) {
        if ((index < 0) || (index >= count))
      throw new StringIndexOutOfBoundsException(index);
  System.arraycopy(value, index+1, value, index, count-index-1);
  count--;
        return this;
    }

    /**
     * Replaces the characters in a substring of this sequence
     * with characters in the specified <code>String</code>. The substring
     * begins at the specified <code>start</code> and extends to the character
     * at index <code>end - 1</code> or to the end of the
     * sequence if no such character exists. First the
     * characters in the substring are removed and then the specified
     * <code>String</code> is inserted at <code>start</code>. (This
     * sequence will be lengthened to accommodate the
     * specified String if necessary.)
     *
     * @param      start    The beginning index, inclusive.
     * @param      end      The ending index, exclusive.
     * @param      str   String that will replace previous contents.
     * @return     This object.
     * @throws     StringIndexOutOfBoundsException  if <code>start</code>
     *             is negative, greater than <code>length()</code>, or
     *       greater than <code>end</code>.
     */
    public StringBuilderOpt replace(int start, int end, String str) {
        if (start < 0)
      throw new StringIndexOutOfBoundsException(start);
  if (start > count)
      throw new StringIndexOutOfBoundsException("start > length()");
  if (start > end)
      throw new StringIndexOutOfBoundsException("start > end");

  if (end > count)
      end = count;
  int len = str.length();
  int newCount = count + len - (end - start);
  if (newCount > value.length)
      expandCapacity(newCount);

        System.arraycopy(value, end, value, start + len, count - end);
        str.getChars(0,len, value, start);
        count = newCount;
        return this;
    }

    /**
     * Returns a new <code>String</code> that contains a subsequence of
     * characters currently contained in this character sequence. The
     * substring begins at the specified index and extends to the end of
     * this sequence.
     *
     * @param      start    The beginning index, inclusive.
     * @return     The new string.
     * @throws     StringIndexOutOfBoundsException  if <code>start</code> is
     *             less than zero, or greater than the length of this object.
     */
    public String substring(int start) {
        return substring(start, count);
    }

    /**
     * Returns a new character sequence that is a subsequence of this sequence.
     *
     * <p> An invocation of this method of the form
     *
     * <blockquote><pre>
     * sb.subSequence(begin,&nbsp;end)</pre></blockquote>
     *
     * behaves in exactly the same way as the invocation
     *
     * <blockquote><pre>
     * sb.substring(begin,&nbsp;end)</pre></blockquote>
     *
     * This method is provided so that this class can
     * implement the {@link CharSequence} interface. </p>
     *
     * @param      start   the start index, inclusive.
     * @param      end     the end index, exclusive.
     * @return     the specified subsequence.
     *
     * @throws  IndexOutOfBoundsException
     *          if <tt>start</tt> or <tt>end</tt> are negative,
     *          if <tt>end</tt> is greater than <tt>length()</tt>,
     *          or if <tt>start</tt> is greater than <tt>end</tt>
     * @spec JSR-51
     */
    public CharSequence subSequence(int start, int end) {
        return substring(start, end);
    }

    /**
     * Returns a new <code>String</code> that contains a subsequence of
     * characters currently contained in this sequence. The
     * substring begins at the specified <code>start</code> and
     * extends to the character at index <code>end - 1</code>.
     *
     * @param      start    The beginning index, inclusive.
     * @param      end      The ending index, exclusive.
     * @return     The new string.
     * @throws     StringIndexOutOfBoundsException  if <code>start</code>
     *             or <code>end</code> are negative or greater than
     *       <code>length()</code>, or <code>start</code> is
     *       greater than <code>end</code>.
     */
    public String substring(int start, int end) {
  if (start < 0)
      throw new StringIndexOutOfBoundsException(start);
  if (end > count)
      throw new StringIndexOutOfBoundsException(end);
  if (start > end)
      throw new StringIndexOutOfBoundsException(end - start);
        return new String(value, start, end - start);
    }
   
    /**
     * M�todo m�s r�pido de conversi�n permitiendo unicode, ignorando el juego de caracteres.
     * @return
     */
    public byte [] getBytes()
    {
        byte[] b = new byte[count << 1];
        for(int i = 0; i < count; i++)
        {
            int bpos = i << 1;
            b[bpos] = (byte) ((value[i]&0xFF00)>>8);
            b[bpos + 1] = (byte) (value[i]&0x00FF);
        }
        return b;
    }

    /**
     * M�todo m�s seguro pero tambien m�s lento de conversi�n de char a bytes
     * @return
     */
    public byte [] getBytes2()
    {
         byte[] b = new byte[count << 1];
         CharBuffer cBuffer = ByteBuffer.wrap(b).asCharBuffer();
         for(int i = 0; i < count; i++)
          cBuffer.put(value[i]);
         return b;
    }
   
    /**
     * Conversi�n dando por echo que todos los caracteres son ASCII. Este es el m�s r�pido, pero s�lo vale para ingles y alg�n idioma m�s.
     * @return array de bytes
     */
    public byte [] getBytes3()
    {
         byte[] b = new byte[count];
         for(int i = 0; i < count; i++)
            b[i] = (byte)value[i];
         return b;
    }
   
   
    /**
     * Inserts the string representation of a subarray of the <code>str</code>
     * array argument into this sequence. The subarray begins at the
     * specified <code>offset</code> and extends <code>len</code> <code>char</code>s.
     * The characters of the subarray are inserted into this sequence at
     * the position indicated by <code>index</code>. The length of this
     * sequence increases by <code>len</code> <code>char</code>s.
     *
     * @param      index    position at which to insert subarray.
     * @param      str       A <code>char</code> array.
     * @param      offset   the index of the first <code>char</code> in subarray to
     *             be inserted.
     * @param      len      the number of <code>char</code>s in the subarray to
     *             be inserted.
     * @return     This object
     * @throws     StringIndexOutOfBoundsException  if <code>index</code>
     *             is negative or greater than <code>length()</code>, or
     *             <code>offset</code> or <code>len</code> are negative, or
     *             <code>(offset+len)</code> is greater than
     *             <code>str.length</code>.
     */
    public StringBuilderOpt insert(int index, char str[], int offset,
                                        int len)
    {
        if ((index < 0) || (index > length()))
      throw new StringIndexOutOfBoundsException(index);
        if ((offset < 0) || (len < 0) || (offset > str.length - len))
            throw new StringIndexOutOfBoundsException(
                "offset " + offset + ", len " + len + ", str.length "
                + str.length);
  int newCount = count + len;
  if (newCount > value.length)
      expandCapacity(newCount);
  System.arraycopy(value, index, value, index + len, count - index);
  System.arraycopy(str, offset, value, index, len);
  count = newCount;
  return this;
    }

    /**
     * Inserts the string representation of the <code>Object</code>
     * argument into this character sequence.
     * <p>
     * The second argument is converted to a string as if by the method
     * <code>String.valueOf</code>, and the characters of that
     * string are then inserted into this sequence at the indicated
     * offset.
     * <p>
     * The offset argument must be greater than or equal to
     * <code>0</code>, and less than or equal to the length of this
     * sequence.
     *
     * @param      offset   the offset.
     * @param      obj      an <code>Object</code>.
     * @return     a reference to this object.
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
     */
    public StringBuilderOpt insert(int offset, Object obj) {
  return insert(offset, String.valueOf(obj));
    }

    /**
     * Inserts the string into this character sequence.
     * <p>
     * The characters of the <code>String</code> argument are inserted, in
     * order, into this sequence at the indicated offset, moving up any
     * characters originally above that position and increasing the length
     * of this sequence by the length of the argument. If
     * <code>str</code> is <code>null</code>, then the four characters
     * <code>"null"</code> are inserted into this sequence.
     * <p>
     * The character at index <i>k</i> in the new character sequence is
     * equal to:
     * <ul>
     * <li>the character at index <i>k</i> in the old character sequence, if
     * <i>k</i> is less than <code>offset</code>
     * <li>the character at index <i>k</i><code>-offset</code> in the
     * argument <code>str</code>, if <i>k</i> is not less than
     * <code>offset</code> but is less than <code>offset+str.length()</code>
     * <li>the character at index <i>k</i><code>-str.length()</code> in the
     * old character sequence, if <i>k</i> is not less than
     * <code>offset+str.length()</code>
     * </ul><p>
     * The offset argument must be greater than or equal to
     * <code>0</code>, and less than or equal to the length of this
     * sequence.
     *
     * @param      offset   the offset.
     * @param      str      a string.
     * @return     a reference to this object.
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
     */
    public StringBuilderOpt insert(int offset, String str) {
  if ((offset < 0) || (offset > length()))
      throw new StringIndexOutOfBoundsException(offset);
  if (str == null)
      str = "null";
  int len = str.length();
  int newCount = count + len;
  if (newCount > value.length)
      expandCapacity(newCount);
  System.arraycopy(value, offset, value, offset + len, count - offset);
  str.getChars(0, len, value, offset);
  count = newCount;
  return this;
    }

    /**
     * Inserts the string representation of the <code>char</code> array
     * argument into this sequence.
     * <p>
     * The characters of the array argument are inserted into the
     * contents of this sequence at the position indicated by
     * <code>offset</code>. The length of this sequence increases by
     * the length of the argument.
     * <p>
     * The overall effect is exactly as if the argument were converted to
     * a string by the method {@link String#valueOf(char[])} and the
     * characters of that string were then
     * {@link #insert(int,String) inserted} into this
     * character sequence at the position indicated by
     * <code>offset</code>.
     *
     * @param      offset   the offset.
     * @param      str      a character array.
     * @return     a reference to this object.
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
     */
    public StringBuilderOpt insert(int offset, char str[]) {
  if ((offset < 0) || (offset > length()))
      throw new StringIndexOutOfBoundsException(offset);
  int len = str.length;
  int newCount = count + len;
  if (newCount > value.length)
      expandCapacity(newCount);
  System.arraycopy(value, offset, value, offset + len, count - offset);
  System.arraycopy(str, 0, value, offset, len);
  count = newCount;
  return this;
    }

    /**
     * Inserts the specified <code>CharSequence</code> into this sequence.
     * <p>
     * The characters of the <code>CharSequence</code> argument are inserted,
     * in order, into this sequence at the indicated offset, moving up
     * any characters originally above that position and increasing the length
     * of this sequence by the length of the argument s.
     * <p>
     * The result of this method is exactly the same as if it were an
     * invocation of this object's insert(dstOffset, s, 0, s.length()) method.
     *
     * <p>If <code>s</code> is <code>null</code>, then the four characters
     * <code>"null"</code> are inserted into this sequence.
     *
     * @param      dstOffset   the offset.
     * @param      s the sequence to be inserted
     * @return     a reference to this object.
     * @throws     IndexOutOfBoundsException  if the offset is invalid.
     */
    public StringBuilderOpt insert(int dstOffset, CharSequence s) {
        if (s == null)
            s = "null";
        if (s instanceof String)
            return this.insert(dstOffset, (String)s);
        return this.insert(dstOffset, s, 0, s.length());
    }

    /**
     * Inserts a subsequence of the specified <code>CharSequence</code> into
     * this sequence.
     * <p>
     * The subsequence of the argument <code>s</code> specified by
     * <code>start</code> and <code>end</code> are inserted,
     * in order, into this sequence at the specified destination offset, moving
     * up any characters originally above that position. The length of this
     * sequence is increased by <code>end - start</code>.
     * <p>
     * The character at index <i>k</i> in this sequence becomes equal to:
     * <ul>
     * <li>the character at index <i>k</i> in this sequence, if
     * <i>k</i> is less than <code>dstOffset</code>
     * <li>the character at index <i>k</i><code>+start-dstOffset</code> in
     * the argument <code>s</code>, if <i>k</i> is greater than or equal to
     * <code>dstOffset</code> but is less than <code>dstOffset+end-start</code>
     * <li>the character at index <i>k</i><code>-(end-start)</code> in this
     * sequence, if <i>k</i> is greater than or equal to
     * <code>dstOffset+end-start</code>
     * </ul><p>
     * The dstOffset argument must be greater than or equal to
     * <code>0</code>, and less than or equal to the length of this
     * sequence.
     * <p>The start argument must be nonnegative, and not greater than
     * <code>end</code>.
     * <p>The end argument must be greater than or equal to
     * <code>start</code>, and less than or equal to the length of s.
     *
     * <p>If <code>s</code> is <code>null</code>, then this method inserts
     * characters as if the s parameter was a sequence containing the four
     * characters <code>"null"</code>.
     *
     * @param      dstOffset   the offset in this sequence.
     * @param      s       the sequence to be inserted.
     * @param      start   the starting index of the subsequence to be inserted.
     * @param      end     the end index of the subsequence to be inserted.
     * @return     a reference to this object.
     * @throws     IndexOutOfBoundsException  if <code>dstOffset</code>
     *             is negative or greater than <code>this.length()</code>, or
     *              <code>start</code> or <code>end</code> are negative, or
     *              <code>start</code> is greater than <code>end</code> or
     *              <code>end</code> is greater than <code>s.length()</code>
     */
     public StringBuilderOpt insert(int dstOffset, CharSequence s,
                                           int start, int end) {
        if (s == null)
            s = "null";
  if ((dstOffset < 0) || (dstOffset > this.length()))
      throw new IndexOutOfBoundsException("dstOffset "+dstOffset);
  if ((start < 0) || (end < 0) || (start > end) || (end > s.length()))
            throw new IndexOutOfBoundsException(
                "start " + start + ", end " + end + ", s.length() "
                + s.length());
  int len = end - start;
        if (len == 0)
            return this;
  int newCount = count + len;
  if (newCount > value.length)
      expandCapacity(newCount);
  System.arraycopy(value, dstOffset, value, dstOffset + len,
                         count - dstOffset);
  for (int i=start; i<end; i++)
            value[dstOffset++] = s.charAt(i);
  count = newCount;
        return this;
    }

    /**
     * Inserts the string representation of the <code>boolean</code>
     * argument into this sequence.
     * <p>
     * The second argument is converted to a string as if by the method
     * <code>String.valueOf</code>, and the characters of that
     * string are then inserted into this sequence at the indicated
     * offset.
     * <p>
     * The offset argument must be greater than or equal to
     * <code>0</code>, and less than or equal to the length of this
     * sequence.
     *
     * @param      offset   the offset.
     * @param      b        a <code>boolean</code>.
     * @return     a reference to this object.
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
     */
    public StringBuilderOpt insert(int offset, boolean b) {
  return insert(offset, String.valueOf(b));
    }

    /**
     * Inserts the string representation of the <code>char</code>
     * argument into this sequence.
     * <p>
     * The second argument is inserted into the contents of this sequence
     * at the position indicated by <code>offset</code>. The length
     * of this sequence increases by one.
     * <p>
     * The overall effect is exactly as if the argument were converted to
     * a string by the method {@link String#valueOf(char)} and the character
     * in that string were then {@link #insert(int, String) inserted} into
     * this character sequence at the position indicated by
     * <code>offset</code>.
     * <p>
     * The offset argument must be greater than or equal to
     * <code>0</code>, and less than or equal to the length of this
     * sequence.
     *
     * @param      offset   the offset.
     * @param      c        a <code>char</code>.
     * @return     a reference to this object.
     * @throws     IndexOutOfBoundsException  if the offset is invalid.
     */
    public StringBuilderOpt insert(int offset, char c) {
  int newCount = count + 1;
  if (newCount > value.length)
      expandCapacity(newCount);
  System.arraycopy(value, offset, value, offset + 1, count - offset);
  value[offset] = c;
  count = newCount;
  return this;
    }

    /**
     * Inserts the string representation of the second <code>int</code>
     * argument into this sequence.
     * <p>
     * The second argument is converted to a string as if by the method
     * <code>String.valueOf</code>, and the characters of that
     * string are then inserted into this sequence at the indicated
     * offset.
     * <p>
     * The offset argument must be greater than or equal to
     * <code>0</code>, and less than or equal to the length of this
     * sequence.
     *
     * @param      offset   the offset.
     * @param      i        an <code>int</code>.
     * @return     a reference to this object.
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
     */
    public StringBuilderOpt insert(int offset, int i) {
  return insert(offset, String.valueOf(i));
    }

    /**
     * Inserts the string representation of the <code>long</code>
     * argument into this sequence.
     * <p>
     * The second argument is converted to a string as if by the method
     * <code>String.valueOf</code>, and the characters of that
     * string are then inserted into this sequence at the position
     * indicated by <code>offset</code>.
     * <p>
     * The offset argument must be greater than or equal to
     * <code>0</code>, and less than or equal to the length of this
     * sequence.
     *
     * @param      offset   the offset.
     * @param      l        a <code>long</code>.
     * @return     a reference to this object.
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
     */
    public StringBuilderOpt insert(int offset, long l) {
  return insert(offset, String.valueOf(l));
    }

    /**
     * Inserts the string representation of the <code>float</code>
     * argument into this sequence.
     * <p>
     * The second argument is converted to a string as if by the method
     * <code>String.valueOf</code>, and the characters of that
     * string are then inserted into this sequence at the indicated
     * offset.
     * <p>
     * The offset argument must be greater than or equal to
     * <code>0</code>, and less than or equal to the length of this
     * sequence.
     *
     * @param      offset   the offset.
     * @param      f        a <code>float</code>.
     * @return     a reference to this object.
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
     */
    public StringBuilderOpt insert(int offset, float f) {
  return insert(offset, String.valueOf(f));
    }

    /**
     * Inserts the string representation of the <code>double</code>
     * argument into this sequence.
     * <p>
     * The second argument is converted to a string as if by the method
     * <code>String.valueOf</code>, and the characters of that
     * string are then inserted into this sequence at the indicated
     * offset.
     * <p>
     * The offset argument must be greater than or equal to
     * <code>0</code>, and less than or equal to the length of this
     * sequence.
     *
     * @param      offset   the offset.
     * @param      d        a <code>double</code>.
     * @return     a reference to this object.
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
     */
    public StringBuilderOpt insert(int offset, double d) {
  return insert(offset, String.valueOf(d));
    }

    /**
     * Returns the index within this string of the first occurrence of the
     * specified substring. The integer returned is the smallest value
     * <i>k</i> such that:
     * <blockquote><pre>
     * this.toString().startsWith(str, <i>k</i>)
     * </pre></blockquote>
     * is <code>true</code>.
     *
     * @param   str   any string.
     * @return  if the string argument occurs as a substring within this
     *          object, then the index of the first character of the first
     *          such substring is returned; if it does not occur as a
     *          substring, <code>-1</code> is returned.
     * @throws  java.lang.NullPointerException if <code>str</code> is
     *          <code>null</code>.
     */
    public int indexOf(String str) {
  return indexOf(str, 0);
    }

    /**
     * Returns the index within this string of the first occurrence of the
     * specified substring, starting at the specified index.  The integer
     * returned is the smallest value <tt>k</tt> for which:
     * <blockquote><pre>
     *     k >= Math.min(fromIndex, str.length()) &&
     *                   this.toString().startsWith(str, k)
     * </pre></blockquote>
     * If no such value of <i>k</i> exists, then -1 is returned.
     *
     * @param   str         the substring for which to search.
     * @param   fromIndex   the index from which to start the search.
     * @return  the index within this string of the first occurrence of the
     *          specified substring, starting at the specified index.
     * @throws  java.lang.NullPointerException if <code>str</code> is
     *            <code>null</code>.
     */
    public int indexOf(String str, int fromIndex)
    {
        int targetCount = str.length();
  if (fromIndex >= count) {
            return (targetCount == 0 ? count : -1);
  }
      if (fromIndex < 0) {
          fromIndex = 0;
      }
  if (targetCount == 0) {
      return fromIndex;
  }

        char first  = str.charAt(0);
        int max = count - targetCount;

        for (int i = fromIndex; i <= max; i++) {
            /* Look for first character. */
            if (value[i] != first) {
                while (++i <= max && value[i] != first);
            }

            /* Found first character, now look at the rest of v2 */
            if (i <= max) {
                int j = i + 1;
                int end = j + targetCount - 1;
                for (int k = 0 + 1; j < end && value[j] ==
                         str.charAt(k); j++, k++);

                if (j == end) {
                    /* Found whole string. */
                    return i;
                }
            }
        }
        return -1;
    }

    /**
     * Code shared by String and StringBuffer to do searches. The
     * source is the character array being searched, and the target
     * is the string being searched for.
     *
     * @param   source       the characters being searched.
     * @param   sourceOffset offset of the source string.
     * @param   sourceCount  count of the source string.
     * @param   target       the characters being searched for.
     * @param   targetOffset offset of the target string.
     * @param   targetCount  count of the target string.
     * @param   fromIndex    the index to begin searching from.
     */
    static int indexOf(char[] source, int sourceOffset, int sourceCount,
                       char[] target, int targetOffset, int targetCount,
                       int fromIndex) {
  if (fromIndex >= sourceCount) {
            return (targetCount == 0 ? sourceCount : -1);
  }
      if (fromIndex < 0) {
          fromIndex = 0;
      }
  if (targetCount == 0) {
      return fromIndex;
  }

        char first  = target[targetOffset];
        int max = sourceOffset + (sourceCount - targetCount);

        for (int i = sourceOffset + fromIndex; i <= max; i++) {
            /* Look for first character. */
            if (source[i] != first) {
                while (++i <= max && source[i] != first);
            }

            /* Found first character, now look at the rest of v2 */
            if (i <= max) {
                int j = i + 1;
                int end = j + targetCount - 1;
                for (int k = targetOffset + 1; j < end && source[j] ==
                         target[k]; j++, k++);

                if (j == end) {
                    /* Found whole string. */
                    return i - sourceOffset;
                }
            }
        }
        return -1;
    }

    /**
     * Returns the index within this string of the first occurrence of the
     * specified substring, starting at the specified index.  The integer
     * returned is the smallest value <tt>k</tt> for which:
     * <blockquote><pre>
     *     k >= Math.min(fromIndex, str.length()) &&
     *                   this.toString().startsWith(str, k)
     * </pre></blockquote>
     * If no such value of <i>k</i> exists, then -1 is returned.
     *
     * @param   str         the substring for which to search.
     * @param   fromIndex   the index from which to start the search.
     * @return  the index within this string of the first occurrence of the
     *          specified substring, starting at the specified index.
     * @throws  java.lang.NullPointerException if <code>str</code> is
     *            <code>null</code>.
     */
    public int indexOf(char str, int fromIndex)
    {
        return indexOf(value, 0, count, str, fromIndex);
    }

    public int indexOf(char str)
    {
        return indexOf(value, 0, count, str, 0);
    }
   
    /**
     * Code shared by String and StringBuffer to do searches. The
     * source is the character array being searched, and the target
     * is the string being searched for.
     *
     * @param   source       the characters being searched.
     * @param   sourceOffset offset of the source string.
     * @param   sourceCount  count of the source string.
     * @param   target       the characters being searched for.
     * @param   targetOffset offset of the target string.
     * @param   targetCount  count of the target string.
     * @param   fromIndex    the index to begin searching from.
     */
    static int indexOf(char[] source, int sourceOffset, int sourceCount,
                       char target, int fromIndex)
    {
  if (fromIndex >= sourceCount) {
            return -1;
  }
      if (fromIndex < 0) {
          fromIndex = 0;
      }

        int max = sourceOffset + (sourceCount - 1);

        int i = sourceOffset + fromIndex;
        while (source[i] != target && ++i <= max);
       
        if(source[i] == target && i <= max)
        {
            return i - sourceOffset;
        }
       
        return -1;
    }
   
    /**
     * Returns the index within this string of the rightmost occurrence
     * of the specified substring.  The rightmost empty string "" is
     * considered to occur at the index value <code>this.length()</code>.
     * The returned index is the largest value <i>k</i> such that
     * <blockquote><pre>
     * this.toString().startsWith(str, k)
     * </pre></blockquote>
     * is true.
     *
     * @param   str   the substring to search for.
     * @return  if the string argument occurs one or more times as a substring
     *          within this object, then the index of the first character of
     *          the last such substring is returned. If it does not occur as
     *          a substring, <code>-1</code> is returned.
     * @throws  java.lang.NullPointerException  if <code>str</code> is
     *          <code>null</code>.
     */
    public int lastIndexOf(String str) {
        return lastIndexOf(str, count);
    }

    /**
     * Returns the index within this string of the last occurrence of the
     * specified substring. The integer returned is the largest value <i>k</i>
     * such that:
     * <blockquote><pre>
     *     k <= Math.min(fromIndex, str.length()) &&
     *                   this.toString().startsWith(str, k)
     * </pre></blockquote>
     * If no such value of <i>k</i> exists, then -1 is returned.
     *
     * @param   str         the substring to search for.
     * @param   fromIndex   the index to start the search from.
     * @return  the index within this sequence of the last occurrence of the
     *          specified substring.
     * @throws  java.lang.NullPointerException if <code>str</code> is
     *          <code>null</code>.
     */
    public int lastIndexOf(String str, int fromIndex) {
        return lastIndexOf(value, 0, count, str.toCharArray(), 0, str.length(), fromIndex);
    }

    /**
     * Code shared by String and StringBuffer to do searches. The
     * source is the character array being searched, and the target
     * is the string being searched for.
     *
     * @param   source       the characters being searched.
     * @param   sourceOffset offset of the source string.
     * @param   sourceCount  count of the source string.
     * @param   target       the characters being searched for.
     * @param   targetOffset offset of the target string.
     * @param   targetCount  count of the target string.
     * @param   fromIndex    the index to begin searching from.
     */
    static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
            char[] target, int targetOffset, int targetCount,
            int fromIndex) {
        /*
         * Check arguments; return immediately where possible. For
         * consistency, don't check for null str.
         */
        int rightIndex = sourceCount - targetCount;
        if (fromIndex < 0) {
            return -1;
        }
        if (fromIndex > rightIndex) {
            fromIndex = rightIndex;
        }
        /* Empty string always matches. */
        if (targetCount == 0) {
            return fromIndex;
        }

        int strLastIndex = targetOffset + targetCount - 1;
        char strLastChar = target[strLastIndex];
        int min = sourceOffset + targetCount - 1;
        int i = min + fromIndex;

        startSearchForLastChar:
        while (true) {
            while (i >= min && source[i] != strLastChar) {
                i--;
            }
            if (i < min) {
                return -1;
            }
            int j = i - 1;
            int start = j - (targetCount - 1);
            int k = strLastIndex - 1;

            while (j > start) {
                if (source[j--] != target[k--]) {
                    i--;
                    continue startSearchForLastChar;
                }
            }
            return start - sourceOffset + 1;
        }
    }
   
    /**
     * Causes this character sequence to be replaced by the reverse of
     * the sequence. If there are any surrogate pairs included in the
     * sequence, these are treated as single characters for the
     * reverse operation. Thus, the order of the high-low surrogates
     * is never reversed.
     *
     * Let <i>n</i> be the character length of this character sequence
     * (not the length in <code>char</code> values) just prior to
     * execution of the <code>reverse</code> method. Then the
     * character at index <i>k</i> in the new character sequence is
     * equal to the character at index <i>n-k-1</i> in the old
     * character sequence.
     *
     * <p>Note that the reverse operation may result in producing
     * surrogate pairs that were unpaired low-surrogates and
     * high-surrogates before the operation. For example, reversing
     * "&#92;uDC00&#92;uD800" produces "&#92;uD800&#92;uDC00" which is
     * a valid surrogate pair.
     *
     * @return  a reference to this object.
     */
    public StringBuilderOpt reverse() {
  boolean hasSurrogate = false;
  int n = count - 1;
  for (int j = (n-1) >> 1; j >= 0; --j) {
      char temp = value[j];
      char temp2 = value[n - j];
      if (!hasSurrogate) {
    hasSurrogate = (temp >= Character.MIN_SURROGATE && temp <= Character.MAX_SURROGATE)
        || (temp2 >= Character.MIN_SURROGATE && temp2 <= Character.MAX_SURROGATE);
      }
      value[j] = temp2;
      value[n - j] = temp;
  }
  if (hasSurrogate) {
      // Reverse back all valid surrogate pairs
      for (int i = 0; i < count - 1; i++) {
    char c2 = value[i];
    if (Character.isLowSurrogate(c2)) {
        char c1 = value[i + 1];
        if (Character.isHighSurrogate(c1)) {
      value[i++] = c1;
      value[i] = c2;
        }
    }
      }
  }
  return this;
    }
   
    public StringBuilderOpt rellena(int valor,int nDigitos)
    {
        int nDigitosActuales = 0;
        if(valor < 10)
            nDigitosActuales = 1;
        else if(valor < 100)
            nDigitosActuales = 2;
        else if(valor < 1000)
            nDigitosActuales = 3;
        else if(valor < 10000)
            nDigitosActuales = 4;
        else if(valor < 100000)
            nDigitosActuales = 5;
        else if(valor < 1000000)
            nDigitosActuales = 6;
        else if(valor < 10000000)
            nDigitosActuales = 7;
       
        for(int i=nDigitosActuales; i < nDigitos; i++)
        {
            append("0");
        }
       
        append(valor);
       
        return this;
    }
   

    /**
     * Returns a string representing the data in this sequence.
     * A new <code>String</code> object is allocated and initialized to
     * contain the character sequence currently represented by this
     * object. This <code>String</code> is then returned. Subsequent
     * changes to this sequence do not affect the contents of the
     * <code>String</code>.
     *
     * @return  a string representation of this sequence of characters.
     */
    public String toString(){
        return String.valueOf(value,0,count);
    }

    /**
     * Needed by <tt>String</tt> for the contentEquals method.
     */
    final char[] getValue() {
        return value;
    }
}
TOP

Related Classes of com.utils.StringBuilderOpt

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.