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/28 21:13:36 UTC

[commons-text] branch master updated: Separate concerns of index validation and index fitting which also fixes SpotBugs issues. Thanks to Rob Tompkins.

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 ef28d14  Separate concerns of index validation and index fitting which also fixes SpotBugs issues. Thanks to Rob Tompkins.
ef28d14 is described below

commit ef28d14c73ff5c453b286ae254419aea679bd3c6
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jun 28 17:07:59 2020 -0400

    Separate concerns of index validation and index fitting which also fixes
    SpotBugs issues. Thanks to Rob Tompkins.
---
 .../org/apache/commons/text/TextStringBuilder.java | 36 +++++++++++++++++-----
 1 file changed, 28 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/apache/commons/text/TextStringBuilder.java b/src/main/java/org/apache/commons/text/TextStringBuilder.java
index 58af923..72a945b 100644
--- a/src/main/java/org/apache/commons/text/TextStringBuilder.java
+++ b/src/main/java/org/apache/commons/text/TextStringBuilder.java
@@ -2686,9 +2686,9 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
     public int readFrom(final Reader reader) throws IOException {
         final int oldSize = size;
         ensureCapacity(size + 1);
-        int read;
-        while ((read = reader.read(buffer, size, buffer.length - size)) != -1) {
-            size += read;
+        int readCount;
+        while ((readCount = reader.read(buffer, size, buffer.length - size)) != -1) {
+            size += readCount;
             ensureCapacity(size + 1);
         }
         return size - oldSize;
@@ -2971,7 +2971,6 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
             return new String(buffer, size - length, length);
         }
     }
-
     /**
      * Sets the character at the specified index.
      *
@@ -2992,6 +2991,7 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
         buffer[index] = ch;
         return this;
     }
+
     /**
      * Updates the length of the builder by either dropping the last characters or adding filler of Unicode zero.
      *
@@ -3183,7 +3183,7 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
      * @since 1.9
      */
     public String toString(final int startIndex, int count) {
-        validateRange(startIndex, startIndex + count);
+        validateIndices(startIndex, startIndex + count);
         return new String(buffer, startIndex, count);
     }
 
@@ -3235,7 +3235,7 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
     /**
      * Validates that an index is in the range {@code 0 <= index <= size}.
      *
-     * @param index the index, must be valid
+     * @param index the index to test.
      * @throws IndexOutOfBoundsException Thrown when the index is not the range {@code 0 <= index <= size}.
      */
     protected void validateIndex(final int index) {
@@ -3245,14 +3245,34 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
     }
 
     /**
+     * Validates indices defining a range in this builder.
+     *
+     * @param startIndex
+     *            the start index, inclusive.
+     * @param endIndex
+     *            the end index, exclusive.
+     * @throws StringIndexOutOfBoundsException
+     *             if the index is invalid
+     * @since 1.9
+     */
+    protected void validateIndices(final int startIndex, final int endIndex) {
+        if (startIndex < 0) {
+            throw new StringIndexOutOfBoundsException(startIndex);
+        }
+        if (startIndex > size) {
+            throw new StringIndexOutOfBoundsException("end < start");
+        }
+    }
+
+    /**
      * Validates parameters defining a range of the builder.
      *
      * @param startIndex
      *            the start index, inclusive, must be valid
      * @param endIndex
      *            the end index, exclusive, must be valid except that if too large it is treated as end of string
-     * @return The new string
-     * @throws IndexOutOfBoundsException
+     * @return A valid end index.
+     * @throws StringIndexOutOfBoundsException
      *             if the index is invalid
      */
     protected int validateRange(final int startIndex, int endIndex) {