(Translated by https://www.hiragana.jp/)
Allow to toInt() to parse a string including "__" (#578) · luuvish/pkl@b5e011d · GitHub
Skip to content

Commit

Permalink
Allow to toInt() to parse a string including "__" (apple#578)
Browse files Browse the repository at this point in the history
Fix the issue where String#toInt() cannot parse a Int string
including sequence of "_" like "1_2__3___".
  • Loading branch information
taichi-ishitani committed Jul 15, 2024
1 parent cdf548c commit b5e011d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
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);
return Long.parseLong(self.replaceAll("_", ""));
} 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);
return Long.parseLong(self.replaceAll("_", ""));
} catch (NumberFormatException e) {
return VmNull.withoutDefault();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ examples {
["toInt()"] {
"123".toInt()
"-123".toInt()
"1_2__3___".toInt()
"-1_2__3___".toInt()
"0".toInt()
"-0".toInt()
module.catch(() -> "1.2".toInt())
Expand All @@ -236,6 +238,8 @@ examples {
["toIntOrNull()"] {
"123".toIntOrNull()
"-123".toIntOrNull()
"1_2__3___".toInt()
"-1_2__3___".toInt()
"0".toIntOrNull()
"-0".toIntOrNull()
"1.2".toIntOrNull()
Expand Down Expand Up @@ -445,7 +449,7 @@ examples {
"".sha256
quickBrownFox.sha256
}

["sha256Int"] {
"".sha256Int
quickBrownFox.sha256Int
Expand All @@ -460,7 +464,7 @@ examples {
"".base64
quickBrownFox.base64
}

["base64Decoded"] {
module.catch(() -> "~~~".base64Decoded)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ examples {
List(97, 98, 99, 100, 101, 102, 103)
}
["toInt()"] {
123
-123
123
-123
0
Expand All @@ -191,6 +193,8 @@ examples {
"Cannot parse string as `Int`. String: \"abc\""
}
["toIntOrNull()"] {
123
-123
123
-123
0
Expand Down

0 comments on commit b5e011d

Please sign in to comment.