import (
"fmt";
"os";
- "strconv"
+ "strconv";
)
// TODO(r): BUG: atob belongs elsewhere
func atob(str string) (value bool, ok bool) {
switch str {
- case "1", "t", "T", "true", "TRUE", "True":
- return true, true;
- case "0", "f", "F", "false", "FALSE", "False":
- return false, true
+ case "1", "t", "T", "true", "TRUE", "True":
+ return true, true;
+ case "0", "f", "F", "false", "FALSE", "False":
+ return false, true;
}
- return false, false
+ return false, false;
}
// -- Bool Value
func newBoolValue(val bool, p *bool) *boolValue {
*p = val;
- return &boolValue{p}
+ return &boolValue{p};
}
func (b *boolValue) set(s string) bool {
- v, ok := atob(s);
+ v, ok := atob(s);
*b.p = v;
- return ok
+ return ok;
}
func (b *boolValue) String() string {
- return fmt.Sprintf("%v", *b.p)
+ return fmt.Sprintf("%v", *b.p);
}
// -- Int Value
type intValue struct {
- p *int;
+ p *int;
}
func newIntValue(val int, p *int) *intValue {
*p = val;
- return &intValue{p}
+ return &intValue{p};
}
func (i *intValue) set(s string) bool {
- v, err := strconv.Atoi(s);
+ v, err := strconv.Atoi(s);
*i.p = int(v);
- return err == nil
+ return err == nil;
}
func (i *intValue) String() string {
- return fmt.Sprintf("%v", *i.p)
+ return fmt.Sprintf("%v", *i.p);
}
// -- Int64 Value
type int64Value struct {
- p *int64;
+ p *int64;
}
func newInt64Value(val int64, p *int64) *int64Value {
*p = val;
- return &int64Value{p}
+ return &int64Value{p};
}
func (i *int64Value) set(s string) bool {
- v, err := strconv.Atoi64(s);
+ v, err := strconv.Atoi64(s);
*i.p = v;
return err == nil;
}
func (i *int64Value) String() string {
- return fmt.Sprintf("%v", *i.p)
+ return fmt.Sprintf("%v", *i.p);
}
// -- Uint Value
type uintValue struct {
- p *uint;
+ p *uint;
}
func newUintValue(val uint, p *uint) *uintValue {
*p = val;
- return &uintValue{p}
+ return &uintValue{p};
}
func (i *uintValue) set(s string) bool {
- v, err := strconv.Atoui(s);
+ v, err := strconv.Atoui(s);
*i.p = uint(v);
return err == nil;
}
func (i *uintValue) String() string {
- return fmt.Sprintf("%v", *i.p)
+ return fmt.Sprintf("%v", *i.p);
}
// -- uint64 Value
type uint64Value struct {
- p *uint64;
+ p *uint64;
}
func newUint64Value(val uint64, p *uint64) *uint64Value {
*p = val;
- return &uint64Value{p}
+ return &uint64Value{p};
}
func (i *uint64Value) set(s string) bool {
}
func (i *uint64Value) String() string {
- return fmt.Sprintf("%v", *i.p)
+ return fmt.Sprintf("%v", *i.p);
}
// -- string Value
type stringValue struct {
- p *string;
+ p *string;
}
func newStringValue(val string, p *string) *stringValue {
*p = val;
- return &stringValue{p}
+ return &stringValue{p};
}
func (s *stringValue) set(val string) bool {
}
func (s *stringValue) String() string {
- return fmt.Sprintf("%s", *s.p)
+ return fmt.Sprintf("%s", *s.p);
}
// FlagValue is the interface to the dynamic value stored in a flag.
// A Flag represents the state of a flag.
type Flag struct {
- Name string; // name as it appears on command line
- Usage string; // help message
- Value FlagValue; // value as set
- DefValue string; // default value (as text); for usage message
+ Name string; // name as it appears on command line
+ Usage string; // help message
+ Value FlagValue; // value as set
+ DefValue string; // default value (as text); for usage message
}
type allFlags struct {
- actual map[string] *Flag;
- formal map[string] *Flag;
+ actual map[string]*Flag;
+ formal map[string]*Flag;
first_arg int; // 0 is the program name, 1 is first arg
}
-var flags *allFlags = &allFlags{make(map[string] *Flag), make(map[string] *Flag), 1}
+var flags *allFlags = &allFlags{make(map[string]*Flag), make(map[string]*Flag), 1}
// VisitAll visits the flags, calling fn for each. It visits all flags, even those not set.
func VisitAll(fn func(*Flag)) {
for _, f := range flags.formal {
- fn(f)
+ fn(f);
}
}
// Visit visits the flags, calling fn for each. It visits only those flags that have been set.
func Visit(fn func(*Flag)) {
for _, f := range flags.actual {
- fn(f)
+ fn(f);
}
}
func Lookup(name string) *Flag {
f, ok := flags.formal[name];
if !ok {
- return nil
+ return nil;
}
- return f
+ return f;
}
// Set sets the value of the named flag. It returns true if the set succeeded; false if
func Set(name, value string) bool {
f, ok := flags.formal[name];
if !ok {
- return false
+ return false;
}
ok = f.Value.set(value);
if !ok {
- return false
+ return false;
}
flags.actual[name] = f;
return true;
format = " -%s=%q: %s\n";
}
fmt.Fprintf(os.Stderr, format, f.Name, f.DefValue, f.Usage);
- })
+ });
}
// Usage prints to standard error a default usage message documenting all defined flags.
}
func NFlag() int {
- return len(flags.actual)
+ return len(flags.actual);
}
// Arg returns the i'th command-line argument. Arg(0) is the first remaining argument
if i < 0 || i >= len(os.Args) {
return "";
}
- return os.Args[i]
+ return os.Args[i];
}
// NArg is the number of arguments remaining after flags have been processed.
func NArg() int {
- return len(os.Args) - flags.first_arg
+ return len(os.Args) - flags.first_arg;
}
// Args returns the non-flag command-line arguments.
func Args() []string {
- return os.Args[flags.first_arg:len(os.Args)];
+ return os.Args[flags.first_arg : len(os.Args)];
}
func add(name string, value FlagValue, usage string) {
return p;
}
-func (f *allFlags) parseOne(index int) (ok bool, next int)
-{
+func (f *allFlags) parseOne(index int) (ok bool, next int) {
s := os.Args[index];
- f.first_arg = index; // until proven otherwise
+ f.first_arg = index; // until proven otherwise
if len(s) == 0 {
- return false, -1
+ return false, -1;
}
if s[0] != '-' {
- return false, -1
+ return false, -1;
}
num_minuses := 1;
if len(s) == 1 {
- return false, index
+ return false, index;
}
if s[1] == '-' {
num_minuses++;
if len(s) == 2 { // "--" terminates the flags
- return false, index + 1
+ return false, index+1;
}
}
- name := s[num_minuses : len(s)];
+ name := s[num_minuses:len(s)];
if len(name) == 0 || name[0] == '-' || name[0] == '=' {
fmt.Fprintln(os.Stderr, "bad flag syntax:", s);
Usage();
// it's a flag. does it have an argument?
has_value := false;
value := "";
- for i := 1; i < len(name); i++ { // equals cannot be first
+ for i := 1; i < len(name); i++ { // equals cannot be first
if name[i] == '=' {
value = name[i+1 : len(name)];
has_value = true;
- name = name[0 : i];
+ name = name[0:i];
break;
}
}
os.Exit(2);
}
m := flags.formal;
- flag, alreadythere = m[name]; // BUG
+ flag, alreadythere = m[name]; // BUG
if !alreadythere {
fmt.Fprintf(os.Stderr, "flag provided but not defined: -%s\n", name);
Usage();
os.Exit(2);
}
} else {
- f.set("true")
+ f.set("true");
}
} else {
// It must have a value, which might be the next argument.
- if !has_value && index < len(os.Args)-1 {
+ if !has_value && index < len(os.Args) - 1 {
// value is the next arg
has_value = true;
index++;
}
}
flags.actual[name] = flag;
- return true, index + 1
+ return true, index+1;
}
// Parse parses the command-line flags. Must be called after all flags are defined
i = next;
}
if !ok {
- break
+ break;
}
}
}
package fmt_test
import (
- . "fmt";
- "io";
- "math";
- "strings";
- "testing";
+ . "fmt";
+ "io";
+ "math";
+ "strings";
+ "testing";
)
func TestFmtInterface(t *testing.T) {
- var i1 interface{};
+ var i1 interface{}
i1 = "abc";
s := Sprintf("%s", i1);
if s != "abc" {
}
type fmtTest struct {
- fmt string;
- val interface { };
- out string;
+ fmt string;
+ val interface{};
+ out string;
}
const b32 uint32 = 1<<32 - 1
const b64 uint64 = 1<<64 - 1
+
var array = []int{1, 2, 3, 4, 5}
var iarray = []interface{}{1, "hello", 2.5, nil}
type A struct {
- i int;
- j uint;
- s string;
- x []int;
+ i int;
+ j uint;
+ s string;
+ x []int;
}
-var b byte;
+var b byte
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"` },
+ fmtTest{"%s", "abc", "abc"},
+ fmtTest{"%x", "abc", "616263"},
+ fmtTest{"%x", "xyz", "78797a"},
+ fmtTest{"%X", "xyz", "78797A"},
+ fmtTest{"%q", "abc", `"abc"`},
// basic bytes
- fmtTest{ "%s", strings.Bytes("abc"), "abc" },
- fmtTest{ "%x", strings.Bytes("abc"), "616263" },
- fmtTest{ "% x", strings.Bytes("abc"), "61 62 63" },
- fmtTest{ "%x", strings.Bytes("xyz"), "78797a" },
- fmtTest{ "%X", strings.Bytes("xyz"), "78797A" },
- fmtTest{ "%q", strings.Bytes("abc"), `"abc"` },
+ fmtTest{"%s", strings.Bytes("abc"), "abc"},
+ fmtTest{"%x", strings.Bytes("abc"), "616263"},
+ fmtTest{"% x", strings.Bytes("abc"), "61 62 63"},
+ fmtTest{"%x", strings.Bytes("xyz"), "78797a"},
+ fmtTest{"%X", strings.Bytes("xyz"), "78797A"},
+ fmtTest{"%q", strings.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"` },
+ 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" },
+ 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"},
+ 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"},
// erroneous formats
- fmtTest{ "", 2, "?(extra int=2)" },
- fmtTest{ "%d", "hello", "%d(string=hello)" },
+ fmtTest{"", 2, "?(extra int=2)"},
+ fmtTest{"%d", "hello", "%d(string=hello)"},
// 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", 01234, "01234" },
- fmtTest{ "%o", uint32(b32), "37777777777" },
- fmtTest{ "%o", uint64(b64), "1777777777777777777777" },
- fmtTest{ "%x", 0x1234abcd, "1234abcd" },
- fmtTest{ "%#x", 0x1234abcd, "0x1234abcd" },
- fmtTest{ "%x", b32-0x1234567, "fedcba98" },
- fmtTest{ "%X", 0x1234abcd, "1234ABCD" },
- fmtTest{ "%X", b32-0x1234567, "FEDCBA98" },
- fmtTest{ "%#X", 0, "0X0" },
- 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{ "%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{ "%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{ "%-#20.8x", 0x1234abc, "0x01234abc " },
- fmtTest{ "%-#20.8X", 0x1234abc, "0X01234ABC " },
- fmtTest{ "%-#20.8o", 01234, "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", math.Inf(1), " +Inf" },
- fmtTest{ "%-20f", math.Inf(-1), "-Inf " },
- fmtTest{ "%20g", math.NaN(), " NaN" },
+ 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", 01234, "01234"},
+ fmtTest{"%o", uint32(b32), "37777777777"},
+ fmtTest{"%o", uint64(b64), "1777777777777777777777"},
+ fmtTest{"%x", 0x1234abcd, "1234abcd"},
+ fmtTest{"%#x", 0x1234abcd, "0x1234abcd"},
+ fmtTest{"%x", b32 - 0x1234567, "fedcba98"},
+ fmtTest{"%X", 0x1234abcd, "1234ABCD"},
+ fmtTest{"%X", b32 - 0x1234567, "FEDCBA98"},
+ fmtTest{"%#X", 0, "0X0"},
+ 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{"%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{"%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{"%-#20.8x", 0x1234abc, "0x01234abc "},
+ fmtTest{"%-#20.8X", 0x1234abc, "0X01234ABC "},
+ fmtTest{"%-#20.8o", 01234, "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", math.Inf(1), " +Inf"},
+ fmtTest{"%-20f", math.Inf(-1), "-Inf "},
+ fmtTest{"%20g", math.NaN(), " NaN"},
// arrays
- fmtTest{ "%v", array, "[1 2 3 4 5]" },
- fmtTest{ "%v", iarray, "[1 hello 2.5 <nil>]" },
- fmtTest{ "%v", &array, "&[1 2 3 4 5]" },
- fmtTest{ "%v", &iarray, "&[1 hello 2.5 <nil>]" },
+ fmtTest{"%v", array, "[1 2 3 4 5]"},
+ fmtTest{"%v", iarray, "[1 hello 2.5 <nil>]"},
+ fmtTest{"%v", &array, "&[1 2 3 4 5]"},
+ fmtTest{"%v", &iarray, "&[1 hello 2.5 <nil>]"},
// structs
- fmtTest{ "%v", A{1,2,"a",[]int{1,2}}, `{1 2 a [1 2]}` },
- fmtTest{ "%+v", A{1,2,"a",[]int{1,2}}, `{i:1 j:2 s:a x:[1 2]}` },
+ fmtTest{"%v", A{1, 2, "a", []int{1, 2}}, `{1 2 a [1 2]}`},
+ fmtTest{"%+v", A{1, 2, "a", []int{1, 2}}, `{i:1 j:2 s:a x:[1 2]}`},
// go syntax
- fmtTest{ "%#v", A{1,2,"a",[]int{1,2}}, `fmt_test.A{i:1, j:0x2, s:"a", x:[]int{1, 2}}` },
- fmtTest{ "%#v", &b, "(*uint8)(PTR)" },
- fmtTest{ "%#v", TestFmtInterface, "(func(*testing.T))(PTR)" },
- fmtTest{ "%#v", make(chan int), "(chan int)(PTR)" },
- fmtTest{ "%#v", uint64(1<<64-1), "0xffffffffffffffff" },
- fmtTest{ "%#v", 1000000000, "1000000000" },
+ fmtTest{"%#v", A{1, 2, "a", []int{1, 2}}, `fmt_test.A{i:1, j:0x2, s:"a", x:[]int{1, 2}}`},
+ fmtTest{"%#v", &b, "(*uint8)(PTR)"},
+ fmtTest{"%#v", TestFmtInterface, "(func(*testing.T))(PTR)"},
+ fmtTest{"%#v", make(chan int), "(chan int)(PTR)"},
+ fmtTest{"%#v", uint64(1<<64 - 1), "0xffffffffffffffff"},
+ fmtTest{"%#v", 1000000000, "1000000000"},
}
func TestSprintf(t *testing.T) {
break;
}
}
- s = s[0:i] + "PTR" + s[j:len(s)];
+ s = s[0:i]+"PTR"+s[j:len(s)];
}
if s != tt.out {
if _, ok := tt.val.(string); ok {
}
}
-type flagPrinter struct { }
+type flagPrinter struct{}
+
func (*flagPrinter) Format(f State, c int) {
s := "%";
for i := 0; i < 128; i++ {
}
type flagTest struct {
- in string;
- out string;
+ in string;
+ out string;
}
-var flagtests = []flagTest {
- flagTest{ "%a", "[%a]" },
- flagTest{ "%-a", "[%-a]" },
- flagTest{ "%+a", "[%+a]" },
- flagTest{ "%#a", "[%#a]" },
- flagTest{ "% a", "[% a]" },
- flagTest{ "%0a", "[%0a]" },
- flagTest{ "%1.2a", "[%1.2a]" },
- flagTest{ "%-1.2a", "[%-1.2a]" },
- flagTest{ "%+1.2a", "[%+1.2a]" },
- flagTest{ "%-+1.2a", "[%+-1.2a]" },
- flagTest{ "%-+1.2abc", "[%+-1.2a]bc" },
- flagTest{ "%-1.2abc", "[%-1.2a]bc" },
+var flagtests = []flagTest{
+ flagTest{"%a", "[%a]"},
+ flagTest{"%-a", "[%-a]"},
+ flagTest{"%+a", "[%+a]"},
+ flagTest{"%#a", "[%#a]"},
+ flagTest{"% a", "[% a]"},
+ flagTest{"%0a", "[%0a]"},
+ flagTest{"%1.2a", "[%1.2a]"},
+ flagTest{"%-1.2a", "[%-1.2a]"},
+ flagTest{"%+1.2a", "[%+1.2a]"},
+ flagTest{"%-+1.2a", "[%+-1.2a]"},
+ flagTest{"%-+1.2abc", "[%+-1.2a]bc"},
+ flagTest{"%-1.2abc", "[%-1.2a]bc"},
}
func TestFlagParser(t *testing.T) {
func TestStructPrinter(t *testing.T) {
var s struct {
- a string;
- b string;
- c int;
- };
+ a string;
+ b string;
+ c int;
+ }
s.a = "abc";
s.b = "def";
s.c = 123;
type Test struct {
- fmt string;
- out string;
+ fmt string;
+ out string;
}
- var tests = []Test {
- Test{ "%v", "{abc def 123}" },
- Test{ "%+v", "{a:abc b:def c:123}" },
+ var tests = []Test{
+ Test{"%v", "{abc def 123}"},
+ Test{"%+v", "{a:abc b:def c:123}"},
};
for _, tt := range tests {
out := Sprintf(tt.fmt, s);
}
func TestMapPrinter(t *testing.T) {
- m0 := make(map[int] string);
+ m0 := make(map[int]string);
s := Sprint(m0);
if s != "map[]" {
t.Errorf("empty map printed as %q not %q", s, "map[]");
}
- m1 := map[int]string{1:"one", 2:"two", 3:"three"};
+ m1 := map[int]string{1: "one", 2: "two", 3: "three"};
a := []string{"1:one", "2:two", "3:three"};
presentInMap(Sprintf("%v", m1), a, t);
presentInMap(Sprint(m1), a, t);
// Write is the function to call to emit formatted output to be printed.
Write(b []byte) (ret int, err os.Error);
// Width returns the value of the width option and whether it has been set.
- Width() (wid int, ok bool);
+ Width() (wid int, ok bool);
// Precision returns the value of the precision option and whether it has been set.
- Precision() (prec int, ok bool);
+ Precision() (prec int, ok bool);
// Flag returns whether the flag c, a character, has been set.
- Flag(int) bool;
+ Flag(int) bool;
}
// Formatter is the interface implemented by values with a custom formatter.
// The String method is used to print values passed as an operand
// to a %s or %v format or to an unformatted printer such as Print.
type Stringer interface {
- String() string
+ String() string;
}
// GoStringer is implemented by any value that has a GoString() method,
// The GoString method is used to print values passed as an operand
// to a %#v format.
type GoStringer interface {
- GoString() string
+ GoString() string;
}
const runeSelf = utf8.RuneSelf
}
func (p *pp) Width() (wid int, ok bool) {
- return p.fmt.wid, p.fmt.wid_present
+ return p.fmt.wid, p.fmt.wid_present;
}
func (p *pp) Precision() (prec int, ok bool) {
- return p.fmt.prec, p.fmt.prec_present
+ return p.fmt.prec, p.fmt.prec_present;
}
func (p *pp) Flag(b int) bool {
case '0':
return p.fmt.zero;
}
- return false
+ return false;
}
func (p *pp) ensure(n int) {
if len(p.buf) < n {
newn := allocSize + len(p.buf);
if newn < n {
- newn = n + allocSize
+ newn = n+allocSize;
}
b := make([]byte, newn);
for i := 0; i < p.n; i++ {
}
func (p *pp) addbytes(b []byte, start, end int) {
- p.ensure(p.n + end-start);
+ p.ensure(p.n + end - start);
for i := start; i < end; i++ {
p.buf[p.n] = b[i];
p.n++;
v := reflect.NewValue(a).(*reflect.StructValue);
p := newPrinter();
p.doprintf(format, v);
- n, error = w.Write(p.buf[0:p.n]);
+ n, error = w.Write(p.buf[0 : p.n]);
return n, error;
}
v := reflect.NewValue(a).(*reflect.StructValue);
p := newPrinter();
p.doprint(v, false, false);
- n, error = w.Write(p.buf[0:p.n]);
+ n, error = w.Write(p.buf[0 : p.n]);
return n, error;
}
v := reflect.NewValue(a).(*reflect.StructValue);
p := newPrinter();
p.doprint(v, true, true);
- n, error = w.Write(p.buf[0:p.n]);
+ n, error = w.Write(p.buf[0 : p.n]);
return n, error;
}
case *reflect.Float32Value:
return float32(v.Get()), true;
case *reflect.FloatValue:
- if v.Type().Size()*8 == 32 {
+ if v.Type().Size() * 8 == 32 {
return float32(v.Get()), true;
}
}
func getFloat64(v reflect.Value) (val float64, ok bool) {
switch v := v.(type) {
case *reflect.FloatValue:
- if v.Type().Size()*8 == 64 {
+ if v.Type().Size() * 8 == 64 {
return float64(v.Get()), true;
}
case *reflect.Float64Value:
func parsenum(s string, start, end int) (n int, got bool, newi int) {
if start >= end {
- return 0, false, end
+ return 0, false, end;
}
isnum := false;
num := 0;
for '0' <= s[start] && s[start] <= '9' {
- num = num*10 + int(s[start] - '0');
+ num = num*10 + int(s[start]-'0');
start++;
isnum = true;
}
return false; // this value is not a string
}
}
- }
- s := "";
+ }
+ s := "";
BigSwitch:
switch f := field.(type) {
case *reflect.BoolValue:
case *reflect.Float64Value:
s = p.fmt.Fmt_g64(f.Get()).Str();
case *reflect.FloatValue:
- if field.Type().Size()*8 == 32 {
+ if field.Type().Size() * 8 == 32 {
s = p.fmt.Fmt_g32(float32(f.Get())).Str();
} else {
s = p.fmt.Fmt_g64(float64(f.Get())).Str();
v := f;
t := v.Type().(*reflect.StructType);
p.fmt.clearflags(); // clear flags for p.printField
- for i := 0; i < v.NumField(); i++ {
+ for i := 0; i < v.NumField(); i++ {
if i > 0 {
if sharp {
p.addstr(", ");
p.addstr(field.Type().String());
p.addstr("(nil)");
} else {
- s = "<nil>"
+ s = "<nil>";
}
} else {
return p.printField(value, plus, sharp, depth+1);
s = "<nil>";
break;
}
- p.fmt.sharp = true; // turn 0x on
+ p.fmt.sharp = true; // turn 0x on
s = p.fmt.Fmt_ux64(uint64(v)).Str();
case uintptrGetter:
v := f.Get();
}
p.addstr(")");
} else {
- p.fmt.sharp = true; // turn 0x on
+ p.fmt.sharp = true; // turn 0x on
p.addstr(p.fmt.Fmt_ux64(uint64(f.Get())).Str());
}
default:
func (p *pp) doprintf(format string, v *reflect.StructValue) {
p.ensure(len(format)); // a good starting size
- end := len(format) - 1;
+ end := len(format)-1;
fieldnum := 0; // we process one field per non-trivial format
- for i := 0; i <= end; {
+ for i := 0; i <= end; {
c, w := utf8.DecodeRuneInString(format[i:len(format)]);
if c != '%' || i == end {
p.add(c);
i++;
// flags and widths
p.fmt.clearflags();
- F: for ; i < end; i++ {
+ F: for ; i < end; i++ {
switch format[i] {
case '#':
p.fmt.sharp = true;
// int
case 'b':
if v, _, ok := getInt(field); ok {
- s = p.fmt.Fmt_b64(uint64(v)).Str() // always unsigned
+ s = p.fmt.Fmt_b64(uint64(v)).Str(); // always unsigned
} else if v, ok := getFloat32(field); ok {
- s = p.fmt.Fmt_fb32(v).Str()
+ s = p.fmt.Fmt_fb32(v).Str();
} else if v, ok := getFloat64(field); ok {
- s = p.fmt.Fmt_fb64(v).Str()
+ s = p.fmt.Fmt_fb64(v).Str();
} else {
- goto badtype
+ goto badtype;
}
case 'c':
if v, _, ok := getInt(field); ok {
- s = p.fmt.Fmt_c(int(v)).Str()
+ s = p.fmt.Fmt_c(int(v)).Str();
} else {
- goto badtype
+ goto badtype;
}
case 'd':
if v, signed, ok := getInt(field); ok {
if signed {
- s = p.fmt.Fmt_d64(v).Str()
+ s = p.fmt.Fmt_d64(v).Str();
} else {
- s = p.fmt.Fmt_ud64(uint64(v)).Str()
+ s = p.fmt.Fmt_ud64(uint64(v)).Str();
}
} else {
- goto badtype
+ goto badtype;
}
case 'o':
if v, signed, ok := getInt(field); ok {
if signed {
- s = p.fmt.Fmt_o64(v).Str()
+ s = p.fmt.Fmt_o64(v).Str();
} else {
- s = p.fmt.Fmt_uo64(uint64(v)).Str()
+ s = p.fmt.Fmt_uo64(uint64(v)).Str();
}
} else {
- goto badtype
+ goto badtype;
}
case 'x':
if v, signed, ok := getInt(field); ok {
if signed {
- s = p.fmt.Fmt_x64(v).Str()
+ s = p.fmt.Fmt_x64(v).Str();
} else {
- s = p.fmt.Fmt_ux64(uint64(v)).Str()
+ s = p.fmt.Fmt_ux64(uint64(v)).Str();
}
} else if v, ok := getString(field); ok {
s = p.fmt.Fmt_sx(v).Str();
} else {
- goto badtype
+ goto badtype;
}
case 'X':
if v, signed, ok := getInt(field); ok {
if signed {
- s = p.fmt.Fmt_X64(v).Str()
+ s = p.fmt.Fmt_X64(v).Str();
} else {
- s = p.fmt.Fmt_uX64(uint64(v)).Str()
+ s = p.fmt.Fmt_uX64(uint64(v)).Str();
}
} else if v, ok := getString(field); ok {
s = p.fmt.Fmt_sX(v).Str();
} else {
- goto badtype
+ goto badtype;
}
// float
case 'e':
if v, ok := getFloat32(field); ok {
- s = p.fmt.Fmt_e32(v).Str()
+ s = p.fmt.Fmt_e32(v).Str();
} else if v, ok := getFloat64(field); ok {
- s = p.fmt.Fmt_e64(v).Str()
+ s = p.fmt.Fmt_e64(v).Str();
} else {
- goto badtype
+ goto badtype;
}
case 'E':
if v, ok := getFloat32(field); ok {
- s = p.fmt.Fmt_E32(v).Str()
+ s = p.fmt.Fmt_E32(v).Str();
} else if v, ok := getFloat64(field); ok {
- s = p.fmt.Fmt_E64(v).Str()
+ s = p.fmt.Fmt_E64(v).Str();
} else {
- goto badtype
+ goto badtype;
}
case 'f':
if v, ok := getFloat32(field); ok {
- s = p.fmt.Fmt_f32(v).Str()
+ s = p.fmt.Fmt_f32(v).Str();
} else if v, ok := getFloat64(field); ok {
- s = p.fmt.Fmt_f64(v).Str()
+ s = p.fmt.Fmt_f64(v).Str();
} else {
- goto badtype
+ goto badtype;
}
case 'g':
if v, ok := getFloat32(field); ok {
- s = p.fmt.Fmt_g32(v).Str()
+ s = p.fmt.Fmt_g32(v).Str();
} else if v, ok := getFloat64(field); ok {
- s = p.fmt.Fmt_g64(v).Str()
+ s = p.fmt.Fmt_g64(v).Str();
} else {
- goto badtype
+ goto badtype;
}
case 'G':
if v, ok := getFloat32(field); ok {
- s = p.fmt.Fmt_G32(v).Str()
+ s = p.fmt.Fmt_G32(v).Str();
} else if v, ok := getFloat64(field); ok {
- s = p.fmt.Fmt_G64(v).Str()
+ s = p.fmt.Fmt_G64(v).Str();
} else {
- goto badtype
+ goto badtype;
}
// string
}
}
if v, ok := getString(field); ok {
- s = p.fmt.Fmt_s(v).Str()
+ s = p.fmt.Fmt_s(v).Str();
} else {
- goto badtype
+ goto badtype;
}
case 'q':
if v, ok := getString(field); ok {
- s = p.fmt.Fmt_q(v).Str()
+ s = p.fmt.Fmt_q(v).Str();
} else {
- goto badtype
+ goto badtype;
}
// pointer
case 'p':
if v, ok := getPtr(field); ok {
if v == 0 {
- s = "<nil>"
+ s = "<nil>";
} else {
- s = "0x" + p.fmt.Fmt_uX64(uint64(v)).Str()
+ s = "0x" + p.fmt.Fmt_uX64(uint64(v)).Str();
}
} else {
- goto badtype
+ goto badtype;
}
// arbitrary value; do your best
p.addstr(field.Type().String());
p.addstr("=");
p.printField(field, false, false, 0);
- if fieldnum + 1 < v.NumField() {
+ if fieldnum+1 < v.NumField() {
p.addstr(", ");
}
}
func (p *pp) doprint(v *reflect.StructValue, addspace, addnewline bool) {
prev_string := false;
- for fieldnum := 0; fieldnum < v.NumField(); fieldnum++ {
+ for fieldnum := 0; fieldnum < v.NumField(); fieldnum++ {
// always add spaces if we're doing println
field := getField(v, fieldnum);
if fieldnum > 0 {
prev_string = p.printField(field, false, false, 0);
}
if addnewline {
- p.add('\n')
+ p.add('\n');
}
}