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/27 13:32:39 UTC

[commons-lang] branch master updated (1621a23 -> 062bc6f)

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

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


    from 1621a23  Make org.apache.commons.lang3.CharSequenceUtils.toCharArray(CharSequence) public.
     new d68a7a4  Add org.apache.commons.lang3.StringUtils.substringAfter(String, int).
     new 75fa90e  Use final.
     new 062bc6f  Add Add org.apache.commons.lang3.StringUtils.substringAfterLast(String, int).

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/changes/changes.xml                            |  2 +
 .../apache/commons/lang3/CharSequenceUtils.java    |  8 +--
 .../java/org/apache/commons/lang3/StringUtils.java | 73 ++++++++++++++++++++++
 .../commons/lang3/CharSequenceUtilsTest.java       | 20 +++---
 .../commons/lang3/StringUtilsSubstringTest.java    | 31 +++++++++
 .../commons/lang3/time/FastDateParserTest.java     |  2 +-
 6 files changed, 121 insertions(+), 15 deletions(-)


[commons-lang] 03/03: Add Add org.apache.commons.lang3.StringUtils.substringAfterLast(String, int).

Posted by gg...@apache.org.
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-lang.git

commit 062bc6fe7d48d4eb4cdf3abec09e7547a0a9aed7
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 27 09:32:30 2020 -0400

    Add Add org.apache.commons.lang3.StringUtils.substringAfterLast(String,
    int).
