shithub: mc

Download patch

ref: 5655bd1555ab9555e2e4f634a8812864f74b98d1
parent: 6b081b7a22e36333259e0ed5bd03803b65df9c6d
author: Ori Bernstein <[email protected]>
date: Sat Jun 21 09:45:27 EDT 2014

Correct support for zero/space padded negative int

    We would either print '-     123' or 000000-123'
    in the past. Fixed.

--- a/libstd/fmt.myr
+++ b/libstd/fmt.myr
@@ -115,6 +115,12 @@
 
 	npad = clamp(padto - i, 0, padto)
 	n = 0
+	if isneg
+		npad--
+	;;
+	if padfill == '0' && isneg && n < buf.len
+		n += encode(buf[n:], '-')
+	;;
 	for j = 0; j < min(npad, buf.len); j++
 		if n >= buf.len
 			break
@@ -121,7 +127,7 @@
 		;;
 		n += encode(buf[n:], padfill)
 	;;
-	if isneg && n < buf.len
+	if padfill != '0' && isneg && n < buf.len
 		n += encode(buf[n:], '-')
 	;;
 	for j = i; j != 0; j--
@@ -162,7 +168,7 @@
 			padfill = ' '
 			(c, fmt) = striter(fmt)
 			/* modifiers */
-			if fmt.len > 0
+			while fmt.len > 0
 				match c
 				| 'x':
 					(c, fmt) = striter(fmt)
@@ -176,7 +182,7 @@
 					(c, fmt) = striter(fmt)
 					padfill = '0'
 				;;
-				if isdigit(c)
+				if isdigit(c) && padto == 0
 					/*
 					We can't get a 0 on the first iteration, since
 					that was matched above. So, no special checks
@@ -187,6 +193,8 @@
 						padto = padto*10 + charval(c, 10)
 						(c, fmt) = striter(fmt)
 					;;
+				else
+					break
 				;;
 			;;
 			/* format specifiers */
--- a/test/stdfmtpad.myr
+++ b/test/stdfmtpad.myr
@@ -7,5 +7,8 @@
 	std.put("%01s\n", "a")
 	std.put("%10i\n", 10)
 	std.put("%010i\n", 10)
+	std.put("%010ui\n", -1)
+	std.put("%010i\n", -1)
+	std.put("%10i\n", -1)
 	std.put("%3i\n", 100000)
 }