package decoder

import (
	
	
	
	

	
)

type floatDecoder struct {
	op         func(unsafe.Pointer, float64)
	structName string
	fieldName  string
	// is32 is set for a float32, whose range is checked: a number out of it is a type error.
	is32 bool
	// typ is the type of the value, which the type errors report.
	typ reflect.Type
}

func newFloatDecoder(,  string,  func(unsafe.Pointer, float64)) *floatDecoder {
	return &floatDecoder{op: , structName: , fieldName: , typ: reflect.TypeOf(float64(0))}
}

// overflowsFloat32 reports whether f is out of the range of a float32, as reflect.Value.OverflowFloat does.
func overflowsFloat32( float64) bool {
	if  < 0 {
		 = -
	}
	return math.MaxFloat32 <  &&  <= math.MaxFloat64
}

var (
	floatTable = [256]bool{
		'0': true,
		'1': true,
		'2': true,
		'3': true,
		'4': true,
		'5': true,
		'6': true,
		'7': true,
		'8': true,
		'9': true,
		'.': true,
		'e': true,
		'E': true,
		'+': true,
		'-': true,
	}

	validEndNumberChar = [256]bool{
		nul:  true,
		' ':  true,
		'\t': true,
		'\r': true,
		'\n': true,
		',':  true,
		':':  true,
		'}':  true,
		']':  true,
	}
)

func ( *floatDecoder) ( []byte,  int64) ([]byte, int64, error) {
	for {
		switch [] {
		case ' ', '\n', '\t', '\r':
			++
			continue
		case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
			 := 
			++
			for floatTable[[]] {
				++
			}
			 := [:]
			return , , nil
		case 'n':
			if  := validateNull(, );  != nil {
				return nil, 0, 
			}
			 += 4
			return nil, , nil
		default:
			return nil, 0, errors.ErrUnexpectedEndOfJSON("float", )
		}
	}
}

func ( *floatDecoder) ( *RuntimeContext, ,  int64,  unsafe.Pointer) (int64, error) {
	 := .Buf
	 = skipWhiteSpace(, )
	if , ,  := parseFloatFast(, );  && validEndNumberChar[[]] {
		.op(, )
		return , nil
	}
	return .decodeSlow(, , , )
}

// float32Decoder is the decoder of a float32, whose range is checked: a number out of it is a type error. It is a
// type of its own, so that the decoder of a float64 checks nothing more.
type float32Decoder struct {
	floatDecoder
}

func ( *float32Decoder) ( *RuntimeContext, ,  int64,  unsafe.Pointer) (int64, error) {
	 := .Buf
	 = skipWhiteSpace(, )
	if , ,  := parseFloatFast(, );  && validEndNumberChar[[]] && !overflowsFloat32() {
		.op(, )
		return , nil
	}
	return .decodeSlow(, , , )
}

// decodeSlow decodes the value at cursor which the fast parse doesn't: null, a number which it can't parse, a
// number out of the range of the type, which is a type error, or a value of another kind, which is a type error
// too. It is a function of its own, so that Decode keeps the size it had.
//
//go:noinline
func ( *floatDecoder) ( *RuntimeContext, ,  int64,  unsafe.Pointer) (int64, error) {
	 := .Buf
	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  == '-' || -'0' <= 9:
		,  := numberEnd(, )
		if  != nil {
			return 0, 
		}
		 := [:]
		,  := strconv.ParseFloat(*(*string)(unsafe.Pointer(&)), 64)
		// a number of the grammar which ParseFloat fails is out of the range of float64
		if  :=  == nil && !(.is32 && overflowsFloat32()); ! {
			// the value is left as it is, as encoding/json leaves it from Go 1.27.2
			return .numberError(, , .typ)
		}
		.op(, )
		return , nil
	case isOtherValue(, numberValue):
		return .skipTypeError(, , .typ)
	}
	return 0, errors.ErrUnexpectedEndOfJSON("float", )
}

func ( *floatDecoder) ( *RuntimeContext, ,  int64) ([][]byte, int64, error) {
	 := .Buf
	, ,  := .decodeByte(, )
	if  != nil {
		return nil, 0, 
	}
	if  == nil {
		return [][]byte{nullbytes}, , nil
	}
	return [][]byte{}, , nil
}