Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/main/java/org/apache/commons/lang3/math/NumberUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import java.util.function.Consumer;

import org.apache.commons.lang3.CharUtils;
import org.apache.commons.lang3.JavaVersion;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.lang3.Validate;

/**
Expand Down Expand Up @@ -620,6 +622,7 @@ public static boolean isCreatable(final String str) {
sz--; // don't want to loop to the last char, check it afterwards
// for type qualifiers
int i = start;
int expPos = -1;
// loop to the next to last char or to the last char if we need another digit to
// make a valid number (e.g. chars[0..5] = "1234E")
while (i < sz || i < sz + 1 && allowSigns && !foundDigit) {
Expand All @@ -643,6 +646,7 @@ public static boolean isCreatable(final String str) {
}
hasExp = true;
allowSigns = true;
expPos = i;
} else if (isSign(chars[i])) {
if (!allowSigns) {
return false;
Expand All @@ -654,6 +658,27 @@ public static boolean isCreatable(final String str) {
}
i++;
}
if (expPos > -1 && expPos < chars.length - 1) {
int expEndPos = chars.length;
final char lastChar = chars[expEndPos - 1];
if (lastChar == 'd' || lastChar == 'D'
|| lastChar == 'f' || lastChar == 'F'
|| lastChar == 'l' || lastChar == 'L') {
expEndPos--;
}
final String expStr = str.substring(expPos + 1, expEndPos);
try {
final int exp = Integer.parseInt(expStr);
if (exp == Integer.MIN_VALUE) {
return false;
}
} catch (final NumberFormatException ignored) {
// Largest positive exponent of Integer.MAX_VALUE + 1 which is supported by Java 21 and later.
final String largestPositiveExponent = "2147483648";
final boolean isLargestPositiveExponent = largestPositiveExponent.equals(expStr) || ("+" + largestPositiveExponent).equals(expStr);
return isLargestPositiveExponent && SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_21);
}
}
if (i < chars.length) {
if (CharUtils.isAsciiNumeric(chars[i])) {
// no type qualifier, OK
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
import java.util.function.Function;

import org.apache.commons.lang3.AbstractLangTest;
import org.apache.commons.lang3.JavaVersion;
import org.apache.commons.lang3.SystemProperties;
import org.apache.commons.lang3.SystemUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand Down Expand Up @@ -887,6 +889,23 @@ void testIsCreatable() {
compareIsCreatableWithCreateNumber(".D", false); // LANG-1646
compareIsCreatableWithCreateNumber(".e10", false); // LANG-1646
compareIsCreatableWithCreateNumber(".e10D", false); // LANG-1646
compareIsCreatableWithCreateNumber("1E2147483647", true);
compareIsCreatableWithCreateNumber("1E+2147483647", true);
compareIsCreatableWithCreateNumber("1E-2147483647", true);
compareIsCreatableWithCreateNumber("1E-2147483648", false);
compareIsCreatableWithCreateNumber("1E2147483648", SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_21));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need more tests around the edge cases.
TY!

compareIsCreatableWithCreateNumber("1E+2147483648", SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_21));
compareIsCreatableWithCreateNumber("1E+2147483649", false);
compareIsCreatableWithCreateNumber("1E-2147483649", false);
compareIsCreatableWithCreateNumber("1E2147483648D", SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_21));
compareIsCreatableWithCreateNumber("1E-2147483648D", false);
compareIsCreatableWithCreateNumber("1E2147483648F", SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_21));
compareIsCreatableWithCreateNumber("1E+2147483648F", SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_21));
compareIsCreatableWithCreateNumber("1E-2147483648F", false);
compareIsCreatableWithCreateNumber("1.0E2147483648", SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_21));
compareIsCreatableWithCreateNumber("1.0E-2147483648", false);
compareIsCreatableWithCreateNumber("1E+999999999999999999999", false);
compareIsCreatableWithCreateNumber("1E-999999999999999999999", false);
}

@Test
Expand Down
Loading