Skip to content

Add StringUtils.truncateToByteLength #1392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/main/java/org/apache/commons/lang3/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8872,6 +8872,36 @@ public static String truncate(final String str, final int maxWidth) {
return truncate(str, 0, maxWidth);
}

public static String truncateToByteLength(String str, int maxBytes, Charset charset) {
if (str == null) {
return null;
}

byte[] bytes = StringUtils.getBytes(str, charset);
if (bytes.length <= maxBytes) {
return str;
}

// Binary search or iterative approach to find the right character length
int low = 0;
int high = str.codePointCount(0, str.length());
int count = 0;
while (low <= high) {
int mid = low + (high - low) / 2;
int charIndex = str.offsetByCodePoints(0, mid);
byte[] currentBytes = StringUtils.getBytes(str.substring(0, charIndex), charset);
if (currentBytes.length <= maxBytes) {
low = mid + 1;
count = mid;
} else {
high = mid - 1;
}
}

int idx = str.offsetByCodePoints(0, count);
return str.substring(0, idx);
}

/**
* Truncates a String. This will turn
* "Now is the time for all good men" into "is the time for all".
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/apache/commons/lang3/StringUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3089,6 +3089,21 @@ public void testTruncate_StringIntInt() {
assertEquals("", StringUtils.truncate("abcdefghijklmno", Integer.MAX_VALUE, Integer.MAX_VALUE));
}

@Test
public void testTruncateToByteLength() {
assertNull(StringUtils.truncateToByteLength(null, 0, Charset.defaultCharset()));
assertEquals("abcdefghij", StringUtils.truncateToByteLength("abcdefghijklmno", 10, Charset.defaultCharset()));
assertEquals("abcdefghijklmno", StringUtils.truncateToByteLength("abcdefghijklmno", 15, Charset.defaultCharset()));
assertEquals("abcdefghijklmno", StringUtils.truncateToByteLength("abcdefghijklmno", 20, Charset.defaultCharset()));
assertEquals("\u4F60\u597D\u55CE", StringUtils.truncateToByteLength("\u4F60\u597D\u55CE", 10, Charset.defaultCharset()));
assertEquals("\u4F60", StringUtils.truncateToByteLength("\u4F60\u597D\u55CE", 5, Charset.defaultCharset()));
assertEquals("\u2713\u2714", StringUtils.truncateToByteLength("\u2713\u2714", 6, Charset.defaultCharset()));
assertEquals("", StringUtils.truncateToByteLength("\u2713\u2714", 2, Charset.defaultCharset()));
assertEquals("\uD83D\uDE80", StringUtils.truncateToByteLength("\uD83D\uDE80\u2728\uD83C\uDF89", 6, Charset.defaultCharset()));
assertEquals("", StringUtils.truncateToByteLength("\uD83D\uDE80\u2728\uD83C\uDF89", 3, Charset.defaultCharset()));
assertEquals("", StringUtils.truncateToByteLength("\uD83D\uDE03", 3, Charset.defaultCharset()));
}

@Test
public void testUnCapitalize() {
assertNull(StringUtils.uncapitalize(null));
Expand Down