Skip to content

Commit

Permalink
Improve the fix for issue 367
Browse files Browse the repository at this point in the history
  • Loading branch information
larsga committed Nov 28, 2024
1 parent c7bc579 commit 40392a2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
17 changes: 6 additions & 11 deletions core/src/main/java/com/schibsted/spt/data/jslt/impl/NodeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,6 @@ private static JsonNode parseNumber(String number) {
if (number.length() == 0)
return null;

if (number.startsWith("-.")) {
number = "-0." + number.substring(2);
}

if (number.startsWith(".")) {
number = "0" + number;
}

int sign = 1;
int pos = 0;
if (number.charAt(0) == '-') {
Expand All @@ -167,8 +159,6 @@ private static JsonNode parseNumber(String number) {
int intStart = pos;

int endInteger = scanDigits(number, pos);
if (endInteger == pos)
return null;
if (endInteger == number.length()) {
if (number.length() < 10)
return new IntNode(Integer.parseInt(number));
Expand All @@ -180,7 +170,12 @@ else if (number.length() < 19)

// since there's stuff after the initial integer it must be either
// the decimal part or the exponent
long intPart = Long.parseLong(number.substring(intStart, endInteger));
long intPart;
if (endInteger == pos)
intPart = 0; // this means there was no zero before the period
else
intPart = Long.parseLong(number.substring(intStart, endInteger));

pos = endInteger;
double value = intPart * sign;

Expand Down
10 changes: 10 additions & 0 deletions core/src/test/resources/function-tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,16 @@
"input" : "{}",
"output" : "-10.0"
},
{
"query" : "number(\".1E2\")",
"input" : "{}",
"output" : "10.0"
},
{
"query" : "number(\"-.1E2\")",
"input" : "{}",
"output" : "-10.0"
},
{
"query" : "number(\"0.1e-2\")",
"input" : "{}",
Expand Down
20 changes: 11 additions & 9 deletions functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,21 @@ if it is a string that cannot be parsed, then the `fallback` value is
returned.

The number format supported is the same as in JSON literals, except
that leading zeroes are allowed.
that leading zeroes are allowed, and it's allowed to omit the zero
before the period.

Examples:

```
number(23) => 23
number("23") => 23
number("023") => 23
number(23.0) => 23.0
number(.23) => 0.23
number(-.23) => -0.23
number(null) => null
number("ab") => error
number(23) => 23
number("23") => 23
number("023") => 23
number(23.0) => 23.0
number(".23") => 0.23
number("-.23") => -0.23
number(null) => null
number("ab") => error
number("ab", 0) => 0
```

### _round(float) -> integer_
Expand Down

0 comments on commit 40392a2

Please sign in to comment.