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 f43534ec04d4c4bae666033e2041ecde21c19a56
Author: Gary Gregory <[email protected]>
AuthorDate: Thu Jan 9 13:19:06 2025 -0500

    Add RegExUtils methods typed to CharSequence input and deprecate old
    versions typed to String
---
 src/changes/changes.xml                            |  4 +-
 .../java/org/apache/commons/lang3/RegExUtils.java  | 68 ++++++++++++++++++----
 .../org/apache/commons/lang3/RegExUtilsTest.java   | 30 +++++++++-
 3 files changed, 84 insertions(+), 18 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 94072e2bd..4d8aa0085 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -92,9 +92,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add BasicThreadFactory.daemon().</action>
     <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add ArrayUtils.startsWith.</action>
     <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add Predicates.</action>
-    <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add RegExUtils.dotAllMatcher(String, CharSequence) and deprecate 
RegExUtils.dotAllMatcher(String, String).</action>
-    <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add RegExUtils.replacePattern(CharSequence, String, String) and 
deprecate RegExUtils.replacePattern(String, String, String).</action>
-    <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add RegExUtils.removePattern(CharSequence, String) and deprecate 
RegExUtils.removePattern(String, String).</action>
+    <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add RegExUtils methods typed to CharSequence input and deprecate old 
versions typed to String.</action>
     <!-- UPDATE -->
     <action                   type="update" dev="ggregory" due-to="Gary 
Gregory, Dependabot">Bump org.apache.commons:commons-parent from 73 to 79 
#1267, #1277, #1283, #1288, #1302.</action>
     <action                   type="update" dev="ggregory" due-to="Gary 
