Add strconv.Atob, Btoa.

Fixes #639

R=rsc
CC=golang-dev
https://golang.org/cl/755041
This commit is contained in:
Rob Pike 2010-03-25 11:50:07 -07:00
parent 732f3919ab
commit 4b40426a90
5 changed files with 94 additions and 17 deletions

View file

@ -53,17 +53,6 @@ import (
"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
}
return false, false
}
// -- Bool Value
type boolValue struct {
p *bool
@ -75,9 +64,9 @@ func newBoolValue(val bool, p *bool) *boolValue {
}
func (b *boolValue) set(s string) bool {
v, ok := atob(s)
v, err := strconv.Atob(s)
*b.p = v
return ok
return err == nil
}
func (b *boolValue) String() string { return fmt.Sprintf("%v", *b.p) }

View file

@ -6,6 +6,7 @@ include ../../Make.$(GOARCH)
TARG=strconv
GOFILES=\
atob.go\
atof.go\
atoi.go\
decimal.go\

28
src/pkg/strconv/atob.go Normal file
View file

@ -0,0 +1,28 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package strconv
import "os"
// Atob returns the boolean value represented by the string.
// It accepts 1, t, T, TRUE, true, 0, f, F, FALSE, false. Any other value returns
// an error.
func Atob(str string) (value bool, err os.Error) {
switch str {
case "1", "t", "T", "true", "TRUE", "True":
return true, nil
case "0", "f", "F", "false", "FALSE", "False":
return false, nil
}
return false, &NumError{str, os.EINVAL}
}
// Btoa returns "true" or "false" according to the value of the boolean argument
func Btoa(b bool) string {
if b {
return "true"
}
return "false"
}

View file

@ -0,0 +1,56 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package strconv_test
import (
"os"
. "strconv"
"testing"
)
type atobTest struct {
in string
out bool
err os.Error
}
var atobtests = []atobTest{
atobTest{"", false, os.EINVAL},
atobTest{"asdf", false, os.EINVAL},
atobTest{"0", false, nil},
atobTest{"f", false, nil},
atobTest{"F", false, nil},
atobTest{"FALSE", false, nil},
atobTest{"false", false, nil},
atobTest{"1", true, nil},
atobTest{"t", true, nil},
atobTest{"T", true, nil},
atobTest{"TRUE", true, nil},
atobTest{"true", true, nil},
}
func TestAtob(t *testing.T) {
for _, test := range atobtests {
b, e := Atob(test.in)
if test.err != nil {
// expect an error
if e == nil {
t.Errorf("%s: expected %s but got nil", test.in, test.err)
} else {
// NumError assertion must succeed; it's the only thing we return.
if test.err != e.(*NumError).Error {
t.Errorf("%s: expected %s but got %s", test.in, test.err, e)
}
}
} else {
if e != nil {
t.Errorf("%s: expected no error but got %s", test.in, test.err, e)
}
if b != test.out {
t.Errorf("%s: expected %t but got %t", test.in, test.out, b)
}
}
}
}

View file

@ -105,8 +105,8 @@ import (
// Unmarshal maps an XML element to a slice by extending the length
// of the slice and mapping the element to the newly created value.
//
// Unmarshal maps an XML element to a bool by setting it true if the
// string value is "true" or "1", or false otherwise.
// Unmarshal maps an XML element to a bool by setting it to the boolean
// value represented by the string.
//
// Unmarshal maps an XML element to an integer or floating-point
// field by setting the field to the result of interpreting the string
@ -473,8 +473,11 @@ Loop:
}
t.Set(ftmp)
case *reflect.BoolValue:
btmp := strings.TrimSpace(string(data))
t.Set(strings.ToLower(btmp) == "true" || btmp == "1")
value, err := strconv.Atob(strings.TrimSpace(string(data)))
if err != nil {
return err
}
t.Set(value)
case *reflect.StringValue:
t.Set(string(data))
case *reflect.SliceValue: