replay CL 19916 and CL 19913 now that the build can handle them

TBR=r
OCL=19924
CL=19934
diff --git a/src/lib/fmt/fmt_test.go b/src/lib/fmt/fmt_test.go
new file mode 100644
index 0000000..ec1e995
--- /dev/null
+++ b/src/lib/fmt/fmt_test.go
@@ -0,0 +1,164 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package fmt
+
+import (
+	"fmt";
+	"syscall";
+	"testing";
+)
+
+export func TestFmtInterface(t *testing.T) {
+	var i1 interface{};
+	i1 = "abc";
+	s := fmt.sprintf("%s", i1);
+	if s != "abc" {
+		t.Errorf(`fmt.sprintf("%%s", empty("abc")) = %q want %q`, s, "abc");
+	}
+}
+
+type FmtTest struct {
+	fmt string;
+	val interface { };
+	out string;
+}
+
+func Bytes(s string) *[]byte {
+	b := new([]byte, len(s)+1);
+	syscall.StringToBytes(b, s);
+	return b[0:len(s)];
+}
+
+const B32 uint32 = 1<<32 - 1
+const B64 uint64 = 1<<64 - 1
+
+var fmttests = []FmtTest{
+	// basic string
+	FmtTest{ "%s",	"abc",	"abc" },
+	FmtTest{ "%x",	"abc",	"616263" },
+	FmtTest{ "%x",	"xyz",	"78797a" },
+	FmtTest{ "%X",	"xyz",	"78797A" },
+	FmtTest{ "%q",	"abc",	`"abc"` },
+
+	// basic bytes
+	FmtTest{ "%s",	Bytes("abc"),	"abc" },
+	FmtTest{ "%x",	Bytes("abc"),	"616263" },
+	FmtTest{ "%x",	Bytes("xyz"),	"78797a" },
+	FmtTest{ "%X",	Bytes("xyz"),	"78797A" },
+	FmtTest{ "%q",	Bytes("abc"),	`"abc"` },
+
+	// escaped strings
+	FmtTest{ "%#q",	`abc`,		"`abc`" },
+	FmtTest{ "%#q",	`"`,		"`\"`" },
+	FmtTest{ "1 %#q", `\n`,		"1 `\\n`" },
+	FmtTest{ "2 %#q", "\n",		`2 "\n"` },
+	FmtTest{ "%q",	`"`,		`"\""` },
+	FmtTest{ "%q",	"\a\b\f\r\n\t\v",	`"\a\b\f\r\n\t\v"` },
+	FmtTest{ "%q",	"abc\xffdef",		`"abc\xffdef"` },
+	FmtTest{ "%q",	"\u263a",	`"\u263a"` },
+	FmtTest{ "%q",	"\U0010ffff",	`"\U0010ffff"` },
+
+	// width
+	FmtTest{ "%5s",		"abc",	"  abc" },
+	FmtTest{ "%-5s",	"abc",	"abc  " },
+	FmtTest{ "%05s",	"abc",	"00abc" },
+
+	// integers
+	FmtTest{ "%d",		12345,	"12345" },
+	FmtTest{ "%d",		-12345,	"-12345" },
+	FmtTest{ "%10d",	12345,	"     12345" },
+	FmtTest{ "%10d",	-12345,	"    -12345" },
+	FmtTest{ "%+10d",	12345,	"    +12345" },
+	FmtTest{ "%010d",	12345,	"0000012345" },
+	FmtTest{ "%010d",	-12345,	"-000012345" },
+	FmtTest{ "%-10d",	12345,	"12345     " },
+	FmtTest{ "%010.3d",	1,	"       001" },
+	FmtTest{ "%010.3d",	-1,	"      -001" },
+	FmtTest{ "%+d",		12345,	"+12345" },
+	FmtTest{ "%+d",		-12345,	"-12345" },
+	FmtTest{ "% d",		12345,	" 12345" },
+	FmtTest{ "% d",		-12345,	"-12345" },
+
+	// old test/fmt_test.go
+	FmtTest{ "%d",		1234,			"1234" },
+	FmtTest{ "%d",		-1234,			"-1234" },
+	FmtTest{ "%d",		uint(1234),		"1234" },
+	FmtTest{ "%d",		uint32(B32),		"4294967295" },
+	FmtTest{ "%d",		uint64(B64),		"18446744073709551615" },
+	FmtTest{ "%o",		01234,			"1234" },
+	FmtTest{ "%o",		uint32(B32),		"37777777777" },
+	FmtTest{ "%o",		uint64(B64),		"1777777777777777777777" },
+	FmtTest{ "%x",		0x1234abcd,		"1234abcd" },
+	FmtTest{ "%x",		B32-0x1234567,		"fedcba98" },
+	FmtTest{ "%X",		0x1234abcd,		"1234ABCD" },
+	FmtTest{ "%X",		B32-0x1234567,		"FEDCBA98" },
+	FmtTest{ "%x",		B64,			"ffffffffffffffff" },
+	FmtTest{ "%b",		7,			"111" },
+	FmtTest{ "%b",		B64,			"1111111111111111111111111111111111111111111111111111111111111111" },
+	FmtTest{ "%e",		float64(1),		"1.000000e+00" },
+	FmtTest{ "%e",		float64(1234.5678e3),	"1.234568e+06" },
+	FmtTest{ "%e",		float64(1234.5678e-8),	"1.234568e-05" },
+	FmtTest{ "%e",		float64(-7),		"-7.000000e+00" },
+	FmtTest{ "%e",		float64(-1e-9),		"-1.000000e-09" },
+	FmtTest{ "%f",		float64(1234.5678e3),	"1234567.800000" },
+	FmtTest{ "%f",		float64(1234.5678e-8),	"0.000012" },
+	FmtTest{ "%f",		float64(-7),		"-7.000000" },
+	FmtTest{ "%f",		float64(-1e-9),		"-0.000000" },
+	FmtTest{ "%g",		float64(1234.5678e3),	"1.2345678e+06" },
+	FmtTest{ "%g",		float32(1234.5678e3),	"1.2345678e+06" },
+	FmtTest{ "%g",		float64(1234.5678e-8),	"1.2345678e-05" },
+	FmtTest{ "%g",		float64(-7),		"-7" },
+	FmtTest{ "%g",		float64(-1e-9),		"-1e-09",	 },
+	FmtTest{ "%g",		float32(-1e-9),		"-1e-09" },
+	FmtTest{ "%c",		'x',			"x" },
+	FmtTest{ "%c",		0xe4,			"ä" },
+	FmtTest{ "%c",		0x672c,			"本" },
+	FmtTest{ "%c",		'日',			"日" },
+	FmtTest{ "%20.8d",	1234,			"            00001234" },
+	FmtTest{ "%20.8d",	-1234,			"           -00001234" },
+	FmtTest{ "%20d",	1234,			"                1234" },
+	FmtTest{ "%-20.8d",	1234,			"00001234            " },
+	FmtTest{ "%-20.8d",	-1234,			"-00001234           " },
+	FmtTest{ "%.20b",	7,			"00000000000000000111" },
+	FmtTest{ "%20.5s",	"qwertyuiop",		"               qwert" },
+	FmtTest{ "%.5s",	"qwertyuiop",		"qwert" },
+	FmtTest{ "%-20.5s",	"qwertyuiop",		"qwert               " },
+	FmtTest{ "%20c",	'x',			"                   x" },
+	FmtTest{ "%-20c",	'x',			"x                   " },
+	FmtTest{ "%20.6e",	1.2345e3,		"        1.234500e+03" },
+	FmtTest{ "%20.6e",	1.2345e-3,		"        1.234500e-03" },
+	FmtTest{ "%20e",	1.2345e3,		"        1.234500e+03" },
+	FmtTest{ "%20e",	1.2345e-3,		"        1.234500e-03" },
+	FmtTest{ "%20.8e",	1.2345e3,		"      1.23450000e+03" },
+	FmtTest{ "%20f",	float64(1.23456789e3),	"         1234.567890" },
+	FmtTest{ "%20f",	float64(1.23456789e-3),	"            0.001235" },
+	FmtTest{ "%20f",	float64(12345678901.23456789),	"  12345678901.234568" },
+	FmtTest{ "%-20f",	float64(1.23456789e3),	"1234.567890         " },
+	FmtTest{ "%20.8f",	float64(1.23456789e3),	"       1234.56789000" },
+	FmtTest{ "%20.8f",	float64(1.23456789e-3),	"          0.00123457" },
+	FmtTest{ "%g",		float64(1.23456789e3),	"1234.56789" },
+	FmtTest{ "%g",		float64(1.23456789e-3),	"0.00123456789" },
+	FmtTest{ "%g",		float64(1.23456789e20),	"1.23456789e+20" },
+	FmtTest{ "%20e",	sys.Inf(1),		"                +Inf" },
+	FmtTest{ "%-20f",	sys.Inf(-1),		"-Inf                " },
+	FmtTest{ "%20g",	sys.NaN(),		"                 NaN" },
+}
+
+export func TestSprintf(t *testing.T) {
+	for i := 0; i < len(fmttests); i++ {
+		tt := fmttests[i];
+		s := fmt.sprintf(tt.fmt, tt.val);
+		if s != tt.out {
+			if ss, ok := tt.val.(string); ok {
+				// Don't requote the already-quoted strings.
+				// It's too confusing to read the errors.
+				t.Errorf("fmt.sprintf(%q, %q) = %s want %s", tt.fmt, tt.val, s, tt.out);
+			} else {
+				t.Errorf("fmt.sprintf(%q, %v) = %q want %q", tt.fmt, tt.val, s, tt.out);
+			}
+		}
+	}
+}
+
diff --git a/src/lib/fmt/format.go b/src/lib/fmt/format.go
index 058c619..64d6c9b 100644
--- a/src/lib/fmt/format.go
+++ b/src/lib/fmt/format.go
@@ -4,7 +4,9 @@
 
 package fmt
 
