Index: javax/swing/text/GapContent.java =================================================================== RCS file: /cvsroot/classpath/classpath/javax/swing/text/GapContent.java,v retrieving revision 1.27 diff -u -r1.27 GapContent.java --- javax/swing/text/GapContent.java 29 Sep 2005 17:32:18 -0000 1.27 +++ javax/swing/text/GapContent.java 30 Sep 2005 14:15:36 -0000 @@ -398,12 +398,7 @@ int delta = newSize - gapEnd + gapStart; // Update the marks after the gapEnd. - Vector v = getPositionsInRange(null, gapEnd, buffer.length - gapEnd); - for (Iterator i = v.iterator(); i.hasNext();) - { - GapContentPosition p = (GapContentPosition) i.next(); - p.mark += delta; - } + adjustPositionsInRange(gapEnd, buffer.length - gapEnd, delta); // Copy the data around. char[] newBuf = (char[]) allocateArray(length() + newSize); @@ -430,13 +425,7 @@ { // Update the positions between newGapStart and (old) gapStart. The marks // must be shifted by (gapEnd - gapStart). - Vector v = getPositionsInRange(null, newGapStart, - gapStart - newGapStart); - for (Iterator i = v.iterator(); i.hasNext();) - { - GapContentPosition p = (GapContentPosition) i.next(); - p.mark += gapEnd - gapStart; - } + adjustPositionsInRange(newGapStart, gapStart - newGapStart, gapEnd - gapStart); System.arraycopy(buffer, newGapStart, buffer, newGapEnd, gapStart - newGapStart); gapStart = newGapStart; @@ -446,13 +435,7 @@ { // Update the positions between newGapEnd and (old) gapEnd. The marks // must be shifted by (gapEnd - gapStart). - Vector v = getPositionsInRange(null, gapEnd, - newGapEnd - gapEnd); - for (Iterator i = v.iterator(); i.hasNext();) - { - GapContentPosition p = (GapContentPosition) i.next(); - p.mark -= gapEnd - gapStart; - } + adjustPositionsInRange(gapEnd, newGapEnd - gapEnd, -(gapEnd - gapStart)); System.arraycopy(buffer, gapEnd, buffer, gapStart, newGapStart - gapStart); gapStart = newGapStart; @@ -477,12 +460,7 @@ assert newGapStart < gapStart : "The new gap start must be less than the " + "old gap start."; - Vector v = getPositionsInRange(null, newGapStart, gapStart - newGapStart); - for (Iterator i = v.iterator(); i.hasNext();) - { - GapContentPosition p = (GapContentPosition) i.next(); - p.mark = gapStart; - } + setPositionsInRange(newGapStart, gapStart - newGapStart, gapStart); gapStart = newGapStart; } @@ -501,12 +479,7 @@ assert newGapEnd > gapEnd : "The new gap end must be greater than the " + "old gap end."; - Vector v = getPositionsInRange(null, gapEnd, newGapEnd - gapEnd); - for (Iterator i = v.iterator(); i.hasNext();) - { - GapContentPosition p = (GapContentPosition) i.next(); - p.mark = newGapEnd + 1; - } + setPositionsInRange(gapEnd, newGapEnd - gapEnd, newGapEnd + 1); gapEnd = newGapEnd; } @@ -608,6 +581,70 @@ } return res; } + + /** + * Sets the mark of all Positions that are in the range + * specified by offset and length within + * the buffer array to value + * + * @param offset the start offset of the range to search + * @param length the length of the range to search + * @param value the new value for each mark + */ + void setPositionsInRange(int offset, int length, int value) + { + int endOffset = offset + length; + + int index1 = Collections.binarySearch(positions, + new GapContentPosition(offset)); + int index2 = Collections.binarySearch(positions, + new GapContentPosition(endOffset)); + if (index1 < 0) + index1 = -(index1 + 1); + if (index2 < 0) + index2 = -(index2 + 1); + for (ListIterator i = positions.listIterator(index1); i.hasNext();) + { + if (i.nextIndex() > index2) + break; + + GapContentPosition p = (GapContentPosition) i.next(); + if (p.mark >= offset && p.mark <= endOffset) + p.mark = value; + } + } + + /** + * Adjusts the mark of all Positions that are in the range + * specified by offset and length within + * the buffer array by increment + * + * @param offset the start offset of the range to search + * @param length the length of the range to search + * @param incr the increment + */ + void adjustPositionsInRange(int offset, int length, int incr) + { + int endOffset = offset + length; + + int index1 = Collections.binarySearch(positions, + new GapContentPosition(offset)); + int index2 = Collections.binarySearch(positions, + new GapContentPosition(endOffset)); + if (index1 < 0) + index1 = -(index1 + 1); + if (index2 < 0) + index2 = -(index2 + 1); + for (ListIterator i = positions.listIterator(index1); i.hasNext();) + { + if (i.nextIndex() > index2) + break; + + GapContentPosition p = (GapContentPosition) i.next(); + if (p.mark >= offset && p.mark <= endOffset) + p.mark += incr; + } + } /** * Resets all Position that have an offset of 0, @@ -620,11 +657,6 @@ if (gapStart != 0) return; - Vector zeroMarks = getPositionsInRange(null, gapEnd, 0); - for (Iterator i = zeroMarks.iterator(); i.hasNext();) - { - GapContentPosition pos = (GapContentPosition) i.next(); - pos.mark = 0; - } + setPositionsInRange(gapEnd, 0, 0); } }