package decoderimport ()// The values which v2 skips, as the ones of the names of no field, are skipped by the grammar of JSON as v1 skips// them, and must have the text which v2 allows ( see textcoder.ValidValue ): valid UTF-8 and no escape of a surrogate// which is not a pair, unless V2AllowInvalidUTF8, and no object with a name twice, unless V2AllowDuplicateNames. With// both, a value is skipped as v1 skips it. Otherwise it is read once, by the loop of skipFast with the bytes which are// not ASCII and the names of the objects too ( see skipFastV2 ).// skipValueV2 skips the value at cursor, which v2 reads whole, nested in depth objects and arrays.func ( *RuntimeContext) (, int64) (int64, error) { := .Bufif .Option.V2&asV1Text == asV1Text {returnskipValue(, , ) } := skipWhiteSpace(, )switch [] {case'{', '[':returnskipNestedV2(, , )case'"':if .Option.V2&V2AllowInvalidUTF8 == 0 {returnskipStringV2(, ) } }// a number and a literal, which are of ASCII, and a string with V2AllowInvalidUTF8, which has no namereturnskipValue(, , )}// skipStringV2 returns the position after the string at cursor, which it checks by the grammar of JSON, and for the// text which v2 allows, in one scan of the string: a string of no byte which is not ASCII and no escape has it. The// words of such a string, which most are, are looked at by the loop here up to its quote, as skipString looks at// them, where the buffer has room for them; any other string is scanned as a string is decoded.func skipStringV2( []byte, int64) (int64, error) { := int64(len())ifhasStringSIMD { = min(, +1+64) }for := + 1; +8 <= ; += 8 { := load64(, )if := keyEndBytes() | &msb; != 0 {if += int64(bits.TrailingZeros64() / 8); [] == '"' {return + 1, nil }break } } , , , := skipStringDecoder.scanString(, )if < 0 { _, , , = skipStringDecoder.scanStringRest(, , --1, ) }if != nil {return0, stringError(, , ) }if (.nonASCII || .firstEscape >= 0) && !validV2Text([:]) {return0, errV2Text }return , nil}// skipNestedV2 skips the object or the array at start, nested in depth objects and arrays, by skipFastV2, which// returns for what it doesn't do: a string of an escape or of a byte which is not ASCII, which is done here, a number// which is not an integer, a byte which is not valid, a name twice, or an object or an array deeper than skipLevels// levels, for which the value is skipped again from start, as v1 skips it, and read by jsontext for its text.func skipNestedV2( *RuntimeContext, , int64) (int64, error) { := .Buf := .V2Call()// high is the top bit of the bytes of the strings, which stops the words of a string at a byte which is not ASCII // if the strings must be valid UTF-8, and names whether the names of the objects are keptvaruint64if .Option.V2&V2AllowInvalidUTF8 == 0 { = msb } := .Option.V2&V2AllowDuplicateNames == 0// the names of the call before the value, which the objects of the value which are open where it stops before its // end leave after them , := .names.mark() := skipMaxLevel() , , , , := skipFastV2(, , 0, 0, resumeValue, , , , )for {varerrorswitch {caseskipDone:return , nilcaseskipAString:varint64if , = skipStringOfV2(, , ); == nil {if && == resumeAfterKey && !.skipNames[-1].addName(, , , ) { = errV2Text } = }caseskipANumber: , = skipNumber(, )caseskipNameTwice: = errV2TextcaseskipDeep:dropSkipNames(, , , )returnskipDeepV2(, , )default: = skipFastError(, , , , ) }if != nil {dropSkipNames(, , , )return0, } , , , , = skipFastV2(, , , , , , , , ) }}// dropSkipNames drops the names of the objects which are open at the levels below level, where skipNestedV2 stops// before the end of its value, and the maps of their sets, which the context of the call would keep: the names of the// call are back to the entries and the copies before the value.func dropSkipNames( *V2Call, int64, , int) { .names.truncate(, )for := range .skipNames[:] { .skipNames[].set = nil }}// skipStringOfV2 returns the position after the string at cursor, which it checks by the grammar of JSON, and for the// text which v2 allows if high is set, the top bit of the bytes ( see skipNestedV2 ).func skipStringOfV2( []byte, int64, uint64) (int64, error) {if != 0 {returnskipStringV2(, ) } , := skipString(, )if != nil {return0, stringError(, , ) }return , nil}// addName adds the name of an object which is skipped, the string from start up to end in buf: as the input has it,// or decoded, if it has an escape or invalid UTF-8, which is replaced, as jsontext compares names. It reports whether// the set didn't have it.func ( *nameSet) ( *V2Call, []byte, , int64) bool {if := [:]; bytes.IndexByte(, '\\') >= 0 || !utf8.Valid() {return .addCopy(, , unquotedName()) }return .add(, , +1, -1)}// skipDeepV2 skips the value at start, nested in depth objects and arrays, which is deeper than the levels of// skipFastV2, as v1 skips it, and reads it by jsontext for its text.func skipDeepV2( *RuntimeContext, , int64) (int64, error) { := .Buf , := skipValue(, , )if != nil {return0, }if !textcoder.ValidValue(skipperOf(), [:]) {return0, errV2Text }return , nil}// skipNameTwice is the event of skipFastV2 for a name of an object which the object had before.const skipNameTwice = skipInvalid + 1// skipFastV2 is skipFast for v2: the words of a string stop at a byte which is not ASCII too, if high is the top bit// of the bytes, and the names of the objects are kept in the sets of skipNames of call, by their levels, if names is// set, which a name twice is the event skipNameTwice of. A name of an escape or of a byte which is not ASCII is left// to the caller, as the strings of an escape are, which adds it to the set of the level: invalid UTF-8, which high// may let a string have, is replaced in a name before it is compared.////nolint:maintidx // one loop of gotos without calls but for the names, whose state stays in the registersfunc skipFastV2( []byte, , int64, uint64, skipResume, int64, uint64, bool, *V2Call,) (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// keyHigh is high for the words of the names, which stop at a byte which is not ASCII if they are kept := if { = msb }varbyteswitch {caseresumeAfterValue:gotocaseresumeAfterKey:goto }:if = char(, ); <= ' ' {forisWhiteSpace[] { ++ = char(, ) } }if == '"' {for := + 1; +8 <= ; += 8 { := load64(, )if := keyEndBytes() | &; != 0 {if += int64(bits.TrailingZeros64() / 8); char(, ) == '"' { = + 1goto }break } }return , , , resumeAfterValue, skipAString }switch {case'{', '[':if +1 > {return , , , resumeValue, skipDeep }if == '{' { |= 1 << uint()if { .skipNames[] = nameSet{} } } else { &^= 1 << uint() } ++ ++ := if = char(, ); <= ' ' {forisWhiteSpace[] { ++ = char(, ) } }if == '{' {if == '}' { ++goto }goto }if == ']' { ++goto }gotocase'1', '2', '3', '4', '5', '6', '7', '8', '9': := ++forchar(, )-'0' <= 9 { ++ }if := char(, ); == '.' || == 'e' || == 'E' {return , , , resumeAfterValue, skipANumber }case'0':if := char(, +1); == '.' || == 'e' || == 'E' {return , , , resumeAfterValue, skipANumber } ++case'-':return , , , resumeAfterValue, skipANumbercase'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 }:if = char(, ); <= ' ' {forisWhiteSpace[] { ++ = char(, ) } }if &(1<<uint(-1)) != 0 {switch {case',': ++if = char(, ); <= ' ' {forisWhiteSpace[] { ++ = char(, ) } }gotocase'}': ++goto }return , , , resumeAfterValue, skipInvalid }switch {case',': ++gotocase']': ++goto }return , , , resumeAfterValue, skipInvalid:if != '"' {return , , , resumeKey, skipInvalid }for := + 1; +8 <= ; += 8 { := load64(, )if := keyEndBytes() | &; != 0 {if += int64(bits.TrailingZeros64() / 8); char(, ) == '"' {if {if := &.skipNames[-1]; !.addListed(, , +1, ) && !.add(, , +1, ) {return , , , resumeAfterKey, skipNameTwice } } = + 1goto }break } }return , , , resumeAfterKey, skipAString:if = char(, ); <= ' ' {forisWhiteSpace[] { ++ = char(, ) } }if != ':' {return , , , resumeAfterKey, skipInvalid } ++ifchar(, ) == ' ' { ++ }goto:// the end of an object or an array, which cursor is afterif && &(1<<uint(-1)) != 0 { .skipNames[-1].end() }if --; == 0 {return , , , resumeAfterValue, skipDone }goto}
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.