package decoder

import (
	
	
	

	
)

type intDecoder struct {
	typ        reflect.Type
	kind       reflect.Kind
	op         func(unsafe.Pointer, int64)
	structName string
	fieldName  string
}

func newIntDecoder( reflect.Type, ,  string,  func(unsafe.Pointer, int64)) *intDecoder {
	return &intDecoder{
		typ:        ,
		kind:       intKindOf(),
		op:         ,
		structName: ,
		fieldName:  ,
	}
}

// intKindOf returns the kind of the integers of the size of typ, whose range the decoder checks: an int is an
// int32 on a 32-bit platform.
func intKindOf( reflect.Type) reflect.Kind {
	switch .Size() {
	case 1:
		return reflect.Int8
	case 2:
		return reflect.Int16
	case 4:
		return reflect.Int32
	}
	return reflect.Int64
}

func ( *intDecoder) ( *RuntimeContext, ,  int64,  unsafe.Pointer) (int64, error) {
	 := .Buf
	 = skipWhiteSpace(, )
	 := 
	switch [] {
	case 'n':
		if  := validateNull(, );  != nil {
			return 0, 
		}
		if .Option.V2&(V2Semantics|V2MergeWithLegacySemantics) == V2Semantics {
			// the semantics of encoding/json/v2 decode null as the zero value, but with MergeWithLegacySemantics
			.op(, 0)
		}
		return  + 4, nil
	case '-':
		++
	}
	if []-'0' > 9 {
		return .decodeSlow(, , , )
	}
	, ,  := parseDigits(, )
	if isFloatContinuation([]) ||  > maxUint64Digits {
		return .decodeSlow(, , , )
	}
	 :=  > 
	if ( &&  > 1<<63) || (! &&  > 1<<63-1) {
		return .decodeSlow(, , , )
	}
	 := int64()
	if  {
		 = -
	}
	switch .kind {
	case reflect.Int8:
		if  < -1*(1<<7) || (1<<7) <=  {
			return .decodeSlow(, , , )
		}
	case reflect.Int16:
		if  < -1*(1<<15) || (1<<15) <=  {
			return .decodeSlow(, , , )
		}
	case reflect.Int32:
		if  < -1*(1<<31) || (1<<31) <=  {
			return .decodeSlow(, , , )
		}
	}
	.op(, )
	return , nil
}

// decodeSlow decodes the value at cursor, which Decode doesn't: a value of another kind, or a number which is not
// an integer of the type, which are type errors, or a syntax error. It is a function of its own, so that Decode
// keeps the size it had.
//
//go:noinline
func ( *intDecoder) ( *RuntimeContext, ,  int64,  unsafe.Pointer) (int64, error) {
	 := .Buf
	 = skipWhiteSpace(, )
	 := 
	switch [] {
	case 'n':
		if  := validateNull(, );  != nil {
			return 0, 
		}
		return  + 4, nil
	case '-':
		++
	}
	if []-'0' > 9 {
		if  >  {
			return 0, errors.ErrSyntax(fmt.Sprintf("invalid character %s in numeric literal", quoteChar([])), +1)
		}
		// a value of another kind
		return .skipTypeError(, , .typ)
	}
	, ,  := parseDigits(, )
	if isFloatContinuation([]) ||  > maxUint64Digits {
		// a number which is not an integer, or too large for any integer
		,  := numberEnd(, )
		if  != nil {
			return 0, 
		}
		return .numberError(, , .typ)
	}
	 :=  > 
	if ( &&  > 1<<63) || (! &&  > 1<<63-1) {
		return .numberError(, , .typ)
	}
	 := int64()
	if  {
		 = -
	}
	switch .kind {
	case reflect.Int8:
		if  < -1*(1<<7) || (1<<7) <=  {
			return .numberError(, , .typ)
		}
	case reflect.Int16:
		if  < -1*(1<<15) || (1<<15) <=  {
			return .numberError(, , .typ)
		}
	case reflect.Int32:
		if  < -1*(1<<31) || (1<<31) <=  {
			return .numberError(, , .typ)
		}
	}
	.op(, )
	return , nil
}

func ( *intDecoder) ( *RuntimeContext, ,  int64) ([][]byte, int64, error) {
	return nil, 0, fmt.Errorf("json: int decoder does not support decode path")
}