Skip to content

Commit

Permalink
Refactor StringUtils#startsWithAny()
Browse files Browse the repository at this point in the history
- Refactor to use Strings
- Gains a case-insensitive implementation
  • Loading branch information
garydgregory committed Sep 26, 2024
1 parent 6e78b38 commit 479532b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
16 changes: 5 additions & 11 deletions src/main/java/org/apache/commons/lang3/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7774,7 +7774,7 @@ private static String[] splitWorker(final String str, final String separatorChar
}

/**
* 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>
Expand Down Expand Up @@ -7802,7 +7802,7 @@ public static boolean startsWith(final CharSequence str, final CharSequence pref
}

/**
* 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
Expand All @@ -7822,17 +7822,11 @@ public static boolean startsWith(final CharSequence str, final CharSequence pref
* 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);
}

/**
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/org/apache/commons/lang3/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -1410,4 +1410,41 @@ public boolean startsWith(final CharSequence str, final CharSequence prefix) {
}
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;
}

}

0 comments on commit 479532b

Please sign in to comment.