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 0cd12b105 Rename new method and make it handle the DEL control
character. (#1689)
0cd12b105 is described below
commit 0cd12b10588b09895a370198d87303c8774205af
Author: Gary Gregory <[email protected]>
AuthorDate: Thu Jun 4 08:47:02 2026 -0400
Rename new method and make it handle the DEL control character. (#1689)
---
.../java/org/apache/commons/lang3/StringUtils.java | 7 ++-
.../apache/commons/lang3/StringUtilsTrimTest.java | 52 +++++++++++-----------
2 files changed, 30 insertions(+), 29 deletions(-)
diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java
b/src/main/java/org/apache/commons/lang3/StringUtils.java
index 74287ab0c..597345f50 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -8796,17 +8796,16 @@ public static String trim(final String str) {
* @return the trimmed string, {@code null} if null String input.
* @since 3.21.0
*/
- public static String trimControl(final String str) {
+ public static String trimAsciiControl(final String str) {
if (str == null) {
return null;
}
int len = str.length();
int st = 0;
-
- while (st < len && str.charAt(st) < ' ') {
+ while (st < len && CharUtils.isAsciiControl(str.charAt(st))) {
st++;
}
- while (st < len && str.charAt(len - 1) < ' ') {
+ while (st < len && CharUtils.isAsciiControl(str.charAt(len - 1))) {
len--;
}
return st > 0 || len < str.length() ? str.substring(st, len) : str;
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTrimTest.java
b/src/test/java/org/apache/commons/lang3/StringUtilsTrimTest.java
index 4e2380665..35de238df 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsTrimTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsTrimTest.java
@@ -30,42 +30,44 @@ class StringUtilsTrimTest extends AbstractLangTest {
private static final String FOO = "foo";
@Test
- void testTrim() {
- assertEquals(FOO, StringUtils.trim(FOO + " "));
- assertEquals(FOO, StringUtils.trim(" " + FOO + " "));
- assertEquals(FOO, StringUtils.trim(" " + FOO));
- assertEquals(FOO, StringUtils.trim(FOO + ""));
- assertEquals("", StringUtils.trim(" \t\r\n\b "));
- assertEquals("", StringUtils.trim(StringUtilsTest.TRIMMABLE));
- assertEquals(StringUtilsTest.NON_TRIMMABLE,
StringUtils.trim(StringUtilsTest.NON_TRIMMABLE));
- assertEquals("", StringUtils.trim(""));
- assertNull(StringUtils.trim(null));
- }
-
- @Test
- void testTrimControl() {
+ void testAsciiTrimControl() {
// null input returns null
- assertNull(StringUtils.trimControl(null));
+ assertNull(StringUtils.trimAsciiControl(null));
+ // empty string stays empty
+ assertEquals("", StringUtils.trimAsciiControl(""));
// empty string stays empty
- assertEquals("", StringUtils.trimControl(""));
+ assertEquals("", StringUtils.trimAsciiControl("\u007f"));
// no control chars: unchanged
- assertEquals(FOO, StringUtils.trimControl(FOO));
+ assertEquals(FOO, StringUtils.trimAsciiControl(FOO));
// spaces are NOT stripped (space is char 32, not a control char)
- assertEquals(" " + FOO + " ", StringUtils.trimControl(" " + FOO + "
"));
+ assertEquals(" " + FOO + " ", StringUtils.trimAsciiControl(" " + FOO +
" "));
// trailing NUL (\u0000) is stripped
- assertEquals(FOO, StringUtils.trimControl(FOO + "\u0000"));
+ assertEquals(FOO, StringUtils.trimAsciiControl(FOO + "\u0000"));
// leading NUL (\u0000) is stripped
- assertEquals(FOO, StringUtils.trimControl("\u0000" + FOO));
+ assertEquals(FOO, StringUtils.trimAsciiControl("\u0000" + FOO));
// control chars on both ends are stripped
- assertEquals(FOO, StringUtils.trimControl("\t\r\n" + FOO + "\t\r\n"));
+ assertEquals(FOO, StringUtils.trimAsciiControl("\t\r\n" + FOO +
"\t\r\n"));
// only control chars becomes empty string
- assertEquals("", StringUtils.trimControl("\u0001\u0002\u001F"));
+ assertEquals("", StringUtils.trimAsciiControl("\u0001\u0002\u001F"));
// embedded control chars are NOT stripped
- assertEquals("a\u0001b", StringUtils.trimControl("a\u0001b"));
+ assertEquals("a\u0001b", StringUtils.trimAsciiControl("a\u0001b"));
// space in the middle preserved, control chars on ends stripped
- assertEquals(" " + FOO + " ", StringUtils.trimControl("\u0001 " + FOO
+ " \u0001"));
+ assertEquals(" " + FOO + " ", StringUtils.trimAsciiControl("\u0001 " +
FOO + " \u0001"));
// char 31 (unit separator) is stripped; char 32 (space) is not
- assertEquals(" abc ", StringUtils.trimControl("\u001F abc \u001F"));
+ assertEquals(" abc ", StringUtils.trimAsciiControl("\u001F abc
\u001F"));
+ }
+
+ @Test
+ void testTrim() {
+ assertEquals(FOO, StringUtils.trim(FOO + " "));
+ assertEquals(FOO, StringUtils.trim(" " + FOO + " "));
+ assertEquals(FOO, StringUtils.trim(" " + FOO));
+ assertEquals(FOO, StringUtils.trim(FOO + ""));
+ assertEquals("", StringUtils.trim(" \t\r\n\b "));
+ assertEquals("", StringUtils.trim(StringUtilsTest.TRIMMABLE));
+ assertEquals(StringUtilsTest.NON_TRIMMABLE,
StringUtils.trim(StringUtilsTest.NON_TRIMMABLE));
+ assertEquals("", StringUtils.trim(""));
+ assertNull(StringUtils.trim(null));
}
@Test