diff options
Diffstat (limited to 'libgo/go/encoding/json')
-rw-r--r-- | libgo/go/encoding/json/decode.go | 90 | ||||
-rw-r--r-- | libgo/go/encoding/json/decode_test.go | 27 | ||||
-rw-r--r-- | libgo/go/encoding/json/encode.go | 27 | ||||
-rw-r--r-- | libgo/go/encoding/json/encode_test.go | 22 | ||||
-rw-r--r-- | libgo/go/encoding/json/indent.go | 12 | ||||
-rw-r--r-- | libgo/go/encoding/json/number_test.go | 133 | ||||
-rw-r--r-- | libgo/go/encoding/json/scanner.go | 101 | ||||
-rw-r--r-- | libgo/go/encoding/json/stream.go | 8 |
8 files changed, 328 insertions, 92 deletions
diff --git a/libgo/go/encoding/json/decode.go b/libgo/go/encoding/json/decode.go index 530e852..539d952 100644 --- a/libgo/go/encoding/json/decode.go +++ b/libgo/go/encoding/json/decode.go @@ -37,6 +37,7 @@ import ( // To unmarshal JSON into a struct, Unmarshal matches incoming object // keys to the keys used by Marshal (either the struct field name or its tag), // preferring an exact match but also accepting a case-insensitive match. +// Unmarshal will only set exported fields of the struct. // // To unmarshal JSON into an interface value, // Unmarshal stores one of these in the interface value: @@ -48,16 +49,26 @@ import ( // map[string]interface{}, for JSON objects // nil for JSON null // -// To unmarshal a JSON array into a slice, Unmarshal resets the slice to nil -// and then appends each element to the slice. +// To unmarshal a JSON array into a slice, Unmarshal resets the slice length +// to zero and then appends each element to the slice. +// As a special case, to unmarshal an empty JSON array into a slice, +// Unmarshal replaces the slice with a new empty slice. // -// To unmarshal a JSON object into a map, Unmarshal replaces the map -// with an empty map and then adds key-value pairs from the object to -// the map. +// To unmarshal a JSON array into a Go array, Unmarshal decodes +// JSON array elements into corresponding Go array elements. +// If the Go array is smaller than the JSON array, +// the additional JSON array elements are discarded. +// If the JSON array is smaller than the Go array, +// the additional Go array elements are set to zero values. +// +// To unmarshal a JSON object into a string-keyed map, Unmarshal first +// establishes a map to use, If the map is nil, Unmarshal allocates a new map. +// Otherwise Unmarshal reuses the existing map, keeping existing entries. +// Unmarshal then stores key-value pairs from the JSON object into the map. // // If a JSON value is not appropriate for a given target type, // or if a JSON number overflows the target type, Unmarshal -// skips that field and completes the unmarshalling as best it can. +// skips that field and completes the unmarshaling as best it can. // If no more serious errors are encountered, Unmarshal returns // an UnmarshalTypeError describing the earliest such error. // @@ -174,6 +185,66 @@ func (n Number) Int64() (int64, error) { return strconv.ParseInt(string(n), 10, 64) } +// isValidNumber reports whether s is a valid JSON number literal. +func isValidNumber(s string) bool { + // This function implements the JSON numbers grammar. + // See https://tools.ietf.org/html/rfc7159#section-6 + // and http://json.org/number.gif + + if s == "" { + return false + } + + // Optional - + if s[0] == '-' { + s = s[1:] + if s == "" { + return false + } + } + + // Digits + switch { + default: + return false + + case s[0] == '0': + s = s[1:] + + case '1' <= s[0] && s[0] <= '9': + s = s[1:] + for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { + s = s[1:] + } + } + + // . followed by 1 or more digits. + if len(s) >= 2 && s[0] == '.' && '0' <= s[1] && s[1] <= '9' { + s = s[2:] + for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { + s = s[1:] + } + } + + // e or E followed by an optional - or + and + // 1 or more digits. + if len(s) >= 2 && (s[0] == 'e' || s[0] == 'E') { + s = s[1:] + if s[0] == '+' || s[0] == '-' { + s = s[1:] + if s == "" { + return false + } + } + for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { + s = s[1:] + } + } + + // Make sure we are at the end. + return s == "" +} + // decodeState represents the state while decoding a JSON value. type decodeState struct { data []byte @@ -241,7 +312,7 @@ func (d *decodeState) scanWhile(op int) int { newOp = d.scan.eof() d.off = len(d.data) + 1 // mark processed EOF with len+1 } else { - c := int(d.data[d.off]) + c := d.data[d.off] d.off++ newOp = d.scan.step(&d.scan, c) } @@ -757,7 +828,7 @@ func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool d.saveError(err) break } - v.Set(reflect.ValueOf(b[0:n])) + v.SetBytes(b[:n]) case reflect.String: v.SetString(string(s)) case reflect.Interface: @@ -781,6 +852,9 @@ func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool default: if v.Kind() == reflect.String && v.Type() == numberType { v.SetString(s) + if !isValidNumber(s) { + d.error(fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", item)) + } break } if fromQuoted { diff --git a/libgo/go/encoding/json/decode_test.go b/libgo/go/encoding/json/decode_test.go index 51b15ef..9546ae4 100644 --- a/libgo/go/encoding/json/decode_test.go +++ b/libgo/go/encoding/json/decode_test.go @@ -728,7 +728,7 @@ func TestErrorMessageFromMisusedString(t *testing.T) { } func noSpace(c rune) rune { - if isSpace(c) { + if isSpace(byte(c)) { //only used for ascii return -1 } return c @@ -1218,12 +1218,12 @@ func TestStringKind(t *testing.T) { data, err := Marshal(m1) if err != nil { - t.Errorf("Unexpected error marshalling: %v", err) + t.Errorf("Unexpected error marshaling: %v", err) } err = Unmarshal(data, &m2) if err != nil { - t.Errorf("Unexpected error unmarshalling: %v", err) + t.Errorf("Unexpected error unmarshaling: %v", err) } if !reflect.DeepEqual(m1, m2) { @@ -1253,6 +1253,27 @@ func TestByteKind(t *testing.T) { } } +// The fix for issue 8962 introduced a regression. +// Issue 12921. +func TestSliceOfCustomByte(t *testing.T) { + type Uint8 uint8 + + a := []Uint8("hello") + + data, err := Marshal(a) + if err != nil { + t.Fatal(err) + } + var b []Uint8 + err = Unmarshal(data, &b) + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(a, b) { + t.Fatal("expected %v == %v", a, b) + } +} + var decodeTypeErrorTests = []struct { dest interface{} src string diff --git a/libgo/go/encoding/json/encode.go b/libgo/go/encoding/json/encode.go index e829a93..69ac7e0 100644 --- a/libgo/go/encoding/json/encode.go +++ b/libgo/go/encoding/json/encode.go @@ -14,6 +14,7 @@ import ( "bytes" "encoding" "encoding/base64" + "fmt" "math" "reflect" "runtime" @@ -30,7 +31,10 @@ import ( // Marshal traverses the value v recursively. // If an encountered value implements the Marshaler interface // and is not a nil pointer, Marshal calls its MarshalJSON method -// to produce JSON. The nil pointer exception is not strictly necessary +// to produce JSON. If no MarshalJSON method is present but the +// value implements encoding.TextMarshaler instead, Marshal calls +// its MarshalText method. +// The nil pointer exception is not strictly necessary // but mimics a similar, necessary exception in the behavior of // UnmarshalJSON. // @@ -445,12 +449,10 @@ func textMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) { } m := v.Interface().(encoding.TextMarshaler) b, err := m.MarshalText() - if err == nil { - _, err = e.stringBytes(b) - } if err != nil { e.error(&MarshalerError{v.Type(), err}) } + e.stringBytes(b) } func addrTextMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) { @@ -461,12 +463,10 @@ func addrTextMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) { } m := va.Interface().(encoding.TextMarshaler) b, err := m.MarshalText() - if err == nil { - _, err = e.stringBytes(b) - } if err != nil { e.error(&MarshalerError{v.Type(), err}) } + e.stringBytes(b) } func boolEncoder(e *encodeState, v reflect.Value, quoted bool) { @@ -530,9 +530,14 @@ var ( func stringEncoder(e *encodeState, v reflect.Value, quoted bool) { if v.Type() == numberType { numStr := v.String() + // In Go1.5 the empty string encodes to "0", while this is not a valid number literal + // we keep compatibility so check validity after this. if numStr == "" { numStr = "0" // Number's zero-val } + if !isValidNumber(numStr) { + e.error(fmt.Errorf("json: invalid number literal %q", numStr)) + } e.WriteString(numStr) return } @@ -780,7 +785,7 @@ func (sv stringValues) Less(i, j int) bool { return sv.get(i) < sv.get(j) } func (sv stringValues) get(i int) string { return sv[i].String() } // NOTE: keep in sync with stringBytes below. -func (e *encodeState) string(s string) (int, error) { +func (e *encodeState) string(s string) int { len0 := e.Len() e.WriteByte('"') start := 0 @@ -852,11 +857,11 @@ func (e *encodeState) string(s string) (int, error) { e.WriteString(s[start:]) } e.WriteByte('"') - return e.Len() - len0, nil + return e.Len() - len0 } // NOTE: keep in sync with string above. -func (e *encodeState) stringBytes(s []byte) (int, error) { +func (e *encodeState) stringBytes(s []byte) int { len0 := e.Len() e.WriteByte('"') start := 0 @@ -928,7 +933,7 @@ func (e *encodeState) stringBytes(s []byte) (int, error) { e.Write(s[start:]) } e.WriteByte('"') - return e.Len() - len0, nil + return e.Len() - len0 } // A field represents a single field found in a struct. diff --git a/libgo/go/encoding/json/encode_test.go b/libgo/go/encoding/json/encode_test.go index 7abfa85..c00491e 100644 --- a/libgo/go/encoding/json/encode_test.go +++ b/libgo/go/encoding/json/encode_test.go @@ -381,16 +381,10 @@ func TestStringBytes(t *testing.T) { r = append(r, i) } s := string(r) + "\xff\xff\xffhello" // some invalid UTF-8 too - _, err := es.string(s) - if err != nil { - t.Fatal(err) - } + es.string(s) esBytes := &encodeState{} - _, err = esBytes.stringBytes([]byte(s)) - if err != nil { - t.Fatal(err) - } + esBytes.stringBytes([]byte(s)) enc := es.Buffer.String() encBytes := esBytes.Buffer.String() @@ -443,6 +437,18 @@ func TestIssue6458(t *testing.T) { } } +func TestIssue10281(t *testing.T) { + type Foo struct { + N Number + } + x := Foo{Number(`invalid`)} + + b, err := Marshal(&x) + if err == nil { + t.Errorf("Marshal(&x) = %#q; want error", b) + } +} + func TestHTMLEscape(t *testing.T) { var b, want bytes.Buffer m := `{"M":"<html>foo &` + "\xe2\x80\xa8 \xe2\x80\xa9" + `</html>"}` diff --git a/libgo/go/encoding/json/indent.go b/libgo/go/encoding/json/indent.go index e1bacaf..7cd9f4d 100644 --- a/libgo/go/encoding/json/indent.go +++ b/libgo/go/encoding/json/indent.go @@ -36,7 +36,7 @@ func compact(dst *bytes.Buffer, src []byte, escape bool) error { dst.WriteByte(hex[src[i+2]&0xF]) start = i + 3 } - v := scan.step(&scan, int(c)) + v := scan.step(&scan, c) if v >= scanSkipSpace { if v == scanError { break @@ -70,8 +70,12 @@ func newline(dst *bytes.Buffer, prefix, indent string, depth int) { // indented line beginning with prefix followed by one or more // copies of indent according to the indentation nesting. // The data appended to dst does not begin with the prefix nor -// any indentation, and has no trailing newline, to make it -// easier to embed inside other formatted JSON data. +// any indentation, to make it easier to embed inside other formatted JSON data. +// Although leading space characters (space, tab, carriage return, newline) +// at the beginning of src are dropped, trailing space characters +// at the end of src are preserved and copied to dst. +// For example, if src has no trailing spaces, neither will dst; +// if src ends in a trailing newline, so will dst. func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error { origLen := dst.Len() var scan scanner @@ -80,7 +84,7 @@ func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error { depth := 0 for _, c := range src { scan.bytes++ - v := scan.step(&scan, int(c)) + v := scan.step(&scan, c) if v == scanSkipSpace { continue } diff --git a/libgo/go/encoding/json/number_test.go b/libgo/go/encoding/json/number_test.go new file mode 100644 index 0000000..4e63cf9 --- /dev/null +++ b/libgo/go/encoding/json/number_test.go @@ -0,0 +1,133 @@ +// Copyright 2011 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 json + +import ( + "regexp" + "testing" +) + +func TestNumberIsValid(t *testing.T) { + // From: http://stackoverflow.com/a/13340826 + var jsonNumberRegexp = regexp.MustCompile(`^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$`) + + validTests := []string{ + "0", + "-0", + "1", + "-1", + "0.1", + "-0.1", + "1234", + "-1234", + "12.34", + "-12.34", + "12E0", + "12E1", + "12e34", + "12E-0", + "12e+1", + "12e-34", + "-12E0", + "-12E1", + "-12e34", + "-12E-0", + "-12e+1", + "-12e-34", + "1.2E0", + "1.2E1", + "1.2e34", + "1.2E-0", + "1.2e+1", + "1.2e-34", + "-1.2E0", + "-1.2E1", + "-1.2e34", + "-1.2E-0", + "-1.2e+1", + "-1.2e-34", + "0E0", + "0E1", + "0e34", + "0E-0", + "0e+1", + "0e-34", + "-0E0", + "-0E1", + "-0e34", + "-0E-0", + "-0e+1", + "-0e-34", + } + + for _, test := range validTests { + if !isValidNumber(test) { + t.Errorf("%s should be valid", test) + } + + var f float64 + if err := Unmarshal([]byte(test), &f); err != nil { + t.Errorf("%s should be valid but Unmarshal failed: %v", test, err) + } + + if !jsonNumberRegexp.MatchString(test) { + t.Errorf("%s should be valid but regexp does not match", test) + } + } + + invalidTests := []string{ + "", + "invalid", + "1.0.1", + "1..1", + "-1-2", + "012a42", + "01.2", + "012", + "12E12.12", + "1e2e3", + "1e+-2", + "1e--23", + "1e", + "e1", + "1e+", + "1ea", + "1a", + "1.a", + "1.", + "01", + "1.e1", + } + + for _, test := range invalidTests { + if isValidNumber(test) { + t.Errorf("%s should be invalid", test) + } + + var f float64 + if err := Unmarshal([]byte(test), &f); err == nil { + t.Errorf("%s should be invalid but unmarshal wrote %v", test, f) + } + + if jsonNumberRegexp.MatchString(test) { + t.Errorf("%s should be invalid but matches regexp", test) + } + } +} + +func BenchmarkNumberIsValid(b *testing.B) { + s := "-61657.61667E+61673" + for i := 0; i < b.N; i++ { + isValidNumber(s) + } +} + +func BenchmarkNumberIsValidRegexp(b *testing.B) { + var jsonNumberRegexp = regexp.MustCompile(`^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$`) + s := "-61657.61667E+61673" + for i := 0; i < b.N; i++ { + jsonNumberRegexp.MatchString(s) + } +} diff --git a/libgo/go/encoding/json/scanner.go b/libgo/go/encoding/json/scanner.go index 38d0b08..ee6622e 100644 --- a/libgo/go/encoding/json/scanner.go +++ b/libgo/go/encoding/json/scanner.go @@ -21,7 +21,7 @@ func checkValid(data []byte, scan *scanner) error { scan.reset() for _, c := range data { scan.bytes++ - if scan.step(scan, int(c)) == scanError { + if scan.step(scan, c) == scanError { return scan.err } } @@ -37,7 +37,7 @@ func checkValid(data []byte, scan *scanner) error { func nextValue(data []byte, scan *scanner) (value, rest []byte, err error) { scan.reset() for i, c := range data { - v := scan.step(scan, int(c)) + v := scan.step(scan, c) if v >= scanEndObject { switch v { // probe the scanner with a space to determine whether we will @@ -50,7 +50,7 @@ func nextValue(data []byte, scan *scanner) (value, rest []byte, err error) { case scanError: return nil, nil, scan.err case scanEnd: - return data[0:i], data[i:], nil + return data[:i], data[i:], nil } } } @@ -85,7 +85,7 @@ type scanner struct { // Also tried using an integer constant and a single func // with a switch, but using the func directly was 10% faster // on a 64-bit Mac Mini, and it's nicer to read. - step func(*scanner, int) int + step func(*scanner, byte) int // Reached end of top-level value. endTop bool @@ -99,7 +99,7 @@ type scanner struct { // 1-byte redo (see undo method) redo bool redoCode int - redoState func(*scanner, int) int + redoState func(*scanner, byte) int // total bytes consumed, updated by decoder.Decode bytes int64 @@ -188,13 +188,13 @@ func (s *scanner) popParseState() { } } -func isSpace(c rune) bool { +func isSpace(c byte) bool { return c == ' ' || c == '\t' || c == '\r' || c == '\n' } // stateBeginValueOrEmpty is the state after reading `[`. -func stateBeginValueOrEmpty(s *scanner, c int) int { - if c <= ' ' && isSpace(rune(c)) { +func stateBeginValueOrEmpty(s *scanner, c byte) int { + if c <= ' ' && isSpace(c) { return scanSkipSpace } if c == ']' { @@ -204,8 +204,8 @@ func stateBeginValueOrEmpty(s *scanner, c int) int { } // stateBeginValue is the state at the beginning of the input. -func stateBeginValue(s *scanner, c int) int { - if c <= ' ' && isSpace(rune(c)) { +func stateBeginValue(s *scanner, c byte) int { + if c <= ' ' && isSpace(c) { return scanSkipSpace } switch c { @@ -244,8 +244,8 @@ func stateBeginValue(s *scanner, c int) int { } // stateBeginStringOrEmpty is the state after reading `{`. -func stateBeginStringOrEmpty(s *scanner, c int) int { - if c <= ' ' && isSpace(rune(c)) { +func stateBeginStringOrEmpty(s *scanner, c byte) int { + if c <= ' ' && isSpace(c) { return scanSkipSpace } if c == '}' { @@ -257,8 +257,8 @@ func stateBeginStringOrEmpty(s *scanner, c int) int { } // stateBeginString is the state after reading `{"key": value,`. -func stateBeginString(s *scanner, c int) int { - if c <= ' ' && isSpace(rune(c)) { +func stateBeginString(s *scanner, c byte) int { + if c <= ' ' && isSpace(c) { return scanSkipSpace } if c == '"' { @@ -270,7 +270,7 @@ func stateBeginString(s *scanner, c int) int { // stateEndValue is the state after completing a value, // such as after reading `{}` or `true` or `["x"`. -func stateEndValue(s *scanner, c int) int { +func stateEndValue(s *scanner, c byte) int { n := len(s.parseState) if n == 0 { // Completed top-level before the current byte. @@ -278,7 +278,7 @@ func stateEndValue(s *scanner, c int) int { s.endTop = true return stateEndTop(s, c) } - if c <= ' ' && isSpace(rune(c)) { + if c <= ' ' && isSpace(c) { s.step = stateEndValue return scanSkipSpace } @@ -319,7 +319,7 @@ func stateEndValue(s *scanner, c int) int { // stateEndTop is the state after finishing the top-level value, // such as after reading `{}` or `[1,2,3]`. // Only space characters should be seen now. -func stateEndTop(s *scanner, c int) int { +func stateEndTop(s *scanner, c byte) int { if c != ' ' && c != '\t' && c != '\r' && c != '\n' { // Complain about non-space byte on next call. s.error(c, "after top-level value") @@ -328,7 +328,7 @@ func stateEndTop(s *scanner, c int) int { } // stateInString is the state after reading `"`. -func stateInString(s *scanner, c int) int { +func stateInString(s *scanner, c byte) int { if c == '"' { s.step = stateEndValue return scanContinue @@ -344,13 +344,12 @@ func stateInString(s *scanner, c int) int { } // stateInStringEsc is the state after reading `"\` during a quoted string. -func stateInStringEsc(s *scanner, c int) int { +func stateInStringEsc(s *scanner, c byte) int { switch c { case 'b', 'f', 'n', 'r', 't', '\\', '/', '"': s.step = stateInString return scanContinue - } - if c == 'u' { + case 'u': s.step = stateInStringEscU return scanContinue } @@ -358,7 +357,7 @@ func stateInStringEsc(s *scanner, c int) int { } // stateInStringEscU is the state after reading `"\u` during a quoted string. -func stateInStringEscU(s *scanner, c int) int { +func stateInStringEscU(s *scanner, c byte) int { if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' { s.step = stateInStringEscU1 return scanContinue @@ -368,7 +367,7 @@ func stateInStringEscU(s *scanner, c int) int { } // stateInStringEscU1 is the state after reading `"\u1` during a quoted string. -func stateInStringEscU1(s *scanner, c int) int { +func stateInStringEscU1(s *scanner, c byte) int { if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' { s.step = stateInStringEscU12 return scanContinue @@ -378,7 +377,7 @@ func stateInStringEscU1(s *scanner, c int) int { } // stateInStringEscU12 is the state after reading `"\u12` during a quoted string. -func stateInStringEscU12(s *scanner, c int) int { +func stateInStringEscU12(s *scanner, c byte) int { if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' { s.step = stateInStringEscU123 return scanContinue @@ -388,7 +387,7 @@ func stateInStringEscU12(s *scanner, c int) int { } // stateInStringEscU123 is the state after reading `"\u123` during a quoted string. -func stateInStringEscU123(s *scanner, c int) int { +func stateInStringEscU123(s *scanner, c byte) int { if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' { s.step = stateInString return scanContinue @@ -398,7 +397,7 @@ func stateInStringEscU123(s *scanner, c int) int { } // stateNeg is the state after reading `-` during a number. -func stateNeg(s *scanner, c int) int { +func stateNeg(s *scanner, c byte) int { if c == '0' { s.step = state0 return scanContinue @@ -412,7 +411,7 @@ func stateNeg(s *scanner, c int) int { // state1 is the state after reading a non-zero integer during a number, // such as after reading `1` or `100` but not `0`. -func state1(s *scanner, c int) int { +func state1(s *scanner, c byte) int { if '0' <= c && c <= '9' { s.step = state1 return scanContinue @@ -421,7 +420,7 @@ func state1(s *scanner, c int) int { } // state0 is the state after reading `0` during a number. -func state0(s *scanner, c int) int { +func state0(s *scanner, c byte) int { if c == '.' { s.step = stateDot return scanContinue @@ -435,7 +434,7 @@ func state0(s *scanner, c int) int { // stateDot is the state after reading the integer and decimal point in a number, // such as after reading `1.`. -func stateDot(s *scanner, c int) int { +func stateDot(s *scanner, c byte) int { if '0' <= c && c <= '9' { s.step = stateDot0 return scanContinue @@ -445,9 +444,8 @@ func stateDot(s *scanner, c int) int { // stateDot0 is the state after reading the integer, decimal point, and subsequent // digits of a number, such as after reading `3.14`. -func stateDot0(s *scanner, c int) int { +func stateDot0(s *scanner, c byte) int { if '0' <= c && c <= '9' { - s.step = stateDot0 return scanContinue } if c == 'e' || c == 'E' { @@ -459,12 +457,8 @@ func stateDot0(s *scanner, c int) int { // stateE is the state after reading the mantissa and e in a number, // such as after reading `314e` or `0.314e`. -func stateE(s *scanner, c int) int { - if c == '+' { - s.step = stateESign - return scanContinue - } - if c == '-' { +func stateE(s *scanner, c byte) int { + if c == '+' || c == '-' { s.step = stateESign return scanContinue } @@ -473,7 +467,7 @@ func stateE(s *scanner, c int) int { // stateESign is the state after reading the mantissa, e, and sign in a number, // such as after reading `314e-` or `0.314e+`. -func stateESign(s *scanner, c int) int { +func stateESign(s *scanner, c byte) int { if '0' <= c && c <= '9' { s.step = stateE0 return scanContinue @@ -484,16 +478,15 @@ func stateESign(s *scanner, c int) int { // stateE0 is the state after reading the mantissa, e, optional sign, // and at least one digit of the exponent in a number, // such as after reading `314e-2` or `0.314e+1` or `3.14e0`. -func stateE0(s *scanner, c int) int { +func stateE0(s *scanner, c byte) int { if '0' <= c && c <= '9' { - s.step = stateE0 return scanContinue } return stateEndValue(s, c) } // stateT is the state after reading `t`. -func stateT(s *scanner, c int) int { +func stateT(s *scanner, c byte) int { if c == 'r' { s.step = stateTr return scanContinue @@ -502,7 +495,7 @@ func stateT(s *scanner, c int) int { } // stateTr is the state after reading `tr`. -func stateTr(s *scanner, c int) int { +func stateTr(s *scanner, c byte) int { if c == 'u' { s.step = stateTru return scanContinue @@ -511,7 +504,7 @@ func stateTr(s *scanner, c int) int { } // stateTru is the state after reading `tru`. -func stateTru(s *scanner, c int) int { +func stateTru(s *scanner, c byte) int { if c == 'e' { s.step = stateEndValue return scanContinue @@ -520,7 +513,7 @@ func stateTru(s *scanner, c int) int { } // stateF is the state after reading `f`. -func stateF(s *scanner, c int) int { +func stateF(s *scanner, c byte) int { if c == 'a' { s.step = stateFa return scanContinue @@ -529,7 +522,7 @@ func stateF(s *scanner, c int) int { } // stateFa is the state after reading `fa`. -func stateFa(s *scanner, c int) int { +func stateFa(s *scanner, c byte) int { if c == 'l' { s.step = stateFal return scanContinue @@ -538,7 +531,7 @@ func stateFa(s *scanner, c int) int { } // stateFal is the state after reading `fal`. -func stateFal(s *scanner, c int) int { +func stateFal(s *scanner, c byte) int { if c == 's' { s.step = stateFals return scanContinue @@ -547,7 +540,7 @@ func stateFal(s *scanner, c int) int { } // stateFals is the state after reading `fals`. -func stateFals(s *scanner, c int) int { +func stateFals(s *scanner, c byte) int { if c == 'e' { s.step = stateEndValue return scanContinue @@ -556,7 +549,7 @@ func stateFals(s *scanner, c int) int { } // stateN is the state after reading `n`. -func stateN(s *scanner, c int) int { +func stateN(s *scanner, c byte) int { if c == 'u' { s.step = stateNu return scanContinue @@ -565,7 +558,7 @@ func stateN(s *scanner, c int) int { } // stateNu is the state after reading `nu`. -func stateNu(s *scanner, c int) int { +func stateNu(s *scanner, c byte) int { if c == 'l' { s.step = stateNul return scanContinue @@ -574,7 +567,7 @@ func stateNu(s *scanner, c int) int { } // stateNul is the state after reading `nul`. -func stateNul(s *scanner, c int) int { +func stateNul(s *scanner, c byte) int { if c == 'l' { s.step = stateEndValue return scanContinue @@ -584,19 +577,19 @@ func stateNul(s *scanner, c int) int { // stateError is the state after reaching a syntax error, // such as after reading `[1}` or `5.1.2`. -func stateError(s *scanner, c int) int { +func stateError(s *scanner, c byte) int { return scanError } // error records an error and switches to the error state. -func (s *scanner) error(c int, context string) int { +func (s *scanner) error(c byte, context string) int { s.step = stateError s.err = &SyntaxError{"invalid character " + quoteChar(c) + " " + context, s.bytes} return scanError } // quoteChar formats c as a quoted character literal -func quoteChar(c int) string { +func quoteChar(c byte) string { // special cases - different from quoted strings if c == '\'' { return `'\''` @@ -623,7 +616,7 @@ func (s *scanner) undo(scanCode int) { } // stateRedo helps implement the scanner's 1-byte undo. -func stateRedo(s *scanner, c int) int { +func stateRedo(s *scanner, c byte) int { s.redo = false s.step = s.redoState return s.redoCode diff --git a/libgo/go/encoding/json/stream.go b/libgo/go/encoding/json/stream.go index dc53bce..8ddcf4d 100644 --- a/libgo/go/encoding/json/stream.go +++ b/libgo/go/encoding/json/stream.go @@ -90,7 +90,7 @@ Input: // Look in the buffer for a new value. for i, c := range dec.buf[scanp:] { dec.scan.bytes++ - v := dec.scan.step(&dec.scan, int(c)) + v := dec.scan.step(&dec.scan, c) if v == scanEnd { scanp += i break Input @@ -157,7 +157,7 @@ func (dec *Decoder) refill() error { func nonSpace(b []byte) bool { for _, c := range b { - if !isSpace(rune(c)) { + if !isSpace(c) { return true } } @@ -433,7 +433,7 @@ func (dec *Decoder) tokenError(c byte) (Token, error) { case tokenObjectComma: context = " after object key:value pair" } - return nil, &SyntaxError{"invalid character " + quoteChar(int(c)) + " " + context, 0} + return nil, &SyntaxError{"invalid character " + quoteChar(c) + " " + context, 0} } // More reports whether there is another element in the @@ -448,7 +448,7 @@ func (dec *Decoder) peek() (byte, error) { for { for i := dec.scanp; i < len(dec.buf); i++ { c := dec.buf[i] - if isSpace(rune(c)) { + if isSpace(c) { continue } dec.scanp = i |