Skip to content

Commit ed5162f

Browse files
authored
fix: std.parseInt accepts + prefix like go-jsonnet (#1030)
## Summary This PR updates `std.parseInt` to accept a leading `+` sign, matching go-jsonnet behavior for decimal integer parsing. ## Behavior comparison Local reference versions used: - C++ jsonnet `v0.22.0` - go-jsonnet `v0.21.0` and `v0.22.0` - jrsonnet `0.5.0-pre99` | Expression | C++ jsonnet | go-jsonnet | jrsonnet | sjsonnet before | sjsonnet after | | --- | --- | --- | --- | --- | --- | | `std.parseInt("+1")` | error | `1` | error | error | `1` | | `std.parseInt("+0")` | error | `0` | error | error | `0` | | `std.parseInt("+01")` | error | `1` | error | error | `1` | | `std.parseInt("+")` | error | error | error | error | error | | `std.parseInt("+-1")` | error | error | error | error | error | | `std.parseInt("-123")` | `-123` | `-123` | `-123` | `-123` | `-123` | | `std.parseInt("123")` | `123` | `123` | `123` | `123` | `123` | The intentional compatibility target here is go-jsonnet for plus-prefixed decimal input. C++ jsonnet and jrsonnet currently reject the leading `+` cases above. ## Changes - `StringModule.scala`: handle a leading `+` sign in `std.parseInt` - `parseInt_positive_sign.jsonnet`: golden regression coverage for accepted `+` inputs and unchanged positive/negative baselines ## Tests ```bash rtk ./mill 'sjsonnet.jvm[3.3.7]'.test.testOnly sjsonnet.FileTests rtk git diff --check upstream/master...HEAD ``` Both passed locally after rebasing onto `upstream/master`.
1 parent 442704e commit ed5162f

3 files changed

Lines changed: 12 additions & 1 deletion

File tree

sjsonnet/src/sjsonnet/stdlib/StringModule.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,12 +1013,14 @@ object StringModule extends AbstractFunctionModule {
10131013
private object ParseInt extends Val.Builtin1("parseInt", "str") {
10141014
def evalRhs(str: Eval, ev: EvalScope, pos: Position): Val = {
10151015
val s = str.value.asString
1016-
if (s.isEmpty || s == "-") {
1016+
if (s.isEmpty || s == "-" || s == "+") {
10171017
Error.fail("Cannot parse '" + s + "' as an integer in base 10")
10181018
}
10191019
if (s.charAt(0) == '-') {
10201020
val result = parseNat(s, 1, 10, "base 10")
10211021
Val.cachedNum(pos, if (result == 0.0) 0.0 else -result)
1022+
} else if (s.charAt(0) == '+') {
1023+
Val.cachedNum(pos, parseNat(s, 1, 10, "base 10"))
10221024
} else Val.cachedNum(pos, parseNat(s, 0, 10, "base 10"))
10231025
}
10241026
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Test that std.parseInt accepts + prefix like go-jsonnet.
2+
assert std.parseInt("+123") == 123 : "parseInt(+123) should return 123";
3+
assert std.parseInt("+0") == 0 : "parseInt(+0) should return 0";
4+
assert std.parseInt("+007") == 7 : "parseInt(+007) should return 7";
5+
assert std.parseInt("+1") == 1 : "parseInt(+1) should return 1";
6+
assert std.parseInt("-123") == -123 : "parseInt(-123) should still work";
7+
assert std.parseInt("123") == 123 : "parseInt(123) should still work";
8+
true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
true

0 commit comments

Comments
 (0)