package decoder

import (
	
	
	
	
	
	
	

	
	
)

type unmarshalTextDecoder struct {
	typ        *runtime.Type
	structName string
	fieldName  string
}

func newUnmarshalTextDecoder( *runtime.Type, ,  string) *unmarshalTextDecoder {
	return &unmarshalTextDecoder{
		typ:        ,
		structName: ,
		fieldName:  ,
	}
}

func ( *unmarshalTextDecoder) ( int64,  error) {
	switch e := .(type) {
	case *errors.UnmarshalTypeError:
		.Struct = .structName
		.Field = .fieldName
	case *errors.SyntaxError:
		.Offset = 
	}
}

var (
	nullbytes = []byte(`null`)
)

func ( *unmarshalTextDecoder) ( *Stream,  int64,  unsafe.Pointer) error {
	.skipWhiteSpace()
	 := .cursor
	if  := .skipValue();  != nil {
		return 
	}
	 := .buf[:.cursor]
	if len() > 0 {
		switch [0] {
		case '[':
			return &errors.UnmarshalTypeError{
				Value:  "array",
				Type:   runtime.RType2Type(.typ),
				Offset: .totalOffset(),
			}
		case '{':
			return &errors.UnmarshalTypeError{
				Value:  "object",
				Type:   runtime.RType2Type(.typ),
				Offset: .totalOffset(),
			}
		case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
			return &errors.UnmarshalTypeError{
				Value:  "number",
				Type:   runtime.RType2Type(.typ),
				Offset: .totalOffset(),
			}
		case 'n':
			if bytes.Equal(, nullbytes) {
				*(*unsafe.Pointer)() = nil
				return nil
			}
		}
	}
	 := make([]byte, len())
	copy(, )

	if ,  := unquoteBytes();  {
		 = 
	}
	 := *(*interface{})(unsafe.Pointer(&emptyInterface{
		typ: .typ,
		ptr: ,
	}))
	if  := .(encoding.TextUnmarshaler).UnmarshalText();  != nil {
		.annotateError(.cursor, )
		return 
	}
	return nil
}

func ( *unmarshalTextDecoder) ( *RuntimeContext, ,  int64,  unsafe.Pointer) (int64, error) {
	 := .Buf
	 = skipWhiteSpace(, )
	 := 
	,  := skipValue(, , )
	if  != nil {
		return 0, 
	}
	 := [:]
	if len() > 0 {
		switch [0] {
		case '[':
			return 0, &errors.UnmarshalTypeError{
				Value:  "array",
				Type:   runtime.RType2Type(.typ),
				Offset: ,
			}
		case '{':
			return 0, &errors.UnmarshalTypeError{
				Value:  "object",
				Type:   runtime.RType2Type(.typ),
				Offset: ,
			}
		case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
			return 0, &errors.UnmarshalTypeError{
				Value:  "number",
				Type:   runtime.RType2Type(.typ),
				Offset: ,
			}
		case 'n':
			if bytes.Equal(, nullbytes) {
				*(*unsafe.Pointer)() = nil
				return , nil
			}
		}
	}

	if ,  := unquoteBytes();  {
		 = 
	}
	 := *(*interface{})(unsafe.Pointer(&emptyInterface{
		typ: .typ,
		ptr: *(*unsafe.Pointer)(unsafe.Pointer(&)),
	}))
	if  := .(encoding.TextUnmarshaler).UnmarshalText();  != nil {
		.annotateError(, )
		return 0, 
	}
	return , nil
}

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

func unquoteBytes( []byte) ( []byte,  bool) { //nolint: nonamedreturns
	 := len()
	if  < 2 || [0] != '"' || [-1] != '"' {
		return
	}
	 = [1 : -1]
	 -= 2

	// Check for unusual characters. If there are none,
	// then no unquoting is needed, so return a slice of the
	// original bytes.
	 := 0
	for  <  {
		 := []
		if  == '\\' ||  == '"' ||  < ' ' {
			break
		}
		if  < utf8.RuneSelf {
			++
			continue
		}
		,  := utf8.DecodeRune([:])
		if  == utf8.RuneError &&  == 1 {
			break
		}
		 += 
	}
	if  ==  {
		return , true
	}

	 := make([]byte, +2*utf8.UTFMax)
	 := copy(, [0:])
	for  <  {
		// Out of room? Can only happen if s is full of
		// malformed UTF-8 and we're replacing each
		// byte with RuneError.
		if  >= len()-2*utf8.UTFMax {
			 := make([]byte, (len()+utf8.UTFMax)*2)
			copy(, [0:])
			 = 
		}
		switch  := []; {
		case  == '\\':
			++
			if  >=  {
				return
			}
			switch [] {
			default:
				return
			case '"', '\\', '/', '\'':
				[] = []
				++
				++
			case 'b':
				[] = '\b'
				++
				++
			case 'f':
				[] = '\f'
				++
				++
			case 'n':
				[] = '\n'
				++
				++
			case 'r':
				[] = '\r'
				++
				++
			case 't':
				[] = '\t'
				++
				++
			case 'u':
				--
				 := getu4([:])
				if  < 0 {
					return
				}
				 += 6
				if utf16.IsSurrogate() {
					 := getu4([:])
					if  := utf16.DecodeRune(, );  != unicode.ReplacementChar {
						// A valid pair; consume.
						 += 6
						 += utf8.EncodeRune([:], )
						break
					}
					// Invalid surrogate; fall back to replacement rune.
					 = unicode.ReplacementChar
				}
				 += utf8.EncodeRune([:], )
			}

		// Quote, control characters are invalid.
		case  == '"',  < ' ':
			return

		// ASCII
		case  < utf8.RuneSelf:
			[] = 
			++
			++

		// Coerce to well-formed UTF-8.
		default:
			,  := utf8.DecodeRune([:])
			 += 
			 += utf8.EncodeRune([:], )
		}
	}
	return [0:], true
}

func getu4( []byte) rune {
	if len() < 6 || [0] != '\\' || [1] != 'u' {
		return -1
	}
	var  rune
	for ,  := range [2:6] {
		switch {
		case '0' <=  &&  <= '9':
			 =  - '0'
		case 'a' <=  &&  <= 'f':
			 =  - 'a' + 10
		case 'A' <=  &&  <= 'F':
			 =  - 'A' + 10
		default:
			return -1
		}
		 = *16 + rune()
	}
	return 
}