package decoderimport ()// The values which are not decoded, as the ones of the keys which match no field, are skipped by the grammar of// JSON, as encoding/json checks the whole input: a value which is not valid is a syntax error wherever it is.// skipState is where a skip of an object or an array is: what it looks for next.type skipState uint8const (// skipValueStart looks for a value. skipValueStart skipState = iota// skipAfterValue looks for what follows a value: a comma or the end of the object or the array it is in. skipAfterValue)// skipRestOfObject returns the position after the end of the object whose value ends at cursor, which it checks by// the grammar of JSON: nesting is how deep the object is nested in the input.func skipRestOfObject( []byte, , int64) (int64, error) {returnskipGrammarFast(, , -1, 1, 1, resumeAfterValue)}// skipResume is where skipFast resumes: at a value, after a value, or after the key of an object.type skipResume uint8const ( resumeValue skipResume = iota resumeAfterValue resumeAfterKey// resumeKey is where a key is waited for, which skipFast doesn't resume at: it tells the error of the byte. resumeKey)// skipEvent is why skipFast returns: the end of the value, or what it doesn't do itself, which its caller does// before it resumes it.type skipEvent uint8const ( skipDone skipEvent = iota// skipAString is a string which has an escape or a control character, or is at the end of the buffer. skipAString// skipANumber is a number which starts with a minus sign or is not an integer. skipANumber// skipDeep is an object or an array deeper than the levels which the bits of a word hold. skipDeep// skipInvalid is a byte which the grammar doesn't have where it is, for which the caller makes the error. skipInvalid)// skipGrammarFast skips the objects and the arrays which are open, level of them whose kinds are the bits of// objects, from cursor, where it resumes, by skipFast, which returns for what it doesn't do: a string with an// escape, a number which is not an integer, a byte which is not valid, which is done here, or an object or an array// deeper than skipLevels levels, which the rest of the value is skipped by skipGrammar for.func skipGrammarFast( []byte, , , int64, uint64, skipResume) (int64, error) {varskipEvent , , , , = skipFast(, , , , , skipMaxLevel())if == skipDone {return , nil }returnskipGrammarEvent(, , , , , , )}// skipLevels is the number of the levels of objects and arrays which skipFast follows at most, by the bits of a// uint64 ( see skipFast ).const skipLevels = 64// skipMaxLevel returns the number of the levels of objects and arrays which skipFast opens at most, for a value// nested nesting levels deep.func skipMaxLevel( int64) int64 {returnmin(maxDecodeNestingDepth-, skipLevels)}// skipGrammarEvent does what skipFast stopped for at cursor, ev, and resumes it, up to the end of the value.func skipGrammarEvent( []byte, , , int64, uint64, skipResume, skipEvent) (int64, error) { := skipMaxLevel()for {switch {caseskipDone:return , nilcaseskipAString: , := skipString(, )if != nil {return0, stringError(, , ) } = caseskipANumber: , := skipNumber(, )if != nil {return0, } = caseskipDeep:// the value at cursor opens the level after skipLevels, or the limit of the nestingreturnskipGrammar(, , , , , skipValueStart)default:return0, skipFastError(, , , , ) } , , , , = skipFast(, , , , , ) }}// skipFastError returns the syntax error of the byte at cursor, which skipFast stopped at, where it resumes.func skipFastError( []byte, , int64, uint64, skipResume) error {switch {caseresumeKey:returnsyntaxErrorAt(, , whereKey)caseresumeAfterKey:returnsyntaxErrorAt(, , whereAfterKey)caseresumeAfterValue:if &(1<<uint(-1)) != 0 {returnsyntaxErrorAt(, , whereAfterPair) }returnsyntaxErrorAt(, , whereAfterElement) }switch [] {case't':returnliteralSyntaxError(, , "true")case'f':returnliteralSyntaxError(, , "false")case'n':returnliteralSyntaxError(, , "null") }returnsyntaxErrorAt(, , whereValue)}// skipFast skips from cursor, where it resumes, the objects and the arrays which are open, level of them whose// kinds are the bits of objects, set for an object, up to the end of the outermost of them, and returns where it// is and why it stops: at the end, or at what it doesn't do itself ( see skipEvent ), after which its caller// resumes it. It calls nothing, so that its loop keeps its state in the registers.//// For skipInvalid, resume is the state at the byte: resumeValue for a value, resumeAfterValue after a value,// resumeKey where a key is waited for and resumeAfterKey where its colon is.////nolint:maintidx // one loop of gotos without calls, whose state stays in the registersfunc skipFast( []byte, , int64, uint64, skipResume, 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() } ++ ++ := 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' {// a number which is not an integer, which skipNumber checks from its startreturn , , , 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 }:// 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',': ++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(, ) == '"' { = + 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}// skipGrammar skips from cursor, in state, the values of the objects and the arrays which are open, level of them// the innermost of which is an object, up to the end of the outermost of them, or the value at cursor if none is// open, and returns the position after it.//// The kinds of the open values are the bits of a word, set for an object, for the first 64 levels, and of the// words of deeper, which are allocated as the nesting gets deeper: the loop keeps them in its registers.////nolint:maintidx // one loop of gotos without calls, whose state stays in the registersfunc skipGrammar( []byte, , , int64, uint64, skipState) (int64, error) { := maxDecodeNestingDepth - := 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(&)).datavar []uint64if == skipAfterValue {goto }:forisWhiteSpace[char(, )] { ++ }switch := char(, ); {case'{', '[':if +1 > {return0, depthSyntaxError(, ) }if < 64 {if == '{' { |= 1 << uint() } else { &^= 1 << uint() } } else { = pushDeeper(, , == '{') } ++ ++forisWhiteSpace[char(, )] { ++ }if == '{' {ifchar(, ) == '}' { ++ --goto }goto }ifchar(, ) == ']' { ++ --goto }gotocase'"':// a string of plain bytes up to its quote without a call, eight bytes at a time, and any other by // skipString, which checks its escapesfor := + 1; +8 <= ; += 8 {if := keyEndBytes(load64(, )); != 0 {if += int64(bits.TrailingZeros64() / 8); char(, ) == '"' { = + 1goto }break } } , := skipString(, )if != nil {return0, stringError(, , ) } = case'1', '2', '3', '4', '5', '6', '7', '8', '9':// an integer, which most numbers are, without a call := ++forchar(, )-'0' <= 9 { ++ }if := char(, ); == '.' || == 'e' || == 'E' { , := skipNumberRest(, , )if != nil {return0, } = }case'0': := ++if := char(, ); == '.' || == 'e' || == 'E' { , := skipNumberRest(, , )if != nil {return0, } = }case'-': , := skipNumber(, )if != nil {return0, } = case't':ifchar(, +1) != 'r' || char(, +2) != 'u' || char(, +3) != 'e' {return0, literalSyntaxError(, , "true") } += 4case'f':ifchar(, +1) != 'a' || char(, +2) != 'l' || char(, +3) != 's' || char(, +4) != 'e' {return0, literalSyntaxError(, , "false") } += 5case'n':ifchar(, +1) != 'u' || char(, +2) != 'l' || char(, +3) != 'l' {return0, literalSyntaxError(, , "null") } += 4default:return0, syntaxErrorAt(, , whereValue) }:if == 0 {return , nil }forisWhiteSpace[char(, )] { ++ }ifinObject(, , -1) {switchchar(, ) {case',': ++forisWhiteSpace[char(, )] { ++ }gotocase'}': ++ --goto }return0, syntaxErrorAt(, , whereAfterPair) }switchchar(, ) {case',': ++gotocase']': ++ --goto }return0, syntaxErrorAt(, , whereAfterElement):ifchar(, ) != '"' {return0, syntaxErrorAt(, , whereKey) }// a key of plain bytes without a call, as the strings of the valuesfor := + 1; +8 <= ; += 8 {if := keyEndBytes(load64(, )); != 0 {if += int64(bits.TrailingZeros64() / 8); char(, ) == '"' { = + 1goto }break } } { , := skipString(, )if != nil {return0, stringError(, , ) } = }:forisWhiteSpace[char(, )] { ++ }ifchar(, ) != ':' {return0, syntaxErrorAt(, , whereAfterKey) } ++goto}// inObject reports whether the value open at level is an object.func inObject( uint64, []uint64, int64) bool {if < 64 {return &(1<<uint()) != 0 }return [(-64)/64]&(1<<uint((-64)%64)) != 0}// pushDeeper sets the kind of the value open at level, which is 64 or deeper, in deeper, which it returns.func pushDeeper( []uint64, int64, bool) []uint64 { , := (-64)/64, uint((-64)%64)forint64(len()) <= { = append(, 0) }if { [] |= 1 << } else { [] &^= 1 << }return}// skipNumber returns the position after the number at cursor, which it checks by the grammar of JSON.func skipNumber( []byte, int64) (int64, error) { := if [] == '-' { ++ }switch {case [] == '0': ++case []-'1' <= 8: = skipDigits(, +1)default:return0, numberSyntaxError(, , , whereNumber) }returnskipNumberRest(, , )}// skipNumberRest returns the position after the fraction and the exponent of the number which starts at start and// whose integer ends at c, which it checks by the grammar of JSON.func skipNumberRest( []byte, , int64) (int64, error) {if [] == '.' { ++if []-'0' > 9 {return0, numberSyntaxError(, , , whereFraction) } = skipDigits(, ) }if [] == 'e' || [] == 'E' { ++if [] == '+' || [] == '-' { ++ }if []-'0' > 9 {return0, numberSyntaxError(, , , whereExponent) } = skipDigits(, ) }return , nil}// stringError returns the syntax error of the string at cursor, which skipString failed with err, as encoding/json// has it.func stringError( []byte, int64, error) error {if := stringSyntaxError(, ); != nil {return }return}// InputSyntaxError returns the first syntax error of the input of the decoding, as encoding/json reports it, or// nil if the input is valid: the decoders report a syntax error as they meet it, which this reports again as// encoding/json does, which checks the whole input first. The input is copied again, since the decoding writes// its buffer, as the escapes of the keys are decoded in it.func ( *RuntimeContext) () error {returninputSyntaxError(NewInput(.origin))}// inputSyntaxError returns the first syntax error of buf, the input followed by the nul byte, or nil.func inputSyntaxError( []byte) error { , := skipValue(, skipWhiteSpace(, 0), 0)if != nil {return }returnValidateEnd(, )}// Valid reports whether buf, the input followed by the nul byte, is a JSON value, by the grammar which the values// which are not decoded are skipped by.func ( []byte) bool { , := skipValue(, skipWhiteSpace(, 0), 0)return == nil && skipWhiteSpace(, ) == int64(len())-1}// ValidateEnd returns the syntax error of what follows the value which ends at cursor in buf, the input followed by// the nul byte: white spaces only may follow it, up to the end of the input. A nul byte in the input is a byte which// the grammar doesn't have there, as encoding/json has it.func ( []byte, int64) error { = skipWhiteSpace(, )if == int64(len())-1 {returnnil }returnsyntaxErrorAt(, , whereAfterTop)}
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.