package decoder

import (
	
	

	
)

type RuntimeContext struct {
	Buf    []byte
	Option *Option
}

var (
	runtimeContextPool = sync.Pool{
		New: func() interface{} {
			return &RuntimeContext{
				Option: &Option{},
			}
		},
	}
)

func () *RuntimeContext {
	return runtimeContextPool.Get().(*RuntimeContext)
}

func ( *RuntimeContext) {
	runtimeContextPool.Put()
}

var (
	isWhiteSpace = [256]bool{}
)

func init() {
	isWhiteSpace[' '] = true
	isWhiteSpace['\n'] = true
	isWhiteSpace['\t'] = true
	isWhiteSpace['\r'] = true
}

func char( unsafe.Pointer,  int64) byte {
	return *(*byte)(unsafe.Pointer(uintptr() + uintptr()))
}

func skipWhiteSpace( []byte,  int64) int64 {
	for isWhiteSpace[[]] {
		++
	}
	return 
}

func skipObject( []byte, ,  int64) (int64, error) {
	 := 1
	for {
		switch [] {
		case '{':
			++
			++
			if  > maxDecodeNestingDepth {
				return 0, errors.ErrExceededMaxDepth([], )
			}
		case '}':
			--
			--
			if  == 0 {
				return  + 1, nil
			}
		case '[':
			++
			if  > maxDecodeNestingDepth {
				return 0, errors.ErrExceededMaxDepth([], )
			}
		case ']':
			--
		case '"':
			for {
				++
				switch [] {
				case '\\':
					++
					if [] == nul {
						return 0, errors.ErrUnexpectedEndOfJSON("string of object", )
					}
				case '"':
					goto 
				case nul:
					return 0, errors.ErrUnexpectedEndOfJSON("string of object", )
				}
			}
		case nul:
			return 0, errors.ErrUnexpectedEndOfJSON("object of object", )
		}
	:
		++
	}
}

func skipArray( []byte, ,  int64) (int64, error) {
	 := 1
	for {
		switch [] {
		case '[':
			++
			++
			if  > maxDecodeNestingDepth {
				return 0, errors.ErrExceededMaxDepth([], )
			}
		case ']':
			--
			--
			if  == 0 {
				return  + 1, nil
			}
		case '{':
			++
			if  > maxDecodeNestingDepth {
				return 0, errors.ErrExceededMaxDepth([], )
			}
		case '}':
			--
		case '"':
			for {
				++
				switch [] {
				case '\\':
					++
					if [] == nul {
						return 0, errors.ErrUnexpectedEndOfJSON("string of object", )
					}
				case '"':
					goto 
				case nul:
					return 0, errors.ErrUnexpectedEndOfJSON("string of object", )
				}
			}
		case nul:
			return 0, errors.ErrUnexpectedEndOfJSON("array of object", )
		}
	:
		++
	}
}

func skipValue( []byte, ,  int64) (int64, error) {
	for {
		switch [] {
		case ' ', '\t', '\n', '\r':
			++
			continue
		case '{':
			return skipObject(, +1, +1)
		case '[':
			return skipArray(, +1, +1)
		case '"':
			for {
				++
				switch [] {
				case '\\':
					++
					if [] == nul {
						return 0, errors.ErrUnexpectedEndOfJSON("string of object", )
					}
				case '"':
					return  + 1, nil
				case nul:
					return 0, errors.ErrUnexpectedEndOfJSON("string of object", )
				}
			}
		case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
			for {
				++
				if floatTable[[]] {
					continue
				}
				break
			}
			return , nil
		case 't':
			if  := validateTrue(, );  != nil {
				return 0, 
			}
			 += 4
			return , nil
		case 'f':
			if  := validateFalse(, );  != nil {
				return 0, 
			}
			 += 5
			return , nil
		case 'n':
			if  := validateNull(, );  != nil {
				return 0, 
			}
			 += 4
			return , nil
		default:
			return , errors.ErrUnexpectedEndOfJSON("null", )
		}
	}
}

func validateTrue( []byte,  int64) error {
	if +3 >= int64(len()) {
		return errors.ErrUnexpectedEndOfJSON("true", )
	}
	if [+1] != 'r' {
		return errors.ErrInvalidCharacter([+1], "true", )
	}
	if [+2] != 'u' {
		return errors.ErrInvalidCharacter([+2], "true", )
	}
	if [+3] != 'e' {
		return errors.ErrInvalidCharacter([+3], "true", )
	}
	return nil
}

func validateFalse( []byte,  int64) error {
	if +4 >= int64(len()) {
		return errors.ErrUnexpectedEndOfJSON("false", )
	}
	if [+1] != 'a' {
		return errors.ErrInvalidCharacter([+1], "false", )
	}
	if [+2] != 'l' {
		return errors.ErrInvalidCharacter([+2], "false", )
	}
	if [+3] != 's' {
		return errors.ErrInvalidCharacter([+3], "false", )
	}
	if [+4] != 'e' {
		return errors.ErrInvalidCharacter([+4], "false", )
	}
	return nil
}

func validateNull( []byte,  int64) error {
	if +3 >= int64(len()) {
		return errors.ErrUnexpectedEndOfJSON("null", )
	}
	if [+1] != 'u' {
		return errors.ErrInvalidCharacter([+1], "null", )
	}
	if [+2] != 'l' {
		return errors.ErrInvalidCharacter([+2], "null", )
	}
	if [+3] != 'l' {
		return errors.ErrInvalidCharacter([+3], "null", )
	}
	return nil
}