Rename new method.
diff --git a/src/main/java/org/apache/commons/text/TextStringBuilder.java b/src/main/java/org/apache/commons/text/TextStringBuilder.java
index fc9aaa6..9c683a1 100644
--- a/src/main/java/org/apache/commons/text/TextStringBuilder.java
+++ b/src/main/java/org/apache/commons/text/TextStringBuilder.java
@@ -1769,6 +1769,28 @@
}
/**
+ * Drains (copies, then deletes) this character sequence into the specified array. This is equivalent to copying the
+ * characters from this sequence into the target and then deleting those character from this sequence.
+ *
+ * @param startIndex first index to copy, inclusive.
+ * @param endIndex last index to copy, exclusive.
+ * @param target the target array, must not be null.
+ * @param targetIndex the index to start copying in target.
+ * @return How many characters where deleted. If this builder is empty, return 0.
+ * @since 1.9
+ */
+ public int drainChars(final int startIndex, final int endIndex, final char[] target, final int targetIndex) {
+ final int length = endIndex - startIndex;
+ if (isEmpty() || length == 0) {
+ return 0;
+ }
+ final int actualLen = Math.min(size, length);
+ getChars(startIndex, actualLen, target, targetIndex);
+ delete(startIndex, actualLen);
+ return actualLen;
+ }
+
+ /**
* Checks whether this builder ends with the specified string.
* <p>
* Note that this method handles null input quietly, unlike String.
@@ -1901,27 +1923,6 @@
}
/**
- * Copies this character array into the specified array and then deletes those character from this source.
- *
- * @param startIndex first index to copy, inclusive.
- * @param endIndex last index to copy, exclusive.
- * @param target the target array, must not be null.
- * @param targetIndex the index to start copying in target.
- * @return How many characters where deleted. If this builder is empty, return 0.
- * @since 1.9
- */
- public int getCharsDelete(final int startIndex, final int endIndex, final char[] target, final int targetIndex) {
- final int length = endIndex - startIndex;
- if (isEmpty() || length == 0) {
- return 0;
- }
- final int actualLen = Math.min(size, length);
- getChars(startIndex, actualLen, target, targetIndex);
- delete(startIndex, actualLen);
- return actualLen;
- }
-
- /**
* Gets the text to be appended when a new line is added.
*
* @return The new line text, null means use system default
diff --git a/src/test/java/org/apache/commons/text/TextStringBuilderTest.java b/src/test/java/org/apache/commons/text/TextStringBuilderTest.java
index 4fe598b..c618509 100644
--- a/src/test/java/org/apache/commons/text/TextStringBuilderTest.java
+++ b/src/test/java/org/apache/commons/text/TextStringBuilderTest.java
@@ -784,6 +784,36 @@
}
@Test
+ public void testDrainCharsIntIntCharArrayInt() {
+ final char[] array = new char[5];
+ final TextStringBuilder sb = new TextStringBuilder();
+ // empty buffer
+ assertEquals(0, sb.drainChars(0, 5, array, 1));
+ // empty buffer, 0 length request
+ assertEquals(0, sb.drainChars(5, 5, array, 1));
+
+ final String data = "junit";
+ sb.append(data);
+ assertEquals(0, sb.drainChars(5, 5, array, 1));
+ assertEquals(5, sb.drainChars(0, 5, array, 0));
+ assertArrayEquals(data.toCharArray(), array);
+
+ final char[] b = new char[5];
+ sb.set(data);
+ assertEquals(2, sb.drainChars(0, 2, b, 3));
+ assertArrayEquals(new char[] {0, 0, 0, 'j', 'u'}, b);
+
+ assertThrows(IndexOutOfBoundsException.class, () -> sb.drainChars(-1, 0, b, 0));
+ assertThrows(IndexOutOfBoundsException.class, () -> sb.drainChars(0, -1, array, 0));
+ assertThrows(IndexOutOfBoundsException.class, () -> sb.drainChars(4, 2, array, 0));
+
+ sb.set(data);
+ // get and delete it all.
+ assertEquals(data.length(), sb.drainChars(0, sb.length() + 1, array, 0));
+ assertArrayEquals(data.toCharArray(), array);
+ }
+
+ @Test
public void testEndsWith() {
final TextStringBuilder sb = new TextStringBuilder();
assertFalse(sb.endsWith("a"));
@@ -891,36 +921,6 @@
}
@Test
- public void testGetCharsDeleteIntIntCharArrayInt() {
- final char[] array = new char[5];
- final TextStringBuilder sb = new TextStringBuilder();
- // empty buffer
- assertEquals(0, sb.getCharsDelete(0, 5, array, 1));
- // empty buffer, 0 length request
- assertEquals(0, sb.getCharsDelete(5, 5, array, 1));
-
- final String data = "junit";
- sb.append(data);
- assertEquals(0, sb.getCharsDelete(5, 5, array, 1));
- assertEquals(5, sb.getCharsDelete(0, 5, array, 0));
- assertArrayEquals(data.toCharArray(), array);
-
- final char[] b = new char[5];
- sb.set(data);
- assertEquals(2, sb.getCharsDelete(0, 2, b, 3));
- assertArrayEquals(new char[] {0, 0, 0, 'j', 'u'}, b);
-
- assertThrows(IndexOutOfBoundsException.class, () -> sb.getCharsDelete(-1, 0, b, 0));
- assertThrows(IndexOutOfBoundsException.class, () -> sb.getCharsDelete(0, -1, array, 0));
- assertThrows(IndexOutOfBoundsException.class, () -> sb.getCharsDelete(4, 2, array, 0));
-
- sb.set(data);
- // get and delete it all.
- assertEquals(data.length(), sb.getCharsDelete(0, sb.length() + 1, array, 0));
- assertArrayEquals(data.toCharArray(), array);
- }
-
- @Test
public void testGetCharsIntIntCharArrayInt() {
final TextStringBuilder sb = new TextStringBuilder();