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-lang.git
commit fcf433fe8f9d470b7a076cd4be065af6fabc23d2 Author: Gary D. Gregory <garydgreg...@gmail.com> AuthorDate: Sun Jun 15 08:01:20 2025 -0400 Add org.apache.commons.lang3.CharUtils.isHex(char) --- src/changes/changes.xml | 1 + .../java/org/apache/commons/lang3/CharUtils.java | 26 ++++++++++++++++++++++ .../org/apache/commons/lang3/math/NumberUtils.java | 5 ++--- .../text/translate/NumericEntityUnescaper.java | 6 ++--- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 43fd65b1f..db1b12a31 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -128,6 +128,7 @@ The <action> type attribute can be add,update,fix,remove. <action type="add" dev="ggregory" due-to="kommalapatiraviteja">Add ArrayFill.fill(boolean[], boolean) #1386.</action> <action type="add" dev="ggregory" due-to="Pankraz76, Gary Gregory">Add ObjectUtils.getIfNull(Object, Object) and deprecate defaultIfNull(Object, Object).</action> <action type="add" dev="ggregory" due-to="Gary Gregory">org.apache.commons.lang3.mutable.Mutable now extends Supplier.</action> + <action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.lang3.CharUtils.isHex(char).</action> <!-- UPDATE --> <action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 73 to 84 #1267, #1277, #1283, #1288, #1302, #1377.</action> <action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">[site] Bump org.codehaus.mojo:taglist-maven-plugin from 3.1.0 to 3.2.1 #1300.</action> diff --git a/src/main/java/org/apache/commons/lang3/CharUtils.java b/src/main/java/org/apache/commons/lang3/CharUtils.java index 7d920e783..3b3637f9d 100644 --- a/src/main/java/org/apache/commons/lang3/CharUtils.java +++ b/src/main/java/org/apache/commons/lang3/CharUtils.java @@ -229,6 +229,32 @@ public static boolean isAsciiPrintable(final char ch) { return ch >= 32 && ch < 127; } + /** + * Tests whether a character is a hexadecimal character. + * + * <pre> + * CharUtils.isAsciiPrintable('0') = true + * CharUtils.isAsciiPrintable('9') = true + * CharUtils.isAsciiPrintable('a') = true + * CharUtils.isAsciiPrintable('f') = true + * CharUtils.isAsciiPrintable('g') = false + * CharUtils.isAsciiPrintable('A') = true + * CharUtils.isAsciiPrintable('F') = true + * CharUtils.isAsciiPrintable('G') = false + * CharUtils.isAsciiPrintable('3') = false + * CharUtils.isAsciiPrintable('-') = false + * CharUtils.isAsciiPrintable('\n') = false + * CharUtils.isAsciiPrintable('©') = false + * </pre> + * + * @param ch the character to test. + * @return true if character is a hexadecimal character. + * @since 3.18.0 + */ + public static boolean isHex(final char ch) { + return isAsciiNumeric(ch) || ch >= 'a' && ch <= 'f' || ch >= 'A' && ch <= 'F'; + } + /** * Converts the Character to a char throwing an exception for {@code null}. * diff --git a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java index 45f4bac7e..e35f48047 100644 --- a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java +++ b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java @@ -22,6 +22,7 @@ import java.math.RoundingMode; import java.util.Objects; +import org.apache.commons.lang3.CharUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Validate; @@ -571,9 +572,7 @@ public static boolean isCreatable(final String str) { } // checking hex (it can't be anything else) for (; i < chars.length; i++) { - if ((chars[i] < '0' || chars[i] > '9') - && (chars[i] < 'a' || chars[i] > 'f') - && (chars[i] < 'A' || chars[i] > 'F')) { + if (!CharUtils.isHex(chars[i])) { return false; } } diff --git a/src/main/java/org/apache/commons/lang3/text/translate/NumericEntityUnescaper.java b/src/main/java/org/apache/commons/lang3/text/translate/NumericEntityUnescaper.java index 426c693bf..56db01a5d 100644 --- a/src/main/java/org/apache/commons/lang3/text/translate/NumericEntityUnescaper.java +++ b/src/main/java/org/apache/commons/lang3/text/translate/NumericEntityUnescaper.java @@ -22,6 +22,8 @@ import java.util.Collections; import java.util.EnumSet; +import org.apache.commons.lang3.CharUtils; + /** * Translate XML numeric entities of the form &#[xX]?\d+;? to the specific code point. * @@ -122,9 +124,7 @@ public int translate(final CharSequence input, final int index, final Writer out int end = start; // Note that this supports character codes without a ; on the end - while (end < seqEnd && (input.charAt(end) >= '0' && input.charAt(end) <= '9' || - input.charAt(end) >= 'a' && input.charAt(end) <= 'f' || - input.charAt(end) >= 'A' && input.charAt(end) <= 'F')) { + while (end < seqEnd && CharUtils.isHex(input.charAt(end))) { end++; }