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
The following commit(s) were added to refs/heads/master by this push: new 479532b83 Refactor StringUtils#startsWithAny() 479532b83 is described below commit 479532b83db2d7b228e4128f2db02233f91e6e91 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Thu Sep 26 11:13:34 2024 -0400 Refactor StringUtils#startsWithAny() - Refactor to use Strings - Gains a case-insensitive implementation --- .../java/org/apache/commons/lang3/StringUtils.java | 16 +++------- .../java/org/apache/commons/lang3/Strings.java | 37 ++++++++++++++++++++++ 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 786d17c0d..b9403c793 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -7774,7 +7774,7 @@ public class StringUtils { } /** - * Check if a CharSequence starts with a specified prefix. + * Tests if a CharSequence starts with a specified prefix. * * <p>{@code null}s are handled without exceptions. Two {@code null} * references are considered to be equal. The comparison is case-sensitive.</p> @@ -7802,7 +7802,7 @@ public class StringUtils { } /** - * Check if a CharSequence starts with any of the provided case-sensitive prefixes. + * Tests if a CharSequence starts with any of the provided case-sensitive prefixes. * * <pre> * StringUtils.startsWithAny(null, null) = false @@ -7822,17 +7822,11 @@ public class StringUtils { * the input {@code sequence} begins with any of the provided case-sensitive {@code searchStrings}. * @since 2.5 * @since 3.0 Changed signature from startsWithAny(String, String[]) to startsWithAny(CharSequence, CharSequence...) + * @deprecated Use {@link Strings#startsWithAny(CharSequence, CharSequence...) Strings.CI.startsWithAny(CharSequence, CharSequence...)} */ + @Deprecated public static boolean startsWithAny(final CharSequence sequence, final CharSequence... searchStrings) { - if (isEmpty(sequence) || ArrayUtils.isEmpty(searchStrings)) { - return false; - } - for (final CharSequence searchString : searchStrings) { - if (Strings.CS.startsWith(sequence, searchString)) { - return true; - } - } - return false; + return Strings.CS.startsWithAny(sequence, searchStrings); } /** diff --git a/src/main/java/org/apache/commons/lang3/Strings.java b/src/main/java/org/apache/commons/lang3/Strings.java index d89fbae90..d9428e3d4 100644 --- a/src/main/java/org/apache/commons/lang3/Strings.java +++ b/src/main/java/org/apache/commons/lang3/Strings.java @@ -1410,4 +1410,41 @@ public abstract class Strings { } return CharSequenceUtils.regionMatches(str, ignoreCase, 0, prefix, 0, preLen); } + + /** + * Tests if a CharSequence starts with any of the provided case-sensitive prefixes. + * + * <p> + * Case-sensitive examples + * </p> + * + * <pre> + * StringUtils.startsWithAny(null, null) = false + * StringUtils.startsWithAny(null, new String[] {"abc"}) = false + * StringUtils.startsWithAny("abcxyz", null) = false + * StringUtils.startsWithAny("abcxyz", new String[] {""}) = true + * StringUtils.startsWithAny("abcxyz", new String[] {"abc"}) = true + * StringUtils.startsWithAny("abcxyz", new String[] {null, "xyz", "abc"}) = true + * StringUtils.startsWithAny("abcxyz", null, "xyz", "ABCX") = false + * StringUtils.startsWithAny("ABCXYZ", null, "xyz", "abc") = false + * </pre> + * + * @param sequence the CharSequence to check, may be null + * @param searchStrings the case-sensitive CharSequence prefixes, may be empty or contain {@code null} + * @see StringUtils#startsWith(CharSequence, CharSequence) + * @return {@code true} if the input {@code sequence} is {@code null} AND no {@code searchStrings} are provided, or the input {@code sequence} begins with + * any of the provided case-sensitive {@code searchStrings}. + */ + public boolean startsWithAny(final CharSequence sequence, final CharSequence... searchStrings) { + if (StringUtils.isEmpty(sequence) || ArrayUtils.isEmpty(searchStrings)) { + return false; + } + for (final CharSequence searchString : searchStrings) { + if (startsWith(sequence, searchString)) { + return true; + } + } + return false; + } + }