diff --git a/src/changes/changes.xml b/src/changes/changes.xml index ca90ceac9..6665d023d 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,7 @@ + StringUtils.normalizeSpace now handles non-breaking spaces (Unicode 00A0) Redundant check for zero in HashCodeBuilder ctor StrSubstitutor now supports default values for variables Adding .gitignore to commons-lang diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 935c2856c..7f0825d44 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -178,7 +178,7 @@ public class StringUtils { * single space, thus matching the same would likely cause a great * many noop replacements. */ - private static final Pattern WHITESPACE_PATTERN = Pattern.compile("(?: \\s|[\\s&&[^ ]])\\s*"); + private static final Pattern WHITESPACE_PATTERN = Pattern.compile("(?: |\\u00A0|\\s|[\\s&&[^ ]])\\s*"); /** *

{@code StringUtils} instances should NOT be constructed in diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java index 99952e575..ad8a198d7 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java @@ -47,11 +47,13 @@ public class StringUtilsTest { static final String WHITESPACE; static final String NON_WHITESPACE; + static final String HARD_SPACE; static final String TRIMMABLE; static final String NON_TRIMMABLE; static { String ws = ""; String nws = ""; + String hs = String.valueOf(((char) 160)); String tr = ""; String ntr = ""; for (int i = 0; i < Character.MAX_VALUE; i++) { @@ -69,6 +71,7 @@ public class StringUtilsTest { } WHITESPACE = ws; NON_WHITESPACE = nws; + HARD_SPACE = hs; TRIMMABLE = tr; NON_TRIMMABLE = ntr; } @@ -2119,14 +2122,14 @@ public class StringUtilsTest { assertEquals("", StringUtils.getCommonPrefix("xyz", "abcde")); assertEquals("i am a ", StringUtils.getCommonPrefix("i am a machine", "i am a robot")); } - + @Test public void testNormalizeSpace() { assertEquals(null, StringUtils.normalizeSpace(null)); assertEquals("", StringUtils.normalizeSpace("")); assertEquals("", StringUtils.normalizeSpace(" ")); assertEquals("", StringUtils.normalizeSpace("\t")); - assertEquals("", StringUtils.normalizeSpace("\n")); + assertEquals("", StringUtils.normalizeSpace("\n")); assertEquals("", StringUtils.normalizeSpace("\u0009")); assertEquals("", StringUtils.normalizeSpace("\u000B")); assertEquals("", StringUtils.normalizeSpace("\u000C")); @@ -2139,6 +2142,7 @@ public class StringUtilsTest { assertEquals("a", StringUtils.normalizeSpace(" a ")); assertEquals("a b c", StringUtils.normalizeSpace(" a b c ")); assertEquals("a b c", StringUtils.normalizeSpace("a\t\f\r b\u000B c\n")); + assertEquals("a b c", StringUtils.normalizeSpace("a\t\f\r " + HARD_SPACE + HARD_SPACE + "b\u000B c\n")); } @Test