package decoder

import (
	
	
	
	

	
	
)

// decodeStringRest is decodeStringValue for a long string, which scanString stopped in: it scans the rest of the
// string as scanStringRest does, and, at an escape, leaves the rest to unescapeRest, which decodes the string in
// the same pass. The loop of a string without an escape keeps nothing more than the one of scanStringRest.
//
//go:noinline
func ( *stringDecoder) ( *RuntimeContext,  []byte,  int64,  stringInfo) (string, int64, bool, error) {
	if .firstEscape >= 0 {
		return .unescapeRest(, , , )
	}
	 := .Buf
	 :=  - int64(len())
	var  uint64
	if .nonASCII {
		 = msb
	}
	 := (*sliceHeader)(unsafe.Pointer(&)).data
	 := int64(len())
	for {
		if , ,  := indexStringSpecial(unsafe.Add(, ), int(-));  {
			 |= 
			 += int64()
		} else {
			for +8 <=  {
				 := load64(, )
				if  := keyEndBytes();  != 0 {
					 := int64(bits.TrailingZeros64() / 8)
					 |=  & msb & (1<<(uint()*8&63) - 1)
					 += 
					break
				}
				 |=  & msb
				 += 8
			}
		}
		 := char(, )
		switch  {
		case '\\':
			return .unescapeRest(, [:], , stringInfo{firstEscape: -1, nonASCII: &msb != 0})
		case '"':
			 := [:]
			if &msb != 0 {
				 = validLiteral()
			}
			return .makeString(),  + 1, true, nil
		case nul:
			return .skipOtherValue(, errors.ErrUnexpectedEndOfJSON("string", ))
		default:
			if  < 0x20 {
				return .skipOtherValue(, errors.ErrSyntax(fmt.Sprintf("invalid character %s in string literal", quoteChar()), +1))
			}
			 |= uint64()
			++
		}
	}
}

// unescapeRest continues decodeStringRest from the bytes of the string so far, literal, and the position after
// them, cursor, which is at an escape or after the first 64 bytes of a string with an escape: it scans the rest
// of the string and decodes it in the same pass, into the scratch bytes of the context. A run of plain bytes is
// scanned by SIMD, and moved at the byte which ends it.
//
//go:noinline
func ( *stringDecoder) ( *RuntimeContext,  []byte,  int64,  stringInfo) (string, int64, bool, error) {
	 := .Buf
	 :=  - int64(len())
	var  uint64
	if .nonASCII {
		 = msb
	}
	 := .firstEscape >= 0
	 := .unescaped[:0]
	if  {
		 = growBytes(, len()+64)
		 = [:jsonstring.UnescapeTo(unsafe.Pointer(unsafe.SliceData()), , .firstEscape)]
	}
	// run is the position of the plain bytes which are not in out yet, once the string has an escape.
	 := 
	 := (*sliceHeader)(unsafe.Pointer(&)).data
	 := int64(len())
	for {
		if , ,  := indexStringSpecial(unsafe.Add(, ), int(-));  {
			 |= 
			 += int64()
		} else {
			for +8 <=  {
				 := load64(, )
				if  := keyEndBytes();  != 0 {
					 := int64(bits.TrailingZeros64() / 8)
					 |=  & msb & (1<<(uint()*8&63) - 1)
					 += 
					break
				}
				 |=  & msb
				 += 8
			}
		}
		 := char(, )
		switch  {
		case '\\':
			if  {
				 = append(, [:]...)
			} else {
				 = true
				 = append(growBytes(, int(-)+64), [:]...)
			}
			++
			switch  := char(, );  {
			case '"', '\\', '/', 'b', 'f', 'n', 'r', 't':
				 = append(, jsonstring.UnescapeMap[])
				++
			case 'u':
				,  := hex4(, +1, )
				if ! {
					return .skipOtherValue(, .hexError(, ))
				}
				 += 5
				if  >= 0xd800 &&  < 0xdc00 && char(, ) == '\\' && +1 <  && char(, +1) == 'u' {
					if ,  := hex4(, +2, );  &&  >= 0xdc00 &&  < 0xe000 {
						 = (-0xd800)<<10 | ( - 0xdc00) + 0x10000
						 += 6
					}
				}
				 = utf8.AppendRune(, )
			default:
				return .skipOtherValue(, errors.ErrUnexpectedEndOfJSON("escaped string", ))
			}
			 = 
		case '"':
			 :=  + 1
			if ! {
				 := [:]
				if &msb != 0 {
					 = validLiteral()
				}
				return .makeString(), , true, nil
			}
			 = append(, [:]...)
			if cap() <= maxUnescapeScratchSize {
				.unescaped = [:0]
			}
			if &msb != 0 && !utf8.Valid() {
				return string(coerceUTF8()), , true, nil
			}
			return string(), , true, nil
		case nul:
			return .skipOtherValue(, errors.ErrUnexpectedEndOfJSON("string", ))
		default:
			if  < 0x20 {
				return .skipOtherValue(, errors.ErrSyntax(fmt.Sprintf("invalid character %s in string literal", quoteChar()), +1))
			}
			 |= uint64()
			++
		}
	}
}

// growBytes returns b with room for n bytes more.
func growBytes( []byte,  int) []byte {
	if cap()-len() <  {
		 = append([:cap()], make([]byte, )...)[:len()]
	}
	return 
}

// hex4 returns the code of the four hexadecimal digits at pos, and false if they are not four of them.
func hex4( unsafe.Pointer, ,  int64) (rune, bool) {
	if +4 >=  {
		return 0, false
	}
	var  rune
	for  := int64(0);  < 4; ++ {
		 := char(, +)
		if !(('0' <=  &&  <= '9') || ('a' <=  &&  <= 'f') || ('A' <=  &&  <= 'F')) {
			return 0, false
		}
		 = <<4 | rune(jsonstring.HexToInt[])
	}
	return , true
}

// hexError returns the error of the \u escape at pos, whose digits are not four hexadecimal ones, as scanString does.
func ( *stringDecoder) ( []byte,  int64) error {
	if +5 >= int64(len()) {
		return errors.ErrUnexpectedEndOfJSON("escaped string", )
	}
	for  := int64(1);  <= 4; ++ {
		 := [+]
		if !(('0' <=  &&  <= '9') || ('a' <=  &&  <= 'f') || ('A' <=  &&  <= 'F')) {
			return errors.ErrSyntax(fmt.Sprintf("json: invalid character %c in \\u hexadecimal character escape", ), +)
		}
	}
	return errors.ErrUnexpectedEndOfJSON("escaped string", )
}