shithub: mc

Download patch

ref: 2a3b64a5642729ab3bf83a21ea6bf59fc2a88f2c
parent: 480b380507201923c5b0d4e8610499e23ee2bea6
author: Ori Bernstein <[email protected]>
date: Wed Apr 3 13:05:14 EDT 2013

Add functions for buffered formatting.

--- a/libstd/alloc.myr
+++ b/libstd/alloc.myr
@@ -2,7 +2,6 @@
 use "sys.use"
 use "types.use"
 use "extremum.use"
-use "fmt.use"
 
 /* 
 The allocator implementation here is based on Bonwick's slab allocator.
--- a/libstd/fmt.myr
+++ b/libstd/fmt.myr
@@ -1,3 +1,4 @@
+use "alloc.use"
 use "die.use"
 use "sys.use"
 use "types.use"
@@ -19,36 +20,59 @@
 */
 
 pkg std =
-	const bfmt	: (buf : byte[:], fmt : byte[:], args : ... -> size)
-	const bfmtv	: (buf : byte[:], fmt : byte[:], ap : valist -> size)
 	const put	: (fmt : byte[:], args : ... -> size)
 	const putv	: (fmt : byte[:], ap : valist -> size)
 	const fatal	: (status : int, fmt : byte[:], args : ... -> void)
 	const fatalv	: (status : int, fmt : byte[:], ap : valist -> void)
+	const fmt	: (fmt : byte[:], args : ... -> byte[:])
+	const fmtv	: (fmt : byte[:], ap : valist -> byte[:])
+	const bfmt	: (buf : byte[:], fmt : byte[:], args : ... -> size)
+	const bfmtv	: (buf : byte[:], fmt : byte[:], ap : valist -> size)
 ;;
 
+/* Writes a string of text up to 2 kb in size to stdout */
+const put = {fmt, args
+	-> putv(fmt, vastart(&args))
+}
+
+/* Writes a string of text up to 2kb long to stdout, using a valist
+   as the source of the arguments */
+const putv = {fmt, ap
+	var buf : byte[2048]
+	var n
+	
+	n = bfmtv(buf[:], fmt, ap)
+	write(1, buf[:n])
+	-> n
+}
+
+/* same as 'put', but exits the program after printing */
 const fatal = {status, fmt, args
 	putv(fmt, vastart(&args))
 	exit(status)
 }
 
+/* same as 'putv', but exits the program after printing */
 const fatalv = {status, fmt, ap
 	putv(fmt, ap)
 	exit(status)
 }
 
-const put = {fmt, args
-	-> putv(fmt, vastart(&args))
+/* formats a string, allocating the slice. FIXME: calculate the
+   size needed. */
+const fmt = {fmt, args
+	-> fmtv(fmt, vastart(&args))
 }
 
-/* Writes a string of text up to 2 kb in size to stdout */
-const putv = {fmt, ap
-	var buf : byte[2048]
-	var n
-	
-	n = bfmtv(buf[:], fmt, ap)
-	write(1, buf[:n])
-	-> n
+/* formats a string, allocating the slice. FIXME: calculate the
+   size needed. Takes a valist as it's last argument. */
+const fmtv = {fmt, ap
+	var buf
+	var sz
+
+	buf = slalloc(2048)
+	sz = bfmtv(buf, fmt, ap)
+	-> buf[:sz]
 }
 
 /* formats a string of text as specified by 'fmt' into 'buf' */