Commit ecb863a9 authored by Russ Cox's avatar Russ Cox

apply gofmt to datafmt, ebnf, exec, expvar, flag, fmt

R=gri
DELTA=456  (6 added, 3 deleted, 447 changed)
OCL=35398
CL=35406
parent 2c2e2c5d
...@@ -23,8 +23,8 @@ type parser struct { ...@@ -23,8 +23,8 @@ type parser struct {
tok token.Token; // one token look-ahead tok token.Token; // one token look-ahead
lit []byte; // token literal lit []byte; // token literal
packs map [string] string; // PackageName -> ImportPath packs map[string]string; // PackageName -> ImportPath
rules map [string] expr; // RuleName -> Expression rules map[string]expr; // RuleName -> Expression
} }
...@@ -43,8 +43,8 @@ func (p *parser) init(filename string, src []byte) { ...@@ -43,8 +43,8 @@ func (p *parser) init(filename string, src []byte) {
p.ErrorVector.Init(); p.ErrorVector.Init();
p.scanner.Init(filename, src, p, scanner.AllowIllegalChars); // return '@' as token.ILLEGAL w/o error message p.scanner.Init(filename, src, p, scanner.AllowIllegalChars); // return '@' as token.ILLEGAL w/o error message
p.next(); // initializes pos, tok, lit p.next(); // initializes pos, tok, lit
p.packs = make(map [string] string); p.packs = make(map[string]string);
p.rules = make(map [string] expr); p.rules = make(map[string]expr);
} }
...@@ -151,7 +151,7 @@ func (p *parser) parseLiteral() literal { ...@@ -151,7 +151,7 @@ func (p *parser) parseLiteral() literal {
// the next segment starts with a % format // the next segment starts with a % format
if i0 < i { if i0 < i {
// the current segment is not empty, split it off // the current segment is not empty, split it off
list.Push(s[i0 : i]); list.Push(s[i0:i]);
i0 = i; i0 = i;
} }
i++; // skip %; let loop skip over char after % i++; // skip %; let loop skip over char after %
...@@ -159,7 +159,7 @@ func (p *parser) parseLiteral() literal { ...@@ -159,7 +159,7 @@ func (p *parser) parseLiteral() literal {
} }
// the final segment may start with any character // the final segment may start with any character
// (it is empty iff the string is empty) // (it is empty iff the string is empty)
list.Push(s[i0 : len(s)]); list.Push(s[i0:len(s)]);
// convert list into a literal // convert list into a literal
lit := make(literal, list.Len()); lit := make(literal, list.Len());
...@@ -248,8 +248,10 @@ func (p *parser) parseSequence() expr { ...@@ -248,8 +248,10 @@ func (p *parser) parseSequence() expr {
// no need for a sequence if list.Len() < 2 // no need for a sequence if list.Len() < 2
switch list.Len() { switch list.Len() {
case 0: return nil; case 0:
case 1: return list.At(0).(expr); return nil;
case 1:
return list.At(0).(expr);
} }
// convert list into a sequence // convert list into a sequence
...@@ -278,8 +280,10 @@ func (p *parser) parseExpression() expr { ...@@ -278,8 +280,10 @@ func (p *parser) parseExpression() expr {
// no need for an alternatives if list.Len() < 2 // no need for an alternatives if list.Len() < 2
switch list.Len() { switch list.Len() {
case 0: return nil; case 0:
case 1: return list.At(0).(expr); return nil;
case 1:
return list.At(0).(expr);
} }
// convert list into a alternatives // convert list into a alternatives
...@@ -340,10 +344,10 @@ func (p *parser) parseFormat() { ...@@ -340,10 +344,10 @@ func (p *parser) parseFormat() {
func remap(p *parser, name string) string { func remap(p *parser, name string) string {
i := strings.Index(name, "."); i := strings.Index(name, ".");
if i >= 0 { if i >= 0 {
packageName, suffix := name[0 : i], name[i : len(name)]; packageName, suffix := name[0:i], name[i:len(name)];
// lookup package // lookup package
if importPath, found := p.packs[packageName]; found { if importPath, found := p.packs[packageName]; found {
name = importPath + suffix; name = importPath+suffix;
} else { } else {
var invalidPos token.Position; var invalidPos token.Position;
p.Error(invalidPos, "package not declared: " + packageName); p.Error(invalidPos, "package not declared: " + packageName);
......
...@@ -78,8 +78,7 @@ func modeToFiles(mode, fd int) (*os.File, *os.File, os.Error) { ...@@ -78,8 +78,7 @@ func modeToFiles(mode, fd int) (*os.File, *os.File, os.Error) {
// If a parameter is Pipe, then the corresponding field (Stdin, Stdout, Stderr) // If a parameter is Pipe, then the corresponding field (Stdin, Stdout, Stderr)
// of the returned Cmd is the other end of the pipe. // of the returned Cmd is the other end of the pipe.
// Otherwise the field in Cmd is nil. // Otherwise the field in Cmd is nil.
func Run(argv0 string, argv, envv []string, stdin, stdout, stderr int) (p *Cmd, err os.Error) func Run(argv0 string, argv, envv []string, stdin, stdout, stderr int) (p *Cmd, err os.Error) {
{
p = new(Cmd); p = new(Cmd);
var fd [3]*os.File; var fd [3]*os.File;
...@@ -184,7 +183,7 @@ func (p *Cmd) Close() os.Error { ...@@ -184,7 +183,7 @@ func (p *Cmd) Close() os.Error {
return err; return err;
} }
func canExec(file string) bool{ func canExec(file string) bool {
d, err := os.Stat(file); d, err := os.Stat(file);
if err != nil { if err != nil {
return false; return false;
...@@ -220,4 +219,3 @@ func LookPath(file string) (string, os.Error) { ...@@ -220,4 +219,3 @@ func LookPath(file string) (string, os.Error) {
} }
return "", os.ENOENT; return "", os.ENOENT;
} }
...@@ -28,7 +28,7 @@ type Int struct { ...@@ -28,7 +28,7 @@ type Int struct {
} }
func (v *Int) String() string { func (v *Int) String() string {
return strconv.Itoa64(v.i) return strconv.Itoa64(v.i);
} }
func (v *Int) Add(delta int64) { func (v *Int) Add(delta int64) {
...@@ -39,7 +39,7 @@ func (v *Int) Add(delta int64) { ...@@ -39,7 +39,7 @@ func (v *Int) Add(delta int64) {
// Map is a string-to-Var map variable, and satisfies the Var interface. // Map is a string-to-Var map variable, and satisfies the Var interface.
type Map struct { type Map struct {
m map[string] Var; m map[string]Var;
mu sync.Mutex; mu sync.Mutex;
} }
...@@ -63,21 +63,21 @@ func (v *Map) String() string { ...@@ -63,21 +63,21 @@ func (v *Map) String() string {
first = false; first = false;
} }
fmt.Fprintf(b, "}"); fmt.Fprintf(b, "}");
return b.String() return b.String();
} }
func (v *Map) Init() *Map { func (v *Map) Init() *Map {
v.m = make(map[string] Var); v.m = make(map[string]Var);
return v return v;
} }
func (v *Map) Get(key string) Var { func (v *Map) Get(key string) Var {
v.mu.Lock(); v.mu.Lock();
defer v.mu.Unlock(); defer v.mu.Unlock();
if av, ok := v.m[key]; ok { if av, ok := v.m[key]; ok {
return av return av;
} }
return nil return nil;
} }
func (v *Map) Set(key string, av Var) { func (v *Map) Set(key string, av Var) {
...@@ -104,7 +104,7 @@ func (v *Map) Add(key string, delta int64) { ...@@ -104,7 +104,7 @@ func (v *Map) Add(key string, delta int64) {
// TODO(rsc): Make sure map access in separate thread is safe. // TODO(rsc): Make sure map access in separate thread is safe.
func (v *Map) iterate(c chan<- KeyValue) { func (v *Map) iterate(c chan<- KeyValue) {
for k, v := range v.m { for k, v := range v.m {
c <- KeyValue{ k, v }; c <- KeyValue{k, v};
} }
close(c); close(c);
} }
...@@ -112,7 +112,7 @@ func (v *Map) iterate(c chan<- KeyValue) { ...@@ -112,7 +112,7 @@ func (v *Map) iterate(c chan<- KeyValue) {
func (v *Map) Iter() <-chan KeyValue { func (v *Map) Iter() <-chan KeyValue {
c := make(chan KeyValue); c := make(chan KeyValue);
go v.iterate(c); go v.iterate(c);
return c return c;
} }
// String is a string variable, and satisfies the Var interface. // String is a string variable, and satisfies the Var interface.
...@@ -121,7 +121,7 @@ type String struct { ...@@ -121,7 +121,7 @@ type String struct {
} }
func (v *String) String() string { func (v *String) String() string {
return strconv.Quote(v.s) return strconv.Quote(v.s);
} }
func (v *String) Set(value string) { func (v *String) Set(value string) {
...@@ -130,16 +130,16 @@ func (v *String) Set(value string) { ...@@ -130,16 +130,16 @@ func (v *String) Set(value string) {
// IntFunc wraps a func() int64 to create a value that satisfies the Var interface. // IntFunc wraps a func() int64 to create a value that satisfies the Var interface.
// The function will be called each time the Var is evaluated. // The function will be called each time the Var is evaluated.
type IntFunc func() int64; type IntFunc func() int64
func (v IntFunc) String() string { func (v IntFunc) String() string {
return strconv.Itoa64(v()) return strconv.Itoa64(v());
} }
// All published variables. // All published variables.
var vars map[string] Var = make(map[string] Var); var vars map[string]Var = make(map[string]Var)
var mutex sync.Mutex; var mutex sync.Mutex
// Publish declares an named exported variable. This should be called from a // Publish declares an named exported variable. This should be called from a
// package's init function when it creates its Vars. If the name is already // package's init function when it creates its Vars. If the name is already
...@@ -156,9 +156,9 @@ func Publish(name string, v Var) { ...@@ -156,9 +156,9 @@ func Publish(name string, v Var) {
// Get retrieves a named exported variable. // Get retrieves a named exported variable.
func Get(name string) Var { func Get(name string) Var {
if v, ok := vars[name]; ok { if v, ok := vars[name]; ok {
return v return v;
} }
return nil return nil;
} }
// RemoveAll removes all exported variables. // RemoveAll removes all exported variables.
...@@ -166,7 +166,7 @@ func Get(name string) Var { ...@@ -166,7 +166,7 @@ func Get(name string) Var {
func RemoveAll() { func RemoveAll() {
mutex.Lock(); mutex.Lock();
defer mutex.Unlock(); defer mutex.Unlock();
vars = make(map[string] Var); vars = make(map[string]Var);
} }
// Convenience functions for creating new exported variables. // Convenience functions for creating new exported variables.
...@@ -174,25 +174,25 @@ func RemoveAll() { ...@@ -174,25 +174,25 @@ func RemoveAll() {
func NewInt(name string) *Int { func NewInt(name string) *Int {
v := new(Int); v := new(Int);
Publish(name, v); Publish(name, v);
return v return v;
} }
func NewMap(name string) *Map { func NewMap(name string) *Map {
v := new(Map).Init(); v := new(Map).Init();
Publish(name, v); Publish(name, v);
return v return v;
} }
func NewString(name string) *String { func NewString(name string) *String {
v := new(String); v := new(String);
Publish(name, v); Publish(name, v);
return v return v;
} }
// TODO(rsc): Make sure map access in separate thread is safe. // TODO(rsc): Make sure map access in separate thread is safe.
func iterate(c chan<- KeyValue) { func iterate(c chan<- KeyValue) {
for k, v := range vars { for k, v := range vars {
c <- KeyValue{ k, v }; c <- KeyValue{k, v};
} }
close(c); close(c);
} }
...@@ -200,7 +200,7 @@ func iterate(c chan<- KeyValue) { ...@@ -200,7 +200,7 @@ func iterate(c chan<- KeyValue) {
func Iter() <-chan KeyValue { func Iter() <-chan KeyValue {
c := make(chan KeyValue); c := make(chan KeyValue);
go iterate(c); go iterate(c);
return c return c;
} }
func expvarHandler(c *http.Conn, req *http.Request) { func expvarHandler(c *http.Conn, req *http.Request) {
......
...@@ -45,7 +45,7 @@ package flag ...@@ -45,7 +45,7 @@ package flag
import ( import (
"fmt"; "fmt";
"os"; "os";
"strconv" "strconv";
) )
// TODO(r): BUG: atob belongs elsewhere // TODO(r): BUG: atob belongs elsewhere
...@@ -54,9 +54,9 @@ func atob(str string) (value bool, ok bool) { ...@@ -54,9 +54,9 @@ func atob(str string) (value bool, ok bool) {
case "1", "t", "T", "true", "TRUE", "True": case "1", "t", "T", "true", "TRUE", "True":
return true, true; return true, true;
case "0", "f", "F", "false", "FALSE", "False": case "0", "f", "F", "false", "FALSE", "False":
return false, true return false, true;
} }
return false, false return false, false;
} }
// -- Bool Value // -- Bool Value
...@@ -66,17 +66,17 @@ type boolValue struct { ...@@ -66,17 +66,17 @@ type boolValue struct {
func newBoolValue(val bool, p *bool) *boolValue { func newBoolValue(val bool, p *bool) *boolValue {
*p = val; *p = val;
return &boolValue{p} return &boolValue{p};
} }
func (b *boolValue) set(s string) bool { func (b *boolValue) set(s string) bool {
v, ok := atob(s); v, ok := atob(s);
*b.p = v; *b.p = v;
return ok return ok;
} }
func (b *boolValue) String() string { func (b *boolValue) String() string {
return fmt.Sprintf("%v", *b.p) return fmt.Sprintf("%v", *b.p);
} }
// -- Int Value // -- Int Value
...@@ -86,17 +86,17 @@ type intValue struct { ...@@ -86,17 +86,17 @@ type intValue struct {
func newIntValue(val int, p *int) *intValue { func newIntValue(val int, p *int) *intValue {
*p = val; *p = val;
return &intValue{p} return &intValue{p};
} }
func (i *intValue) set(s string) bool { func (i *intValue) set(s string) bool {
v, err := strconv.Atoi(s); v, err := strconv.Atoi(s);
*i.p = int(v); *i.p = int(v);
return err == nil return err == nil;
} }
func (i *intValue) String() string { func (i *intValue) String() string {
return fmt.Sprintf("%v", *i.p) return fmt.Sprintf("%v", *i.p);
} }
// -- Int64 Value // -- Int64 Value
...@@ -106,7 +106,7 @@ type int64Value struct { ...@@ -106,7 +106,7 @@ type int64Value struct {
func newInt64Value(val int64, p *int64) *int64Value { func newInt64Value(val int64, p *int64) *int64Value {
*p = val; *p = val;
return &int64Value{p} return &int64Value{p};
} }
func (i *int64Value) set(s string) bool { func (i *int64Value) set(s string) bool {
...@@ -116,7 +116,7 @@ func (i *int64Value) set(s string) bool { ...@@ -116,7 +116,7 @@ func (i *int64Value) set(s string) bool {
} }
func (i *int64Value) String() string { func (i *int64Value) String() string {
return fmt.Sprintf("%v", *i.p) return fmt.Sprintf("%v", *i.p);
} }
// -- Uint Value // -- Uint Value
...@@ -126,7 +126,7 @@ type uintValue struct { ...@@ -126,7 +126,7 @@ type uintValue struct {
func newUintValue(val uint, p *uint) *uintValue { func newUintValue(val uint, p *uint) *uintValue {
*p = val; *p = val;
return &uintValue{p} return &uintValue{p};
} }
func (i *uintValue) set(s string) bool { func (i *uintValue) set(s string) bool {
...@@ -136,7 +136,7 @@ func (i *uintValue) set(s string) bool { ...@@ -136,7 +136,7 @@ func (i *uintValue) set(s string) bool {
} }
func (i *uintValue) String() string { func (i *uintValue) String() string {
return fmt.Sprintf("%v", *i.p) return fmt.Sprintf("%v", *i.p);
} }
// -- uint64 Value // -- uint64 Value
...@@ -146,7 +146,7 @@ type uint64Value struct { ...@@ -146,7 +146,7 @@ type uint64Value struct {
func newUint64Value(val uint64, p *uint64) *uint64Value { func newUint64Value(val uint64, p *uint64) *uint64Value {
*p = val; *p = val;
return &uint64Value{p} return &uint64Value{p};
} }
func (i *uint64Value) set(s string) bool { func (i *uint64Value) set(s string) bool {
...@@ -156,7 +156,7 @@ func (i *uint64Value) set(s string) bool { ...@@ -156,7 +156,7 @@ func (i *uint64Value) set(s string) bool {
} }
func (i *uint64Value) String() string { func (i *uint64Value) String() string {
return fmt.Sprintf("%v", *i.p) return fmt.Sprintf("%v", *i.p);
} }
// -- string Value // -- string Value
...@@ -166,7 +166,7 @@ type stringValue struct { ...@@ -166,7 +166,7 @@ type stringValue struct {
func newStringValue(val string, p *string) *stringValue { func newStringValue(val string, p *string) *stringValue {
*p = val; *p = val;
return &stringValue{p} return &stringValue{p};
} }
func (s *stringValue) set(val string) bool { func (s *stringValue) set(val string) bool {
...@@ -175,7 +175,7 @@ func (s *stringValue) set(val string) bool { ...@@ -175,7 +175,7 @@ func (s *stringValue) set(val string) bool {
} }
func (s *stringValue) String() string { 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. // FlagValue is the interface to the dynamic value stored in a flag.
...@@ -194,24 +194,24 @@ type Flag struct { ...@@ -194,24 +194,24 @@ type Flag struct {
} }
type allFlags struct { type allFlags struct {
actual map[string] *Flag; actual map[string]*Flag;
formal map[string] *Flag; formal map[string]*Flag;
first_arg int; // 0 is the program name, 1 is first arg 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. // VisitAll visits the flags, calling fn for each. It visits all flags, even those not set.
func VisitAll(fn func(*Flag)) { func VisitAll(fn func(*Flag)) {
for _, f := range flags.formal { 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. // Visit visits the flags, calling fn for each. It visits only those flags that have been set.
func Visit(fn func(*Flag)) { func Visit(fn func(*Flag)) {
for _, f := range flags.actual { for _, f := range flags.actual {
fn(f) fn(f);
} }
} }
...@@ -219,9 +219,9 @@ func Visit(fn func(*Flag)) { ...@@ -219,9 +219,9 @@ func Visit(fn func(*Flag)) {
func Lookup(name string) *Flag { func Lookup(name string) *Flag {
f, ok := flags.formal[name]; f, ok := flags.formal[name];
if !ok { 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 // Set sets the value of the named flag. It returns true if the set succeeded; false if
...@@ -229,11 +229,11 @@ func Lookup(name string) *Flag { ...@@ -229,11 +229,11 @@ func Lookup(name string) *Flag {
func Set(name, value string) bool { func Set(name, value string) bool {
f, ok := flags.formal[name]; f, ok := flags.formal[name];
if !ok { if !ok {
return false return false;
} }
ok = f.Value.set(value); ok = f.Value.set(value);
if !ok { if !ok {
return false return false;
} }
flags.actual[name] = f; flags.actual[name] = f;
return true; return true;
...@@ -248,7 +248,7 @@ func PrintDefaults() { ...@@ -248,7 +248,7 @@ func PrintDefaults() {
format = " -%s=%q: %s\n"; format = " -%s=%q: %s\n";
} }
fmt.Fprintf(os.Stderr, format, f.Name, f.DefValue, f.Usage); fmt.Fprintf(os.Stderr, format, f.Name, f.DefValue, f.Usage);
}) });
} }
// Usage prints to standard error a default usage message documenting all defined flags. // Usage prints to standard error a default usage message documenting all defined flags.
...@@ -259,7 +259,7 @@ var Usage = func() { ...@@ -259,7 +259,7 @@ var Usage = func() {
} }
func NFlag() int { 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 // Arg returns the i'th command-line argument. Arg(0) is the first remaining argument
...@@ -269,17 +269,17 @@ func Arg(i int) string { ...@@ -269,17 +269,17 @@ func Arg(i int) string {
if i < 0 || i >= len(os.Args) { if i < 0 || i >= len(os.Args) {
return ""; return "";
} }
return os.Args[i] return os.Args[i];
} }
// NArg is the number of arguments remaining after flags have been processed. // NArg is the number of arguments remaining after flags have been processed.
func NArg() int { 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. // Args returns the non-flag command-line arguments.
func Args() []string { 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) { func add(name string, value FlagValue, usage string) {
...@@ -377,27 +377,26 @@ func String(name, value string, usage string) *string { ...@@ -377,27 +377,26 @@ func String(name, value string, usage string) *string {
return p; 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]; s := os.Args[index];
f.first_arg = index; // until proven otherwise f.first_arg = index; // until proven otherwise
if len(s) == 0 { if len(s) == 0 {
return false, -1 return false, -1;
} }
if s[0] != '-' { if s[0] != '-' {
return false, -1 return false, -1;
} }
num_minuses := 1; num_minuses := 1;
if len(s) == 1 { if len(s) == 1 {
return false, index return false, index;
} }
if s[1] == '-' { if s[1] == '-' {
num_minuses++; num_minuses++;
if len(s) == 2 { // "--" terminates the flags 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] == '=' { if len(name) == 0 || name[0] == '-' || name[0] == '=' {
fmt.Fprintln(os.Stderr, "bad flag syntax:", s); fmt.Fprintln(os.Stderr, "bad flag syntax:", s);
Usage(); Usage();
...@@ -411,7 +410,7 @@ func (f *allFlags) parseOne(index int) (ok bool, next int) ...@@ -411,7 +410,7 @@ func (f *allFlags) parseOne(index int) (ok bool, next int)
if name[i] == '=' { if name[i] == '=' {
value = name[i+1 : len(name)]; value = name[i+1 : len(name)];
has_value = true; has_value = true;
name = name[0 : i]; name = name[0:i];
break; break;
} }
} }
...@@ -436,11 +435,11 @@ func (f *allFlags) parseOne(index int) (ok bool, next int) ...@@ -436,11 +435,11 @@ func (f *allFlags) parseOne(index int) (ok bool, next int)
os.Exit(2); os.Exit(2);
} }
} else { } else {
f.set("true") f.set("true");
} }
} else { } else {
// It must have a value, which might be the next argument. // 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 // value is the next arg
has_value = true; has_value = true;
index++; index++;
...@@ -459,7 +458,7 @@ func (f *allFlags) parseOne(index int) (ok bool, next int) ...@@ -459,7 +458,7 @@ func (f *allFlags) parseOne(index int) (ok bool, next int)
} }
} }
flags.actual[name] = flag; 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 // Parse parses the command-line flags. Must be called after all flags are defined
...@@ -472,7 +471,7 @@ func Parse() { ...@@ -472,7 +471,7 @@ func Parse() {
i = next; i = next;
} }
if !ok { if !ok {
break break;
} }
} }
} }
...@@ -20,13 +20,13 @@ var ( ...@@ -20,13 +20,13 @@ var (
func boolString(s string) string { func boolString(s string) string {
if s == "0" { if s == "0" {
return "false" return "false";
} }
return "true" return "true";
} }
func TestEverything(t *testing.T) { func TestEverything(t *testing.T) {
m := make(map[string] *Flag); m := make(map[string]*Flag);
desired := "0"; desired := "0";
visitor := func(f *Flag) { visitor := func(f *Flag) {
if len(f.Name) > 5 && f.Name[0:5] == "test_" { if len(f.Name) > 5 && f.Name[0:5] == "test_" {
...@@ -47,15 +47,15 @@ func TestEverything(t *testing.T) { ...@@ -47,15 +47,15 @@ func TestEverything(t *testing.T) {
if len(m) != 6 { if len(m) != 6 {
t.Error("VisitAll misses some flags"); t.Error("VisitAll misses some flags");
for k, v := range m { for k, v := range m {
t.Log(k, *v) t.Log(k, *v);
} }
} }
m = make(map[string] *Flag); m = make(map[string]*Flag);
Visit(visitor); Visit(visitor);
if len(m) != 0 { if len(m) != 0 {
t.Errorf("Visit sees unset flags"); t.Errorf("Visit sees unset flags");
for k, v := range m { for k, v := range m {
t.Log(k, *v) t.Log(k, *v);
} }
} }
// Now set all flags // Now set all flags
...@@ -70,7 +70,7 @@ func TestEverything(t *testing.T) { ...@@ -70,7 +70,7 @@ func TestEverything(t *testing.T) {
if len(m) != 6 { if len(m) != 6 {
t.Error("Visit fails after set"); t.Error("Visit fails after set");
for k, v := range m { for k, v := range m {
t.Log(k, *v) t.Log(k, *v);
} }
} }
} }
This diff is collapsed.
...@@ -9,8 +9,8 @@ import ( ...@@ -9,8 +9,8 @@ import (
) )
const nByte = 64; const nByte = 64
const nPows10 = 160; const nPows10 = 160
var ldigits string = "0123456789abcdef" // var not const because we take its address var ldigits string = "0123456789abcdef" // var not const because we take its address
var udigits string = "0123456789ABCDEF" var udigits string = "0123456789ABCDEF"
...@@ -140,9 +140,9 @@ func (f *Fmt) pad(s string) { ...@@ -140,9 +140,9 @@ func (f *Fmt) pad(s string) {
buf[i] = padchar; buf[i] = padchar;
} }
if left { if left {
s = string(buf) + s; s = string(buf)+s;
} else { } else {
s = s + string(buf); s = s+string(buf);
} }
} }
} }
...@@ -155,7 +155,7 @@ func (f *Fmt) pad(s string) { ...@@ -155,7 +155,7 @@ func (f *Fmt) pad(s string) {
// marginally faster by splitting the 32-bit case out into a separate function // marginally faster by splitting the 32-bit case out into a separate function
// but it's not worth the duplication, so val has 64 bits. // but it's not worth the duplication, so val has 64 bits.
func putint(buf []byte, base, val uint64, digits string) int { func putint(buf []byte, base, val uint64, digits string) int {
i := len(buf) - 1; i := len(buf)-1;
for val >= base { for val >= base {
buf[i] = digits[val%base]; buf[i] = digits[val%base];
i--; i--;
...@@ -190,7 +190,7 @@ func (f *Fmt) integer(a int64, base uint, is_signed bool, digits string) string ...@@ -190,7 +190,7 @@ func (f *Fmt) integer(a int64, base uint, is_signed bool, digits string) string
if f.prec_present { if f.prec_present {
prec = f.prec; prec = f.prec;
f.zero = false; f.zero = false;
} else if f.zero && f.wid_present && !f.minus && f.wid > 0{ } else if f.zero && f.wid_present && !f.minus && f.wid > 0 {
prec = f.wid; prec = f.wid;
if negative || f.plus || f.space { if negative || f.plus || f.space {
prec--; // leave room for sign prec--; // leave room for sign
...@@ -211,7 +211,7 @@ func (f *Fmt) integer(a int64, base uint, is_signed bool, digits string) string ...@@ -211,7 +211,7 @@ func (f *Fmt) integer(a int64, base uint, is_signed bool, digits string) string
i--; i--;
} }
case 16: case 16:
buf[i] = 'x' + digits[10]-'a'; buf[i] = 'x'+digits[10]-'a';
i--; i--;
buf[i] = '0'; buf[i] = '0';
i--; i--;
...@@ -228,7 +228,7 @@ func (f *Fmt) integer(a int64, base uint, is_signed bool, digits string) string ...@@ -228,7 +228,7 @@ func (f *Fmt) integer(a int64, base uint, is_signed bool, digits string) string
buf[i] = ' '; buf[i] = ' ';
i--; i--;
} }
return string(buf[i+1:nByte]); return string(buf[i+1 : nByte]);
} }
// Fmt_d64 formats an int64 in decimal. // Fmt_d64 formats an int64 in decimal.
...@@ -395,7 +395,7 @@ func (f *Fmt) Fmt_c(v int) *Fmt { ...@@ -395,7 +395,7 @@ func (f *Fmt) Fmt_c(v int) *Fmt {
func (f *Fmt) Fmt_s(s string) *Fmt { func (f *Fmt) Fmt_s(s string) *Fmt {
if f.prec_present { if f.prec_present {
if f.prec < len(s) { if f.prec < len(s) {
s = s[0:f.prec]; s = s[0 : f.prec];
} }
} }
f.pad(s); f.pad(s);
...@@ -527,28 +527,28 @@ func (f *Fmt) Fmt_fb32(v float32) *Fmt { ...@@ -527,28 +527,28 @@ func (f *Fmt) Fmt_fb32(v float32) *Fmt {
// float // float
func (x *Fmt) f(a float) *Fmt { func (x *Fmt) f(a float) *Fmt {
if strconv.FloatSize == 32 { if strconv.FloatSize == 32 {
return x.Fmt_f32(float32(a)) return x.Fmt_f32(float32(a));
} }
return x.Fmt_f64(float64(a)) return x.Fmt_f64(float64(a));
} }
func (x *Fmt) e(a float) *Fmt { func (x *Fmt) e(a float) *Fmt {
if strconv.FloatSize == 32 { if strconv.FloatSize == 32 {
return x.Fmt_e32(float32(a)) return x.Fmt_e32(float32(a));
} }
return x.Fmt_e64(float64(a)) return x.Fmt_e64(float64(a));
} }
func (x *Fmt) g(a float) *Fmt { func (x *Fmt) g(a float) *Fmt {
if strconv.FloatSize == 32 { if strconv.FloatSize == 32 {
return x.Fmt_g32(float32(a)) return x.Fmt_g32(float32(a));
} }
return x.Fmt_g64(float64(a)) return x.Fmt_g64(float64(a));
} }
func (x *Fmt) fb(a float) *Fmt { func (x *Fmt) fb(a float) *Fmt {
if strconv.FloatSize == 32 { if strconv.FloatSize == 32 {
return x.Fmt_fb32(float32(a)) return x.Fmt_fb32(float32(a));
} }
return x.Fmt_fb64(float64(a)) return x.Fmt_fb64(float64(a));
} }
...@@ -110,7 +110,7 @@ type Formatter interface { ...@@ -110,7 +110,7 @@ type Formatter interface {
// The String method is used to print values passed as an operand // 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. // to a %s or %v format or to an unformatted printer such as Print.
type Stringer interface { type Stringer interface {
String() string String() string;
} }
// GoStringer is implemented by any value that has a GoString() method, // GoStringer is implemented by any value that has a GoString() method,
...@@ -118,7 +118,7 @@ type Stringer interface { ...@@ -118,7 +118,7 @@ type Stringer interface {
// The GoString method is used to print values passed as an operand // The GoString method is used to print values passed as an operand
// to a %#v format. // to a %#v format.
type GoStringer interface { type GoStringer interface {
GoString() string GoString() string;
} }
const runeSelf = utf8.RuneSelf const runeSelf = utf8.RuneSelf
...@@ -137,11 +137,11 @@ func newPrinter() *pp { ...@@ -137,11 +137,11 @@ func newPrinter() *pp {
} }
func (p *pp) Width() (wid int, ok bool) { 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) { 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 { func (p *pp) Flag(b int) bool {
...@@ -157,14 +157,14 @@ func (p *pp) Flag(b int) bool { ...@@ -157,14 +157,14 @@ func (p *pp) Flag(b int) bool {
case '0': case '0':
return p.fmt.zero; return p.fmt.zero;
} }
return false return false;
} }
func (p *pp) ensure(n int) { func (p *pp) ensure(n int) {
if len(p.buf) < n { if len(p.buf) < n {
newn := allocSize + len(p.buf); newn := allocSize + len(p.buf);
if newn < n { if newn < n {
newn = n + allocSize newn = n+allocSize;
} }
b := make([]byte, newn); b := make([]byte, newn);
for i := 0; i < p.n; i++ { for i := 0; i < p.n; i++ {
...@@ -184,7 +184,7 @@ func (p *pp) addstr(s string) { ...@@ -184,7 +184,7 @@ func (p *pp) addstr(s string) {
} }
func (p *pp) addbytes(b []byte, start, end int) { 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++ { for i := start; i < end; i++ {
p.buf[p.n] = b[i]; p.buf[p.n] = b[i];
p.n++; p.n++;
...@@ -215,7 +215,7 @@ func Fprintf(w io.Writer, format string, a ...) (n int, error os.Error) { ...@@ -215,7 +215,7 @@ func Fprintf(w io.Writer, format string, a ...) (n int, error os.Error) {
v := reflect.NewValue(a).(*reflect.StructValue); v := reflect.NewValue(a).(*reflect.StructValue);
p := newPrinter(); p := newPrinter();
p.doprintf(format, v); 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; return n, error;
} }
...@@ -242,7 +242,7 @@ func Fprint(w io.Writer, a ...) (n int, error os.Error) { ...@@ -242,7 +242,7 @@ func Fprint(w io.Writer, a ...) (n int, error os.Error) {
v := reflect.NewValue(a).(*reflect.StructValue); v := reflect.NewValue(a).(*reflect.StructValue);
p := newPrinter(); p := newPrinter();
p.doprint(v, false, false); 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; return n, error;
} }
...@@ -273,7 +273,7 @@ func Fprintln(w io.Writer, a ...) (n int, error os.Error) { ...@@ -273,7 +273,7 @@ func Fprintln(w io.Writer, a ...) (n int, error os.Error) {
v := reflect.NewValue(a).(*reflect.StructValue); v := reflect.NewValue(a).(*reflect.StructValue);
p := newPrinter(); p := newPrinter();
p.doprint(v, true, true); 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; return n, error;
} }
...@@ -360,7 +360,7 @@ func getFloat32(v reflect.Value) (val float32, ok bool) { ...@@ -360,7 +360,7 @@ func getFloat32(v reflect.Value) (val float32, ok bool) {
case *reflect.Float32Value: case *reflect.Float32Value:
return float32(v.Get()), true; return float32(v.Get()), true;
case *reflect.FloatValue: case *reflect.FloatValue:
if v.Type().Size()*8 == 32 { if v.Type().Size() * 8 == 32 {
return float32(v.Get()), true; return float32(v.Get()), true;
} }
} }
...@@ -370,7 +370,7 @@ func getFloat32(v reflect.Value) (val float32, ok bool) { ...@@ -370,7 +370,7 @@ func getFloat32(v reflect.Value) (val float32, ok bool) {
func getFloat64(v reflect.Value) (val float64, ok bool) { func getFloat64(v reflect.Value) (val float64, ok bool) {
switch v := v.(type) { switch v := v.(type) {
case *reflect.FloatValue: case *reflect.FloatValue:
if v.Type().Size()*8 == 64 { if v.Type().Size() * 8 == 64 {
return float64(v.Get()), true; return float64(v.Get()), true;
} }
case *reflect.Float64Value: case *reflect.Float64Value:
...@@ -391,12 +391,12 @@ func getPtr(v reflect.Value) (val uintptr, ok bool) { ...@@ -391,12 +391,12 @@ func getPtr(v reflect.Value) (val uintptr, ok bool) {
func parsenum(s string, start, end int) (n int, got bool, newi int) { func parsenum(s string, start, end int) (n int, got bool, newi int) {
if start >= end { if start >= end {
return 0, false, end return 0, false, end;
} }
isnum := false; isnum := false;
num := 0; num := 0;
for '0' <= s[start] && s[start] <= '9' { for '0' <= s[start] && s[start] <= '9' {
num = num*10 + int(s[start] - '0'); num = num*10 + int(s[start]-'0');
start++; start++;
isnum = true; isnum = true;
} }
...@@ -433,7 +433,7 @@ BigSwitch: ...@@ -433,7 +433,7 @@ BigSwitch:
case *reflect.Float64Value: case *reflect.Float64Value:
s = p.fmt.Fmt_g64(f.Get()).Str(); s = p.fmt.Fmt_g64(f.Get()).Str();
case *reflect.FloatValue: case *reflect.FloatValue:
if field.Type().Size()*8 == 32 { if field.Type().Size() * 8 == 32 {
s = p.fmt.Fmt_g32(float32(f.Get())).Str(); s = p.fmt.Fmt_g32(float32(f.Get())).Str();
} else { } else {
s = p.fmt.Fmt_g64(float64(f.Get())).Str(); s = p.fmt.Fmt_g64(float64(f.Get())).Str();
...@@ -502,7 +502,7 @@ BigSwitch: ...@@ -502,7 +502,7 @@ BigSwitch:
p.addstr(field.Type().String()); p.addstr(field.Type().String());
p.addstr("(nil)"); p.addstr("(nil)");
} else { } else {
s = "<nil>" s = "<nil>";
} }
} else { } else {
return p.printField(value, plus, sharp, depth+1); return p.printField(value, plus, sharp, depth+1);
...@@ -604,7 +604,7 @@ BigSwitch: ...@@ -604,7 +604,7 @@ BigSwitch:
func (p *pp) doprintf(format string, v *reflect.StructValue) { func (p *pp) doprintf(format string, v *reflect.StructValue) {
p.ensure(len(format)); // a good starting size 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 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)]); c, w := utf8.DecodeRuneInString(format[i:len(format)]);
...@@ -681,105 +681,105 @@ func (p *pp) doprintf(format string, v *reflect.StructValue) { ...@@ -681,105 +681,105 @@ func (p *pp) doprintf(format string, v *reflect.StructValue) {
// int // int
case 'b': case 'b':
if v, _, ok := getInt(field); ok { 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 { } 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 { } else if v, ok := getFloat64(field); ok {
s = p.fmt.Fmt_fb64(v).Str() s = p.fmt.Fmt_fb64(v).Str();
} else { } else {
goto badtype goto badtype;
} }
case 'c': case 'c':
if v, _, ok := getInt(field); ok { if v, _, ok := getInt(field); ok {
s = p.fmt.Fmt_c(int(v)).Str() s = p.fmt.Fmt_c(int(v)).Str();
} else { } else {
goto badtype goto badtype;
} }
case 'd': case 'd':
if v, signed, ok := getInt(field); ok { if v, signed, ok := getInt(field); ok {
if signed { if signed {
s = p.fmt.Fmt_d64(v).Str() s = p.fmt.Fmt_d64(v).Str();
} else { } else {
s = p.fmt.Fmt_ud64(uint64(v)).Str() s = p.fmt.Fmt_ud64(uint64(v)).Str();
} }
} else { } else {
goto badtype goto badtype;
} }
case 'o': case 'o':
if v, signed, ok := getInt(field); ok { if v, signed, ok := getInt(field); ok {
if signed { if signed {
s = p.fmt.Fmt_o64(v).Str() s = p.fmt.Fmt_o64(v).Str();
} else { } else {
s = p.fmt.Fmt_uo64(uint64(v)).Str() s = p.fmt.Fmt_uo64(uint64(v)).Str();
} }
} else { } else {
goto badtype goto badtype;
} }
case 'x': case 'x':
if v, signed, ok := getInt(field); ok { if v, signed, ok := getInt(field); ok {
if signed { if signed {
s = p.fmt.Fmt_x64(v).Str() s = p.fmt.Fmt_x64(v).Str();
} else { } else {
s = p.fmt.Fmt_ux64(uint64(v)).Str() s = p.fmt.Fmt_ux64(uint64(v)).Str();
} }
} else if v, ok := getString(field); ok { } else if v, ok := getString(field); ok {
s = p.fmt.Fmt_sx(v).Str(); s = p.fmt.Fmt_sx(v).Str();
} else { } else {
goto badtype goto badtype;
} }
case 'X': case 'X':
if v, signed, ok := getInt(field); ok { if v, signed, ok := getInt(field); ok {
if signed { if signed {
s = p.fmt.Fmt_X64(v).Str() s = p.fmt.Fmt_X64(v).Str();
} else { } else {
s = p.fmt.Fmt_uX64(uint64(v)).Str() s = p.fmt.Fmt_uX64(uint64(v)).Str();
} }
} else if v, ok := getString(field); ok { } else if v, ok := getString(field); ok {
s = p.fmt.Fmt_sX(v).Str(); s = p.fmt.Fmt_sX(v).Str();
} else { } else {
goto badtype goto badtype;
} }
// float // float
case 'e': case 'e':
if v, ok := getFloat32(field); ok { 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 { } else if v, ok := getFloat64(field); ok {
s = p.fmt.Fmt_e64(v).Str() s = p.fmt.Fmt_e64(v).Str();
} else { } else {
goto badtype goto badtype;
} }
case 'E': case 'E':
if v, ok := getFloat32(field); ok { 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 { } else if v, ok := getFloat64(field); ok {
s = p.fmt.Fmt_E64(v).Str() s = p.fmt.Fmt_E64(v).Str();
} else { } else {
goto badtype goto badtype;
} }
case 'f': case 'f':
if v, ok := getFloat32(field); ok { 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 { } else if v, ok := getFloat64(field); ok {
s = p.fmt.Fmt_f64(v).Str() s = p.fmt.Fmt_f64(v).Str();
} else { } else {
goto badtype goto badtype;
} }
case 'g': case 'g':
if v, ok := getFloat32(field); ok { 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 { } else if v, ok := getFloat64(field); ok {
s = p.fmt.Fmt_g64(v).Str() s = p.fmt.Fmt_g64(v).Str();
} else { } else {
goto badtype goto badtype;
} }
case 'G': case 'G':
if v, ok := getFloat32(field); ok { 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 { } else if v, ok := getFloat64(field); ok {
s = p.fmt.Fmt_G64(v).Str() s = p.fmt.Fmt_G64(v).Str();
} else { } else {
goto badtype goto badtype;
} }
// string // string
...@@ -792,27 +792,27 @@ func (p *pp) doprintf(format string, v *reflect.StructValue) { ...@@ -792,27 +792,27 @@ func (p *pp) doprintf(format string, v *reflect.StructValue) {
} }
} }
if v, ok := getString(field); ok { if v, ok := getString(field); ok {
s = p.fmt.Fmt_s(v).Str() s = p.fmt.Fmt_s(v).Str();
} else { } else {
goto badtype goto badtype;
} }
case 'q': case 'q':
if v, ok := getString(field); ok { if v, ok := getString(field); ok {
s = p.fmt.Fmt_q(v).Str() s = p.fmt.Fmt_q(v).Str();
} else { } else {
goto badtype goto badtype;
} }
// pointer // pointer
case 'p': case 'p':
if v, ok := getPtr(field); ok { if v, ok := getPtr(field); ok {
if v == 0 { if v == 0 {
s = "<nil>" s = "<nil>";
} else { } else {
s = "0x" + p.fmt.Fmt_uX64(uint64(v)).Str() s = "0x" + p.fmt.Fmt_uX64(uint64(v)).Str();
} }
} else { } else {
goto badtype goto badtype;
} }
// arbitrary value; do your best // arbitrary value; do your best
...@@ -842,7 +842,7 @@ func (p *pp) doprintf(format string, v *reflect.StructValue) { ...@@ -842,7 +842,7 @@ func (p *pp) doprintf(format string, v *reflect.StructValue) {
p.addstr(field.Type().String()); p.addstr(field.Type().String());
p.addstr("="); p.addstr("=");
p.printField(field, false, false, 0); p.printField(field, false, false, 0);
if fieldnum + 1 < v.NumField() { if fieldnum+1 < v.NumField() {
p.addstr(", "); p.addstr(", ");
} }
} }
...@@ -864,6 +864,6 @@ func (p *pp) doprint(v *reflect.StructValue, addspace, addnewline bool) { ...@@ -864,6 +864,6 @@ func (p *pp) doprint(v *reflect.StructValue, addspace, addnewline bool) {
prev_string = p.printField(field, false, false, 0); prev_string = p.printField(field, false, false, 0);
} }
if addnewline { if addnewline {
p.add('\n') p.add('\n');
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment