commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject [jira] [Created] (TEXT-87) StrBuilder Add Prefix Optimization
Date Mon, 12 Jun 2017 14:34:00 GMT
BELUGA BEHR created TEXT-87:

             Summary: StrBuilder Add Prefix Optimization
                 Key: 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.


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) {
        // 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;
        return this;

This message was sent by Atlassian JIRA

View raw message