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 70b30b95c Correct StringUtils and WordUtils Javadoc
70b30b95c is described below

commit 70b30b95c3348178410f288ccb9f766b57916b07
Author: Gary Gregory <[email protected]>
AuthorDate: Thu Dec 3 15:12:02 2026 +0000

    Correct StringUtils and WordUtils Javadoc
    
    Document UTF-16 lengths, surrogate pair adjustments, and abbreviation
    offset behavior. Correct slicing and truncation ranges, center padding
    defaults, and wrapping examples.
    
    Add regression assertions for abbreviation offsets, empty and null
    overlays, and truncation.
---
 .../java/org/apache/commons/lang3/StringUtils.java | 159 ++++++++++++++-------
 .../org/apache/commons/lang3/text/WordUtils.java   |  20 ++-
 .../commons/lang3/StringUtilsAbbreviateTest.java   |   8 ++
 .../org/apache/commons/lang3/StringUtilsTest.java  |   4 +
 4 files changed, 136 insertions(+), 55 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java 
b/src/main/java/org/apache/commons/lang3/StringUtils.java
index 050630057..b8d35ccce 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -228,12 +228,17 @@ public class StringUtils {
      * Specifically:
      * </p>
      * <ul>
-     * <li>If the number of characters in {@code str} is less than or equal to 
{@code maxWidth}, return {@code str}.</li>
-     * <li>Else abbreviate it to {@code (substring(str, 0, max - 3) + 
"...")}.</li>
-     * <li>If {@code maxWidth} is less than {@code 4}, throw an {@link 
IllegalArgumentException}.</li>
-     * <li>In no case will it return a String of length greater than {@code 
maxWidth}.</li>
+     * <li>A {@code null} or empty String is returned unchanged.</li>
+     * <li>For a nonempty String, if {@code maxWidth} is less than {@code 4}, 
throw an {@link IllegalArgumentException}.</li>
+     * <li>If the length of {@code str} is less than or equal to {@code 
maxWidth}, return {@code str}.</li>
+     * <li>Otherwise, retain at most {@code maxWidth} minus three UTF-16 code 
units from the start of {@code str}, followed by the ellipses.</li>
      * </ul>
      *
+     * <p>
+     * Lengths are measured in UTF-16 code units. If the cut would split a 
surrogate pair, the entire pair is omitted, so the result can be shorter than
+     * {@code maxWidth}.
+     * </p>
+     *
      * <pre>
      * StringUtils.abbreviate(null, *)      = null
      * StringUtils.abbreviate("", 4)        = ""
@@ -241,11 +246,12 @@ public class StringUtils {
      * StringUtils.abbreviate("abcdefg", 7) = "abcdefg"
      * StringUtils.abbreviate("abcdefg", 8) = "abcdefg"
      * StringUtils.abbreviate("abcdefg", 4) = "a..."
+     * StringUtils.abbreviate("😀abcdef", 4) = "..."
      * StringUtils.abbreviate("abcdefg", 3) = Throws {@link 
IllegalArgumentException}.
      * </pre>
      *
      * @param str      The String to check, may be null.
-     * @param maxWidth maximum length of result String, must be at least 4.
+     * @param maxWidth Maximum result length in UTF-16 code units, must be at 
least 4.
      * @return abbreviated String, {@code null} if null String input.
      * @throws IllegalArgumentException Thrown if the width is too small.
      * @since 2.0
@@ -258,11 +264,13 @@ public static String abbreviate(final String str, final 
int maxWidth) {
      * Abbreviates a String using ellipses. This will convert "Now is the time 
for all good men" into "...is the time for...".
      *
      * <p>
-     * Works like {@code abbreviate(String, int)}, but allows you to specify a 
"left edge" offset. Note that this left edge is not necessarily going to be the
-     * leftmost character in the result, or the first character following the 
ellipses, but it will appear somewhere in the result.
+     * Works like {@link #abbreviate(String, int)}, but allows an offset to 
influence the portion of the source String that is retained.
+     * The character at the offset is not guaranteed to appear in the result.
      * </p>
+     *
      * <p>
-     * In no case will it return a String of length greater than {@code 
maxWidth}.
+     * The offset and maximum width are measured in UTF-16 code units. If a 
cut would split a surrogate pair, the entire pair is omitted.
+     * An abbreviated result never exceeds {@code maxWidth}, but can be 
shorter.
      * </p>
      *
      * <pre>
@@ -273,6 +281,7 @@ public static String abbreviate(final String str, final int 
maxWidth) {
      * StringUtils.abbreviate("abcdefghijklmno", 1, 10)  = "abcdefg..."
      * StringUtils.abbreviate("abcdefghijklmno", 4, 10)  = "abcdefg..."
      * StringUtils.abbreviate("abcdefghijklmno", 5, 10)  = "...fghi..."
+     * StringUtils.abbreviate("abcd😀efghijkl", 5, 7)    = "...e..."
      * StringUtils.abbreviate("abcdefghijklmno", 6, 10)  = "...ghij..."
      * StringUtils.abbreviate("abcdefghijklmno", 8, 10)  = "...ijklmno"
      * StringUtils.abbreviate("abcdefghijklmno", 10, 10) = "...ijklmno"
@@ -282,8 +291,9 @@ public static String abbreviate(final String str, final int 
maxWidth) {
      * </pre>
      *
      * @param str      The String to check, may be null.
-     * @param offset   left edge of source String.
-     * @param maxWidth maximum length of result String, must be at least 4.
+     * @param offset   An offset into the source String, in UTF-16 code units.
+     * @param maxWidth Maximum result length in UTF-16 code units, must be at 
least 4.
+     *                 When ellipses are needed at both ends, must be at least 
7.
      * @return abbreviated String, {@code null} if null String input.
      * @throws IllegalArgumentException Thrown if the width is too small.
      * @since 2.0
@@ -300,17 +310,24 @@ public static String abbreviate(final String str, final 
int offset, final int ma
      * Specifically:
      * </p>
      * <ul>
-     * <li>If the number of characters in {@code str} is less than or equal to 
{@code maxWidth}, return {@code str}.</li>
-     * <li>Else abbreviate it to {@code (substring(str, 0, max - 
abbrevMarker.length) + abbrevMarker)}.</li>
-     * <li>If {@code maxWidth} is less than {@code abbrevMarker.length + 1}, 
throw an {@link IllegalArgumentException}.</li>
-     * <li>In no case will it return a String of length greater than {@code 
maxWidth}.</li>
+     * <li>A {@code null} or empty String is returned unchanged.</li>
+     * <li>For a nonempty String, if {@code maxWidth} is less than the marker 
length plus one, throw an {@link IllegalArgumentException}.</li>
+     * <li>If the length of {@code str} is less than or equal to {@code 
maxWidth}, return {@code str}.</li>
+     * <li>Otherwise, retain at most {@code maxWidth} minus the marker length 
UTF-16 code units from the start of {@code str},
+     * followed by the replacement marker.</li>
      * </ul>
      *
+     * <p>
+     * Lengths are measured in UTF-16 code units. If the cut would split a 
surrogate pair, the entire pair is omitted, so the result can be shorter than
+     * {@code maxWidth}.
+     * </p>
+     *
      * <pre>
      * StringUtils.abbreviate(null, "...", *)      = null
      * StringUtils.abbreviate("abcdefg", null, 4)  = "abcd"
      * StringUtils.abbreviate("", "...", 4)        = ""
      * StringUtils.abbreviate("abcdefg", ".", 5)   = "abcd."
+     * StringUtils.abbreviate("😀abc", ".", 2)     = "."
      * StringUtils.abbreviate("abcdefg", ".", 7)   = "abcdefg"
      * StringUtils.abbreviate("abcdefg", ".", 8)   = "abcdefg"
      * StringUtils.abbreviate("abcdefg", "..", 4)  = "ab.."
@@ -321,7 +338,7 @@ public static String abbreviate(final String str, final int 
offset, final int ma
      *
      * @param str          The String to check, may be null.
      * @param abbrevMarker The String used as replacement marker, null is 
treated as an empty marker.
-     * @param maxWidth     maximum length of result String, must be at least 
the marker length plus one (one for a null or empty marker).
+     * @param maxWidth     Maximum result length in UTF-16 code units, must be 
at least the marker length plus one (one for a null or empty marker).
      * @return abbreviated String, {@code null} if null String input.
      * @throws IllegalArgumentException Thrown if the width is too small.
      * @since 3.6
@@ -333,12 +350,15 @@ public static String abbreviate(final String str, final 
String abbrevMarker, fin
     /**
      * Abbreviates a String using a given replacement marker. This will 
convert "Now is the time for all good men" into "...is the time for..." when 
"..." is
      * the replacement marker.
+     *
      * <p>
-     * Works like {@code abbreviate(String, String, int)}, but allows you to 
specify a "left edge" offset. Note that this left edge is not necessarily going 
to
-     * be the leftmost character in the result, or the first character 
following the replacement marker, but it will appear somewhere in the result.
+     * Works like {@link #abbreviate(String, String, int)}, but allows an 
offset to influence the portion of the source String that is retained.
+     * The character at the offset is not guaranteed to appear in the result.
      * </p>
+     *
      * <p>
-     * In no case will it return a String of length greater than {@code 
maxWidth}.
+     * The offset and maximum width are measured in UTF-16 code units. If a 
cut would split a surrogate pair, the entire pair is omitted.
+     * An abbreviated result never exceeds {@code maxWidth}, but can be 
shorter.
      * </p>
      *
      * <pre>
@@ -351,6 +371,7 @@ public static String abbreviate(final String str, final 
String abbrevMarker, fin
      * StringUtils.abbreviate("abcdefghijklmno", ",", 2, 10)    = "abcdefghi,"
      * StringUtils.abbreviate("abcdefghijklmno", "::", 4, 10)   = "::efghij::"
      * StringUtils.abbreviate("abcdefghijklmno", "...", 6, 10)  = "...ghij..."
+     * StringUtils.abbreviate("abcd😀efghijkl", ".", 5, 3)      = ".e."
      * StringUtils.abbreviate("abcdefghijklmno", "…", 6, 10)    = "…ghijklmno"
      * StringUtils.abbreviate("abcdefghijklmno", "*", 9, 10)    = "*ghijklmno"
      * StringUtils.abbreviate("abcdefghijklmno", "'", 10, 10)   = "'ghijklmno"
@@ -362,8 +383,8 @@ public static String abbreviate(final String str, final 
String abbrevMarker, fin
      * @param str          The String to check, may be null.
      * @param abbrevMarker The String used as replacement marker, for example 
"...", or Unicode HORIZONTAL ELLIPSIS, U+2026 '…';
      *                     null is treated as an empty marker.
-     * @param offset       left edge of source String.
-     * @param maxWidth     maximum length of result String, must be at least 
the marker length plus one (one for a null or empty marker).
+     * @param offset       An offset into the source String, in UTF-16 code 
units.
+     * @param maxWidth     Maximum result length in UTF-16 code units, must be 
at least the marker length plus one (one for a null or empty marker).
      *                     When markers are needed at both ends, must be at 
least twice the marker length plus one.
      * @return abbreviated String, {@code null} if null String input.
      * @throws IllegalArgumentException Thrown if the width is too small.
@@ -684,9 +705,8 @@ public static String center(String str, final int size, 
final char padChar) {
      *
      * @param str    The String to center, may be null.
      * @param size   The int size of new String, negative treated as zero.
-     * @param padStr The String to pad the new String with, must not be null 
or empty.
+     * @param padStr The String to pad the new String with, null or empty 
treated as a single space.
      * @return centered String, {@code null} if null String input.
-     * @throws IllegalArgumentException Thrown if padStr is {@code null} or 
empty.
      */
     public static String center(String str, final int size, String padStr) {
         if (str == null || size <= 0) {
@@ -5173,11 +5193,16 @@ public static int lastOrdinalIndexOf(final CharSequence 
str, final CharSequence
     }
 
     /**
-     * Gets the leftmost {@code len} characters of a String.
+     * Gets up to {@code len} UTF-16 code units from the start of a String.
      *
      * <p>
-     * If {@code len} characters are not available, or the String is {@code 
null}, the String will be returned without an exception. An empty String is 
returned
-     * if len is negative.
+     * If {@code len} UTF-16 code units are not available, or the String is 
{@code null}, the String is returned unchanged. An empty String is returned
+     * if {@code len} is negative.
+     * </p>
+     *
+     * <p>
+     * If the requested boundary splits a surrogate pair, the entire pair is 
omitted. The result can therefore be shorter than {@code len},
+     * even when the input String contains enough UTF-16 code units.
      * </p>
      *
      * <pre>
@@ -5187,10 +5212,11 @@ public static int lastOrdinalIndexOf(final CharSequence 
str, final CharSequence
      * StringUtils.left("abc", 0)   = ""
      * StringUtils.left("abc", 2)   = "ab"
      * StringUtils.left("abc", 4)   = "abc"
+     * StringUtils.left("😀", 1)    = ""
      * </pre>
      *
      * @param str The String to get the leftmost characters from, may be null.
-     * @param len The length of the required String.
+     * @param len The maximum number of UTF-16 code units to return.
      * @return The leftmost characters, {@code null} if null String input.
      */
     public static String left(final String str, final int len) {
@@ -5458,11 +5484,16 @@ private static int[] matches(final CharSequence first, 
final CharSequence second
     }
 
     /**
-     * Gets {@code len} characters from the middle of a String.
+     * Gets up to {@code len} UTF-16 code units from the middle of a String.
      *
      * <p>
-     * If {@code len} characters are not available, the remainder of the 
String will be returned without an exception. If the String is {@code null},
-     * {@code null} will be returned. An empty String is returned if len is 
negative or exceeds the length of {@code str}.
+     * A {@code null} String returns {@code null}. A negative {@code pos} is 
treated as zero. An empty String is returned if {@code len} is negative
+     * or {@code pos} is at or beyond the end of {@code str}. If the requested 
range extends past the end, it is limited to the remaining String.
+     * </p>
+     *
+     * <p>
+     * The position and length are measured in UTF-16 code units. If either 
boundary of the requested range splits a surrogate pair, the entire pair
+     * is omitted. The requested range is determined before adjusting either 
boundary, so the result can be shorter than {@code len}.
      * </p>
      *
      * <pre>
@@ -5474,11 +5505,12 @@ private static int[] matches(final CharSequence first, 
final CharSequence second
      * StringUtils.mid("abc", 2, 4)   = "c"
      * StringUtils.mid("abc", 4, 2)   = ""
      * StringUtils.mid("abc", -2, 2)  = "ab"
+     * StringUtils.mid("a😀b", 2, 2)  = "b"
      * </pre>
      *
      * @param str The String to get the characters from, may be null.
-     * @param pos The position to start from, negative treated as zero.
-     * @param len The length of the required String.
+     * @param pos The position to start from, in UTF-16 code units, negative 
treated as zero.
+     * @param len The maximum number of UTF-16 code units to return.
      * @return The middle characters, {@code null} if null String input.
      */
     public static String mid(final String str, int pos, final int len) {
@@ -5693,6 +5725,12 @@ private static int ordinalIndexOf(final CharSequence 
str, final CharSequence sea
      * length. The start index is always the smaller of the two indices.
      * </p>
      *
+     * <p>
+     * Indices are measured in UTF-16 code units. After the indices are 
limited to the String length and ordered, a start index inside a surrogate pair
+     * is moved to the start of the pair, and an end index inside a surrogate 
pair is moved past the pair. Consequently, equal start and end indices
+     * inside a surrogate pair replace the entire pair.
+     * </p>
+     *
      * <pre>
      * StringUtils.overlay(null, *, *, *)            = null
      * StringUtils.overlay("", "abc", 0, 0)          = "abc"
@@ -5705,12 +5743,14 @@ private static int ordinalIndexOf(final CharSequence 
str, final CharSequence sea
      * StringUtils.overlay("abcdef", "zzzz", 2, 8)   = "abzzzz"
      * StringUtils.overlay("abcdef", "zzzz", -2, -3) = "zzzzabcdef"
      * StringUtils.overlay("abcdef", "zzzz", 8, 10)  = "abcdefzzzz"
+     * StringUtils.overlay("a😀b", "X", 2, 2)       = "aXb"
+     * StringUtils.overlay("a😀b", "", 2, 2)        = "ab"
      * </pre>
      *
      * @param str     The String to do overlaying in, may be null.
-     * @param overlay The String to overlay, may be null.
-     * @param start   The position to start overlaying at.
-     * @param end     The position to stop overlaying before.
+     * @param overlay The String to overlay, null treated as empty.
+     * @param start   The position to start overlaying at, in UTF-16 code 
units.
+     * @param end     The position to stop overlaying before, in UTF-16 code 
units.
      * @return overlaid String, {@code null} if null String input.
      * @since 2.0
      */
@@ -7011,11 +7051,16 @@ public static String reverseDelimited(final String str, 
final char separatorChar
     }
 
     /**
-     * Gets the rightmost {@code len} characters of a String.
+     * Gets up to {@code len} UTF-16 code units from the end of a String.
+     *
+     * <p>
+     * If {@code len} UTF-16 code units are not available, or the String is 
{@code null}, the String is returned unchanged. An empty String is returned
+     * if {@code len} is negative.
+     * </p>
      *
      * <p>
-     * If {@code len} characters are not available, or the String is {@code 
null}, the String will be returned without an exception. An empty String is
-     * returned if len is negative.
+     * If the requested boundary splits a surrogate pair, the entire pair is 
omitted. The result can therefore be shorter than {@code len},
+     * even when the input String contains enough UTF-16 code units.
      * </p>
      *
      * <pre>
@@ -7025,10 +7070,11 @@ public static String reverseDelimited(final String str, 
final char separatorChar
      * StringUtils.right("abc", 0)   = ""
      * StringUtils.right("abc", 2)   = "bc"
      * StringUtils.right("abc", 4)   = "abc"
+     * StringUtils.right("😀", 1)    = ""
      * </pre>
      *
      * @param str The String to get the rightmost characters from, may be null.
-     * @param len The length of the required String.
+     * @param len The maximum number of UTF-16 code units to return.
      * @return The rightmost characters, {@code null} if null String input.
      */
     public static String right(final String str, final int len) {
@@ -9062,17 +9108,23 @@ public static String trimToNull(final String str) {
      * Specifically:
      * </p>
      * <ul>
-     * <li>If {@code str} is less than {@code maxWidth} characters long, 
return it.</li>
-     * <li>Else truncate it to {@code substring(str, 0, maxWidth)}.</li>
-     * <li>If {@code maxWidth} is less than {@code 0}, throw an {@link 
IllegalArgumentException}.</li>
-     * <li>In no case will it return a String of length greater than {@code 
maxWidth}.</li>
+     * <li>If {@code maxWidth} is negative, throw an {@link 
IllegalArgumentException}.</li>
+     * <li>A {@code null} String returns {@code null}.</li>
+     * <li>If the length of {@code str} is less than or equal to {@code 
maxWidth}, return it.</li>
+     * <li>Otherwise, retain at most {@code maxWidth} UTF-16 code units from 
the start of {@code str}.</li>
      * </ul>
      *
+     * <p>
+     * Lengths are measured in UTF-16 code units. If the cut would split a 
surrogate pair, the entire pair is omitted, so the result can be shorter than
+     * {@code maxWidth}.
+     * </p>
+     *
      * <pre>
      * StringUtils.truncate(null, 0)       = null
      * StringUtils.truncate(null, 2)       = null
      * StringUtils.truncate("", 4)         = ""
      * StringUtils.truncate("abcdefg", 4)  = "abcd"
+     * StringUtils.truncate("a😀b", 2)     = "a"
      * StringUtils.truncate("abcdefg", 6)  = "abcdef"
      * StringUtils.truncate("abcdefg", 7)  = "abcdefg"
      * StringUtils.truncate("abcdefg", 8)  = "abcdefg"
@@ -9080,7 +9132,7 @@ public static String trimToNull(final String str) {
      * </pre>
      *
      * @param str      The String to truncate, may be null.
-     * @param maxWidth maximum length of result String, must be non-negative.
+     * @param maxWidth The maximum result length in UTF-16 code units, must be 
non-negative.
      * @return truncated String, {@code null} if null String input.
      * @throws IllegalArgumentException Thrown if {@code maxWidth} is less 
than {@code 0}.
      * @since 3.5
@@ -9100,19 +9152,26 @@ public static String truncate(final String str, final 
int maxWidth) {
      * Specifically:
      * </p>
      * <ul>
-     * <li>If {@code str} is less than {@code maxWidth} characters long, 
return it.</li>
-     * <li>Else truncate it to {@code substring(str, offset, maxWidth)}.</li>
-     * <li>If {@code maxWidth} is less than {@code 0}, throw an {@link 
IllegalArgumentException}.</li>
-     * <li>If {@code offset} is less than {@code 0}, throw an {@link 
IllegalArgumentException}.</li>
-     * <li>In no case will it return a String of length greater than {@code 
maxWidth}.</li>
+     * <li>If {@code offset} or {@code maxWidth} is negative, throw an {@link 
IllegalArgumentException}.</li>
+     * <li>A {@code null} String returns {@code null}.</li>
+     * <li>If {@code offset} is at or beyond the end of {@code str}, return an 
empty String.</li>
+     * <li>Otherwise, take the range starting at {@code offset} and spanning 
at most {@code maxWidth} UTF-16 code units, stopping at the end of the 
String.</li>
      * </ul>
      *
+     * <p>
+     * The offset and maximum width are measured in UTF-16 code units. If 
either boundary of the requested range splits a surrogate pair, the entire
+     * pair is omitted. The requested range is determined before adjusting 
either boundary, so the result can be shorter than {@code maxWidth}.
+     * </p>
+     *
      * <pre>
      * StringUtils.truncate(null, 0, 0) = null
      * StringUtils.truncate(null, 2, 4) = null
      * StringUtils.truncate("", 0, 10) = ""
      * StringUtils.truncate("", 2, 10) = ""
      * StringUtils.truncate("abcdefghij", 0, 3) = "abc"
+     * StringUtils.truncate("abcdef", 2, 3) = "cde"
+     * StringUtils.truncate("abc", 1, 10) = "bc"
+     * StringUtils.truncate("😀ab", 1, 2) = "a"
      * StringUtils.truncate("abcdefghij", 5, 6) = "fghij"
      * StringUtils.truncate("raspberry peach", 10, 15) = "peach"
      * StringUtils.truncate("abcdefghijklmno", 0, 10) = "abcdefghij"
@@ -9141,8 +9200,8 @@ public static String truncate(final String str, final int 
maxWidth) {
      * </pre>
      *
      * @param str      The String to truncate, may be null.
-     * @param offset   left edge of source String.
-     * @param maxWidth maximum length of result String, must be non-negative.
+     * @param offset   The position to start from, in UTF-16 code units, must 
be non-negative.
+     * @param maxWidth The maximum result length in UTF-16 code units, must be 
non-negative.
      * @return truncated String, {@code null} if null String input.
      * @throws IllegalArgumentException Thrown if {@code offset} or {@code 
maxWidth} is less than {@code 0}.
      * @since 3.5
diff --git a/src/main/java/org/apache/commons/lang3/text/WordUtils.java 
b/src/main/java/org/apache/commons/lang3/text/WordUtils.java
index 28f16953b..e97827b74 100644
--- a/src/main/java/org/apache/commons/lang3/text/WordUtils.java
+++ b/src/main/java/org/apache/commons/lang3/text/WordUtils.java
@@ -518,7 +518,7 @@ public static String uncapitalize(final String str, final 
char... delimiters) {
      * (assuming that '\n' is the systems line separator)
      *
      * @param str  The String to be word wrapped, may be null.
-     * @param wrapLength  The column to wrap the words at, less than 1 is 
treated as 1.
+     * @param wrapLength  The target line length in UTF-16 code units, less 
than 1 is treated as 1.
      * @return A line with newlines inserted, {@code null} if null input.
      */
     public static String wrap(final String str, final int wrapLength) {
@@ -533,6 +533,11 @@ public static String wrap(final String str, final int 
wrapLength) {
      * Trailing spaces are not stripped.
      * </p>
      *
+     * <p>
+     * When {@code wrapLongWords} is true, a cut within a long word that would 
split a surrogate pair is moved past the pair. That line may contain
+     * one more UTF-16 code unit than {@code wrapLength}. When {@code 
wrapLongWords} is false, long words may exceed the requested line length.
+     * </p>
+     *
      * <table border="1">
      *  <caption>Examples</caption>
      *  <tr>
@@ -589,12 +594,12 @@ public static String wrap(final String str, final int 
wrapLength) {
      *   <td>20</td>
      *   <td>"\n"</td>
      *   <td>true</td>
-     *   <td>"Click here to jump\nto the commons\nwebsite 
-\nhttps://commons.apach\ne.org";</td>
+     *   <td>"Click here to jump\nto the commons\nwebsite 
-\nhttps://commons.apac\nhe.org";</td>
      *  </tr>
      * </table>
      *
      * @param str  The String to be word wrapped, may be null.
-     * @param wrapLength  The column to wrap the words at, less than 1 is 
treated as 1.
+     * @param wrapLength  The target line length in UTF-16 code units, less 
than 1 is treated as 1.
      * @param newLineStr  The string to insert for a new line,
      *  {@code null} uses the system property line separator.
      * @param wrapLongWords  true if long words (such as URLs) should be 
wrapped.
@@ -612,6 +617,11 @@ public static String wrap(final String str, final int 
wrapLength, final String n
      * Trailing spaces are not stripped.
      * </p>
      *
+     * <p>
+     * When {@code wrapLongWords} is true, a cut within a long word that would 
split a surrogate pair is moved past the pair. That line may contain
+     * one more UTF-16 code unit than {@code wrapLength}. When {@code 
wrapLongWords} is false, long words may exceed the requested line length.
+     * </p>
+     *
      * <table border="1">
      *  <caption>Examples</caption>
      *  <tr>
@@ -676,7 +686,7 @@ public static String wrap(final String str, final int 
wrapLength, final String n
      *   <td>"\n"</td>
      *   <td>true</td>
      *   <td>" "</td>
-     *   <td>"Click here to jump\nto the commons\nwebsite 
-\nhttps://commons.apach\ne.org";</td>
+     *   <td>"Click here to jump\nto the commons\nwebsite 
-\nhttps://commons.apac\nhe.org";</td>
      *  </tr>
      *  <tr>
      *   <td>"flammable/inflammable"</td>
@@ -689,7 +699,7 @@ public static String wrap(final String str, final int 
wrapLength, final String n
      * </table>
      *
      * @param str  The String to be word wrapped, may be null.
-     * @param wrapLength  The column to wrap the words at, less than 1 is 
treated as 1.
+     * @param wrapLength  The target line length in UTF-16 code units, less 
than 1 is treated as 1.
      * @param newLineStr  The string to insert for a new line,
      *  {@code null} uses the system property line separator.
      * @param wrapLongWords  true if long words (such as URLs) should be 
wrapped.
diff --git 
a/src/test/java/org/apache/commons/lang3/StringUtilsAbbreviateTest.java 
b/src/test/java/org/apache/commons/lang3/StringUtilsAbbreviateTest.java
index 99503e363..dcd44d9c3 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsAbbreviateTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsAbbreviateTest.java
@@ -104,6 +104,8 @@ void testAbbreviate_StringIntInt() {
         assertAbbreviateWithOffset("...ijklmno", 15, 10);
         assertAbbreviateWithOffset("...ijklmno", 16, 10);
         assertAbbreviateWithOffset("...ijklmno", Integer.MAX_VALUE, 10);
+        // A narrow result need not include the character at the requested 
offset.
+        assertEquals("a...", StringUtils.abbreviate("abcdef", 4, 4));
         // abbreviating a shorter string allows maxWidth < 7
         assertEquals("...efg", StringUtils.abbreviate("abcdefg", 5, 6));
     }
@@ -169,6 +171,8 @@ void testAbbreviate_StringStringIntInt() {
         assertAbbreviateWithAbbrevMarkerAndOffset("999ijklmno", "999", 15, 10);
         assertAbbreviateWithAbbrevMarkerAndOffset("_ghijklmno", "_", 16, 10);
         assertAbbreviateWithAbbrevMarkerAndOffset("+ghijklmno", "+", 
Integer.MAX_VALUE, 10);
+        // A narrow result need not include the character at the requested 
offset.
+        assertEquals("a.", StringUtils.abbreviate("abcdef", ".", 2, 2));
         // abbreviating a shorter string allows maxWidth < abbrevMarker.length 
* 2 + 1
         assertEquals("..de", StringUtils.abbreviate("abcde", "..", 4, 4));
         assertEquals("....fg", StringUtils.abbreviate("abcdefg", "....", 5, 
6));
@@ -262,6 +266,10 @@ void testAbbreviateSurrogatePair() {
         // the head cut backs off the pair so the marker is never preceded by 
a lone high surrogate
         assertEquals("...", StringUtils.abbreviate(grin + "abcdef", 4));
         assertEquals(grin + "...", StringUtils.abbreviate(grin + "abcdef", 5));
+        assertEquals(".", StringUtils.abbreviate(grin + "abc", ".", 2));
+        // A cut starting at an offset inside a surrogate pair skips the 
entire pair.
+        assertEquals("...e...", StringUtils.abbreviate("abcd" + grin + 
"efghijkl", 5, 7));
+        assertEquals(".e.", StringUtils.abbreviate("abcd" + grin + "efghijkl", 
".", 5, 3));
         // a trailing supplementary code point is kept whole rather than 
sliced into a lone low surrogate
         assertEquals("..." + grin, StringUtils.abbreviate("abcdef" + grin, 6, 
5));
         // an input that is only the pair is kept whole or dropped, never 
split into a lone surrogate
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java 
b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
index 36d8bbfec..10848f595 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
@@ -1576,6 +1576,9 @@ void testOverlaySurrogatePair() {
         assertEquals("X", StringUtils.overlay(grin, "X", 1, 1));
         assertEquals("aXb", StringUtils.overlay("a" + grin + "b", "X", 1, 3));
         assertEquals("aXb", StringUtils.overlay("a" + grin + "b", "X", 2, 2));
+        // An empty overlay still removes the pair when both indices fall 
inside it.
+        assertEquals("ab", StringUtils.overlay("a" + grin + "b", "", 2, 2));
+        assertEquals("ab", StringUtils.overlay("a" + grin + "b", null, 2, 2));
 
         final String source = "a" + grin + "b" + grin + "cd" + grin + "ef";
         for (int start = 0; start <= source.length(); start++) {
@@ -3356,6 +3359,7 @@ void testTruncate_StringIntInt_surrogatePair() {
         // U+1F600 GRINNING FACE is a single supplementary code point stored 
as a surrogate pair
         final String grin = "😀";
         // a cut that would land between the two halves keeps the result well 
formed instead of emitting a lone surrogate
+        assertEquals("a", StringUtils.truncate("a" + grin + "b", 2));
         assertEquals("a", StringUtils.truncate("a" + grin + "b", 0, 2));
         assertEquals(grin, StringUtils.truncate("a" + grin + "b", 1, 2));
         assertEquals("ab", StringUtils.truncate("ab" + grin, 0, 3));

Reply via email to