From ceed634708eae65f859730bb0e5d2b5504dd1116 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Thu, 7 May 2020 13:51:17 +0900 Subject: [PATCH] Remove goto statement --- decode_string.go | 76 ++++++++++++++++++++++++------------------------ decode_struct.go | 5 +++- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/decode_string.go b/decode_string.go index 94940e8..d2d4a9e 100644 --- a/decode_string.go +++ b/decode_string.go @@ -23,46 +23,46 @@ func (d *stringDecoder) decode(buf []byte, cursor int, p uintptr) (int, error) { } func (d *stringDecoder) decodeByte(buf []byte, cursor int) ([]byte, int, error) { -LOOP: - switch buf[cursor] { - case '\000': - return nil, 0, errors.New("unexpected error key delimiter") - case ' ', '\n', '\t', '\r': - cursor++ - goto LOOP - case '"': - cursor++ - start := cursor - for { - switch buf[cursor] { - case '\\': - cursor++ - case '"': - literal := buf[start:cursor] - cursor++ - return literal, cursor, nil - case '\000': - return nil, 0, errors.New("unexpected error string") - } + for { + switch buf[cursor] { + case '\000': + return nil, 0, errors.New("unexpected error key delimiter") + case ' ', '\n', '\t', '\r': cursor++ + case '"': + cursor++ + start := cursor + for { + switch buf[cursor] { + case '\\': + cursor++ + case '"': + literal := buf[start:cursor] + cursor++ + return literal, cursor, nil + case '\000': + return nil, 0, errors.New("unexpected error string") + } + cursor++ + } + return nil, 0, errors.New("unexpected error string") + case 'n': + buflen := len(buf) + if cursor+3 >= buflen { + return nil, 0, errors.New("unexpected error. invalid bool character") + } + if buf[cursor+1] != 'u' { + return nil, 0, errors.New("unexpected error. invalid bool character") + } + if buf[cursor+2] != 'l' { + return nil, 0, errors.New("unexpected error. invalid bool character") + } + if buf[cursor+3] != 'l' { + return nil, 0, errors.New("unexpected error. invalid bool character") + } + cursor += 5 + return []byte{'n', 'u', 'l', 'l'}, cursor, nil } - return nil, 0, errors.New("unexpected error string") - case 'n': - buflen := len(buf) - if cursor+3 >= buflen { - return nil, 0, errors.New("unexpected error. invalid bool character") - } - if buf[cursor+1] != 'u' { - return nil, 0, errors.New("unexpected error. invalid bool character") - } - if buf[cursor+2] != 'l' { - return nil, 0, errors.New("unexpected error. invalid bool character") - } - if buf[cursor+3] != 'l' { - return nil, 0, errors.New("unexpected error. invalid bool character") - } - cursor += 5 - return []byte{'n', 'u', 'l', 'l'}, cursor, nil } return nil, 0, errors.New("unexpected error key delimiter") } diff --git a/decode_struct.go b/decode_struct.go index 1c4c66c..e9bd700 100644 --- a/decode_struct.go +++ b/decode_struct.go @@ -27,8 +27,10 @@ func (d *structDecoder) skipValue(buf []byte, cursor int) (int, error) { braceCount := 0 bracketCount := 0 buflen := len(buf) - for ; cursor < buflen; cursor++ { + for { switch buf[cursor] { + case '\000': + return cursor, errors.New("unexpected error value") case '{': braceCount++ case '[': @@ -59,6 +61,7 @@ func (d *structDecoder) skipValue(buf []byte, cursor int) (int, error) { } QUOTE_END: } + cursor++ } return cursor, errors.New("unexpected error value") }