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 76d30201faf8f5c116c3130b880d8b494a5bfc8f Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Tue Apr 15 15:43:54 2025 -0400 Reimplement StringUtils.capitalize(String) to use java.lang.CharSequence.codePoints() --- src/changes/changes.xml | 1 + src/main/java/org/apache/commons/lang3/StringUtils.java | 17 ++++------------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 8cd9319fe..e9aaac334 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -85,6 +85,7 @@ The <action> type attribute can be add,update,fix,remove. <action type="fix" dev="ggregory" due-to="Gary Gregory">Several hash collisions in Fraction class.</action> <action issue="LANG-1768" type="fix" dev="ggregory" due-to="Wang Hailong, Gary Gregory">MutableLong and friends should provide better parsing exceptions Javadocs.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Reimplement StringUtils.toCodePoints(CharSequence) to use java.lang.CharSequence.codePoints().</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">Reimplement StringUtils.capitalize(String) to use java.lang.CharSequence.codePoints().</action> <!-- ADD --> <action type="add" dev="ggregory" due-to="Gary Gregory">Add Strings and refactor StringUtils.</action> <action issue="LANG-1747" type="add" dev="ggregory" due-to="Oliver B. Fischer, Gary Gregory">Add StopWatch.run([Failable]Runnable) and get([Failable]Supplier).</action> diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 207fb025b..b509fdf68 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -517,27 +517,18 @@ public static String appendIfMissingIgnoreCase(final String str, final CharSeque * @since 2.0 */ public static String capitalize(final String str) { - final int strLen = length(str); - if (strLen == 0) { + if (isEmpty(str)) { return str; } - final int firstCodepoint = str.codePointAt(0); final int newCodePoint = Character.toTitleCase(firstCodepoint); if (firstCodepoint == newCodePoint) { // already capitalized return str; } - - final int[] newCodePoints = new int[strLen]; // cannot be longer than the char array - int outOffset = 0; - newCodePoints[outOffset++] = newCodePoint; // copy the first code point - for (int inOffset = Character.charCount(firstCodepoint); inOffset < strLen; ) { - final int codePoint = str.codePointAt(inOffset); - newCodePoints[outOffset++] = codePoint; // copy the remaining ones - inOffset += Character.charCount(codePoint); - } - return new String(newCodePoints, 0, outOffset); + final int[] newCodePoints = str.codePoints().toArray(); + newCodePoints[0] = newCodePoint; // copy the first code point + return new String(newCodePoints, 0, newCodePoints.length); } /**