From f9b486a17171359e880bfa91a14602fe95a5dbf8 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Sun, 13 Oct 2013 09:02:07 +0000 Subject: [PATCH] Adding 00A0 to the list of whitespace characters per LANG-910 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1531643 13f79535-47bb-0310-9956-ffa450edef68 --- src/changes/changes.xml | 1 + src/main/java/org/apache/commons/lang3/StringUtils.java | 2 +- .../java/org/apache/commons/lang3/StringUtilsTest.java | 8 ++++++-- 3 files changed, 8 insertions(+), 3 deletions(-) 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