package decoder

import (
	
	
	
	
	
	

	
)

var (
	// errStringOptionRest tells that the value of a string of the string option is followed by more bytes.
	errStringOptionRest = errors.New("json: value followed by more bytes in a string of the string option")
	// errMapKeyType is returned by the decoder of a key of a map which is not of the type of the keys, or which the
	// UnmarshalText of the keys fails for, whose error is recorded: the map skips the value of the entry, and drops
	// the entry, as encoding/json does.
	errMapKeyType = errors.New("json: map key of another type")
)

type wrappedStringDecoder struct {
	typ           reflect.Type
	dec           Decoder
	stringDecoder *stringDecoder
	structName    string
	fieldName     string
	isPtrType     bool
	// isMapKey is set for the decoder of the keys of a map, whose errors are the ones of the keys.
	isMapKey bool
	// numberKind is the kind of a number which the string has, read by strconv as encoding/json reads it ( see
	// decodeNumber ), or reflect.Invalid for a value of any other type.
	numberKind reflect.Kind
	// numberType is the type of the number, which typ is or points to.
	numberType reflect.Type
	// isJSONNumber is set for a json.Number, or a pointer to one, whose bytes must be a number by the grammar of the
	// numbers.
	isJSONNumber bool
}

func newWrappedStringDecoder( reflect.Type,  Decoder, ,  string) *wrappedStringDecoder {
	 := &wrappedStringDecoder{
		typ:           ,
		dec:           ,
		stringDecoder: newStringDecoder(, ),
		structName:    ,
		fieldName:     ,
		isPtrType:     .Kind() == reflect.Ptr,
	}
	 := 
	if .Kind() == reflect.Pointer {
		 = .Elem()
	}
	.isJSONNumber =  == jsonNumberType
	switch .Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
		reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
		reflect.Float32, reflect.Float64:
		.numberKind, .numberType = .Kind(), 
	}
	return 
}

func ( *wrappedStringDecoder) ( *RuntimeContext, ,  int64,  unsafe.Pointer) (int64, error) {
	 := .Buf
	 = skipWhiteSpace(, )
	 := 
	if  := [];  != 'n' && isOtherValue(, stringValue) {
		// a value which is not in a string
		if .isPtrType {
			.allocate()
		}
		return .stringOptionUnquoted(, , .typ)
	}
	, ,  := .stringDecoder.decodeByte(, )
	if  != nil {
		return 0, 
	}
	if  == nil {
		if .isPtrType {
			*(*unsafe.Pointer)() = nil
		}
		return , nil
	}
	if .isJSONNumber && !.isMapKey {
		if jsonnum.IsValid() {
			*(*json.Number)(.target()) = json.Number(.makeString())
			return , nil
		}
		return .stringError(, , , , )
	}
	if .numberKind != reflect.Invalid && len() > 0 {
		if .decodeNumber(, ) {
			return , nil
		}
		if !.isMapKey && string() == "null" {
			// encoding/json decodes the string "null" as null: it leaves the number, and sets a pointer to nil
			if .isPtrType {
				*(*unsafe.Pointer)() = nil
			}
			return , nil
		}
		return .stringError(, , , , )
	}
	if len() != 0 && (isWhiteSpace[[0]] || isWhiteSpace[[len()-1]]) {
		// encoding/json reads the bytes of the string as a value with nothing around it
		return .stringError(, , , , )
	}
	// The value is decoded from a copy of its bytes followed by a nul byte, which its strings refer to under
	// NoCopyStringOption. A type error of it is the one of the string, which is made after it ( see
	// stringOptionError ).
	 := .typeError
	.typeError = nil
	,  := .Buf, .origin
	.Buf = NewInput()
	.origin = .Buf
	,  := .dec.Decode(, 0, , )
	if  == nil && skipWhiteSpace(.Buf, ) != int64(len()) {
		// the value is followed by more bytes in the string
		 = errStringOptionRest
	}
	.Buf, .origin = , 
	 :=  != nil || .typeError != nil
	.typeError = 
	if  || len() == 0 {
		return .stringError(, , , , )
	}
	return , nil
}

// stringError records or returns the error of the string between start and end, whose bytes are not a value of
// the type.
func ( *wrappedStringDecoder) ( *RuntimeContext, ,  int64,  []byte,  unsafe.Pointer) (int64, error) {
	if .isMapKey {
		.keyTypeError(, , , .elemType())
		return , errMapKeyType
	}
	if .isPtrType {
		.allocate()
	}
	return .stringOptionError(, , , .typ)
}

// decodeNumber reads the number which the bytes of the string are, as encoding/json reads the number of a string of
// the string option or of a key of a map, by strconv: a number which it fails, or which is out of the range of the
// type, is not stored. It reports whether the number is stored.
func ( *wrappedStringDecoder) ( []byte,  unsafe.Pointer) bool {
	 := *(*string)(unsafe.Pointer(&))
	 := int(.numberType.Size() * 8)
	switch .numberKind {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		,  := strconv.ParseInt(, 10, )
		if  != nil {
			return false
		}
		storeInt(.target(), , uint64())
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		,  := strconv.ParseUint(, 10, )
		if  != nil {
			return false
		}
		storeInt(.target(), , )
	default:
		,  := strconv.ParseFloat(, )
		if  != nil {
			return false
		}
		if  == 32 {
			*(*float32)(.target()) = float32()
		} else {
			*(*float64)(.target()) = 
		}
	}
	return true
}

// storeInt stores the lowest bits of n, an integer of the size of bits, at p.
func storeInt( unsafe.Pointer,  int,  uint64) {
	switch  {
	case 8:
		*(*uint8)() = uint8()
	case 16:
		*(*uint16)() = uint16()
	case 32:
		*(*uint32)() = uint32()
	default:
		*(*uint64)() = 
	}
}

// target returns where the number is stored: at p, or where the pointer at p points to, which is allocated if it
// is nil.
func ( *wrappedStringDecoder) ( unsafe.Pointer) unsafe.Pointer {
	if !.isPtrType {
		return 
	}
	.allocate()
	return *(*unsafe.Pointer)()
}

// elemType returns the type of the value, which the pointers of the type point to.
func ( *wrappedStringDecoder) () reflect.Type {
	 := .typ
	for .Kind() == reflect.Pointer {
		 = .Elem()
	}
	return 
}

// allocate sets the pointer at p, if it is nil, to a zero value, as encoding/json does for a value it fails to
// decode.
func ( *wrappedStringDecoder) ( unsafe.Pointer) {
	if *(*unsafe.Pointer)() == nil {
		*(*unsafe.Pointer)() = newValue(.typ.Elem())
	}
}

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