package decoderimport ()// A value of a stream which v2 skips, as the value of a name of no field, and which the buffer doesn't hold whole, is// skipped with V2AllowDuplicateNames and V2AllowInvalidUTF8 as v1 skips it ( see Stream.skipNested ): by the grammar,// read as it goes past what has been read, each byte once, by skipFastPath, which is skipFast with the levels of the// value followed. An error of encoding/json/v2 has its place, the path to it ( see V2Call.Path ), which a decoder of// v1 doesn't know: the levels of the value which are open where the skip stops are added to the path for it, with// their counts and their last names, which are copied when a read moves the buffer ( see Stream.copyNames ), and the// error is found again from the end of the token before it, which the buffer keeps while the skip reads.// skipPath is what skipFastPath follows of the levels of the value which it skips, the levels 0 up to the one where// it stops, for the place of an error: for an array, the commas after its elements, and for an object, the number// of its names, and the total offset of the quote of the last one in the buffer, or copiedName once the name was// copied into names, when a read moved the buffer. level and objects are the levels which are open while the stream// reads, whose names are copied then; active is set while a value is skipped.type skipPath struct { counts, quotes [skipLevels]int64 names [skipLevels][]byte level int64 objects uint64 active bool}// copiedName is the place of the quote of the last name of a level of a skipPath whose name is copied into names.const copiedName = -1// skipFastPath is skipFast for a value of a stream whose levels tr follows: an open level has no comma and no name, a// comma of an array and a name which is read are counted, and the place of the name, whose quote is at the total// offset base in buf, kept. A name which skipFast leaves to its caller is counted by the caller. The numbers which// are not integers are read here, as most numbers of a value are valid and whole in what has been read.////nolint:maintidx // one loop of gotos without calls, whose state stays in the registersfunc skipFastPath( []byte, , int64, uint64, skipResume, int64, *skipPath, int64,) (int64, int64, uint64, skipResume, skipEvent) { := int64(len())// the bytes are read without the checks of the bounds: every loop stops at the nul byte at the end of the // buffer, and a literal is compared byte by byte up to the first byte which differs := (*sliceHeader)(unsafe.Pointer(&)).data// c is the byte at cursor after the white spaces are skipped: a byte above ' ' isn't one, so that the table // of the white spaces is read only for the bytes which may bevarbyteswitch {caseresumeAfterValue:gotocaseresumeAfterKey:goto }:if = char(, ); <= ' ' {forisWhiteSpace[] { ++ = char(, ) } }// a string, which most values are, before the switch of the othersif == '"' {// a string of plain bytes up to its quote, eight bytes at a timefor := + 1; +8 <= ; += 8 {if := keyEndBytes(load64(, )); != 0 {if += int64(bits.TrailingZeros64() / 8); char(, ) == '"' { = + 1goto }break } }return , , , resumeAfterValue, skipAString }switch {case'{', '[':if +1 > {return , , , resumeValue, skipDeep }if == '{' { |= 1 << uint() } else { &^= 1 << uint() }// the mask of the index, below skipLevels, tells the compiler that it is in the bounds .counts[&(skipLevels-1)] = 0 ++ ++ := if = char(, ); <= ' ' {forisWhiteSpace[] { ++ = char(, ) } }if == '{' {if == '}' { ++goto }goto }if == ']' { ++goto }gotocase'-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':// a number, read here by the grammar of the numbers: one which is not valid is left to the caller from its // start, which reports its error, as is one which the end of what has been read cuts after its sign, its // point or its exponent, which the caller reads whole. One which it cuts after a digit ends there: the nul // byte is not a byte after a value, where the caller reads on ( see Stream.skipResume ). := if == '-' { ++ }// a byte minus '0' is 9 at most for a digit only, and minus '1' 8 at most for a digit but 0, as the bytes // below them wrap aroundswitch := char(, ); {case == '0': ++case -'1' <= 8: ++forchar(, )-'0' <= 9 { ++ }default:return , , , resumeAfterValue, skipANumber }ifchar(, ) == '.' {if ++; char(, )-'0' > 9 {return , , , resumeAfterValue, skipANumber }forchar(, )-'0' <= 9 { ++ } }if := char(, ); == 'e' || == 'E' {if ++; char(, ) == '+' || char(, ) == '-' { ++ }ifchar(, )-'0' > 9 {return , , , resumeAfterValue, skipANumber }forchar(, )-'0' <= 9 { ++ } }case't':ifchar(, +1) != 'r' || char(, +2) != 'u' || char(, +3) != 'e' {return , , , resumeValue, skipInvalid } += 4case'f':ifchar(, +1) != 'a' || char(, +2) != 'l' || char(, +3) != 's' || char(, +4) != 'e' {return , , , resumeValue, skipInvalid } += 5case'n':ifchar(, +1) != 'u' || char(, +2) != 'l' || char(, +3) != 'l' {return , , , resumeValue, skipInvalid } += 4default:return , , , resumeValue, skipInvalid }:// a value here is in an object or an array: the value which skipFast starts at is one of them, and the end of // the outermost is returned at closedif = char(, ); <= ' ' {forisWhiteSpace[] { ++ = char(, ) } }if &(1<<uint(-1)) != 0 {switch {case',': ++if = char(, ); <= ' ' {forisWhiteSpace[] { ++ = char(, ) } }gotocase'}': ++goto }return , , , resumeAfterValue, skipInvalid }switch {case',': ++ .counts[(-1)&(skipLevels-1)]++gotocase']': ++goto }return , , , resumeAfterValue, skipInvalid:if != '"' {return , , , resumeKey, skipInvalid }// a key of plain bytes, as the strings of the valuesfor := + 1; +8 <= ; += 8 {if := keyEndBytes(load64(, )); != 0 {if += int64(bits.TrailingZeros64() / 8); char(, ) == '"' { .counts[(-1)&(skipLevels-1)]++ .quotes[(-1)&(skipLevels-1)] = + = + 1goto }break } }return , , , resumeAfterKey, skipAString:if = char(, ); <= ' ' {forisWhiteSpace[] { ++ = char(, ) } }if != ':' {return , , , resumeAfterKey, skipInvalid } ++// the single space after the colon, which most objects have, is skipped before the loop over the othersifchar(, ) == ' ' { ++ }goto:// the end of an object or an array, which cursor is afterif --; == 0 {return , , , resumeAfterValue, skipDone }goto}// skipLongV2 skips the object or the array at the cursor, nested in depth objects and arrays, which the buffer// doesn't hold whole, with V2AllowDuplicateNames and V2AllowInvalidUTF8, as skipNested skips it, its levels followed// for the place of an error ( see skipPathError ): the levels which are open when the stream reads are set in its// state, which a read moves the buffer for ( see Stream.grow ).func ( *Stream) ( int64) error { := &.ctx.v2.skip .active = true := skipMaxLevel() , , , := .cursor, int64(0), uint64(0), resumeValuefor {varskipEvent , , , , = skipFastPath(.buf[:.length+1], , , , , , , .offset) .cursor = switch {caseskipDone: .active = falsereturnnilcaseskipAString, skipANumber:// a string or a number which skipFastPath leaves to its caller, read whole if what has been read cuts it := .offset + .cursor .level, .objects = , := .skipScalar()if != nil {return .skipPathError(, , , , ) }if == skipAString && == resumeAfterKey {// a name, which is counted once it is read .counts[-1]++ .quotes[-1] = }caseskipDeep:if := .skipDeepV2(, , ); != nil {return } = resumeAfterValuedefault: .level, .objects = , , , := .skipResume(, , )if != nil {// the error of the byte where skipResume stopped, or of the integer which it read whole from its // startreturn .skipPathError(, , , , .offset+.cursor) }if == resumeKey && == resumeAfterKey {// a name which skipResume read, which is counted .counts[-1]++ .quotes[-1] = .offset + .quoteBefore(.cursor) }if , = , ; == 0 { .active = falsereturnnil } } = .cursor }}// skipKeepFrom returns the position in the buffer from which a read keeps the bytes while skipLongV2 reads: the last// one before the cursor which is not white space, before a number which ends at the cursor, which skipResume reads// whole from its start, as a read may go on with it. It is the end of the token before the value or the name which// is read, or the delimiter after which it is, which an error is found again from ( see skipPathError ).func ( *Stream) () int64 { := .cursorfor > 0 && isNumberByte(.buf[-1]) { -- }return .tokenBefore()}// tokenBefore returns the position of the last byte before at in the buffer which is not white space, or 0 if there// is none: the end of a token, or a delimiter.func ( *Stream) ( int64) int64 { := - 1for > 0 && isWhiteSpace[.buf[]] { -- }returnmax(, 0)}// quoteBefore returns the position of the quote which opens the valid string of the buffer which ends at end, after// its closing quote: the one before it which no backslash escapes, an even number of backslashes before it.func ( *Stream) ( int64) int64 {// the search starts before the closing quote, which is at end-1 := - 2for {if .buf[] == '"' { := int64(0)for --1 >= 0 && .buf[--1] == '\\' { ++ }if %2 == 0 {return } } -- }}// skipPathError returns err, the error of skipLongV2 at the total offset at, where it stopped with the levels level// open, the kinds of which are the bits of objects, in the state resume, and records its place ( see V2Call.Place ):// the levels are added to the path, and the error is found again from the end of the token before at.func ( *Stream) ( error, int64, uint64, skipResume, int64) error { := .pushSkipLevels(, , , ) .ctx.v2.skip.active = falsereturn .placeDelimiterError(, )}// pushSkipLevels adds the levels of the value which skipLongV2 skips, level of them open at the total offset at, the// kinds of which are the bits of objects, in the state resume, to the path, and returns the total offset of the end of// the token before at, which the innermost level is counted up to: a level around it is in the value after its// last name, or its last comma.func ( *Stream) ( int64, uint64, skipResume, int64) int64 { := &.ctx.v2.skip := .tokenBefore( - .offset) := .offset + + 1for := int64(0); < ; ++ { := &(1<<uint()) != 0 := .level(.pushLevel()) := .counts[]switch := .buf[]; {case < -1 && :// in the value of the last name .Count = countAfterName( - 1)case < -1:// in the element after the last comma .Count = case == '[' || == '{':// the first element or name of the innermost level .Count = 0case == ',' || == ':':// a value or a name after the delimiter, which is read again --switch {case !: .Count = case == ':': .Count = countAfterName( - 1)default: .Count = countAfterValue( - 1) }case && != resumeAfterKey:// after a value .Count = countAfterValue( - 1)case :// after a name, before its colon .Count = countAfterName( - 1)default:// after an element .Count = + 1 }if && > 0 { .Name = append(.Name[:0], .skipName()...)if .Name == nil { .Name = []byte{} // the name "", which is a name } } }return}// skipName returns the last name of the object open at the level i of the value which skipLongV2 skips: the one of// the buffer, or the one which was copied before a read moved the buffer.func ( *Stream) ( int64) []byte { := &.ctx.v2.skipif .quotes[] == copiedName {return .names[] } := .quotes[] - .offset// a name of ASCII without an escape, up to its quote: any other is decoded, with the replacement character for // invalid UTF-8, as encoding/json/v2 compares and gives the namesfor := + 1; < .length; ++ {if := .buf[]; == '"' {return .buf[+1 : ] } elseif == '\\' || >= utf8.RuneSelf {break } } , := skipString(.buf[:.length+1], )if != nil {// a name which was read whole before: it is validreturnnil }returnunquotedName(.buf[:])}// copySkipNames copies the last names of the objects open in the value which skipLongV2 skips, which the buffer has,// before a read moves it ( see Stream.copyNames ).func ( *Stream) () { := &.ctx.v2.skipfor := int64(0); < .level; ++ {if .objects&(1<<uint()) != 0 && .counts[] > 0 && .quotes[] != copiedName { .names[] = append(.names[][:0], .skipName()...) .quotes[] = copiedName } }}// skipDeepV2 skips the object or the array at the cursor, which opens the level after the level levels of the value// which skipLongV2 skips, which follows skipLevels levels at most, nested in depth objects and arrays: its levels are// added to the path, and the value is skipped by skipLongV2, which follows its own levels, after which the levels are// followed again from the path. A value nested too deep is the error of its bracket.func ( *Stream) (, int64, uint64) error { := .offset + .cursorif + >= maxDecodeNestingDepth {return .skipPathError(.grammarError(depthSyntaxError(.buf[:.length+1], .cursor)), , , resumeValue, ) } := .ctx.v2 := &.skip := len(.Path) .pushSkipLevels(, , resumeValue, )if := .skipLongV2( + ); != nil {return }for := int64(0); < ; ++ { := &.Path[+int()]if .counts[] = .Count; .Object { .counts[] = namesCounted(.Count) .names[] = append(.names[][:0], .Name...) } .quotes[] = copiedName } .popLevel() .active = truereturnnil}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.