---
 src/changes/changes.xml                            |  3 +-
 .../java/org/apache/commons/lang3/StringUtils.java | 37 ++++++++++++++++++++++
 .../commons/lang3/StringUtilsSubstringTest.java    | 16 ++++++++++
 3 files changed, 55 insertions(+), 1 deletion(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 6133923..2506d0c 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -82,7 +82,8 @@ The <action> type attribute can be add,update,fix,remove.
     <action issue="LANG-1567" type="update" dev="ggregory" due-to="Miguel Muñoz, Bruno P. Kinoshita, Gary Gregory">Fixed Javadocs for setTestRecursive() #556.</action>
     <action issue="LANG-1542" type="update" dev="ggregory" due-to=" Trần Ngọc Khoa, Gary Gregory">ToStringBuilder.reflectionToString - Wrong JSON format when object has a List of Enum.</action>
     <action                   type="update" dev="ggregory">Make org.apache.commons.lang3.CharSequenceUtils.toCharArray(CharSequence) public.</action>
-    <action                   type="add" dev="ggregory">Add org.apache.commons.lang3.StringUtils.substringAfter(String, int).</action>    
+    <action                   type="add" dev="ggregory">Add org.apache.commons.lang3.StringUtils.substringAfter(String, int).</action>
+    <action                   type="add" dev="ggregory">Add org.apache.commons.lang3.StringUtils.substringAfterLast(String, int).</action>
     <action                   type="update" dev="ggregory">org.apache.commons:commons-parent 50 -> 51.</action>
     <action                   type="update" dev="ggregory">org.junit-pioneer:junit-pioneer 0.5.4 -> 0.6.0.</action>
     <action                   type="update" dev="ggregory">org.junit.jupiter:junit-jupiter 5.6.0 -> 5.6.1.</action>
diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java
index 36dc7ac..6778720 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -8672,6 +8672,43 @@ public class StringUtils {
         return str.substring(pos + separator.length());
     }
 
+    /**
+     * <p>Gets the substring after the last occurrence of a separator.
+     * The separator is not returned.</p>
+     *
+     * <p>A {@code null} string input will return {@code null}.
+     * An empty ("") string input will return the empty string.
+     *
+     * <p>If nothing is found, the empty string is returned.</p>
+     *
+     * <pre>
+     * StringUtils.substringAfterLast(null, *)      = null
+     * StringUtils.substringAfterLast("", *)        = ""
+     * StringUtils.substringAfterLast("abc", 'a')   = "bc"
+     * StringUtils.substringAfterLast(" bc", 32)    = "bc"
+     * StringUtils.substringAfterLast("abcba", 'b') = "a"
+     * StringUtils.substringAfterLast("abc", 'c')   = ""
+     * StringUtils.substringAfterLast("a", 'a')     = ""
+     * StringUtils.substringAfterLast("a", 'z')     = ""
+     * </pre>
+     *
+     * @param str  the String to get a substring from, may be null
+     * @param separator  the String to search for, may be null
+     * @return the substring after the last occurrence of the separator,
+     *  {@code null} if null String input
+     * @since 3.11
+     */
+    public static String substringAfterLast(final String str, final int separator) {
+        if (isEmpty(str)) {
+            return str;
+        }
+        final int pos = str.lastIndexOf(separator);
+        if (pos == INDEX_NOT_FOUND || pos == str.length() - 1) {
+            return EMPTY;
+        }
+        return str.substring(pos + 1);
+    }
+
     // SubStringAfter/SubStringBefore
     //-----------------------------------------------------------------------
     /**
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java
index a930e03..9a1c014 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java
@@ -228,6 +228,22 @@ public class StringUtilsSubstringTest  {
         assertEquals("", StringUtils.substringAfterLast("abc", ""));
     }
 
+    @Test
+    public void testSubstringAfterLast_StringInt() {
+        assertNull(StringUtils.substringAfterLast(null, 0));
+        assertNull(StringUtils.substringAfterLast(null, 'X'));
+        assertEquals("", StringUtils.substringAfterLast("", 0));
+        assertEquals("", StringUtils.substringAfterLast("", 'a'));
+
+        assertEquals("", StringUtils.substringAfterLast("foo", 0));
+        assertEquals("", StringUtils.substringAfterLast("foo", 'b'));
+        assertEquals("t", StringUtils.substringAfterLast("foot", 'o'));
+        assertEquals("bc", StringUtils.substringAfterLast("abc", 'a'));
+        assertEquals("a", StringUtils.substringAfterLast("abcba", 'b'));
+        assertEquals("", StringUtils.substringAfterLast("abc", 'c'));
+        assertEquals("", StringUtils.substringAfterLast("", 'd'));
+    }
+
     //-----------------------------------------------------------------------
     @Test
     public void testSubstringBetween_StringString() {


[commons-lang] 01/03: Add org.apache.commons.lang3.StringUtils.substringAfter(String, int).

Posted by gg...@apache.org.
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-lang.git

commit d68a7a4189be8032045f952952c1aa9c764a3b9a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 27 09:24:06 2020 -0400

    Add org.apache.commons.lang3.StringUtils.substringAfter(String, int).
---
 src/changes/changes.xml                            |  1 +
 .../java/org/apache/commons/lang3/StringUtils.java | 36 ++++++++++++++++++++++
 .../commons/lang3/StringUtilsSubstringTest.java    | 15 +++++++++
 3 files changed, 52 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 5c8b3e1..6133923 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -82,6 +82,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action issue="LANG-1567" type="update" dev="ggregory" due-to="Miguel Muñoz, Bruno P. Kinoshita, Gary Gregory">Fixed Javadocs for setTestRecursive() #556.</action>
     <action issue="LANG-1542" type="update" dev="ggregory" due-to=" Trần Ngọc Khoa, Gary Gregory">ToStringBuilder.reflectionToString - Wrong JSON format when object has a List of Enum.</action>
     <action                   type="update" dev="ggregory">Make org.apache.commons.lang3.CharSequenceUtils.toCharArray(CharSequence) public.</action>
+    <action                   type="add" dev="ggregory">Add org.apache.commons.lang3.StringUtils.substringAfter(String, int).</action>    
     <action                   type="update" dev="ggregory">org.apache.commons:commons-parent 50 -> 51.</action>
     <action                   type="update" dev="ggregory">org.junit-pioneer:junit-pioneer 0.5.4 -> 0.6.0.</action>
     <action                   type="update" dev="ggregory">org.junit.jupiter:junit-jupiter 5.6.0 -> 5.6.1.</action>
diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java
index 5b87230..36dc7ac 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -8554,6 +8554,42 @@ public class StringUtils {
      *
      * <p>A {@code null} string input will return {@code null}.
      * An empty ("") string input will return the empty string.
+     *
+     * <p>If nothing is found, the empty string is returned.</p>
+     *
+     * <pre>
+     * StringUtils.substringAfter(null, *)      = null
+     * StringUtils.substringAfter("", *)        = ""
+     * StringUtils.substringAfter("abc", 'a')   = "bc"
+     * StringUtils.substringAfter("abcba", 'b') = "cba"
+     * StringUtils.substringAfter("abc", 'c')   = ""
+     * StringUtils.substringAfter("abc", 'd')   = ""
+     * StringUtils.substringAfter(" abc", 32)   = "abc"
+     * </pre>
+     *
+     * @param str  the String to get a substring from, may be null
+     * @param separator  the character to search.
+     * @return the substring after the first occurrence of the separator,
+     *  {@code null} if null String input
+     * @since 3.11
+     */
+    public static String substringAfter(final String str, final int separator) {
+        if (isEmpty(str)) {
+            return str;
+        }
+        final int pos = str.indexOf(separator);
+        if (pos == INDEX_NOT_FOUND) {
+            return EMPTY;
+        }
+        return str.substring(pos + 1);
+    }
+
+    /**
+     * <p>Gets the substring after the first occurrence of a separator.
+     * The separator is not returned.</p>
+     *
+     * <p>A {@code null} string input will return {@code null}.
+     * An empty ("") string input will return the empty string.
      * A {@code null} separator will return the empty string if the
      * input string is not {@code null}.</p>
      *
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java
index 76bd282..a930e03 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java
@@ -169,6 +169,21 @@ public class StringUtilsSubstringTest  {
     }
 
     @Test
+    public void testSubstringAfter_StringInt() {
+        assertNull(StringUtils.substringAfter(null, 0));
+        assertNull(StringUtils.substringAfter(null, 'X'));
+        assertEquals("", StringUtils.substringAfter("", 0));
+        assertEquals("", StringUtils.substringAfter("", 'X'));
+
+        assertEquals("", StringUtils.substringAfter("foo", 0));
+        assertEquals("ot", StringUtils.substringAfter("foot", 'o'));
+        assertEquals("bc", StringUtils.substringAfter("abc", 'a'));
+        assertEquals("cba", StringUtils.substringAfter("abcba", 'b'));
+        assertEquals("", StringUtils.substringAfter("abc", 'c'));
+        assertEquals("", StringUtils.substringAfter("abc", 'd'));
+    }
+
+    @Test
     public void testSubstringBeforeLast_StringString() {
         assertEquals("fooXXbar", StringUtils.substringBeforeLast("fooXXbarXXbaz", "XX"));
 


[commons-lang] 02/03: Use final.

Posted by gg...@apache.org.
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-lang.git

commit 75fa90e96c3ea16f92e1a178b257534990ac1616
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 27 09:24:58 2020 -0400

    Use final.
---
 .../org/apache/commons/lang3/CharSequenceUtils.java  |  8 ++++----
 .../apache/commons/lang3/CharSequenceUtilsTest.java  | 20 ++++++++++----------
 .../commons/lang3/time/FastDateParserTest.java       |  2 +-
 3 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/CharSequenceUtils.java b/src/main/java/org/apache/commons/lang3/CharSequenceUtils.java
index 494670b..fc5350f 100644
--- a/src/main/java/org/apache/commons/lang3/CharSequenceUtils.java
+++ b/src/main/java/org/apache/commons/lang3/CharSequenceUtils.java
@@ -237,8 +237,8 @@ public class CharSequenceUtils {
             }
         }
 
-        int len1 = cs.length();
-        int len2 = searchChar.length();
+        final int len1 = cs.length();
+        final int len2 = searchChar.length();
 
         if (start > len1) {
             start = len1;
@@ -266,7 +266,7 @@ public class CharSequenceUtils {
             start = len1 - len2;
         }
 
-        char char0 = searchChar.charAt(0);
+        final char char0 = searchChar.charAt(0);
 
         int i = start;
         while (true) {
@@ -286,7 +286,7 @@ public class CharSequenceUtils {
         }
     }
 
-    private static boolean checkLaterThan1(final CharSequence cs, final CharSequence searchChar, int len2, int start1) {
+    private static boolean checkLaterThan1(final CharSequence cs, final CharSequence searchChar, final int len2, final int start1) {
         for (int i = 1, j = len2 - 1; i <= j; i++, j--) {
             if (cs.charAt(start1 + i) != searchChar.charAt(i)
                     ||
diff --git a/src/test/java/org/apache/commons/lang3/CharSequenceUtilsTest.java b/src/test/java/org/apache/commons/lang3/CharSequenceUtilsTest.java
index dc0db4e..b21144c 100644
--- a/src/test/java/org/apache/commons/lang3/CharSequenceUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/CharSequenceUtilsTest.java
@@ -189,9 +189,9 @@ public class CharSequenceUtilsTest {
     }
 
     static class WrapperString implements CharSequence {
-        private CharSequence inner;
+        private final CharSequence inner;
 
-        WrapperString(CharSequence inner) {
+        WrapperString(final CharSequence inner) {
             this.inner = inner;
         }
 
@@ -201,12 +201,12 @@ public class CharSequenceUtilsTest {
         }
 
         @Override
-        public char charAt(int index) {
+        public char charAt(final int index) {
             return inner.charAt(index);
         }
 
         @Override
-        public CharSequence subSequence(int start, int end) {
+        public CharSequence subSequence(final int start, final int end) {
             return inner.subSequence(start, end);
         }
 
@@ -243,8 +243,8 @@ public class CharSequenceUtilsTest {
         testNewLastIndexOfSingle("oraoraoraora", "r");
         testNewLastIndexOfSingle("mudamudamudamuda", "d");
 
-        Random random = new Random();
-        StringBuilder seg = new StringBuilder();
+        final Random random = new Random();
+        final StringBuilder seg = new StringBuilder();
         while (seg.length() <= CharSequenceUtils.TO_STRING_LIMIT) {
             seg.append(random.nextInt());
         }
@@ -260,8 +260,8 @@ public class CharSequenceUtilsTest {
         }
     }
 
-    private void testNewLastIndexOfSingle(CharSequence a, CharSequence b) {
-        int maxa = Math.max(a.length(), b.length());
+    private void testNewLastIndexOfSingle(final CharSequence a, final CharSequence b) {
+        final int maxa = Math.max(a.length(), b.length());
         for (int i = -maxa - 10; i <= maxa + 10; i++) {
             testNewLastIndexOfSingle(a, b, i);
         }
@@ -269,12 +269,12 @@ public class CharSequenceUtilsTest {
         testNewLastIndexOfSingle(a, b, Integer.MAX_VALUE);
     }
 
-    private void testNewLastIndexOfSingle(CharSequence a, CharSequence b, int start) {
+    private void testNewLastIndexOfSingle(final CharSequence a, final CharSequence b, final int start) {
         testNewLastIndexOfSingleSingle(a, b, start);
         testNewLastIndexOfSingleSingle(b, a, start);
     }
 
-    private void testNewLastIndexOfSingleSingle(CharSequence a, CharSequence b, int start) {
+    private void testNewLastIndexOfSingleSingle(final CharSequence a, final CharSequence b, final int start) {
         assertEquals(
                 a.toString().lastIndexOf(b.toString(), start),
                 CharSequenceUtils.lastIndexOf(new WrapperString(a.toString()), new WrapperString(b.toString()), start),
diff --git a/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java b/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java
index eb02340..5a15ea3 100644
--- a/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java
@@ -732,7 +732,7 @@ public class FastDateParserTest {
         testSingleLocale(buggyLocale);
     }
 
-    private void testSingleLocale(Locale locale) throws ParseException {
+    private void testSingleLocale(final Locale locale) throws ParseException {
         final Calendar cal = Calendar.getInstance(GMT);
         cal.clear();
         cal.set(2003, Calendar.FEBRUARY, 10);