diff options
author | Ian Lance Taylor <ian@gcc.gnu.org> | 2013-01-30 01:37:13 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2013-01-30 01:37:13 +0000 |
commit | 777133fefb9704c957884216e44bf7ba25ca2fae (patch) | |
tree | ac10866a1e77c784cb9d48b872fd8013ba124fd0 /libgo/go/encoding | |
parent | 900f0840363c7a110723b18024b361f22b8892b9 (diff) | |
download | gcc-777133fefb9704c957884216e44bf7ba25ca2fae.zip gcc-777133fefb9704c957884216e44bf7ba25ca2fae.tar.gz gcc-777133fefb9704c957884216e44bf7ba25ca2fae.tar.bz2 |
libgo: Update Go library to master revision 15502/229081515358.
From-SVN: r195569
Diffstat (limited to 'libgo/go/encoding')
-rw-r--r-- | libgo/go/encoding/json/bench_test.go | 34 | ||||
-rw-r--r-- | libgo/go/encoding/json/decode.go | 19 | ||||
-rw-r--r-- | libgo/go/encoding/json/decode_test.go | 23 | ||||
-rw-r--r-- | libgo/go/encoding/json/stream.go | 7 | ||||
-rw-r--r-- | libgo/go/encoding/json/stream_test.go | 24 |
5 files changed, 86 insertions, 21 deletions
diff --git a/libgo/go/encoding/json/bench_test.go b/libgo/go/encoding/json/bench_test.go index 333c1c0..29dbc26 100644 --- a/libgo/go/encoding/json/bench_test.go +++ b/libgo/go/encoding/json/bench_test.go @@ -153,5 +153,37 @@ func BenchmarkCodeUnmarshalReuse(b *testing.B) { b.Fatal("Unmmarshal:", err) } } - b.SetBytes(int64(len(codeJSON))) +} + +func BenchmarkUnmarshalString(b *testing.B) { + data := []byte(`"hello, world"`) + var s string + + for i := 0; i < b.N; i++ { + if err := Unmarshal(data, &s); err != nil { + b.Fatal("Unmarshal:", err) + } + } +} + +func BenchmarkUnmarshalFloat64(b *testing.B) { + var f float64 + data := []byte(`3.14`) + + for i := 0; i < b.N; i++ { + if err := Unmarshal(data, &f); err != nil { + b.Fatal("Unmarshal:", err) + } + } +} + +func BenchmarkUnmarshalInt64(b *testing.B) { + var x int64 + data := []byte(`3`) + + for i := 0; i < b.N; i++ { + if err := Unmarshal(data, &x); err != nil { + b.Fatal("Unmarshal:", err) + } + } } diff --git a/libgo/go/encoding/json/decode.go b/libgo/go/encoding/json/decode.go index 6e6815f..95e9120 100644 --- a/libgo/go/encoding/json/decode.go +++ b/libgo/go/encoding/json/decode.go @@ -52,25 +52,6 @@ import ( // an UnmarshalTypeError describing the earliest such error. // func Unmarshal(data []byte, v interface{}) error { - - // skip heavy processing for primitive values - var first byte - var i int - for i, first = range data { - if !isSpace(rune(first)) { - break - } - } - if first != '{' && first != '[' { - rv := reflect.ValueOf(v) - if rv.Kind() != reflect.Ptr || rv.IsNil() { - return &InvalidUnmarshalError{reflect.TypeOf(v)} - } - var d decodeState - d.literalStore(data[i:], rv.Elem(), false) - return d.savedError - } - d := new(decodeState).init(data) // Quick check for well-formedness. diff --git a/libgo/go/encoding/json/decode_test.go b/libgo/go/encoding/json/decode_test.go index 97f2a41..a91c6da 100644 --- a/libgo/go/encoding/json/decode_test.go +++ b/libgo/go/encoding/json/decode_test.go @@ -1059,12 +1059,33 @@ func TestUnmarshalTypeError(t *testing.T) { for _, item := range decodeTypeErrorTests { err := Unmarshal([]byte(item.src), item.dest) if _, ok := err.(*UnmarshalTypeError); !ok { - t.Errorf("expected type error for Unmarshal(%q, type %T): got %v instead", + t.Errorf("expected type error for Unmarshal(%q, type %T): got %T", item.src, item.dest, err) } } } +var unmarshalSyntaxTests = []string{ + "tru", + "fals", + "nul", + "123e", + `"hello`, + `[1,2,3`, + `{"key":1`, + `{"key":1,`, +} + +func TestUnmarshalSyntax(t *testing.T) { + var x interface{} + for _, src := range unmarshalSyntaxTests { + err := Unmarshal([]byte(src), &x) + if _, ok := err.(*SyntaxError); !ok { + t.Errorf("expected syntax error for Unmarshal(%q): got %T", src, err) + } + } +} + // Test handling of unexported fields that should be ignored. // Issue 4660 type unexportedFields struct { diff --git a/libgo/go/encoding/json/stream.go b/libgo/go/encoding/json/stream.go index 9592467..00f4726 100644 --- a/libgo/go/encoding/json/stream.go +++ b/libgo/go/encoding/json/stream.go @@ -5,6 +5,7 @@ package json import ( + "bytes" "errors" "io" ) @@ -58,6 +59,12 @@ func (dec *Decoder) Decode(v interface{}) error { return err } +// Buffered returns a reader of the data remaining in the Decoder's +// buffer. The reader is valid until the next call to Decode. +func (dec *Decoder) Buffered() io.Reader { + return bytes.NewReader(dec.buf) +} + // readValue reads a JSON value into dec.buf. // It returns the length of the encoding. func (dec *Decoder) readValue() (int, error) { diff --git a/libgo/go/encoding/json/stream_test.go b/libgo/go/encoding/json/stream_test.go index 4d66f55..07c9e1d 100644 --- a/libgo/go/encoding/json/stream_test.go +++ b/libgo/go/encoding/json/stream_test.go @@ -6,8 +6,10 @@ package json import ( "bytes" + "io/ioutil" "net" "reflect" + "strings" "testing" ) @@ -83,6 +85,28 @@ func TestDecoder(t *testing.T) { } } +func TestDecoderBuffered(t *testing.T) { + r := strings.NewReader(`{"Name": "Gopher"} extra `) + var m struct { + Name string + } + d := NewDecoder(r) + err := d.Decode(&m) + if err != nil { + t.Fatal(err) + } + if m.Name != "Gopher" { + t.Errorf("Name = %q; want Gopher", m.Name) + } + rest, err := ioutil.ReadAll(d.Buffered()) + if err != nil { + t.Fatal(err) + } + if g, w := string(rest), " extra "; g != w { + t.Errorf("Remaining = %q; want %q", g, w) + } +} + func nlines(s string, n int) string { if n <= 0 { return "" |