Skip to content

Commit

Permalink
Merge branch '2.17' into 2.18
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 13, 2024
2 parents 50e0c00 + 55261be commit 4607d0b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
5 changes: 5 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ a pure JSON library.
(reported, fix contributed by Jared S)
#1277: Add back Java 22 optimisation in FastDoubleParser

2.17.2 (not yet released)

#1308: Relax validation by `NumberInput.looksLikeValidNumber()` to allow
trailing dot (like `3.`)

2.17.1 (04-May-2024)

#1241: Fix `NumberInput.looksLikeValidNumber()` implementation
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 @@ -35,12 +35,22 @@ public final class NumberInput
/**
* Regexp used to pre-validate "Stringified Numbers": slightly looser than
* JSON Number definition (allows leading zeroes, positive sign).
*
*
* @since 2.17
*/
private final static Pattern PATTERN_FLOAT = Pattern.compile(
"[+-]?[0-9]*[\\.]?[0-9]+([eE][+-]?[0-9]+)?");


/**
* Secondary regexp used along with {@code PATTERN_FLOAT} to cover
* case where number ends with dot, like {@code "+12."}
*
* @since 2.17.2
*/
private final static Pattern PATTERN_FLOAT_TRAILING_DOT = Pattern.compile(
"[+-]?[0-9]+[\\.]");

/**
* Fast method for parsing unsigned integers that are known to fit into
* regular 32-bit signed int type. This means that length is
Expand Down Expand Up @@ -641,6 +651,7 @@ public static boolean looksLikeValidNumber(final String s) {
char c = s.charAt(0);
return (c <= '9') && (c >= '0');
}
return PATTERN_FLOAT.matcher(s).matches();
return PATTERN_FLOAT.matcher(s).matches()
|| PATTERN_FLOAT_TRAILING_DOT.matcher(s).matches();
}
}
37 changes: 33 additions & 4 deletions src/test/java/com/fasterxml/jackson/core/io/NumberInputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void parseBigIntegerFailsWithENotation()
}

@Test
void looksLikeValidNumber()
void looksLikeValidNumberTrue()
{
assertTrue(NumberInput.looksLikeValidNumber("0"));
assertTrue(NumberInput.looksLikeValidNumber("1"));
Expand All @@ -83,10 +83,11 @@ void looksLikeValidNumber()

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

assertTrue(NumberInput.looksLikeValidNumber("0.01"));
assertTrue(NumberInput.looksLikeValidNumber("-0.10"));
Expand All @@ -104,11 +105,39 @@ void looksLikeValidNumber()
assertTrue(NumberInput.looksLikeValidNumber("1.4E-45"));
assertTrue(NumberInput.looksLikeValidNumber("1.4e+45"));

// https://github.com/FasterXML/jackson-core/issues/1308
assertTrue(NumberInput.looksLikeValidNumber("0."));
assertTrue(NumberInput.looksLikeValidNumber("6."));
assertTrue(NumberInput.looksLikeValidNumber("65."));
assertTrue(NumberInput.looksLikeValidNumber("654."));
assertTrue(NumberInput.looksLikeValidNumber("65432."));
assertTrue(NumberInput.looksLikeValidNumber("-0."));
assertTrue(NumberInput.looksLikeValidNumber("-6."));
assertTrue(NumberInput.looksLikeValidNumber("-65."));
assertTrue(NumberInput.looksLikeValidNumber("-654."));
assertTrue(NumberInput.looksLikeValidNumber("-65432."));
assertTrue(NumberInput.looksLikeValidNumber("+0."));
assertTrue(NumberInput.looksLikeValidNumber("+6."));
assertTrue(NumberInput.looksLikeValidNumber("+65."));
assertTrue(NumberInput.looksLikeValidNumber("+654."));
assertTrue(NumberInput.looksLikeValidNumber("+65432."));
}

@Test
void looksLikeValidNumberFalse()
{
// https://github.com/FasterXML/jackson-databind/issues/4435 and
// https://github.com/FasterXML/jackson-core/issues/1308
assertFalse(NumberInput.looksLikeValidNumber(""));
assertFalse(NumberInput.looksLikeValidNumber(" "));
assertFalse(NumberInput.looksLikeValidNumber(" "));
assertFalse(NumberInput.looksLikeValidNumber("."));
assertFalse(NumberInput.looksLikeValidNumber("0."));
assertFalse(NumberInput.looksLikeValidNumber("10_000"));
assertFalse(NumberInput.looksLikeValidNumber("-"));
assertFalse(NumberInput.looksLikeValidNumber("+"));
assertFalse(NumberInput.looksLikeValidNumber("-."));
assertFalse(NumberInput.looksLikeValidNumber("+."));
assertFalse(NumberInput.looksLikeValidNumber("-E"));
assertFalse(NumberInput.looksLikeValidNumber("+E"));
}
}

0 comments on commit 4607d0b

Please sign in to comment.