Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ func formatHelper(l *State, fs string, argCount int) string {
ArgumentError(l, arg, "no value")
}
f := scanFormat(l, fs[i:])
const uint_max = ^uint(0)
const int_max = int(uint_max >> 1)
const int_min = -int_max - 1
switch i += len(f) - 2; fs[i] {
case 'c':
// Ensure each character is represented by a single byte, while preserving format modifiers.
Expand All @@ -91,18 +94,16 @@ func formatHelper(l *State, fs string, argCount int) string {
fallthrough
case 'd':
n := CheckNumber(l, arg)
ArgumentCheck(l, float64(int_min) <= n && n < float64(int_max), arg, "not a number in proper range")
ni := int(n)
diff := n - float64(ni)
ArgumentCheck(l, -1 < diff && diff < 1, arg, "not a number in proper range")
fmt.Fprintf(&b, f, ni)
case 'u': // The fmt package doesn't support %u.
f = f[:len(f)-1] + "d"
fallthrough
case 'o', 'x', 'X':
n := CheckNumber(l, arg)
ArgumentCheck(l, 0 <= n && n < float64(uint_max), arg, "not a non-negative number in proper range")
ni := uint(n)
diff := n - float64(ni)
ArgumentCheck(l, -1 < diff && diff < 1, arg, "not a non-negative number in proper range")
fmt.Fprintf(&b, f, ni)
case 'e', 'E', 'f', 'g', 'G':
fmt.Fprintf(&b, f, CheckNumber(l, arg))
Expand Down