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
commit 924eb8d7fc0feea3a2e43a868fc1247dfc2ece3f Author: Gary Gregory <[email protected]> AuthorDate: Sun Nov 15 11:20:02 2026 -0500 Clarify NumberUtils hexadecimal parsing and extend boundary tests Document signed range selection, leading zeros, and hexadecimal suffix handling. Add tests for Long.MIN_VALUE + 1 and suffixed overflow below Long.MIN_VALUE. Consolidate the release note to explain the corrected return types. Validation: Full Maven verification passed. Assisted-By: OpenAI Codex --- src/changes/changes.xml | 1 - .../java/org/apache/commons/lang3/math/NumberUtils.java | 16 ++++++++++------ .../org/apache/commons/lang3/math/NumberUtilsTest.java | 2 ++ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 5db2aad63..0a5cbdb19 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -89,7 +89,6 @@ The <action> type attribute can be add,update,fix,remove. <action type="fix" dev="ggregory" due-to="Gary Gregory">Prevent length-check overflow in StringUtils.unwrap(String, String) for very large wrapping tokens.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Javadoc: Correct the null-marker example, null-marker handling, and minimum width in StringUtils.abbreviate(String, String, int).</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Javadoc: Correct the null-marker example, null-marker handling, and minimum widths in StringUtils.abbreviate(String, String, int, int).</action> - <action type="fix" dev="ggregory" due-to="Gary Gregory, Amar Ghugare">Preserve numeric types for negative hexadecimal minima (#1877).</action> <action type="fix" dev="ggregory" due-to="Gary Gregory, Amar Ghugare">Correct NumberUtils.createNumber(String) result types for negative hexadecimal minima: Integer.MIN_VALUE now returns Integer instead of Long, and Long.MIN_VALUE returns Long instead of BigInteger; numeric values are unchanged (#1877).</action> <!-- ADD --> <!-- UPDATE --> 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 041d73177..13703c8b3 100644 --- a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java +++ b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java @@ -323,17 +323,21 @@ public static Long createLong(final String str) { * Creates a {@link Number} from a {@link String}. * * <p> - * If the string starts with {@code 0x} or {@code -0x} (lower or upper case) or {@code #} or {@code -#}, it will be interpreted as a hexadecimal Integer - - * or Long, if the number of digits after the prefix is more than 8 - or BigInteger if there are more than 16 digits. + * Strings with a {@code 0x}, {@code 0X}, or {@code #} prefix, optionally preceded by a {@code +} or {@code -} sign, are interpreted as hexadecimal + * integers. The result is the smallest of {@link Integer}, {@link Long}, or {@link BigInteger} that can represent the signed value. Leading zeros do not + * affect type selection. A trailing {@code 'l'} or {@code 'L'} requests a {@link Long}, or a {@link BigInteger} if the value is out of range for a Long. + * For example, {@code "-0x80000000"} returns {@link Integer#MIN_VALUE} as an Integer, and {@code "-0x8000000000000000"} returns {@link Long#MIN_VALUE} + * as a Long. The hexadecimal digits {@code 'f'}, {@code 'F'}, {@code 'd'}, and {@code 'D'} are not treated as type qualifiers. * </p> + * * <p> - * Then, the value is examined for a type qualifier on the end, i.e. one of {@code 'f', 'F', 'd', 'D', 'l', 'L'}. If it is found, it starts trying to create - * successively larger types from the type specified until one is found that can represent the value. + * For non-hexadecimal input, the value is examined for a type qualifier on the end, i.e. one of {@code 'f', 'F', 'd', 'D', 'l', 'L'}. If it is found, + * it starts trying to create successively larger types from the type specified until one is found that can represent the value. * </p> * * <p> - * If a type specifier is not found, it will check for a decimal point and then try successively larger types from {@link Integer} to {@link BigInteger} and - * from {@link Float} to {@link BigDecimal}. + * For non-hexadecimal input without a type specifier, it will check for a decimal point and then try successively larger types from {@link Integer} to + * {@link BigInteger} and from {@link Float} to {@link BigDecimal}. * </p> * * <p> diff --git a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java index d8d02bc3f..db1769fa0 100644 --- a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java @@ -833,7 +833,9 @@ void testCreateNumberNegativeHexRangeBoundaries() { assertEquals(Integer.valueOf(Integer.MIN_VALUE + 1), NumberUtils.createNumber("-0x7FFFFFFF")); assertEquals(Long.valueOf((long) Integer.MIN_VALUE - 1), NumberUtils.createNumber("-0x80000001")); assertEquals(Long.valueOf(Integer.MIN_VALUE), NumberUtils.createNumber("-0x80000000L")); + assertEquals(Long.valueOf(Long.MIN_VALUE + 1), NumberUtils.createNumber("-0x7FFFFFFFFFFFFFFF")); assertEquals(new BigInteger("-8000000000000001", 16), NumberUtils.createNumber("-0x8000000000000001")); + assertEquals(new BigInteger("-8000000000000001", 16), NumberUtils.createNumber("-0x8000000000000001L")); } /**
