Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NumberInput.looksLikeValidNumber() implementation #1241

Merged
merged 6 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ a pure JSON library.
=== Releases ===
------------------------------------------------------------------------

2.17.1 (not yet released)

#1241: Fix `NumberInput.looksLikeValidNumber()` implementation
(contributed by @pjfanning)

2.17.0 (12-Mar-2024)

#507: Add `JsonWriteFeature.ESCAPE_FORWARD_SLASHES` to allow escaping of '/' for
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/com/fasterxml/jackson/core/io/NumberInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class NumberInput
* @since 2.17
*/
private final static Pattern PATTERN_FLOAT = Pattern.compile(
"[+-]?[0-9]+(\\.[0-9]+)?([eE][+-]?[0-9]+)?");
"[+-]?[0-9]*[\\.]?[0-9]+([eE][+-]?[0-9]+)?");

/**
* Fast method for parsing unsigned integers that are known to fit into
Expand Down Expand Up @@ -570,6 +570,9 @@ public static BigInteger parseBigIntegerWithRadix(final String s, final int radi
*<p>
* Note: no trimming ({@code String.trim()}) nor null checks are performed
* on String passed.
*<p>
* Note: this method returning {@code true} DOES NOT GUARANTEE String is valid
* number but just that it looks close enough.
*
* @param s String to validate
*
Expand All @@ -578,6 +581,14 @@ public static BigInteger parseBigIntegerWithRadix(final String s, final int radi
* @since 2.17
*/
public static boolean looksLikeValidNumber(final String s) {
return (s != null) && PATTERN_FLOAT.matcher(s).matches();
// the PATTERN_FLOAT match returns true for empty strings, so we need to check for digits separately
cowtowncoder marked this conversation as resolved.
Show resolved Hide resolved
if (s == null || s.isEmpty()) {
return false;
}
if (s.length() == 1) {
char c = s.charAt(0);
return (c <= '9') && (c >= '0');
}
return PATTERN_FLOAT.matcher(s).matches();
}
}
15 changes: 15 additions & 0 deletions src/test/java/com/fasterxml/jackson/core/io/NumberInputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,24 @@ void looksLikeValidNumber()
assertTrue(NumberInput.looksLikeValidNumber("0"));
assertTrue(NumberInput.looksLikeValidNumber("1"));
assertTrue(NumberInput.looksLikeValidNumber("-1"));
assertTrue(NumberInput.looksLikeValidNumber("+1")); // non-JSON
assertTrue(NumberInput.looksLikeValidNumber("0001")); // non-JSON

// https://github.com/FasterXML/jackson-databind/issues/4435
assertTrue(NumberInput.looksLikeValidNumber(".0"));
assertTrue(NumberInput.looksLikeValidNumber(".01"));
assertTrue(NumberInput.looksLikeValidNumber("+.01"));
assertTrue(NumberInput.looksLikeValidNumber("-.01"));
assertTrue(NumberInput.looksLikeValidNumber("-.0"));

assertTrue(NumberInput.looksLikeValidNumber("0.01"));
assertTrue(NumberInput.looksLikeValidNumber("-0.10"));
assertTrue(NumberInput.looksLikeValidNumber("+0.25")); // non-JSON

assertTrue(NumberInput.looksLikeValidNumber("10.33"));
assertTrue(NumberInput.looksLikeValidNumber("-1.39"));
assertTrue(NumberInput.looksLikeValidNumber("+125.0")); // non-JSON

assertTrue(NumberInput.looksLikeValidNumber("1E10"));
assertTrue(NumberInput.looksLikeValidNumber("-1E10"));
assertTrue(NumberInput.looksLikeValidNumber("1e-10"));
Expand All @@ -93,7 +105,10 @@ void looksLikeValidNumber()
assertTrue(NumberInput.looksLikeValidNumber("1.4e+45"));

assertFalse(NumberInput.looksLikeValidNumber(""));
assertFalse(NumberInput.looksLikeValidNumber(" "));
assertFalse(NumberInput.looksLikeValidNumber(" "));
assertFalse(NumberInput.looksLikeValidNumber("."));
assertFalse(NumberInput.looksLikeValidNumber("0."));
assertFalse(NumberInput.looksLikeValidNumber("10_000"));
}
}
Loading