Pass bigTest

This commit is contained in:
Tnze
2021-05-27 14:19:16 +08:00
parent d829d3bccf
commit 82a2efd6a0
4 changed files with 68 additions and 26 deletions

View File

@ -67,6 +67,7 @@ func writeLiteralPayload(e *Encoder, v interface{}) error {
} }
func writeCompoundPayload(e *Encoder, d *decodeState) error { func writeCompoundPayload(e *Encoder, d *decodeState) error {
defer d.scanNext()
for { for {
d.scanWhile(scanSkipSpace) d.scanWhile(scanSkipSpace)
if d.opcode == scanEndValue { if d.opcode == scanEndValue {
@ -78,7 +79,12 @@ func writeCompoundPayload(e *Encoder, d *decodeState) error {
// read tag name // read tag name
start := d.readIndex() start := d.readIndex()
d.scanWhile(scanContinue) d.scanWhile(scanContinue)
tagName := string(d.data[start:d.readIndex()]) var tagName string
if tt, v := parseLiteral(d.data[start:d.readIndex()]); tt == TagString {
tagName = v.(string)
} else {
tagName = string(d.data[start:d.readIndex()])
}
// read value // read value
if d.opcode == scanSkipSpace { if d.opcode == scanSkipSpace {
d.scanWhile(scanSkipSpace) d.scanWhile(scanSkipSpace)
@ -107,6 +113,7 @@ func writeListOrArray(e *Encoder, d *decodeState, writeTag bool, tagName string)
d.scanWhile(scanSkipSpace) d.scanWhile(scanSkipSpace)
if d.opcode == scanEndValue { // ']', empty TAG_List if d.opcode == scanEndValue { // ']', empty TAG_List
e.writeListHeader(TagEnd, tagName, 0, writeTag) e.writeListHeader(TagEnd, tagName, 0, writeTag)
d.scanNext()
return TagList, nil return TagList, nil
} }
@ -121,6 +128,9 @@ func writeListOrArray(e *Encoder, d *decodeState, writeTag bool, tagName string)
case scanBeginLiteral: case scanBeginLiteral:
d.scanWhile(scanContinue) d.scanWhile(scanContinue)
literal := d.data[start:d.readIndex()] literal := d.data[start:d.readIndex()]
if d.opcode == scanSkipSpace {
d.scanWhile(scanSkipSpace)
}
if d.opcode == scanListType { // TAG_X_Array if d.opcode == scanListType { // TAG_X_Array
var elemType byte var elemType byte
switch literal[0] { switch literal[0] {
@ -139,14 +149,19 @@ func writeListOrArray(e *Encoder, d *decodeState, writeTag bool, tagName string)
if writeTag { if writeTag {
e.writeTag(tagType, tagName) e.writeTag(tagType, tagName)
} }
if d.opcode == scanSkipSpace {
d.scanWhile(scanSkipSpace)
}
d.scanWhile(scanSkipSpace) // ;
if d.opcode == scanEndValue { // ]
// empty array
e.writeInt32(0)
break
}
for { for {
d.scanNext()
if d.opcode == scanSkipSpace { if d.opcode == scanSkipSpace {
d.scanWhile(scanSkipSpace) d.scanWhile(scanSkipSpace)
} }
if d.opcode == scanEndValue { // ]
break
}
if d.opcode != scanBeginLiteral { if d.opcode != scanBeginLiteral {
return tagType, errors.New("not literal in Array") return tagType, errors.New("not literal in Array")
} }
@ -167,6 +182,17 @@ func writeListOrArray(e *Encoder, d *decodeState, writeTag bool, tagName string)
e2.writeInt64(litVal.(int64)) e2.writeInt64(litVal.(int64))
} }
count++ count++
if d.opcode == scanSkipSpace {
d.scanWhile(scanSkipSpace)
}
if d.opcode == scanEndValue { // ]
break
}
if d.opcode != scanListValue {
panic(phasePanicMsg)
}
d.scanWhile(scanSkipSpace) // ,
} }
e.writeInt32(int32(count)) e.writeInt32(int32(count))
e.w.Write(buf.Bytes()) e.w.Write(buf.Bytes())
@ -197,7 +223,7 @@ func writeListOrArray(e *Encoder, d *decodeState, writeTag bool, tagName string)
if d.opcode != scanListValue { if d.opcode != scanListValue {
panic(phasePanicMsg) panic(phasePanicMsg)
} }
d.scanNext() d.scanWhile(scanSkipSpace)
start = d.readIndex() start = d.readIndex()
d.scanWhile(scanContinue) d.scanWhile(scanContinue)
literal = d.data[start:d.readIndex()] literal = d.data[start:d.readIndex()]
@ -221,11 +247,7 @@ func writeListOrArray(e *Encoder, d *decodeState, writeTag bool, tagName string)
if d.opcode == scanSkipSpace { if d.opcode == scanSkipSpace {
d.scanWhile(scanSkipSpace) d.scanWhile(scanSkipSpace)
} }
// read ',' or ']' // ',' or ']'
d.scanNext()
if d.opcode == scanSkipSpace {
d.scanWhile(scanSkipSpace)
}
if d.opcode == scanEndValue { if d.opcode == scanEndValue {
break break
} }
@ -251,7 +273,6 @@ func writeListOrArray(e *Encoder, d *decodeState, writeTag bool, tagName string)
d.scanWhile(scanSkipSpace) d.scanWhile(scanSkipSpace)
} }
// read ',' or ']' // read ',' or ']'
d.scanNext()
if d.opcode == scanSkipSpace { if d.opcode == scanSkipSpace {
d.scanWhile(scanSkipSpace) d.scanWhile(scanSkipSpace)
} }
@ -267,6 +288,7 @@ func writeListOrArray(e *Encoder, d *decodeState, writeTag bool, tagName string)
e.writeListHeader(TagCompound, tagName, count, writeTag) e.writeListHeader(TagCompound, tagName, count, writeTag)
e.w.Write(buf.Bytes()) e.w.Write(buf.Bytes())
} }
d.scanNext()
return return
} }
@ -334,7 +356,7 @@ func parseLiteral(literal []byte) (byte, interface{}) {
if isNumber(c) { if isNumber(c) {
continue continue
} else if integer { } else if integer {
if i == strlen-1 && isIntegerType(c) { if i == strlen-1 && i != 0 && isIntegerType(c) {
numberType = c numberType = c
strlen-- strlen--
} else if i > 0 || i == 0 && c != '-' { } else if i > 0 || i == 0 && c != '-' {

View File

@ -25,22 +25,28 @@ func TestEncoder_WriteSNBT(t *testing.T) {
{`{}`, []byte{10, 0, 0, 0}}, {`{}`, []byte{10, 0, 0, 0}},
{`{a:1b}`, []byte{10, 0, 0, 1, 0, 1, 'a', 1, 0}}, {`{a:1b}`, []byte{10, 0, 0, 1, 0, 1, 'a', 1, 0}},
{`{ a : 1b }`, []byte{10, 0, 0, 1, 0, 1, 'a', 1, 0}}, {`{ a : 1b }`, []byte{10, 0, 0, 1, 0, 1, 'a', 1, 0}},
{`{a:1,2:c}`, []byte{10, 0, 0, 3, 0, 1, 'a', 0, 0, 0, 1, 8, 0, 1, '2', 0, 1, 'c', 0}}, {`{b:1,2:c}`, []byte{10, 0, 0, 3, 0, 1, 'b', 0, 0, 0, 1, 8, 0, 1, '2', 0, 1, 'c', 0}},
{`{a:{b:{}}}`, []byte{10, 0, 0, 10, 0, 1, 'a', 10, 0, 1, 'b', 0, 0, 0}}, {`{c:{d:{}}}`, []byte{10, 0, 0, 10, 0, 1, 'c', 10, 0, 1, 'd', 0, 0, 0}},
{`{h:{},"i":{}}`, []byte{10, 0, 0, 10, 0, 1, 'h', 0, 10, 0, 1, 'i', 0, 0}},
{`[]`, []byte{9, 0, 0, 0, 0, 0, 0, 0}}, {`[]`, []byte{9, 0, 0, 0, 0, 0, 0, 0}},
{`[1b,2b,3b]`, []byte{9, 0, 0, 1, 0, 0, 0, 3, 1, 2, 3}}, {`[1b,2b,3b]`, []byte{9, 0, 0, 1, 0, 0, 0, 3, 1, 2, 3}},
{`[ 1b , 2b , 3b ]`, []byte{9, 0, 0, 1, 0, 0, 0, 3, 1, 2, 3}},
{`[a,"b",'c']`, []byte{9, 0, 0, 8, 0, 0, 0, 3, 0, 1, 'a', 0, 1, 'b', 0, 1, 'c'}}, {`[a,"b",'c']`, []byte{9, 0, 0, 8, 0, 0, 0, 3, 0, 1, 'a', 0, 1, 'b', 0, 1, 'c'}},
{`[{},{a:1b},{}]`, []byte{9, 0, 0, 10, 0, 0, 0, 3, 0, 1, 0, 1, 'a', 1, 0, 0}}, {`[{},{a:1b},{}]`, []byte{9, 0, 0, 10, 0, 0, 0, 3, 0, 1, 0, 1, 'a', 1, 0, 0}},
{`[ { } , { a : 1b } , { } ] `, []byte{9, 0, 0, 10, 0, 0, 0, 3, 0, 1, 0, 1, 'a', 1, 0, 0}}, {`[ { } , { a : 1b } , { } ] `, []byte{9, 0, 0, 10, 0, 0, 0, 3, 0, 1, 0, 1, 'a', 1, 0, 0}},
{`[[],[]]`, []byte{9, 0, 0, 9, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, {`[[],[]]`, []byte{9, 0, 0, 9, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
{`[B;]`, []byte{7, 0, 0, 0, 0, 0, 0}}, {`[B; ]`, []byte{7, 0, 0, 0, 0, 0, 0}},
{`[B;1b,2B,3B]`, []byte{7, 0, 0, 0, 0, 0, 3, 1, 2, 3}}, {`[B; 1b ,2B,3B]`, []byte{7, 0, 0, 0, 0, 0, 3, 1, 2, 3}},
{`[I;]`, []byte{11, 0, 0, 0, 0, 0, 0}}, {`[I;]`, []byte{11, 0, 0, 0, 0, 0, 0}},
{`[I;1,2,3]`, []byte{11, 0, 0, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}}, {`[I; 1, 2 ,3]`, []byte{11, 0, 0, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}},
{`[L;]`, []byte{12, 0, 0, 0, 0, 0, 0}}, {`[L;]`, []byte{12, 0, 0, 0, 0, 0, 0}},
{`[L;1L,2L,3L]`, []byte{12, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3}}, {`[ L; 1L,2L,3L]`, []byte{12, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3}},
{`{d:[]}`, []byte{10, 0, 0, 9, 0, 1, 'd', 0, 0, 0, 0, 0, 0}},
{`{e:[]}`, []byte{10, 0, 0, 9, 0, 1, 'e', 0, 0, 0, 0, 0, 0}},
{`{f:[], g:[]}`, []byte{10, 0, 0, 9, 0, 1, 'f', 0, 0, 0, 0, 0, 9, 0, 1, 'g', 0, 0, 0, 0, 0, 0}},
} }
for i := range testCases { for i := range testCases {
buf.Reset() buf.Reset()
@ -55,3 +61,13 @@ func TestEncoder_WriteSNBT(t *testing.T) {
} }
} }
} }
func TestEncoder_WriteSNBT_bigTest(t *testing.T) {
var buf bytes.Buffer
e := NewEncoder(&buf)
err := e.WriteSNBT(bigTestSNBT)
if err != nil {
t.Error(err)
}
}

View File

@ -10,10 +10,10 @@ const (
scanBeginLiteral // end implied by next result != scanContinue scanBeginLiteral // end implied by next result != scanContinue
scanBeginCompound // begin TAG_Compound (after left-brace ) scanBeginCompound // begin TAG_Compound (after left-brace )
scanBeginList // begin TAG_List (after left-brack) scanBeginList // begin TAG_List (after left-brack)
scanListValue // just finished read list value scanListValue // just finished read list value (after comma)
scanListType // just finished read list type (after "B;" or "L;") scanListType // just finished read list type (after "B;" or "L;")
scanCompoundTagName // just finished read tag name (before colon) scanCompoundTagName // just finished read tag name (before colon)
scanCompoundValue // just finished read value (before comma or right-brace ) scanCompoundValue // just finished read value (after comma)
scanSkipSpace // space byte; can skip; known to be last "continue" result scanSkipSpace // space byte; can skip; known to be last "continue" result
scanEndValue scanEndValue
@ -197,7 +197,7 @@ func (s *scanner) stateInSingleQuotedString(c byte) int {
func (s *scanner) stateInSingleQuotedStringEsc(c byte) int { func (s *scanner) stateInSingleQuotedStringEsc(c byte) int {
switch c { switch c {
case 'b', 'f', 'n', 'r', 't', '\\', '/', '\'': case '\\', '\'':
s.step = s.stateInSingleQuotedString s.step = s.stateInSingleQuotedString
return scanContinue return scanContinue
} }
@ -256,6 +256,9 @@ func (s *scanner) stateListOrArrayT(c byte) int {
} }
func (s *scanner) stateArrayT(c byte) int { func (s *scanner) stateArrayT(c byte) int {
if isSpace(c) {
return scanSkipSpace
}
if c == ']' { // empty array if c == ']' { // empty array
return scanEndValue return scanEndValue
} }

View File

@ -9,7 +9,7 @@ func TestSNBT_checkScanCode(t *testing.T) {
//t.SkipNow() //t.SkipNow()
var s scanner var s scanner
s.reset() s.reset()
for _, c := range []byte(`[{},{a:1b},{}]`) { for _, c := range []byte(`{ ghi: [B; 2B, 3B], klm: []}`) {
t.Logf("[%c] - %d", c, s.step(c)) t.Logf("[%c] - %d", c, s.step(c))
} }
t.Logf("[%c] - %d", ' ', s.eof()) t.Logf("[%c] - %d", ' ', s.eof())
@ -41,13 +41,14 @@ func TestSNBT_number(t *testing.T) {
} }
//go:embed bigTest_test.snbt //go:embed bigTest_test.snbt
var bigTest string var bigTestSNBT string
func TestSNBT_compound(t *testing.T) { func TestSNBT_compound(t *testing.T) {
goods := []string{ goods := []string{
`{}`, `{name:3.14f}`, `{ "name" : 12345 }`, `{}`, `{name:3.14f}`, `{ "name" : 12345 }`,
`{ abc: { }}`, `{ "a b\"c": {}, def: 12345}`, `{ abc: { }}`, `{ "a b\"c": {}, def: 12345}`,
bigTest, `{ ghi: [], klm: 1}`,
bigTestSNBT,
} }
var s scanner var s scanner
for _, str := range goods { for _, str := range goods {
@ -67,7 +68,7 @@ func TestSNBT_list(t *testing.T) {
`[{}, {}, {"a\"b":520}]`, // List of Compound `[{}, {}, {"a\"b":520}]`, // List of Compound
`[B,C,D]`, `[L, "abc"]`, // List of string (like array) `[B,C,D]`, `[L, "abc"]`, // List of string (like array)
`[B; 01B, 02B, 3B, 10B, 127B]`, // Array `[B; 01B, 02B, 3B, 10B, 127B]`, // Array
`[I;]`, // Empty array `[I;]`, `[B; ]`, // Empty array
} }
var s scanner var s scanner
scan := func(str string) bool { scan := func(str string) bool {