You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "BELUGA BEHR (JIRA)" <ji...@apache.org> on 2017/06/12 14:35:00 UTC
[jira] [Updated] (TEXT-87) StrBuilder Add Prefix Optimization
[ https://issues.apache.org/jira/browse/TEXT-87?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
BELUGA BEHR updated TEXT-87:
----------------------------
Description:
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}
was:
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}
> 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)