diff options
| author | ian <ian@138bc75d-0d04-0410-961f-82ee72b054a4> | 2013-01-30 01:37:13 +0000 |
|---|---|---|
| committer | ian <ian@138bc75d-0d04-0410-961f-82ee72b054a4> | 2013-01-30 01:37:13 +0000 |
| commit | 1513deedeb785271eed97fc879cc4420f74bfc61 (patch) | |
| tree | ac10866a1e77c784cb9d48b872fd8013ba124fd0 /libgo/go/encoding | |
| parent | 06cb9a772b0363f1b0460dd3dccb254aa06ec061 (diff) | |
| download | ppe42-gcc-1513deedeb785271eed97fc879cc4420f74bfc61.tar.gz ppe42-gcc-1513deedeb785271eed97fc879cc4420f74bfc61.zip | |
libgo: Update Go library to master revision 15502/229081515358.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@195569 138bc75d-0d04-0410-961f-82ee72b054a4
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 333c1c0ce9e..29dbc26d417 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 6e6815ff131..95e91209184 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 97f2a41eb76..a91c6da01d3 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 9592467d25b..00f4726cf7f 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 4d66f556767..07c9e1d390c 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 "" |

