Uploaded image for project: 'Commons Text'
  1. Commons Text
  2. TEXT-87

StrBuilder Add Prefix Optimization

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Open
    • Trivial
    • Resolution: Unresolved
    • None
    • 1.x
    • None

    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.

      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;
          }
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            belugabehr David Mollitor
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: