package decoder

import (
	
	
	
	

	
)

// 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) {
	 := .Buf
	if .Option.V2&asV1Text == asV1Text {
		return skipValue(, , )
	}
	 := skipWhiteSpace(, )
	switch [] {
	case '{', '[':
		return skipNestedV2(, , )
	case '"':
		if .Option.V2&V2AllowInvalidUTF8 == 0 {
			return skipStringV2(, )
		}
	}
	// a number and a literal, which are of ASCII, and a string with V2AllowInvalidUTF8, which has no name
	return skipValue(, , )
}

// 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())
	if hasStringSIMD {
		 = 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 {
		return 0, stringError(, , )
	}
	if (.nonASCII || .firstEscape >= 0) && !validV2Text([:]) {
		return 0, 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 kept
	var  uint64
	if .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 {
		var  error
		switch  {
		case skipDone:
			return , nil
		case skipAString:
			var  int64
			if ,  = skipStringOfV2(, , );  == nil {
				if  &&  == resumeAfterKey && !.skipNames[-1].addName(, , , ) {
					 = errV2Text
				}
				 = 
			}
		case skipANumber:
			,  = skipNumber(, )
		case skipNameTwice:
			 = errV2Text
		case skipDeep:
			dropSkipNames(, , , )
			return skipDeepV2(, , )
		default:
			 = skipFastError(, , , , )
		}
		if  != nil {
			dropSkipNames(, , , )
			return 0, 
		}
		, , , ,  = 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 {
		return skipStringV2(, )
	}
	,  := skipString(, )
	if  != nil {
		return 0, 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 {
		return 0, 
	}
	if !textcoder.ValidValue(skipperOf(), [:]) {
		return 0, 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 registers
func 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
	}
	var  byte
	switch  {
	case resumeAfterValue:
		goto 
	case resumeAfterKey:
		goto 
	}
:
	if  = char(, );  <= ' ' {
		for isWhiteSpace[] {
			++
			 = char(, )
		}
	}
	if  == '"' {
		for  :=  + 1; +8 <= ;  += 8 {
			 := load64(, )
			if  := keyEndBytes() | &;  != 0 {
				if  += int64(bits.TrailingZeros64() / 8); char(, ) == '"' {
					 =  + 1
					goto 
				}
				break
			}
		}
		return , , , resumeAfterValue, skipAString
	}
	switch  {
	case '{', '[':
		if +1 >  {
			return , , , resumeValue, skipDeep
		}
		if  == '{' {
			 |= 1 << uint()
			if  {
				.skipNames[] = nameSet{}
			}
		} else {
			 &^= 1 << uint()
		}
		++
		++
		 := 
		if  = char(, );  <= ' ' {
			for isWhiteSpace[] {
				++
				 = char(, )
			}
		}
		if  == '{' {
			if  == '}' {
				++
				goto 
			}
			goto 
		}
		if  == ']' {
			++
			goto 
		}
		goto 
	case '1', '2', '3', '4', '5', '6', '7', '8', '9':
		 := 
		++
		for char(, )-'0' <= 9 {
			++
		}
		if  := char(, );  == '.' ||  == 'e' ||  == 'E' {
			return , , , resumeAfterValue, skipANumber
		}
	case '0':
		if  := char(, +1);  == '.' ||  == 'e' ||  == 'E' {
			return , , , resumeAfterValue, skipANumber
		}
		++
	case '-':
		return , , , resumeAfterValue, skipANumber
	case 't':
		if char(, +1) != 'r' || char(, +2) != 'u' || char(, +3) != 'e' {
			return , , , resumeValue, skipInvalid
		}
		 += 4
	case 'f':
		if char(, +1) != 'a' || char(, +2) != 'l' || char(, +3) != 's' || char(, +4) != 'e' {
			return , , , resumeValue, skipInvalid
		}
		 += 5
	case 'n':
		if char(, +1) != 'u' || char(, +2) != 'l' || char(, +3) != 'l' {
			return , , , resumeValue, skipInvalid
		}
		 += 4
	default:
		return , , , resumeValue, skipInvalid
	}
:
	if  = char(, );  <= ' ' {
		for isWhiteSpace[] {
			++
			 = char(, )
		}
	}
	if &(1<<uint(-1)) != 0 {
		switch  {
		case ',':
			++
			if  = char(, );  <= ' ' {
				for isWhiteSpace[] {
					++
					 = char(, )
				}
			}
			goto 
		case '}':
			++
			goto 
		}
		return , , , resumeAfterValue, skipInvalid
	}
	switch  {
	case ',':
		++
		goto 
	case ']':
		++
		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
					}
				}
				 =  + 1
				goto 
			}
			break
		}
	}
	return , , , resumeAfterKey, skipAString
:
	if  = char(, );  <= ' ' {
		for isWhiteSpace[] {
			++
			 = char(, )
		}
	}
	if  != ':' {
		return , , , resumeAfterKey, skipInvalid
	}
	++
	if char(, ) == ' ' {
		++
	}
	goto 
:
	// the end of an object or an array, which cursor is after
	if  && &(1<<uint(-1)) != 0 {
		.skipNames[-1].end()
	}
	if --;  == 0 {
		return , , , resumeAfterValue, skipDone
	}
	goto 
}