Skip to content

Commit

Permalink
Allow leanier string to int conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
ldemailly committed Dec 11, 2024
1 parent 225f9ba commit f59dc7d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 7 additions & 1 deletion extensions/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,13 @@ func createMisc() {
}
return object.Integer{Value: r}
case object.STRING:
i, serr := strconv.ParseInt(o.(object.String).Value, 0, 64)
str := o.(object.String).Value
str = strings.TrimSpace(str)
str = strings.TrimLeft(str, "0")
if str == "" {
return object.Integer{Value: 0}
}
i, serr := strconv.ParseInt(str, 0, 64)
if serr != nil {
return s.Error(serr)
}
Expand Down
3 changes: 3 additions & 0 deletions tests/conversions.gr
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ Assert("int() is int", int(3.7) == 3)
Assert("int(true) is 1", int(true) == 1)
Assert("int(false) is 0", int(false) == 0)
Assert("int(nil) is 0", int(nil) == 0)
Assert("int(\"\") is 0", int("") == 0)
Assert("int() with whitespaces work", int("\n 123\n\t") == 123)
Assert("int(\"00123\n\") is 123", int("000123\n") == 123)

0 comments on commit f59dc7d

Please sign in to comment.