You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/06/29 12:25:13 UTC

[commons-text] branch master updated: Add org.apache.commons.text.TextStringBuilder.charAtDelete(int).

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-text.git


The following commit(s) were added to refs/heads/master by this push:
     new 709138b  Add org.apache.commons.text.TextStringBuilder.charAtDelete(int).
709138b is described below

commit 709138b0720f25d16fc851b816c6b56074e74b7f
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Jun 29 08:25:06 2020 -0400

    Add org.apache.commons.text.TextStringBuilder.charAtDelete(int).
---
 src/changes/changes.xml                               |  1 +
 .../org/apache/commons/text/TextStringBuilder.java    | 19 +++++++++++++++++++
 .../apache/commons/text/TextStringBuilderTest.java    | 17 +++++++++++++++++
 3 files changed, 37 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index dac7735..a411a6c 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -59,6 +59,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                  type="add" dev="ggregory" due-to="Gary Gregory">Add BiStringLookup and implementation BiFunctionStringLookup.</action>
     <action                  type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.text.TextStringBuilder.toString(int, int).</action>
     <action                  type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.text.TextStringBuilder.readFrom(Reader, int).</action>
+    <action                  type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.text.TextStringBuilder.charAtDelete(int).</action>
     <action                  type="update" dev="ggregory" due-to="Gary Gregory">[test] junit-jupiter 5.5.1 -> 5.5.2.</action>
     <action                  type="update" dev="ggregory" due-to="Gary Gregory">[test] org.assertj:assertj-core 3.13.2 -> 3.16.1.</action>
     <action                  type="update" dev="ggregory" due-to="Gary Gregory">[build] com.puppycrawl.tools:checkstyle 8.23 -> 8.27.</action>
diff --git a/src/main/java/org/apache/commons/text/TextStringBuilder.java b/src/main/java/org/apache/commons/text/TextStringBuilder.java
index beea03c..8953f50 100644
--- a/src/main/java/org/apache/commons/text/TextStringBuilder.java
+++ b/src/main/java/org/apache/commons/text/TextStringBuilder.java
@@ -1642,6 +1642,25 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
     }
 
     /**
+     * Gets the character at the specified index before deleting it.
+     *
+     * @see #charAt(int)
+     * @see #deleteCharAt(int)
+     * @param index
+     *            the index to retrieve, must be valid
+     * @return The character at the index
+     * @throws IndexOutOfBoundsException
+     *             if the index is invalid
+     * @since 1.9
+     */
+    public char charAtDelete(final int index) {
+        validateIndex(index);
+        final char c = buffer[index];
+        deleteCharAt(index);
+        return c;
+    }
+
+    /**
      * Clears the string builder (convenience Collections API style method).
      * <p>
      * This method does not reduce the size of the internal character buffer. To do that, call {@code clear()}
diff --git a/src/test/java/org/apache/commons/text/TextStringBuilderTest.java b/src/test/java/org/apache/commons/text/TextStringBuilderTest.java
index 7734561..94cac23 100644
--- a/src/test/java/org/apache/commons/text/TextStringBuilderTest.java
+++ b/src/test/java/org/apache/commons/text/TextStringBuilderTest.java
@@ -693,6 +693,23 @@ public class TextStringBuilderTest {
     }
 
     @Test
+    public void testCharAtDelete() {
+        final String str = "abc";
+        //
+        final TextStringBuilder sb1 = new TextStringBuilder(str);
+        assertEquals('a', sb1.charAtDelete(0));
+        assertEquals("bc", sb1.toString());
+        //
+        final TextStringBuilder sb2 = new TextStringBuilder(str);
+        assertEquals('c', sb2.charAtDelete(str.length() - 1));
+        assertEquals("ab", sb2.toString());
+        //
+        final TextStringBuilder sb3 = new TextStringBuilder(str);
+        assertThrows(IndexOutOfBoundsException.class, () -> sb3.charAtDelete(str.length()));
+        assertThrows(IndexOutOfBoundsException.class, () -> sb3.charAtDelete(1000));
+    }
+
+    @Test
     public void testDeleteCharAtWithNegative() {
         assertThatExceptionOfType(StringIndexOutOfBoundsException.class).isThrownBy(() -> {
             new TextStringBuilder().deleteCharAt((-1258));