Skip to content

Commit

Permalink
Support _ in String[toInt|toIntOrNull|toFloat|toFloatOrNull] (apple…
Browse files Browse the repository at this point in the history
…#580)

The logic is the same as the Pkl parser
  • Loading branch information
stackoverflow authored Jul 16, 2024
1 parent b5e011d commit 24cc95a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
27 changes: 23 additions & 4 deletions pkl-core/src/main/java/org/pkl/core/stdlib/base/StringNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ public abstract static class toInt extends ExternalMethod0Node {
@Specialization
protected long eval(String self) {
try {
return Long.parseLong(self.replaceAll("_", ""));
return Long.parseLong(removeUnderlinesFromNumber(self));
} catch (NumberFormatException e) {
throw exceptionBuilder()
.evalError("cannotParseStringAs", "Int")
Expand All @@ -783,7 +783,7 @@ public abstract static class toIntOrNull extends ExternalMethod0Node {
@Specialization
protected Object eval(String self) {
try {
return Long.parseLong(self.replaceAll("_", ""));
return Long.parseLong(removeUnderlinesFromNumber(self));
} catch (NumberFormatException e) {
return VmNull.withoutDefault();
}
Expand All @@ -795,7 +795,7 @@ public abstract static class toFloat extends ExternalMethod0Node {
@Specialization
protected double eval(String self) {
try {
return Double.parseDouble(self);
return Double.parseDouble(removeUnderlinesFromNumber(self));
} catch (NumberFormatException e) {
throw exceptionBuilder()
.evalError("cannotParseStringAs", "Float")
Expand All @@ -810,7 +810,7 @@ public abstract static class toFloatOrNull extends ExternalMethod0Node {
@Specialization
protected Object eval(String self) {
try {
return Double.parseDouble(self);
return Double.parseDouble(removeUnderlinesFromNumber(self));
} catch (NumberFormatException e) {
return VmNull.withoutDefault();
}
Expand Down Expand Up @@ -934,4 +934,23 @@ private static String applyMapper(Matcher matcher, VmFunction mapper) {
var replacement = mapper.applyString(regexMatch);
return Matcher.quoteReplacement(replacement);
}

/**
* Removes `_` from numbers to be parsed to be compatible with how Pkl parses numbers. Will return
* the string unmodified if it's invalid.
*/
private static String removeUnderlinesFromNumber(String number) {
var builder = new StringBuilder();
var numberStart = true;
for (var i = 0; i < number.length(); i++) {
var c = number.charAt(i);
if (c != '_') {
builder.append(c);
} else if (numberStart) return number;

numberStart = c == '.' || c == 'e' || c == 'E';
}

return builder.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -233,19 +233,21 @@ examples {
module.catch(() -> "9223372036854775808".toInt())
module.catch(() -> "-9223372036854775809".toInt())
module.catch(() -> "abc".toInt())
module.catch(() -> "_1_000".toInt())
}

["toIntOrNull()"] {
"123".toIntOrNull()
"-123".toIntOrNull()
"1_2__3___".toInt()
"-1_2__3___".toInt()
"1_2__3___".toIntOrNull()
"-1_2__3___".toIntOrNull()
"0".toIntOrNull()
"-0".toIntOrNull()
"1.2".toIntOrNull()
"9223372036854775808".toIntOrNull()
"-9223372036854775809".toIntOrNull()
"abc".toIntOrNull()
"_1_2__3___".toIntOrNull()
}

["toFloat()"] {
Expand Down Expand Up @@ -282,13 +284,17 @@ examples {
"-.45e-9".toFloat()
"9e1024".toFloat()
"-9e1024".toFloat()
"-1_2__3___.2_e+1_".toFloat()
module.catch(() -> "abc".toFloat())
module.catch(() -> "123._34".toFloat())
module.catch(() -> "123e_34".toFloat())
}

["toFloatOrNull()"] {
"0".toFloatOrNull()
"-0".toFloatOrNull()
"abc".toFloatOrNull()
"_123_.1_".toFloatOrNull()
}

["take()"] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ examples {
"Cannot parse string as `Int`. String: \"9223372036854775808\""
"Cannot parse string as `Int`. String: \"-9223372036854775809\""
"Cannot parse string as `Int`. String: \"abc\""
"Cannot parse string as `Int`. String: \"_1_000\""
}
["toIntOrNull()"] {
123
Expand All @@ -203,6 +204,7 @@ examples {
null
null
null
null
}
["toFloat()"] {
0.0
Expand Down Expand Up @@ -238,12 +240,16 @@ examples {
-4.5E-10
Infinity
-Infinity
-1232.0
"Cannot parse string as `Float`. String: \"abc\""
"Cannot parse string as `Float`. String: \"123._34\""
"Cannot parse string as `Float`. String: \"123e_34\""
}
["toFloatOrNull()"] {
0.0
-0.0
null
null
}
["take()"] {
""
Expand Down

0 comments on commit 24cc95a

Please sign in to comment.