You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/06/17 15:26:45 UTC

[commons-codec] branch master updated: Deprecate BaseNCodec.isWhiteSpace(byte) and use Character.isWhitespace(int)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git


The following commit(s) were added to refs/heads/master by this push:
     new 10d9e046 Deprecate BaseNCodec.isWhiteSpace(byte) and use Character.isWhitespace(int)
10d9e046 is described below

commit 10d9e0467e29c2fed3c4a4fead24f39bc6ef87bb
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 17 11:26:40 2023 -0400

    Deprecate BaseNCodec.isWhiteSpace(byte) and use
    Character.isWhitespace(int)
---
 src/changes/changes.xml                                  |  1 +
 .../java/org/apache/commons/codec/binary/Base32.java     |  2 +-
 .../java/org/apache/commons/codec/binary/Base64.java     |  2 +-
 .../java/org/apache/commons/codec/binary/BaseNCodec.java | 16 +++++-----------
 .../org/apache/commons/codec/binary/BaseNCodecTest.java  | 14 ++++++++++----
 5 files changed, 18 insertions(+), 17 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 9acae476..71160a59 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -57,6 +57,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action                   dev="kinow" type="fix" due-to="Marc Wrobel">Fix several typos, improve writing in some javadocs #139.</action>
       <action                   dev="ggregory" type="fix" due-to="Gary Gregory">BaseNCodecOutputStream.eof() should not throw IOException.</action>
       <action                   dev="ggregory" type="fix" due-to="Gary Gregory">Javadoc improvements and cleanups.</action>
+      <action                   dev="ggregory" type="fix" due-to="Gary Gregory">Deprecate BaseNCodec.isWhiteSpace(byte) and use Character.isWhitespace(int).</action>
       <!-- ADD -->
       <action issue="CODEC-296" dev="mattsicker" type="add" due-to="Matt Sicker">Add support for Blake3 family of hashes.</action>
       <action                   dev="ggregory" type="add">Add github/codeql-action.</action>
diff --git a/src/main/java/org/apache/commons/codec/binary/Base32.java b/src/main/java/org/apache/commons/codec/binary/Base32.java
index 0ede1ba7..520ba960 100644
--- a/src/main/java/org/apache/commons/codec/binary/Base32.java
+++ b/src/main/java/org/apache/commons/codec/binary/Base32.java
@@ -335,7 +335,7 @@ public class Base32 extends BaseNCodec {
         }
         this.decodeSize = this.encodeSize - 1;
 
-        if (isInAlphabet(padding) || isWhiteSpace(padding)) {
+        if (isInAlphabet(padding) || Character.isWhitespace(padding)) {
             throw new IllegalArgumentException("pad must not be in alphabet or whitespace");
         }
     }
diff --git a/src/main/java/org/apache/commons/codec/binary/Base64.java b/src/main/java/org/apache/commons/codec/binary/Base64.java
index d1049307..44225ed2 100644
--- a/src/main/java/org/apache/commons/codec/binary/Base64.java
+++ b/src/main/java/org/apache/commons/codec/binary/Base64.java
@@ -389,7 +389,7 @@ public class Base64 extends BaseNCodec {
      */
     public static boolean isBase64(final byte[] arrayOctet) {
         for (final byte element : arrayOctet) {
-            if (!isBase64(element) && !isWhiteSpace(element)) {
+            if (!isBase64(element) && !Character.isWhitespace(element)) {
                 return false;
             }
         }
diff --git a/src/main/java/org/apache/commons/codec/binary/BaseNCodec.java b/src/main/java/org/apache/commons/codec/binary/BaseNCodec.java
index 88589bd9..47b04193 100644
--- a/src/main/java/org/apache/commons/codec/binary/BaseNCodec.java
+++ b/src/main/java/org/apache/commons/codec/binary/BaseNCodec.java
@@ -277,21 +277,15 @@ public abstract class BaseNCodec implements BinaryEncoder, BinaryDecoder {
 
     /**
      * Checks if a byte value is whitespace or not.
-     * Whitespace is taken to mean: space, tab, CR, LF
      * @param byteToCheck
      *            the byte to check
      * @return true if byte is whitespace, false otherwise
+     * @see Character#isWhitespace(int)
+     * @deprecated Use {@link Character#isWhitespace(int)}.
      */
+    @Deprecated
     protected static boolean isWhiteSpace(final byte byteToCheck) {
-        switch (byteToCheck) {
-            case ' ' :
-            case '\n' :
-            case '\r' :
-            case '\t' :
-                return true;
-            default :
-                return false;
-        }
+        return Character.isWhitespace(byteToCheck);
     }
 
     /**
@@ -641,7 +635,7 @@ public abstract class BaseNCodec implements BinaryEncoder, BinaryDecoder {
     public boolean isInAlphabet(final byte[] arrayOctet, final boolean allowWSPad) {
         for (final byte octet : arrayOctet) {
             if (!isInAlphabet(octet) &&
-                    (!allowWSPad || (octet != pad) && !isWhiteSpace(octet))) {
+                    (!allowWSPad || (octet != pad) && !Character.isWhitespace(octet))) {
                 return false;
             }
         }
diff --git a/src/test/java/org/apache/commons/codec/binary/BaseNCodecTest.java b/src/test/java/org/apache/commons/codec/binary/BaseNCodecTest.java
index 00cd10c7..7f6ed1f3 100644
--- a/src/test/java/org/apache/commons/codec/binary/BaseNCodecTest.java
+++ b/src/test/java/org/apache/commons/codec/binary/BaseNCodecTest.java
@@ -105,10 +105,16 @@ public class BaseNCodecTest {
 //
     @Test
     public void testIsWhiteSpace() {
-        assertTrue(BaseNCodec.isWhiteSpace((byte) ' '));
-        assertTrue(BaseNCodec.isWhiteSpace((byte) '\n'));
-        assertTrue(BaseNCodec.isWhiteSpace((byte) '\r'));
-        assertTrue(BaseNCodec.isWhiteSpace((byte) '\t'));
+        assertTrue(Character.isWhitespace((byte) ' '));
+        assertTrue(Character.isWhitespace((byte) '\n'));
+        assertTrue(Character.isWhitespace((byte) '\r'));
+        assertTrue(Character.isWhitespace((byte) '\t'));
+        assertTrue(Character.isWhitespace((byte) '\f'));
+        assertTrue(Character.isWhitespace((byte) '\u000B'));
+        for (byte b = Byte.MIN_VALUE; b < Byte.MAX_VALUE; b++) {
+            final byte byteToCheck = b;
+            assertEquals(Character.isWhitespace(b), Character.isWhitespace(byteToCheck));
+        }
     }
 //
 //    @Test