This is an automated email from the ASF dual-hosted git repository.
garydgregory 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 ae5f487d3 Clarify surrogate handling in StringUtils.abbreviateMiddle
(#1907)
ae5f487d3 is described below
commit ae5f487d3d00b12a6ce996c78de5a9eb2b105463
Author: wenwenwu999-maker <[email protected]>
AuthorDate: Thu Dec 3 22:42:42 2026 +0800
Clarify surrogate handling in StringUtils.abbreviateMiddle (#1907)
Assisted-By: OpenAI Codex
---
src/main/java/org/apache/commons/lang3/StringUtils.java | 15 +++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)
diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java
b/src/main/java/org/apache/commons/lang3/StringUtils.java
index 653e91152..4d96b240b 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -412,21 +412,23 @@ public static String abbreviate(final String str, String
abbrevMarker, final int
}
/**
- * Abbreviates a String to the length passed, replacing the middle
characters with the supplied replacement String.
+ * Abbreviates a String to at most the specified length by replacing its
middle characters with the supplied replacement String.
*
* <p>
- * This abbreviation only occurs if the following criteria is met:
+ * Abbreviation occurs only when all of the following conditions hold:
* </p>
* <ul>
* <li>Neither the String for abbreviation nor the replacement String are
null or empty</li>
* <li>The length to truncate to is less than the length of the supplied
String</li>
- * <li>The length to truncate to is greater than 0</li>
- * <li>The abbreviated String will have enough room for the length
supplied replacement String and the first and last characters of the supplied
String for
- * abbreviation</li>
+ * <li>The requested length exceeds the replacement String's length by at
least two UTF-16 code units</li>
* </ul>
* <p>
* Otherwise, the returned String will be the same as the supplied String
for abbreviation.
* </p>
+ * <p>
+ * If a cut would split a surrogate pair, the entire pair is omitted. The
result can therefore be shorter than the requested length,
+ * even when the pair represents the first or last character.
+ * </p>
*
* <pre>
* StringUtils.abbreviateMiddle(null, null, 0) = null
@@ -434,11 +436,12 @@ public static String abbreviate(final String str, String
abbrevMarker, final int
* StringUtils.abbreviateMiddle("abc", ".", 0) = "abc"
* StringUtils.abbreviateMiddle("abc", ".", 3) = "abc"
* StringUtils.abbreviateMiddle("abcdef", ".", 4) = "ab.f"
+ * StringUtils.abbreviateMiddle("😀abc😀", "..", 5) = "😀.."
* </pre>
*
* @param str The String to abbreviate, may be null.
* @param middle The String to replace the middle characters with, may be
null.
- * @param length The length to abbreviate {@code str} to.
+ * @param length The requested length in UTF-16 code units.
* @return The abbreviated String if the above criteria is met, or the
original String supplied for abbreviation.
* @since 2.5
*/