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 9abd79314 Add CharUtils isHex(int), isAsciiNumeric(int), isOctal(int).
(#1685)
9abd79314 is described below
commit 9abd79314b19676c54eab02d441cfb3051bb2d8c
Author: Gary Gregory <[email protected]>
AuthorDate: Wed Jun 3 07:29:57 2026 -0400
Add CharUtils isHex(int), isAsciiNumeric(int), isOctal(int). (#1685)
---
.../java/org/apache/commons/lang3/CharUtils.java | 62 +++++++++++++++++++++-
.../org/apache/commons/lang3/CharUtilsTest.java | 61 +++++++++++++++++++++
2 files changed, 121 insertions(+), 2 deletions(-)
diff --git a/src/main/java/org/apache/commons/lang3/CharUtils.java
b/src/main/java/org/apache/commons/lang3/CharUtils.java
index fda1bf618..a454dd088 100644
--- a/src/main/java/org/apache/commons/lang3/CharUtils.java
+++ b/src/main/java/org/apache/commons/lang3/CharUtils.java
@@ -207,6 +207,26 @@ public static boolean isAsciiNumeric(final char ch) {
return ch >= '0' && ch <= '9';
}
+ /**
+ * Tests whether the character is ASCII 7 bit numeric.
+ *
+ * <pre>
+ * CharUtils.isAsciiNumeric('a') = false
+ * CharUtils.isAsciiNumeric('A') = false
+ * CharUtils.isAsciiNumeric('3') = true
+ * CharUtils.isAsciiNumeric('-') = false
+ * CharUtils.isAsciiNumeric('\n') = false
+ * CharUtils.isAsciiNumeric('©') = false
+ * </pre>
+ *
+ * @param ch the code point to check.
+ * @return true if between 48 and 57 inclusive.
+ * @since 3.21.0
+ */
+ public static boolean isAsciiNumeric(final int ch) {
+ return ch >= '0' && ch <= '9';
+ }
+
/**
* Tests whether the character is ASCII 7 bit printable.
*
@@ -253,11 +273,49 @@ public static boolean isHex(final char ch) {
return isAsciiNumeric(ch) || ch >= 'a' && ch <= 'f' || ch >= 'A' && ch
<= 'F';
}
+ /**
+ * Tests whether a character is a hexadecimal character.
+ *
+ * <pre>
+ * CharUtils.isHex('0') = true
+ * CharUtils.isHex('3') = true
+ * CharUtils.isHex('9') = true
+ * CharUtils.isHex('a') = true
+ * CharUtils.isHex('f') = true
+ * CharUtils.isHex('g') = false
+ * CharUtils.isHex('A') = true
+ * CharUtils.isHex('F') = true
+ * CharUtils.isHex('G') = false
+ * CharUtils.isHex('#') = false
+ * CharUtils.isHex('-') = false
+ * CharUtils.isHex('\n') = false
+ * CharUtils.isHex('©') = false
+ * </pre>
+ *
+ * @param ch the code point to test.
+ * @return true if character is a hexadecimal character.
+ * @since 3.21.0
+ */
+ public static boolean isHex(final int ch) {
+ return isAsciiNumeric(ch) || ch >= 'a' && ch <= 'f' || ch >= 'A' && ch
<= 'F';
+ }
+
+ /**
+ * Tests if the given char is an octal digit. Octal digits are the
character representations of the digits 0 to 7.
+ *
+ * @param ch the byte to check.
+ * @return true if the given char is the character representation of one
of the digits from 0 to 7.
+ * @since 3.21.0
+ */
+ public static boolean isOctal(final byte ch) {
+ return ch >= '0' && ch <= '7';
+ }
+
/**
* Tests if the given char is an octal digit. Octal digits are the
character representations of the digits 0 to 7.
*
- * @param ch the char to check
- * @return true if the given char is the character representation of one
of the digits from 0 to 7
+ * @param ch the char to check.
+ * @return true if the given char is the character representation of one
of the digits from 0 to 7.
* @since 3.18.0
*/
public static boolean isOctal(final char ch) {
diff --git a/src/test/java/org/apache/commons/lang3/CharUtilsTest.java
b/src/test/java/org/apache/commons/lang3/CharUtilsTest.java
index f187b7de9..b02b002ec 100644
--- a/src/test/java/org/apache/commons/lang3/CharUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/CharUtilsTest.java
@@ -179,6 +179,23 @@ void testIsAsciiNumeric_char() {
}
}
+ @Test
+ void testIsAsciiNumeric_int() {
+ assertFalse(CharUtils.isAsciiNumeric((int) 'a'));
+ assertFalse(CharUtils.isAsciiNumeric((int) 'A'));
+ assertTrue(CharUtils.isAsciiNumeric((int) '3'));
+ assertFalse(CharUtils.isAsciiNumeric((int) '-'));
+ assertFalse(CharUtils.isAsciiNumeric((int) '\n'));
+ assertFalse(CharUtils.isAsciiNumeric((int) CHAR_COPY));
+ for (int i = 0; i < 196; i++) {
+ if (i >= '0' && i <= '9') {
+ assertTrue(CharUtils.isAsciiNumeric(i));
+ } else {
+ assertFalse(CharUtils.isAsciiNumeric(i));
+ }
+ }
+ }
+
@Test
void testIsAsciiPrintable_char() {
assertTrue(CharUtils.isAsciiPrintable('a'));
@@ -197,6 +214,50 @@ void testIsAsciiPrintable_char() {
}
}
+ @Test
+ void testIsHex_int() {
+ assertTrue(CharUtils.isHex((int) '0'));
+ assertTrue(CharUtils.isHex((int) '3'));
+ assertTrue(CharUtils.isHex((int) '9'));
+ assertTrue(CharUtils.isHex((int) 'a'));
+ assertTrue(CharUtils.isHex((int) 'f'));
+ assertFalse(CharUtils.isHex((int) 'g'));
+ assertTrue(CharUtils.isHex((int) 'A'));
+ assertTrue(CharUtils.isHex((int) 'F'));
+ assertFalse(CharUtils.isHex((int) 'G'));
+ assertFalse(CharUtils.isHex((int) '#'));
+ assertFalse(CharUtils.isHex((int) '-'));
+ assertFalse(CharUtils.isHex((int) '\n'));
+ assertFalse(CharUtils.isHex((int) CHAR_COPY));
+ for (int i = 0; i < 196; i++) {
+ if (i >= '0' && i <= '9' || i >= 'a' && i <= 'f' || i >= 'A' && i
<= 'F') {
+ assertTrue(CharUtils.isHex(i));
+ } else {
+ assertFalse(CharUtils.isHex(i));
+ }
+ }
+ }
+
+ @Test
+ void testIsOctal_byte() {
+ assertFalse(CharUtils.isOctal((byte) 'a'));
+ assertFalse(CharUtils.isOctal((byte) 'A'));
+ assertTrue(CharUtils.isOctal((byte) '0'));
+ assertTrue(CharUtils.isOctal((byte) '3'));
+ assertTrue(CharUtils.isOctal((byte) '7'));
+ assertFalse(CharUtils.isOctal((byte) '8'));
+ assertFalse(CharUtils.isOctal((byte) '9'));
+ assertFalse(CharUtils.isOctal((byte) '-'));
+ assertFalse(CharUtils.isOctal((byte) '\n'));
+ for (byte i = 0; i < 127; i++) {
+ if (i >= '0' && i <= '7') {
+ assertTrue(CharUtils.isOctal(i));
+ } else {
+ assertFalse(CharUtils.isOctal(i));
+ }
+ }
+ }
+
@Test
void testToChar_Character() {
assertEquals('A', CharUtils.toChar(CHARACTER_A));