package jx

import (
	

	
)

// Skip skips a json object and positions to relatively the next json object.
func ( *Decoder) () error {
	,  := .next()
	if  != nil {
		return 
	}
	switch  {
	case '"':
		if  := .skipStr();  != nil {
			return errors.Wrap(, "str")
		}
		return nil
	case 'n':
		.unread()
		return .Null()
	case 't', 'f':
		.unread()
		,  := .Bool()
		return 
	case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
		.unread()
		return .skipNumber()
	case '[':
		if  := .skipArr();  != nil {
			return errors.Wrap(, "array")
		}
		return nil
	case '{':
		if  := .skipObj();  != nil {
			return errors.Wrap(, "object")
		}
		return nil
	default:
		return badToken(, .offset()-1)
	}
}

var skipNumberSet = [256]byte{
	'0': 1,
	'1': 1,
	'2': 1,
	'3': 1,
	'4': 1,
	'5': 1,
	'6': 1,
	'7': 1,
	'8': 1,
	'9': 1,

	',':  2,
	']':  2,
	'}':  2,
	' ':  2,
	'\t': 2,
	'\n': 2,
	'\r': 2,
}

// skipNumber reads one JSON number.
//
// Assumes d.buf is not empty.
func ( *Decoder) () error {
	const (
		  byte = 1
		 byte = 2
	)
	 := .buf[.head]
	.head++
	switch  {
	case '-':
		,  := .byte()
		if  != nil {
			return 
		}
		// Character after '-' must be a digit.
		if skipNumberSet[] !=  {
			return badToken(, .offset()-1)
		}
		if  != '0' {
			break
		}
		fallthrough
	case '0':
		// If buffer is empty, try to read more.
		if .head == .tail {
			 := .read()
			if  != nil {
				// There is no data anymore.
				if  == io.EOF {
					return nil
				}
				return 
			}
		}

		 = .buf[.head]
		if skipNumberSet[] ==  {
			return nil
		}
		switch  {
		case '.':
			goto 
		case 'e', 'E':
			goto 
		default:
			return badToken(, .offset())
		}
	}
	for {
		for ,  := range .buf[.head:.tail] {
			switch skipNumberSet[] {
			case :
				.head += 
				return nil
			case :
				continue
			}

			switch  {
			case '.':
				.head += 
				goto 
			case 'e', 'E':
				.head += 
				goto 
			default:
				return badToken(, .offset()+)
			}
		}

		if  := .read();  != nil {
			// There is no data anymore.
			if  == io.EOF {
				.head = .tail
				return nil
			}
			return 
		}
	}

:
	.head++
	{
		var  byte = '.'
		for {
			for ,  := range .buf[.head:.tail] {
				switch skipNumberSet[] {
				case :
					.head += 
					// Check that dot is not last character.
					if  == '.' {
						return io.ErrUnexpectedEOF
					}
					return nil
				case :
					 = 
					continue
				}

				switch  {
				case 'e', 'E':
					if  == '.' {
						return badToken(, .offset()+)
					}
					.head += 
					goto 
				default:
					return badToken(, .offset()+)
				}
			}

			if  := .read();  != nil {
				// There is no data anymore.
				if  == io.EOF {
					.head = .tail
					// Check that dot is not last character.
					if  == '.' {
						return io.ErrUnexpectedEOF
					}
					return nil
				}
				return 
			}
		}
	}
:
	.head++
	// There must be a number or sign after e.
	{
		,  := .byte()
		if  != nil {
			return 
		}
		if skipNumberSet[] !=  { // If next character is not a digit, check for sign.
			if  == '-' ||  == '+' {
				,  := .byte()
				if  != nil {
					return 
				}
				// There must be a number after sign.
				if skipNumberSet[] !=  {
					return badToken(, .offset()-1)
				}
			} else {
				return badToken(, .offset()-1)
			}
		}
	}
	for {
		for ,  := range .buf[.head:.tail] {
			if skipNumberSet[] ==  {
				.head += 
				return nil
			}
			if skipNumberSet[] == 0 {
				return badToken(, .offset()+)
			}
		}

		if  := .read();  != nil {
			// There is no data anymore.
			if  == io.EOF {
				.head = .tail
				return nil
			}
			return 
		}
	}
}

var (
	escapedStrSet = [256]byte{
		'"':  '"',
		'\\': '\\',
		'/':  '/',
		'b':  '\b',
		'f':  '\f',
		'n':  '\n',
		'r':  '\r',
		't':  '\t',
		'u':  'u',
	}
	hexSet = [256]byte{
		'0': 0x0 + 1, '1': 0x1 + 1, '2': 0x2 + 1, '3': 0x3 + 1,
		'4': 0x4 + 1, '5': 0x5 + 1, '6': 0x6 + 1, '7': 0x7 + 1,
		'8': 0x8 + 1, '9': 0x9 + 1,

		'A': 0xA + 1, 'B': 0xB + 1, 'C': 0xC + 1, 'D': 0xD + 1,
		'E': 0xE + 1, 'F': 0xF + 1,

		'a': 0xa + 1, 'b': 0xb + 1, 'c': 0xc + 1, 'd': 0xd + 1,
		'e': 0xe + 1, 'f': 0xf + 1,
	}
)

// skipStr reads one JSON string.
//
// Assumes first quote was consumed.
func ( *Decoder) () error {
	var (
		 byte
		 int
	)
:
	for {
		 = 0
		 := .buf[.head:.tail]
		for len() >= 8 {
			 = [0]
			if safeSet[] != 0 {
				goto 
			}
			++

			 = [1]
			if safeSet[] != 0 {
				goto 
			}
			++

			 = [2]
			if safeSet[] != 0 {
				goto 
			}
			++

			 = [3]
			if safeSet[] != 0 {
				goto 
			}
			++

			 = [4]
			if safeSet[] != 0 {
				goto 
			}
			++

			 = [5]
			if safeSet[] != 0 {
				goto 
			}
			++

			 = [6]
			if safeSet[] != 0 {
				goto 
			}
			++

			 = [7]
			if safeSet[] != 0 {
				goto 
			}
			++

			 = [8:]
		}
		var  int
		for ,  = range  {
			if safeSet[] != 0 {
				 += 
				goto 
			}
		}

		if  := .read();  != nil {
			if  == io.EOF {
				 = io.ErrUnexpectedEOF
			}
			return 
		}
	}

:
	; // Bug in cover tool, see https://github.com/golang/go/issues/28319.
	switch {
	case  == '"':
		.head +=  + 1
		return nil
	case  == '\\':
		.head +=  + 1
		,  := .byte()
		if  != nil {
			return 
		}
		switch escapedStrSet[] {
		case 'u':
			for range [4]struct{}{} {
				,  := .byte()
				if  != nil {
					return 
				}
				if hexSet[] == 0 {
					return badToken(, .offset()-1)
				}
			}
		case 0:
			return badToken(, .offset()-1)
		}
	case  < ' ':
		return badToken(, .offset()+)
	}
	goto 
}

// skipObj reads JSON object.
//
// Assumes first bracket was consumed.
func ( *Decoder) () error {
	if  := .incDepth();  != nil {
		return errors.Wrap(, "inc")
	}

	,  := .more()
	if  != nil {
		return errors.Wrap(, `'"' or "}" expected`)
	}
	switch  {
	case '}':
		return .decDepth()
	case '"':
		.unread()
	default:
		return badToken(, .offset()-1)
	}

	for {
		if  := .consume('"');  != nil {
			return errors.Wrap(, `'"' expected`)
		}
		if  := .skipStr();  != nil {
			return errors.Wrap(, "read field name")
		}
		if  := .consume(':');  != nil {
			return errors.Wrap(, `":" expected`)
		}
		if  := .Skip();  != nil {
			return 
		}
		,  := .more()
		if  != nil {
			return errors.Wrap(, `"," or "}" expected`)
		}
		switch  {
		case ',':
			continue
		case '}':
			return .decDepth()
		default:
			return badToken(, .offset()-1)
		}
	}
}

// skipArr reads JSON array.
//
// Assumes first bracket was consumed.
func ( *Decoder) () error {
	if  := .incDepth();  != nil {
		return errors.Wrap(, "inc")
	}

	,  := .more()
	if  != nil {
		return errors.Wrap(, `value or "]" expected`)
	}
	if  == ']' {
		return .decDepth()
	}
	.unread()

	for {
		if  := .Skip();  != nil {
			return 
		}
		,  := .more()
		if  != nil {
			return errors.Wrap(, `"," or "]" expected`)
		}
		switch  {
		case ',':
			continue
		case ']':
			return .decDepth()
		default:
			return badToken(, .offset()-1)
		}
	}
}

// skipSpace skips space characters.
//
// Returns io.ErrUnexpectedEOF if got io.EOF.
func ( *Decoder) () error {
	// Skip space.
	if ,  := .more();  != nil {
		return 
	}
	.unread()
	return nil
}