shithub: mc

Download patch

ref: 4f57d78598daddaab2b204fe418cf217b541b70e
parent: 8fef0e65ac05920b30d13ef4816dd166c3c83c47
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Jun 6 17:38:10 EDT 2015

Convert fltfmt to use a strbuf.

--- a/libstd/fltfmt.myr
+++ b/libstd/fltfmt.myr
@@ -1,36 +1,41 @@
+use "alloc.use"
 use "bigint.use"
-use "types.use"
-use "fltbits.use"
+use "die.use"
 use "extremum.use"
-use "utf.use"
-use "alloc.use"
+use "fltbits.use"
 use "slpush.use"
-use "die.use"
+use "strbuf.use"
+use "types.use"
+use "utf.use"
 
 pkg std =
-	pkglocal const flt64bfmt	: (buf : byte[:], val : flt64, mode : int, precision : int -> size)
-	pkglocal const flt32bfmt	: (buf : byte[:], val : flt32, mode : int, precision : int -> size)
+	pkglocal const MNormal = 0
+	pkglocal const MAbsolute = 1
+	pkglocal const MRelative = 2
+
+	pkglocal const flt64bfmt	: (sb : strbuf#, val : flt64, mode : int, precision : int -> void)
+	pkglocal const flt32bfmt	: (sb : strbuf#, val : flt32, mode : int, precision : int -> void)
 ;;
 
 const Dblbias = 1023
 const Fltbias = 127
 
-const flt64bfmt = {buf, val, mode, precision
+const flt64bfmt = {sb, val, mode, precision
 	var isneg, exp, mant
 
 	(isneg, mant, exp) = flt64explode(val)
-	-> dragon4(buf, isneg, mant, (exp - 52) castto(int64), Dblbias, mode, precision)
+	dragon4(sb, isneg, mant, (exp - 52) castto(int64), Dblbias, mode, precision)
 }
 
-const flt32bfmt = {buf, val, mode, precision
+const flt32bfmt = {sb, val, mode, precision
 	var isneg, exp, mant
 
 	(isneg, mant, exp) = flt32explode(val)
-	-> dragon4(buf, isneg, mant castto(int64), (exp - 52) castto(int64), Fltbias, mode, precision)
+	dragon4(sb, isneg, mant castto(int64), (exp - 52) castto(int64), Fltbias, mode, precision)
 }
 
 /*
-buf: output buffer
+sb: output buffer
 e: exponent
 p: precision
 f: mantissa
@@ -37,7 +42,7 @@
 
 flting value: x = f^(e - p)
 */
-const dragon4 = {buf, isneg, f, e, p, mode, cutoff
+const dragon4 = {sb, isneg, f, e, p, mode, cutoff
 	var r, s, t, u, v, y
 	var udig
 	var mm, mp	/* margins above and below */
@@ -49,13 +54,10 @@
 	/* if we have zero for the mantissa, we can return early */
 	n = 0
 	if isneg
-		n += encode(buf, '-')
+		sbputs(sb, "-")
 	;;
 	if f == 0
-		n += encode(buf[n:], '0')
-		n += encode(buf[n:], '.')
-		n += encode(buf[n:], '0')
-		-> n
+		sbputs(sb, "0.0")
 	;;
 
 	/* initialize */
@@ -112,13 +114,14 @@
 				bigfree(u)
 			;;
 		;;
-		if mode == 0
-			cutoff = k - buf.len - 1
+		if mode == MNormal
+			cutoff = k
 		else
-			if mode == 2
-				cutoff += k
+			if mode == MRelative
+				cutoff += k - 1
 			;;
-			a = cutoff - k
+			/* common between relative and absolute */
+			a = cutoff - k - 1
 			y = bigdup(s)
 			if a < 0
 				for i = 0; i < a; i++
@@ -156,8 +159,7 @@
 	;;
 
 	if k <= 0
-		n += encode(buf[n:], '0')
-		n += encode(buf[n:], '.')
+		sbputs(sb, "0.")
 	;;
 	while true
 		k--
@@ -193,7 +195,7 @@
 		if low || high || k == cutoff
 			break
 		;;
-		n += format(buf[n:], lowdig(u), k)
+		format(sb, lowdig(u), k)
 		bigfree(u)
 	;;
 
@@ -200,15 +202,15 @@
 	/* format the last digit */
 	udig = lowdig(u)
 	if low && !high
-		n += format(buf[n:], udig, k)
+		format(sb, udig, k)
 	elif high && !low
-		n += format(buf[n:], udig + 1, k)
+		format(sb, udig + 1, k)
 	else
 		bigmuli(r, 2)
 		match bigcmp(r, s)
-		| `Before:	n += format(buf[n:], udig, k)
-		| `Equal:	n += format(buf[n:], udig, k)
-		| `After:	n += format(buf[n:], udig + 1, k)
+		| `Before:	format(sb, udig, k)
+		| `Equal:	format(sb, udig, k)
+		| `After:	format(sb, udig + 1, k)
 		;;
 	;;
 	-> n
@@ -221,19 +223,17 @@
 	-> 0
 }
 
-const format = {buf, d, k
+const format = {sb, d, k
 	const dig = "0123456789"
-	var n, i
+	var i
 
-	n = 0
 	if k < 0
-		for i = 0; i < -k; i++
-			n += encode(buf[n:], '0')
+		for i = 0; i < -k - 1; i++
+			sbputs(sb, "0")
 		;;
 	;;
-	buf[n++] = dig[d]
+	sbputb(sb, dig[d])
 	if k == 0
-		n += encode(buf[n:], '.')
+		sbputs(sb, ".")
 	;;
-	-> n
 }
--- a/libstd/fmt.myr
+++ b/libstd/fmt.myr
@@ -5,6 +5,7 @@
 use "extremum.use"
 use "fltfmt.use"
 use "introspect.use"
+use "strbuf.use"
 use "syswrap-ss.use"
 use "syswrap.use"
 use "types.use"
@@ -169,6 +170,7 @@
 	var p_val : byte#
         var c_val : char
 	var f_val : flt64, F_val : flt32
+	var sb
 
 	n = 0
 	while fmt.len != 0
@@ -221,11 +223,17 @@
 			| 'f':
 				(f_val, ap) = vanext(ap)
 				b = buf[n:]
-				/* FIXME(ori): bug, b[n:].len fails since b[n:] isn't an lval */
-				n += flt64bfmt(buf[n:], f_val, 0, b.len)
+				sb = mkbufsb(b)
+				flt64bfmt(sb, f_val, 0, b.len)
+				n += sb.len
+				strfmt(buf[n:], sbfin(sb), 0, ' ')
 			| 'F':
 				(F_val, ap) = vanext(ap)
-				n += flt64bfmt(buf[n:], F_val castto(flt64), 0, b.len)
+				b = buf[n:]
+				sb = mkbufsb(b)
+				flt64bfmt(sb, F_val castto(flt64), 0, b.len)
+				n += sb.len
+				strfmt(buf[n:], sbfin(sb), 0, ' ')
 			/* format integers */
 			| 'b':
 				if signed
--- a/libstd/fmt2.myr
+++ b/libstd/fmt2.myr
@@ -187,10 +187,11 @@
 		(l_val, ap) = vanext(ap)
 		intfmt(sb, intparams(params), false, ul_val)
 	| `Tyflt32:
-		(l_val, ap) = vanext(ap)
-		sbputs(sb, "flt32")
+		(f32_val, ap) = vanext(ap)
+		flt32bfmt(sb, f32_val, MNormal, 0)
 	| `Tyflt64:
-		sbputs(sb, "flt64")
+		(f64_val, ap) = vanext(ap)
+		flt64bfmt(sb, f64_val, MNormal, 0)
 	| `Tyvalist:
 		sbputs(sb, "...")
 
--- a/libstd/strbuf.myr
+++ b/libstd/strbuf.myr
@@ -4,7 +4,6 @@
 use "slcp.use"
 use "types.use"
 use "utf.use"
-use "fmt.use"
 
 pkg std =
 	type strbuf = struct