Skip to content
Closed
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
48 changes: 37 additions & 11 deletions src/main/java/org/apache/commons/lang3/math/NumberUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -753,25 +753,51 @@ private static boolean isParsableDecimal(final String str, final int beginIdx) {
// See https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-NonZeroDigit
int decimalPoints = 0;
boolean asciiNumeric = true;
for (int i = beginIdx; i < str.length(); i++) {
boolean hasExponent = false;
boolean expectExponentDigit = false;
final int lastIndex = str.length() - 1;
char lastChar = str.charAt(lastIndex);
boolean hasSuffix = false;
if (lastChar == 'f' || lastChar == 'F' || lastChar == 'd' || lastChar == 'D') {
hasSuffix = true;
}
int endIndex;
if (hasSuffix) {
endIndex = lastIndex;
} else {
endIndex = str.length();
}
for (int i = beginIdx; i < endIndex; i++) {
final char ch = str.charAt(i);
final boolean isDecimalPoint = ch == '.';
if (isDecimalPoint) {
decimalPoints++;
if (Character.isDigit(ch)) {
expectExponentDigit = false;
asciiNumeric &= CharUtils.isAsciiNumeric(ch);
continue;
}
if (decimalPoints > 1 || !isDecimalPoint && !Character.isDigit(ch)) {
return false;
if (ch == '.' && decimalPoints == 0 && !hasExponent) {
decimalPoints++;
continue;
}
if (!isDecimalPoint) {
asciiNumeric &= CharUtils.isAsciiNumeric(ch);
if ((ch == 'e' || ch == 'E') && !hasExponent) {
hasExponent = true;
expectExponentDigit = true;
continue;
}
if (decimalPoints > 0 && !asciiNumeric) {
return false;
if ((ch == '+' || ch == '-') && expectExponentDigit) {
continue;
}
return false;
}
return true;
if (expectExponentDigit) {
return false;
}
if (decimalPoints == 0) {
return true;
}
return asciiNumeric;
}


private static boolean isSign(final char ch) {
return ch == '-' || ch == '+';
}
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1028,12 +1028,12 @@ void testIsParsable() {
assertTrue(NumberUtils.isParsable("-018.2"));
assertTrue(NumberUtils.isParsable("-.236"));
assertTrue(NumberUtils.isParsable("2."));
// TODO assertTrue(NumberUtils.isParsable("2.f"));
// TODO assertTrue(NumberUtils.isParsable("2.d"));
// Float.parseFloat("1.2e-5f")
// TODO assertTrue(NumberUtils.isParsable("1.2e-5f"));
// Double.parseDouble("1.2e-5d")
// TODO assertTrue(NumberUtils.isParsable("1.2e-5d"));
assertTrue(NumberUtils.isParsable("2.f"));
assertTrue(NumberUtils.isParsable("2.d"));
Float.parseFloat("1.2e-5f");
assertTrue(NumberUtils.isParsable("1.2e-5f"));
Double.parseDouble("1.2e-5d");
assertTrue(NumberUtils.isParsable("1.2e-5d"));
}

/**
Expand Down
Loading