From f59dc7d3d3a56c31b5a1caed88a61c10f86eea8c Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Wed, 11 Dec 2024 08:42:39 -0800 Subject: [PATCH] Allow leanier string to int conversion --- extensions/extension.go | 8 +++++++- tests/conversions.gr | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/extensions/extension.go b/extensions/extension.go index ec9b2dc..b806777 100644 --- a/extensions/extension.go +++ b/extensions/extension.go @@ -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) } diff --git a/tests/conversions.gr b/tests/conversions.gr index c22792a..0e500ed 100644 --- a/tests/conversions.gr +++ b/tests/conversions.gr @@ -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)