From fe8439f9fc4ff6e39a928c88fb7157e3243062c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20G=C3=B6kdemir?= Date: Fri, 6 Nov 2026 17:29:44 +0300 Subject: [PATCH] fix: preserve 64-bit string format integers --- string.go | 6 +++--- string_test.go | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 string_test.go diff --git a/string.go b/string.go index ad389f3..0a41082 100644 --- a/string.go +++ b/string.go @@ -93,18 +93,18 @@ func formatHelper(l *State, fs string, argCount int) string { case 'd': n := CheckNumber(l, arg) ArgumentCheck(l, math.Floor(n) == n && -math.Pow(2, 63) <= n && n < math.Pow(2, 63), arg, "number has no integer representation") - ni := int(n) + ni := int64(n) fmt.Fprintf(&b, f, ni) case 'u': // The fmt package doesn't support %u. f = f[:len(f)-1] + "d" n := CheckNumber(l, arg) ArgumentCheck(l, math.Floor(n) == n && 0.0 <= n && n < math.Pow(2, 64), arg, "not a non-negative number in proper range") - ni := uint(n) + ni := uint64(n) fmt.Fprintf(&b, f, ni) case 'o', 'x', 'X': n := CheckNumber(l, arg) ArgumentCheck(l, 0.0 <= n && n < math.Pow(2, 64), arg, "not a non-negative number in proper range") - ni := uint(n) + ni := uint64(n) fmt.Fprintf(&b, f, ni) case 'e', 'E', 'f', 'g', 'G': fmt.Fprintf(&b, f, CheckNumber(l, arg)) diff --git a/string_test.go b/string_test.go new file mode 100644 index 0000000..67bc1ca --- /dev/null +++ b/string_test.go @@ -0,0 +1,14 @@ +package lua + +import "testing" + +func TestFormatLargeIntegers(t *testing.T) { + testString(t, ` + assert(string.format("%012d", 34567890121111) == "34567890121111") + assert(string.format("%i", -34567890121111) == "-34567890121111") + assert(string.format("%u", 34567890121111) == "34567890121111") + assert(string.format("%o", 34567890121111) == "767016663730627") + assert(string.format("%x", 34567890121111) == "1f7076cfb197") + assert(string.format("%X", 34567890121111) == "1F7076CFB197") + `) +}