Skip to content
Merged
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
6 changes: 5 additions & 1 deletion src/main/java/org/apache/commons/lang3/text/WordUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public static String initials(final String str, final char... delimiters) {
return StringUtils.EMPTY;
}
final int strLen = str.length();
final char[] buf = new char[strLen / 2 + 1];
final char[] buf = new char[strLen];
int count = 0;
boolean lastWasGap = true;
for (int i = 0; i < strLen; i++) {
Expand All @@ -276,6 +276,10 @@ public static String initials(final String str, final char... delimiters) {
}
if (lastWasGap) {
buf[count++] = ch;
// keep a supplementary code point's low surrogate with its high half
if (Character.isHighSurrogate(ch) && i + 1 < strLen && Character.isLowSurrogate(str.charAt(i + 1))) {
buf[count++] = str.charAt(++i);
}
lastWasGap = false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ void testInitials_String() {
assertEquals("iah1", WordUtils.initials("i am here 123"));
}

@Test
void testInitials_SupplementaryCodePoint() {
final String emoji = new String(Character.toChars(0x1F600));
assertEquals("B" + emoji + "L", WordUtils.initials("Ben " + emoji + "mile Lee"));
assertEquals(emoji, WordUtils.initials(emoji + "abc"));
assertEquals("B" + emoji + "L", WordUtils.initials("Ben." + emoji + "mile.Lee", '.'));
}

@Test
void testInitials_String_charArray() {
char[] array = null;
Expand Down
Loading