]> Cypherpunks repositories - gostls13.git/commitdiff
Add strconv.Atob, Btoa.
authorRob Pike <r@golang.org>
Thu, 25 Mar 2010 18:50:07 +0000 (11:50 -0700)
committerRob Pike <r@golang.org>
Thu, 25 Mar 2010 18:50:07 +0000 (11:50 -0700)
Fixes #639

R=rsc
CC=golang-dev
https://golang.org/cl/755041

src/pkg/flag/flag.go
src/pkg/strconv/Makefile
src/pkg/strconv/atob.go [new file with mode: 0644]
src/pkg/strconv/atob_test.go [new file with mode: 0644]
src/pkg/xml/read.go

index d57a59c033d36e51410a58ad7b36103e3c6f0f88..e51bf7ddc6b011411dde116afe60b258006e901f 100644 (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) }
index 8b20273b8568ce04f84f64c694351f737ae075b3..57849a82175b73dc2890bf354bb9551783f86613 100644 (file)
@@ -6,6 +6,7 @@ include ../../Make.$(GOARCH)
 
 TARG=strconv
 GOFILES=\
+       atob.go\
        atof.go\
        atoi.go\
        decimal.go\
diff --git a/src/pkg/strconv/atob.go b/src/pkg/strconv/atob.go
new file mode 100644 (file)
index 0000000..69fa229
--- /dev/null
@@ -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"
+}
diff --git a/src/pkg/strconv/atob_test.go b/src/pkg/strconv/atob_test.go
new file mode 100644 (file)
index 0000000..ffad4b2
--- /dev/null
@@ -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)
+                       }
+               }
+       }
+}
index dedf68944674edc47c94637253863cdf759b120d..e3ae2c402d8729c2655b10568d8f04fe73a6cd1a 100644 (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: