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
...@@ -18,13 +18,13 @@ import ( ...@@ -18,13 +18,13 @@ import (
type parser struct { type parser struct {
scanner.ErrorVector; scanner.ErrorVector;
scanner scanner.Scanner; scanner scanner.Scanner;
pos token.Position; // token position pos token.Position; // token position
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
} }
...@@ -34,17 +34,17 @@ func (p *parser) next() { ...@@ -34,17 +34,17 @@ func (p *parser) next() {
case token.CHAN, token.FUNC, token.INTERFACE, token.MAP, token.STRUCT: case token.CHAN, token.FUNC, token.INTERFACE, token.MAP, token.STRUCT:
// Go keywords for composite types are type names // Go keywords for composite types are type names
// returned by reflect. Accept them as identifiers. // returned by reflect. Accept them as identifiers.
p.tok = token.IDENT; // p.lit is already set correctly p.tok = token.IDENT; // p.lit is already set correctly
} }
} }
func (p *parser) init(filename string, src []byte) { 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);
} }
...@@ -67,7 +67,7 @@ func (p *parser) expect(tok token.Token) token.Position { ...@@ -67,7 +67,7 @@ func (p *parser) expect(tok token.Token) token.Position {
if p.tok != tok { if p.tok != tok {
p.errorExpected(pos, "'" + tok.String() + "'"); p.errorExpected(pos, "'" + tok.String() + "'");
} }
p.next(); // make progress in any case p.next(); // make progress in any case
return pos; return pos;
} }
...@@ -114,7 +114,7 @@ func (p *parser) parseRuleName() (string, bool) { ...@@ -114,7 +114,7 @@ func (p *parser) parseRuleName() (string, bool) {
p.next(); p.next();
default: default:
p.errorExpected(p.pos, "rule name"); p.errorExpected(p.pos, "rule name");
p.next(); // make progress in any case p.next(); // make progress in any case
} }
return name, isIdent; return name, isIdent;
} }
...@@ -151,15 +151,15 @@ func (p *parser) parseLiteral() literal { ...@@ -151,15 +151,15 @@ 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 %
} }
} }
// 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());
...@@ -231,7 +231,7 @@ func (p *parser) parseOperand() (x expr) { ...@@ -231,7 +231,7 @@ func (p *parser) parseOperand() (x expr) {
p.expect(token.RBRACE); p.expect(token.RBRACE);
default: default:
x = p.parseField(); // may be nil x = p.parseField(); // may be nil
} }
return x; return x;
...@@ -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
...@@ -324,7 +328,7 @@ func (p *parser) parseFormat() { ...@@ -324,7 +328,7 @@ func (p *parser) parseFormat() {
default: default:
p.errorExpected(p.pos, "package declaration or format rule"); p.errorExpected(p.pos, "package declaration or format rule");
p.next(); // make progress in any case p.next(); // make progress in any case
} }
if p.tok == token.SEMICOLON { if p.tok == token.SEMICOLON {
...@@ -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);
......
...@@ -12,7 +12,7 @@ import ( ...@@ -12,7 +12,7 @@ import (
// Arguments to Run. // Arguments to Run.
const ( const (
DevNull = iota; DevNull = iota;
PassThrough; PassThrough;
Pipe; Pipe;
MergeWithStdout; MergeWithStdout;
...@@ -24,10 +24,10 @@ const ( ...@@ -24,10 +24,10 @@ const (
// or else nil, depending on the arguments to Run. // or else nil, depending on the arguments to Run.
// Pid is the running command's operating system process ID. // Pid is the running command's operating system process ID.
type Cmd struct { type Cmd struct {
Stdin *os.File; Stdin *os.File;
Stdout *os.File; Stdout *os.File;
Stderr *os.File; Stderr *os.File;
Pid int; Pid int;
} }
// Given mode (DevNull, etc), return file for child // Given mode (DevNull, etc), return file for child
...@@ -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;
} }
...@@ -23,12 +23,12 @@ type Var interface { ...@@ -23,12 +23,12 @@ type Var interface {
// Int is a 64-bit integer variable, and satisfies the Var interface. // Int is a 64-bit integer variable, and satisfies the Var interface.
type Int struct { type Int struct {
i int64; i int64;
mu sync.Mutex; mu sync.Mutex;
} }
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,14 +39,14 @@ func (v *Int) Add(delta int64) { ...@@ -39,14 +39,14 @@ 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;
} }
// KeyValue represents a single entry in a Map. // KeyValue represents a single entry in a Map.
type KeyValue struct { type KeyValue struct {
Key string; Key string;
Value Var; Value Var;
} }
func (v *Map) String() string { func (v *Map) String() string {
...@@ -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,18 +45,18 @@ package flag ...@@ -45,18 +45,18 @@ package flag
import ( import (
"fmt"; "fmt";
"os"; "os";
"strconv" "strconv";
) )
// TODO(r): BUG: atob belongs elsewhere // TODO(r): BUG: atob belongs elsewhere
func atob(str string) (value bool, ok bool) { func atob(str string) (value bool, ok bool) {
switch str { switch str {
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,87 +66,87 @@ type boolValue struct { ...@@ -66,87 +66,87 @@ 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
type intValue struct { type intValue struct {
p *int; p *int;
} }
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
type int64Value struct { type int64Value struct {
p *int64; p *int64;
} }
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 {
v, err := strconv.Atoi64(s); v, err := strconv.Atoi64(s);
*i.p = v; *i.p = v;
return err == nil; return err == nil;
} }
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
type uintValue struct { type uintValue struct {
p *uint; p *uint;
} }
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 {
v, err := strconv.Atoui(s); v, err := strconv.Atoui(s);
*i.p = uint(v); *i.p = uint(v);
return err == nil; return err == nil;
} }
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
type uint64Value struct { type uint64Value struct {
p *uint64; p *uint64;
} }
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,17 +156,17 @@ func (i *uint64Value) set(s string) bool { ...@@ -156,17 +156,17 @@ 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
type stringValue struct { type stringValue struct {
p *string; p *string;
} }
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.
...@@ -187,31 +187,31 @@ type FlagValue interface { ...@@ -187,31 +187,31 @@ type FlagValue interface {
// A Flag represents the state of a flag. // A Flag represents the state of a flag.
type Flag struct { type Flag struct {
Name string; // name as it appears on command line Name string; // name as it appears on command line
Usage string; // help message Usage string; // help message
Value FlagValue; // value as set Value FlagValue; // value as set
DefValue string; // default value (as text); for usage message DefValue string; // default value (as text); for usage message
} }
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();
...@@ -407,11 +406,11 @@ func (f *allFlags) parseOne(index int) (ok bool, next int) ...@@ -407,11 +406,11 @@ func (f *allFlags) parseOne(index int) (ok bool, next int)
// it's a flag. does it have an argument? // it's a flag. does it have an argument?
has_value := false; has_value := false;
value := ""; 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] == '=' { 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;
} }
} }
...@@ -422,7 +421,7 @@ func (f *allFlags) parseOne(index int) (ok bool, next int) ...@@ -422,7 +421,7 @@ func (f *allFlags) parseOne(index int) (ok bool, next int)
os.Exit(2); os.Exit(2);
} }
m := flags.formal; m := flags.formal;
flag, alreadythere = m[name]; // BUG flag, alreadythere = m[name]; // BUG
if !alreadythere { if !alreadythere {
fmt.Fprintf(os.Stderr, "flag provided but not defined: -%s\n", name); fmt.Fprintf(os.Stderr, "flag provided but not defined: -%s\n", name);
Usage(); Usage();
...@@ -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;
} }
} }
} }
...@@ -5,28 +5,28 @@ ...@@ -5,28 +5,28 @@
package flag_test package flag_test
import ( import (
. "flag"; . "flag";
"testing"; "testing";
) )
var ( var (
test_bool = Bool("test_bool", false, "bool value"); test_bool = Bool("test_bool", false, "bool value");
test_int = Int("test_int", 0, "int value"); test_int = Int("test_int", 0, "int value");
test_int64 = Int64("test_int64", 0, "int64 value"); test_int64 = Int64("test_int64", 0, "int64 value");
test_uint = Uint("test_uint", 0, "uint value"); test_uint = Uint("test_uint", 0, "uint value");
test_uint64 = Uint64("test_uint64", 0, "uint64 value"); test_uint64 = Uint64("test_uint64", 0, "uint64 value");
test_string = String("test_string", "0", "string value"); test_string = String("test_string", "0", "string value");
) )
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,10 +9,10 @@ import ( ...@@ -9,10 +9,10 @@ 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"
/* /*
...@@ -29,17 +29,17 @@ var udigits string = "0123456789ABCDEF" ...@@ -29,17 +29,17 @@ var udigits string = "0123456789ABCDEF"
f.Fmt_ud(1<<63).Putnl(); // print string with automatic newline f.Fmt_ud(1<<63).Putnl(); // print string with automatic newline
*/ */
type Fmt struct { type Fmt struct {
buf string; buf string;
wid int; wid int;
wid_present bool; wid_present bool;
prec int; prec int;
prec_present bool; prec_present bool;
// flags // flags
minus bool; minus bool;
plus bool; plus bool;
sharp bool; sharp bool;
space bool; space bool;
zero bool; zero bool;
} }
func (f *Fmt) clearflags() { func (f *Fmt) clearflags() {
...@@ -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,10 +190,10 @@ func (f *Fmt) integer(a int64, base uint, is_signed bool, digits string) string ...@@ -190,10 +190,10 @@ 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));
} }
This diff is collapsed.
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