From ad648cf8a8a90bdee129266ca7b686a5b9a87561 Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Wed, 7 Jun 2017 09:54:34 +0200 Subject: [PATCH] LANG-1336: Add NUL Byte To CharUtils. Thanks to Beluga Behr. --- src/changes/changes.xml | 1 + src/main/java/org/apache/commons/lang3/CharUtils.java | 6 ++++++ .../java/org/apache/commons/lang3/StringUtils.java | 6 +++--- .../java/org/apache/commons/lang3/text/StrBuilder.java | 3 ++- .../java/org/apache/commons/lang3/StringUtilsTest.java | 10 +++++----- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 4496b9b2a..b005ab630 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -46,6 +46,7 @@ The type attribute can be add,update,fix,remove. + Add NUL Byte To CharUtils Fix test failures in IBM JDK 8 for ToStringBuilderTest Add method in StringUtils to determine if string contains both mixed cased characters Deprecate CharEncoding in favour of java.nio.charset.StandardCharsets diff --git a/src/main/java/org/apache/commons/lang3/CharUtils.java b/src/main/java/org/apache/commons/lang3/CharUtils.java index f697c086b..51b347a96 100644 --- a/src/main/java/org/apache/commons/lang3/CharUtils.java +++ b/src/main/java/org/apache/commons/lang3/CharUtils.java @@ -50,6 +50,12 @@ public class CharUtils { */ public static final char CR = '\r'; + /** + * {@code \u0000} null control character ('\0'), abbreviated NUL. + * + * @since 3.6 + */ + public static final char NUL = '\0'; static { for (char c = 0; c < CHAR_STRING_ARRAY.length; c++) { diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index f98433a43..97a073f6d 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -9009,7 +9009,7 @@ public class StringUtils { */ public static String wrap(final String str, final char wrapWith) { - if (isEmpty(str) || wrapWith == '\0') { + if (isEmpty(str) || wrapWith == CharUtils.NUL) { return str; } @@ -9080,7 +9080,7 @@ public class StringUtils { * @since 3.5 */ public static String wrapIfMissing(final String str, final char wrapWith) { - if (isEmpty(str) || wrapWith == '\0') { + if (isEmpty(str) || wrapWith == CharUtils.NUL) { return str; } final StringBuilder builder = new StringBuilder(str.length() + 2); @@ -9205,7 +9205,7 @@ public class StringUtils { * @since 3.6 */ public static String unwrap(final String str, final char wrapChar) { - if (isEmpty(str) || wrapChar == '\0') { + if (isEmpty(str) || wrapChar == CharUtils.NUL) { return str; } diff --git a/src/main/java/org/apache/commons/lang3/text/StrBuilder.java b/src/main/java/org/apache/commons/lang3/text/StrBuilder.java index 4e69cd5d3..cf42495fd 100644 --- a/src/main/java/org/apache/commons/lang3/text/StrBuilder.java +++ b/src/main/java/org/apache/commons/lang3/text/StrBuilder.java @@ -26,6 +26,7 @@ import java.util.List; import java.util.Objects; import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.lang3.CharUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.builder.Builder; @@ -213,7 +214,7 @@ public class StrBuilder implements CharSequence, Appendable, Serializable, Build final int newEnd = length; size = length; for (int i = oldEnd; i < newEnd; i++) { - buffer[i] = '\0'; + buffer[i] = CharUtils.NUL; } } return this; diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java index aa9ec73a1..615030ecc 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java @@ -3106,10 +3106,10 @@ public class StringUtilsTest { @Test public void testWrap_StringChar() { - assertNull(StringUtils.wrap(null, '\0')); + assertNull(StringUtils.wrap(null, CharUtils.NUL)); assertNull(StringUtils.wrap(null, '1')); - assertEquals("", StringUtils.wrap("", '\0')); + assertEquals("", StringUtils.wrap("", CharUtils.NUL)); assertEquals("xabx", StringUtils.wrap("ab", 'x')); assertEquals("\"ab\"", StringUtils.wrap("ab", '\"')); assertEquals("\"\"ab\"\"", StringUtils.wrap("\"ab\"", '\"')); @@ -3121,10 +3121,10 @@ public class StringUtilsTest { @Test public void testWrapIfMissing_StringChar() { - assertNull(StringUtils.wrapIfMissing(null, '\0')); + assertNull(StringUtils.wrapIfMissing(null, CharUtils.NUL)); assertNull(StringUtils.wrapIfMissing(null, '1')); - assertEquals("", StringUtils.wrapIfMissing("", '\0')); + assertEquals("", StringUtils.wrapIfMissing("", CharUtils.NUL)); assertEquals("xabx", StringUtils.wrapIfMissing("ab", 'x')); assertEquals("\"ab\"", StringUtils.wrapIfMissing("ab", '\"')); assertEquals("\"ab\"", StringUtils.wrapIfMissing("\"ab\"", '\"')); @@ -3202,7 +3202,7 @@ public class StringUtilsTest { @Test public void testUnwrap_StringChar() { assertNull(StringUtils.unwrap(null, null)); - assertNull(StringUtils.unwrap(null, '\0')); + assertNull(StringUtils.unwrap(null, CharUtils.NUL)); assertNull(StringUtils.unwrap(null, '1')); assertEquals("abc", StringUtils.unwrap("abc", null));