shithub: mc

Download patch

ref: 2edc2786c55409339a666cd0dc41e21099017d24
parent: 330fd3f777d230d333c58d609a0eb2330e30d6df
author: Ori Bernstein <ori@eigenstate.org>
date: Tue Feb 7 18:10:05 EST 2017

Format minimum values correctly.

	-x == -x; handle that right.

--- a/lib/std/fmt.myr
+++ b/lib/std/fmt.myr
@@ -575,7 +575,7 @@
 ]
 generic intfmt = {sb, opts, signed, bits : @a::(integral,numeric)
 	var isneg
-	var val
+	var sval, val
 	var b : char[32]
 	var i, j, npad
 	var base
@@ -582,12 +582,18 @@
 
 	base = (opts.base : uint64)
 	if signed && bits < 0
-		val = (-bits : uint64)
+		sval = -(bits : int64)
+		val = (sval : uint64)
 		isneg = true
 	else
 		val = (bits : uint64)
 		val &= ~0 >> (8*(sizeof(uint64)-sizeof(@a)))
 		isneg = false
+	;;
+	/* if its negative after inverting, we have int64 min */
+	if sval < 0
+		std.sbputs(sb, "-9223372036854775808")
+		-> void
 	;;
 
 	i = 0
--- a/lib/std/test/fmt.myr
+++ b/lib/std/test/fmt.myr
@@ -69,6 +69,12 @@
 	check("0.000101323461002", "{}", 0.000101323461002)
 	check("(1, `Bar 123, `Foo )", "{}", (1, `Bar 123, `Foo))
 
+	/* tricky cases: min values for integers */
+	check("-128", "{}", (-128 : int8))
+	check("-32768", "{}", (-32768 : int16))
+	check("-2147483648", "{}", (-2147483648 : int32))
+	check("-9223372036854775808", "{}", -9223372036854775808l)
+
 	/* 
 	compound types, followed by single value to make
 	sure we consume the right byte count.