shithub: mc

Download patch

ref: 56273857e915a16847c703d77ac812779299612d
parent: 0cc2e13e08c6812fb2a70aae573d0f2e198ea63c
author: Ori Bernstein <ori@eigenstate.org>
date: Tue Jan 26 16:42:02 EST 2016

Add format specifier for raw byte strings.

--- a/lib/std/fmt.myr
+++ b/lib/std/fmt.myr
@@ -448,26 +448,38 @@
 
 const strfmt = {sb, str, params
 	var opts
-	var w, p, i
+	var w, p, i, raw
 
 	p = ' '
 	w = 0
+	raw = false
 	opts = parseparams(params, [
 		("w", true),
-		("p", true)][:])
+		("p", true),
+		("r", false)
+	][:])
+
 	for o in opts
 		match o
 		| ("w", wid):	w = getint(wid, "fmt: width must be integer")
 		| ("p", pad):	p = decode(pad)
+		| ("r", ""):	raw = true
 		| _:	std.die("unreachable")
 		;;
 	;;
 	iassert(p >= 0, "pad must be >= 0")
 	std.slfree(opts)
-	for i = 0; i < w - graphemewidth(str); i++
-		sbputc(sb, p)
+	if raw
+		for b in str
+			sbputs(sb, "\\x")
+			intfmt(sb, [.padto=2, .padfill='0', .base=16], false, b)
+		;;
+	else
+		for i = 0; i < w - graphemewidth(str); i++
+			sbputc(sb, p)
+		;;
+		sbputs(sb, str)
 	;;
-	sbputs(sb, str)
 }
 
 /*