shithub: mc

Download patch

ref: 6c8c2b6b32f8b567703dea1aed5ea1fc9e5caf23
parent: c7f8059e7ca08d7f31451090b96c6355724a164b
author: Ori Bernstein <[email protected]>
date: Sun Aug 5 23:43:27 EDT 2012

Copy input to output of format strings.

--- a/fmt.myr
+++ b/fmt.myr
@@ -1,7 +1,7 @@
 use "die.use"
 use "sys.use"
 use "types.use"
-use "str.use"
+use "utf.use"
 
 pkg std =
 	const bfmt	: (buf : byte[,], fmt : byte[,], args:... -> size)
@@ -8,7 +8,7 @@
 	const fmt	: (fmt : byte[,], args:... -> size)
 ;;
 
-const fmt = {fmt, args
+const bfmt = {buf, fmt, args
 	var c
 	var n
 
@@ -19,17 +19,27 @@
 			(c, fmt) = striter(fmt)
 			match c
 			's':
-				write(1, "str")
+				write(1, "str\n")
 				;;
 			'd':
-				write(1, "int")
+				write(1, "int\n")
 				;;
 			'p':
-				write(1, "ptr")
+				write(1, "ptr\n")
 				;;
 			;;
+		else
+			n += encode(buf[n,buf.len], c)
 		;;
 	;;
-	write(1, "\n")
+	-> n
+}
+
+const fmt = {fmt, args
+	var buf : byte[2048]
+	var n
+	
+	n = bfmt(buf[0,2048], fmt, args)
+	write(1, buf[0,n])
 	-> n
 }
--- a/test.myr
+++ b/test.myr
@@ -45,13 +45,13 @@
 		else
 			std.write(1, "Dunno\n")
 		;;
-		if !std.encode(c, buf[0,std.charlen(c)])
+		if !std.encode(buf[0,std.charlen(c)], c)
 			std.write(1, "couldn't encode\n")
 		;;
 		std.write(1, buf[0,std.charlen(c)])
 		std.write(1, "\n")
 	;;
-	if !std.encode(-1, buf[0,3])
+	if !std.encode(buf[0,3], -1)
 		std.write(1, "couldn't encode\n")
 	;;
 }
--- a/utf.myr
+++ b/utf.myr
@@ -6,7 +6,7 @@
 	const Badchar	: char = -1 castto(char)
 
 	const charlen	: (chr : char -> size)
-	const encode	: (chr : char, buf : byte[,] -> bool)
+	const encode	: (buf : byte[,], chr : char -> size)
 	const decode	: (buf : byte[,] -> char)
 	const striter	: (str : byte[,] -> [char, byte[,]])
 
@@ -30,7 +30,7 @@
 	;;
 }
 
-const encode = {c, buf
+const encode = {buf, c
 	var len
 	var mark
 	var i
@@ -37,7 +37,7 @@
 
 	len = charlen(c)
 	if len < 0 || buf.len < len
-		-> false
+		-> -1
 	;;
 
 	if (len == 1)
@@ -52,7 +52,7 @@
 	;;
 
 	buf[0] = (c | mark) castto(byte)
-	-> true
+	-> len
 }
 
 const decode = {buf