-import "strconv"
+import (
+	"strconv";
+)
 
 /*
 	Raw formatter. See print.go for a more palatable interface.
@@ -39,11 +41,22 @@
 	wid_present bool;
 	prec int;
 	prec_present bool;
+	// flags
+	minus bool;
+	plus bool;
+	sharp bool;
+	space bool;
+	zero bool;
 }
 
 func (f *Fmt) clearflags() {
 	f.wid_present = false;
 	f.prec_present = false;
+	f.minus = false;
+	f.plus = false;
+	f.sharp = false;
+	f.space = false;
+	f.zero = false;
 }
 
 func (f *Fmt) clearbuf() {
@@ -101,24 +114,28 @@
 	return f;
 }
 
-// append s to buf, padded on left (w > 0) or right (w < 0)
+// append s to buf, padded on left (w > 0) or right (w < 0 or f.minus)
 // padding is in bytes, not characters (agrees with ANSIC C, not Plan 9 C)
 func (f *Fmt) pad(s string) {
 	if f.wid_present && f.wid != 0 {
-		left := true;
+		left := !f.minus;
 		w := f.wid;
 		if w < 0 {
 			left = false;
 			w = -w;
 		}
 		w -= len(s);
+		padchar := byte(' ');
+		if left && f.zero {
+			padchar = '0';
+		}
 		if w > 0 {
 			if w > NByte {
 				w = NByte;
 			}
 			buf := new([]byte, w);
 			for i := 0; i < w; i++ {
-				buf[i] = ' ';
+				buf[i] = padchar;
 			}
 			if left {
 				s = string(buf) + s;
@@ -163,16 +180,35 @@
 	if negative {
 		a = -a;
 	}
-	i := putint(&buf, NByte-1, uint64(base), uint64(a), digits);
+
+	// two ways to ask for extra leading zero digits: %.3d or %03d.
+	// apparently the first cancels the second.
+	prec := 0;
 	if f.prec_present {
-		for i > 0 && f.prec > (NByte-1-i) {
-			buf[i] = '0';
-			i--;
+		prec = f.prec;
+		f.zero = false;
+	} else if f.zero && f.wid_present && !f.minus && f.wid > 0{
+		prec = f.wid;
+		if negative || f.plus || f.space {
+			prec--;  // leave room for sign
 		}
 	}
+
+	i := putint(&buf, NByte-1, uint64(base), uint64(a), digits);
+	for i > 0 && prec > (NByte-1-i) {
+		buf[i] = '0';
+		i--;
+	}
+
 	if negative {
 		buf[i] = '-';
 		i--;
+	} else if f.plus {
+		buf[i] = '+';
+		i--;
+	} else if f.space {
+		buf[i] = ' ';
+		i--;
 	}
 	return string(buf)[i+1:NByte];
 }
@@ -334,6 +370,44 @@
 	return f;
 }
 
+// hexadecimal string
+func (f *Fmt) sx(s string) *Fmt {
+	t := "";
+	for i := 0; i < len(s); i++ {
+		v := s[i];
+		t += string(ldigits[v>>4]);
+		t += string(ldigits[v&0xF]);
+	}
+	f.pad(t);
+	f.clearflags();
+	return f;
+}
+
+func (f *Fmt) sX(s string) *Fmt {
+	t := "";
+	for i := 0; i < len(s); i++ {
+		v := s[i];
+		t += string(udigits[v>>4]);
+		t += string(udigits[v&0xF]);
+	}
+	f.pad(t);
+	f.clearflags();
+	return f;
+}
+
+// quoted string
+func (f *Fmt) q(s string) *Fmt {
+	var quoted string;
+	if f.sharp && strconv.CanBackquote(s) {
+		quoted = "`"+s+"`";
+	} else {
+		quoted = strconv.Quote(s);
+	}
+	f.pad(quoted);
+	f.clearflags();
+	return f;
+}
+
 // floating-point
 
 func Prec(f *Fmt, def int) int {
@@ -370,7 +444,7 @@
 // cannot defer to float64 versions
 // because it will get rounding wrong in corner cases.
 func (f *Fmt) e32(a float32) *Fmt {
-	return FmtString(f, strconv.ftoa32(a, 'e', Prec(f, -1)));
+	return FmtString(f, strconv.ftoa32(a, 'e', Prec(f, 6)));
 }
 
 func (f *Fmt) f32(a float32) *Fmt {
diff --git a/src/lib/fmt/print.go b/src/lib/fmt/print.go
index ce7a4f2..5a2dc67 100644
--- a/src/lib/fmt/print.go
+++ b/src/lib/fmt/print.go
@@ -186,6 +186,19 @@
 	return s;
 }
 
+
+// Get the i'th arg of the struct value.
+// If the arg itself is an interface, return a value for
+// the thing inside the interface, not the interface itself.
+func getField(v reflect.StructValue, i int) reflect.Value {
+	val := v.Field(i);
+	if val.Kind() == reflect.InterfaceKind {
+		inter := val.(reflect.InterfaceValue).Get();
+		return reflect.NewValue(inter);
+	}
+	return val;
+}
+
 // Getters for the fields of the argument structure.
 
 func getBool(v reflect.Value) (val bool, ok bool) {
@@ -227,6 +240,9 @@
 	case reflect.StringKind:
 		return v.(reflect.StringValue).Get(), true;
 	}
+	if valb, okb := v.Interface().(*[]byte); okb {
+		return string(valb), true;
+	}
 	return "", false;
 }
 
@@ -280,12 +296,6 @@
 	if start >= end {
 		return 0, false, end
 	}
-	if s[start] == '-' {
-		a, b, c := parsenum(s, start+1, end);
-		if b {
-			return -a, b, c;
-		}
-	}
 	isnum := false;
 	num := 0;
 	for '0' <= s[start] && s[start] <= '9' {
@@ -371,10 +381,28 @@
 			i += w;
 			continue;
 		}
-		// saw % - do we have %20 (width)?
-		p.wid, p.wid_ok, i = parsenum(format, i+1, end);
+		i++;
+		// flags
+		F: for ; i < end; i++ {
+			switch format[i] {
+			case '#':
+				p.fmt.sharp = true;
+			case '0':
+				p.fmt.zero = true;
+			case '+':
+				p.fmt.plus = true;
+			case '-':
+				p.fmt.minus = true;
+			case ' ':
+				p.fmt.space = true;
+			default:
+				break F;
+			}
+		}
+		// do we have 20 (width)?
+		p.wid, p.wid_ok, i = parsenum(format, i, end);
 		p.prec_ok = false;
-		// do we have %.20 (precision)?
+		// do we have .20 (precision)?
 		if i < end && format[i] == '.' {
 			p.prec, p.prec_ok, i = parsenum(format, i+1, end);
 		}
@@ -391,7 +419,7 @@
 			p.addstr("(missing)");
 			continue;
 		}
-		field := v.Field(fieldnum);
+		field := getField(v, fieldnum);
 		fieldnum++;
 		if c != 'T' {	// don't want thing to describe itself if we're asking for its type
 			if formatter, ok := field.Interface().(Format); ok {
@@ -463,6 +491,20 @@
 					} else {
 						s = p.fmt.ux64(uint64(v)).str()
 					}
+				} else if v, ok := getString(field); ok {
+					s = p.fmt.sx(v).str();
+				} else {
+					goto badtype
+				}
+			case 'X':
+				if v, signed, ok := getInt(field); ok {
+					if signed {
+						s = p.fmt.X64(v).str()
+					} else {
+						s = p.fmt.uX64(uint64(v)).str()
+					}
+				} else if v, ok := getString(field); ok {
+					s = p.fmt.sX(v).str();
 				} else {
 					goto badtype
 				}
@@ -500,6 +542,12 @@
 				} else {
 					goto badtype
 				}
+			case 'q':
+				if v, ok := getString(field); ok {
+					s = p.fmt.q(v).str()
+				} else {
+					goto badtype
+				}
 
 			// pointer
 			case 'p':
@@ -530,7 +578,7 @@
 	if fieldnum < v.Len() {
 		p.addstr("?(extra ");
 		for ; fieldnum < v.Len(); fieldnum++ {
-			p.addstr(v.Field(fieldnum).Type().String());
+			p.addstr(getField(v, fieldnum).Type().String());
 			if fieldnum + 1 < v.Len() {
 				p.addstr(", ");
 			}
@@ -543,7 +591,7 @@
 	prev_string := false;
 	for fieldnum := 0; fieldnum < v.Len();  fieldnum++ {
 		// always add spaces if we're doing println
-		field := v.Field(fieldnum);
+		field := getField(v, fieldnum);
 		if fieldnum > 0 {
 			if addspace {
 				p.add(' ')