Gregory, Dependabot">[site] Bump org.codehaus.mojo:taglist-maven-plugin from 
3.1.0 to 3.2.1 #1300.</action>
diff --git a/src/main/java/org/apache/commons/lang3/RegExUtils.java 
b/src/main/java/org/apache/commons/lang3/RegExUtils.java
index 52abaaf0b..606285c44 100644
--- a/src/main/java/org/apache/commons/lang3/RegExUtils.java
+++ b/src/main/java/org/apache/commons/lang3/RegExUtils.java
@@ -94,12 +94,12 @@ public class RegExUtils {
      * @return  the text with any removes processed,
      *              {@code null} if null String input
      *
-     * @see #replaceAll(String, Pattern, String)
+     * @see #replaceAll(CharSequence, Pattern, String)
      * @see java.util.regex.Matcher#replaceAll(String)
      * @see java.util.regex.Pattern
      */
     public static String removeAll(final String text, final Pattern regex) {
-        return replaceAll(text, regex, StringUtils.EMPTY);
+        return replaceAll((CharSequence) text, regex, StringUtils.EMPTY);
     }
 
     /**
@@ -255,8 +255,9 @@ public class RegExUtils {
      * @see #replacePattern(CharSequence, String, String)
      * @see String#replaceAll(String, String)
      * @see Pattern#DOTALL
+     * @since 3.18.0
      */
-    public static CharSequence removePattern(final CharSequence text, final 
String regex) {
+    public static String removePattern(final CharSequence text, final String 
regex) {
         return replacePattern(text, regex, StringUtils.EMPTY);
     }
 
@@ -290,7 +291,7 @@ public class RegExUtils {
      */
     @Deprecated
     public static String removePattern(final String text, final String regex) {
-        return replacePattern(text, regex, StringUtils.EMPTY);
+        return replacePattern((CharSequence) text, regex, StringUtils.EMPTY);
     }
 
     /**
@@ -329,13 +330,55 @@ public class RegExUtils {
      * @see java.util.regex.Matcher#replaceAll(String)
      * @see java.util.regex.Pattern
      */
-    public static String replaceAll(final String text, final Pattern regex, 
final String replacement) {
+    public static String replaceAll(final CharSequence text, final Pattern 
regex, final String replacement) {
         if (ObjectUtils.anyNull(text, regex, replacement)) {
-            return text;
+            return toStringOrNull(text);
         }
         return regex.matcher(text).replaceAll(replacement);
     }
 
+    /**
+     * Replaces each substring of the text String that matches the given 
regular expression pattern with the given replacement.
+     *
+     * This method is a {@code null} safe equivalent to:
+     * <ul>
+     *  <li>{@code pattern.matcher(text).replaceAll(replacement)}</li>
+     * </ul>
+     *
+     * <p>A {@code null} reference passed to this method is a no-op.</p>
+     *
+     * <pre>{@code
+     * StringUtils.replaceAll(null, *, *)       = null
+     * StringUtils.replaceAll("any", (Pattern) null, *)   = "any"
+     * StringUtils.replaceAll("any", *, null)   = "any"
+     * StringUtils.replaceAll("", Pattern.compile(""), "zzz")    = "zzz"
+     * StringUtils.replaceAll("", Pattern.compile(".*"), "zzz")  = "zzz"
+     * StringUtils.replaceAll("", Pattern.compile(".+"), "zzz")  = ""
+     * StringUtils.replaceAll("abc", Pattern.compile(""), "ZZ")  = 
"ZZaZZbZZcZZ"
+     * StringUtils.replaceAll("<__>\n<__>", Pattern.compile("<.*>"), "z")      
           = "z\nz"
+     * StringUtils.replaceAll("<__>\n<__>", Pattern.compile("<.*>", 
Pattern.DOTALL), "z") = "z"
+     * StringUtils.replaceAll("<__>\n<__>", Pattern.compile("(?s)<.*>"), "z")  
           = "z"
+     * StringUtils.replaceAll("ABCabc123", Pattern.compile("[a-z]"), "_")      
 = "ABC___123"
+     * StringUtils.replaceAll("ABCabc123", Pattern.compile("[^A-Z0-9]+"), "_") 
 = "ABC_123"
+     * StringUtils.replaceAll("ABCabc123", Pattern.compile("[^A-Z0-9]+"), "")  
 = "ABC123"
+     * StringUtils.replaceAll("Lorem ipsum  dolor   sit", Pattern.compile("( 
+)([a-z]+)"), "_$2")  = "Lorem_ipsum_dolor_sit"
+     * }</pre>
+     *
+     * @param text  text to search and replace in, may be null
+     * @param regex  the regular expression pattern to which this string is to 
be matched
+     * @param replacement  the string to be substituted for each match
+     * @return  the text with any replacements processed,
+     *              {@code null} if null String input
+     *
+     * @see java.util.regex.Matcher#replaceAll(String)
+     * @see java.util.regex.Pattern
+     * @deprecated Use {@link #replaceAll(CharSequence, Pattern, String)}.
+     */
+    @Deprecated
+    public static String replaceAll(final String text, final Pattern regex, 
final String replacement) {
+        return replaceAll((CharSequence) text, regex, replacement);
+    }
+
     /**
      * Replaces each substring of the text String that matches the given 
regular expression
      * with the given replacement.
@@ -523,9 +566,9 @@ public class RegExUtils {
      * @see Pattern#DOTALL
      * @since 3.18.0
      */
-    public static CharSequence replacePattern(final CharSequence text, final 
String regex, final String replacement) {
+    public static String replacePattern(final CharSequence text, final String 
regex, final String replacement) {
         if (ObjectUtils.anyNull(text, regex, replacement)) {
-            return text;
+            return toStringOrNull(text);
         }
         return dotAllMatcher(regex, text).replaceAll(replacement);
     }
@@ -570,10 +613,11 @@ public class RegExUtils {
      */
     @Deprecated
     public static String replacePattern(final String text, final String regex, 
final String replacement) {
-        if (ObjectUtils.anyNull(text, regex, replacement)) {
-            return text;
-        }
-        return dotAllMatcher(regex, text).replaceAll(replacement);
+        return replacePattern((CharSequence) text, regex, replacement);
+    }
+
+    private static String toStringOrNull(final CharSequence text) {
+        return text != null ? text.toString() : null;
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/lang3/RegExUtilsTest.java 
b/src/test/java/org/apache/commons/lang3/RegExUtilsTest.java
index b4d5b0890..79cfe3091 100644
--- a/src/test/java/org/apache/commons/lang3/RegExUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/RegExUtilsTest.java
@@ -159,7 +159,32 @@ public class RegExUtilsTest extends AbstractLangTest {
     }
 
     @Test
-    public void testReplaceAll_StringPatternString() {
+    public void testReplaceAll() {
+        assertNull(RegExUtils.replaceAll((CharSequence) null, 
Pattern.compile(""), ""));
+
+        assertEquals("any", RegExUtils.replaceAll((CharSequence) "any", 
(Pattern) null, ""));
+        assertEquals("any", RegExUtils.replaceAll((CharSequence) "any", 
Pattern.compile(""), null));
+
+        assertEquals("zzz", RegExUtils.replaceAll((CharSequence) "", 
Pattern.compile(""), "zzz"));
+        assertEquals("zzz", RegExUtils.replaceAll((CharSequence) "", 
Pattern.compile(".*"), "zzz"));
+        assertEquals("", RegExUtils.replaceAll((CharSequence) "", 
Pattern.compile(".+"), "zzz"));
+        assertEquals("ZZaZZbZZcZZ", RegExUtils.replaceAll((CharSequence) 
"abc", Pattern.compile(""), "ZZ"));
+
+        assertEquals("z\nz", RegExUtils.replaceAll((CharSequence) 
"<__>\n<__>", Pattern.compile("<.*>"), "z"));
+        assertEquals("z", RegExUtils.replaceAll((CharSequence) "<__>\n<__>", 
Pattern.compile("(?s)<.*>"), "z"));
+
+        assertEquals("z", RegExUtils.replaceAll((CharSequence) "<__>\n<__>", 
Pattern.compile("<.*>", Pattern.DOTALL), "z"));
+        assertEquals("z", RegExUtils.replaceAll((CharSequence) "<__>\\n<__>", 
Pattern.compile("<.*>"), "z"));
+        assertEquals("X", RegExUtils.replaceAll((CharSequence) 
"<A>\nxy\n</A>", Pattern.compile("<A>.*</A>", Pattern.DOTALL), "X"));
+
+        assertEquals("ABC___123", RegExUtils.replaceAll((CharSequence) 
"ABCabc123", Pattern.compile("[a-z]"), "_"));
+        assertEquals("ABC_123", RegExUtils.replaceAll((CharSequence) 
"ABCabc123", Pattern.compile("[^A-Z0-9]+"), "_"));
+        assertEquals("ABC123", RegExUtils.replaceAll((CharSequence) 
"ABCabc123", Pattern.compile("[^A-Z0-9]+"), ""));
+        assertEquals("Lorem_ipsum_dolor_sit", 
RegExUtils.replaceAll((CharSequence) "Lorem ipsum  dolor   sit", 
Pattern.compile("( +)([a-z]+)"), "_$2"));
+    }
+
+    @Test
+    public void testReplaceAllDeprecated() {
         assertNull(RegExUtils.replaceAll(null, Pattern.compile(""), ""));
 
         assertEquals("any", RegExUtils.replaceAll("any", (Pattern) null, ""));
@@ -180,8 +205,7 @@ public class RegExUtilsTest extends AbstractLangTest {
         assertEquals("ABC___123", RegExUtils.replaceAll("ABCabc123", 
Pattern.compile("[a-z]"), "_"));
         assertEquals("ABC_123", RegExUtils.replaceAll("ABCabc123", 
Pattern.compile("[^A-Z0-9]+"), "_"));
         assertEquals("ABC123", RegExUtils.replaceAll("ABCabc123", 
Pattern.compile("[^A-Z0-9]+"), ""));
-        assertEquals("Lorem_ipsum_dolor_sit",
-                RegExUtils.replaceAll("Lorem ipsum  dolor   sit", 
Pattern.compile("( +)([a-z]+)"), "_$2"));
+        assertEquals("Lorem_ipsum_dolor_sit", RegExUtils.replaceAll("Lorem 
ipsum  dolor   sit", Pattern.compile("( +)([a-z]+)"), "_$2"));
     }
 
     @Test

Reply via email to