Commit fcf8143d authored by Marvin Stenger's avatar Marvin Stenger Committed by Andrew Gerrand

encoding/json: scanner: use byte, more consistent

The fields step and redoState of struct scanner are now defined as
`func(s *scanner, c byte) int` instead of
`func(s *scanner, c int) int`, since bytes are sufficient.
Further changes improve the consistency in the scanner.go file.

Change-Id: Ifb85f2130d728d2b936d79914d87a1f0b5c6ee7d
Reviewed-on: https://go-review.googlesource.com/14801Reviewed-by: default avatarAndrew Gerrand <adg@golang.org>
Run-TryBot: Andrew Gerrand <adg@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 6d017835
......@@ -241,7 +241,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)
}
......
......@@ -716,7 +716,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
......
......@@ -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
......@@ -80,7 +80,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
}
......
......@@ -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
......
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment