BELUGA BEHR created TEXT-87:
-------------------------------

             Summary: StrBuilder Add Prefix Optimization
                 Key: TEXT-87
                 URL: https://issues.apache.org/jira/browse/TEXT-87
             Project: Commons Text
          Issue Type: New Feature
            Reporter: BELUGA BEHR
            Priority: Trivial


As a nice differentiation to Java's String Builder, please add an optimization 
for pre-appending.  Create the buffer a little larger and keep a pointer at the 
0-index of the buffer so that pre-appending isn't necessarily a copy operations 
and instead is simply a pointer update and an array value update.

{code}

public StrBuilder(int initialCapacity, int preCapacity);

// So that on something like this, insert(0, 'A'), isn't a guaranteed array copy

    /**
     * Inserts the value into this builder.
     *
     * @param index  the index to add at, must be valid
     * @param value  the value to insert
     * @return this, to enable chaining
     * @throws IndexOutOfBoundsException if the index is invalid
     */
    public StrBuilder insert(final int index, final char value) {
        validateIndex(index);
        // if index == 0 and (bufIndex != 0)
        // buffer[bufIndexindex--] = value;
        // return this;
        ensureCapacity(size + 1);
        System.arraycopy(buffer, index, buffer, index + 1, size - index);
        buffer[index] = value;
        size++;
        return this;
    }
{code}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to