package encoder

import (
	
	
	
	

	
)

var (
	isWhiteSpace = [256]bool{
		' ':  true,
		'\n': true,
		'\t': true,
		'\r': true,
	}
	isHTMLEscapeChar = [256]bool{
		'<': true,
		'>': true,
		'&': true,
	}
	nul = byte('\000')
)

func ( *bytes.Buffer,  []byte,  bool) error {
	if len() == 0 {
		return errors.ErrUnexpectedEndOfJSON("", 0)
	}
	.Grow(len())
	 := .Bytes()

	 := TakeRuntimeContext()
	 := .Buf[:0]
	 = append(append(, ...), nul)
	.Buf = 

	if  := compactAndWrite(, , , );  != nil {
		ReleaseRuntimeContext()
		return 
	}
	ReleaseRuntimeContext()
	return nil
}

func compactAndWrite( *bytes.Buffer,  []byte,  []byte,  bool) error {
	,  := compact(, , )
	if  != nil {
		return 
	}
	if ,  := .Write();  != nil {
		return 
	}
	return nil
}

func compact(,  []byte,  bool) ([]byte, error) {
	, ,  := compactValue(, , 0, )
	if  != nil {
		return nil, 
	}
	if  := validateEndBuf(, );  != nil {
		return nil, 
	}
	return , nil
}

func validateEndBuf( []byte,  int64) error {
	for {
		switch [] {
		case ' ', '\t', '\n', '\r':
			++
			continue
		case nul:
			return nil
		}
		return errors.ErrSyntax(
			fmt.Sprintf("invalid character '%c' after top-level value", []),
			+1,
		)
	}
}

func skipWhiteSpace( []byte,  int64) int64 {
:
	if isWhiteSpace[[]] {
		++
		goto 
	}
	return 
}

func compactValue(,  []byte,  int64,  bool) ([]byte, int64, error) {
	for {
		switch [] {
		case ' ', '\t', '\n', '\r':
			++
			continue
		case '{':
			return compactObject(, , , )
		case '}':
			return nil, 0, errors.ErrSyntax("unexpected character '}'", )
		case '[':
			return compactArray(, , , )
		case ']':
			return nil, 0, errors.ErrSyntax("unexpected character ']'", )
		case '"':
			return compactString(, , , )
		case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
			return compactNumber(, , )
		case 't':
			return compactTrue(, , )
		case 'f':
			return compactFalse(, , )
		case 'n':
			return compactNull(, , )
		default:
			return nil, 0, errors.ErrSyntax(fmt.Sprintf("unexpected character '%c'", []), )
		}
	}
}

func compactObject(,  []byte,  int64,  bool) ([]byte, int64, error) {
	if [] == '{' {
		 = append(, '{')
	} else {
		return nil, 0, errors.ErrExpected("expected { character for object value", )
	}
	 = skipWhiteSpace(, +1)
	if [] == '}' {
		 = append(, '}')
		return ,  + 1, nil
	}
	var  error
	for {
		 = skipWhiteSpace(, )
		, ,  = compactString(, , , )
		if  != nil {
			return nil, 0, 
		}
		 = skipWhiteSpace(, )
		if [] != ':' {
			return nil, 0, errors.ErrExpected("colon after object key", )
		}
		 = append(, ':')
		, ,  = compactValue(, , +1, )
		if  != nil {
			return nil, 0, 
		}
		 = skipWhiteSpace(, )
		switch [] {
		case '}':
			 = append(, '}')
			++
			return , , nil
		case ',':
			 = append(, ',')
		default:
			return nil, 0, errors.ErrExpected("comma after object value", )
		}
		++
	}
}

func compactArray(,  []byte,  int64,  bool) ([]byte, int64, error) {
	if [] == '[' {
		 = append(, '[')
	} else {
		return nil, 0, errors.ErrExpected("expected [ character for array value", )
	}
	 = skipWhiteSpace(, +1)
	if [] == ']' {
		 = append(, ']')
		return ,  + 1, nil
	}
	var  error
	for {
		, ,  = compactValue(, , , )
		if  != nil {
			return nil, 0, 
		}
		 = skipWhiteSpace(, )
		switch [] {
		case ']':
			 = append(, ']')
			++
			return , , nil
		case ',':
			 = append(, ',')
		default:
			return nil, 0, errors.ErrExpected("comma after array value", )
		}
		++
	}
}

func compactString(,  []byte,  int64,  bool) ([]byte, int64, error) {
	if [] != '"' {
		return nil, 0, errors.ErrInvalidCharacter([], "string", )
	}
	 := 
	for {
		++
		 := []
		if  {
			if isHTMLEscapeChar[] {
				 = append(, [:]...)
				 = append(, `\u00`...)
				 = append(, hex[>>4], hex[&0xF])
				 =  + 1
			} else if  == 0xE2 && +2 < int64(len()) && [+1] == 0x80 && [+2]&^1 == 0xA8 {
				 = append(, [:]...)
				 = append(, `\u202`...)
				 = append(, hex[[+2]&0xF])
				 =  + 3
				 += 2
			}
		}
		switch  {
		case '\\':
			++
			if [] == nul {
				return nil, 0, errors.ErrUnexpectedEndOfJSON("string", int64(len()))
			}
		case '"':
			++
			return append(, [:]...), , nil
		case nul:
			return nil, 0, errors.ErrUnexpectedEndOfJSON("string", int64(len()))
		}
	}
}

func compactNumber(,  []byte,  int64) ([]byte, int64, error) {
	 := 
	for {
		++
		if floatTable[[]] {
			continue
		}
		break
	}
	 := [:]
	if ,  := strconv.ParseFloat(*(*string)(unsafe.Pointer(&)), 64);  != nil {
		return nil, 0, 
	}
	 = append(, ...)
	return , , nil
}

func compactTrue(,  []byte,  int64) ([]byte, int64, error) {
	if +3 >= int64(len()) {
		return nil, 0, errors.ErrUnexpectedEndOfJSON("true", )
	}
	if !bytes.Equal([:+4], []byte(`true`)) {
		return nil, 0, errors.ErrInvalidCharacter([], "true", )
	}
	 = append(, "true"...)
	 += 4
	return , , nil
}

func compactFalse(,  []byte,  int64) ([]byte, int64, error) {
	if +4 >= int64(len()) {
		return nil, 0, errors.ErrUnexpectedEndOfJSON("false", )
	}
	if !bytes.Equal([:+5], []byte(`false`)) {
		return nil, 0, errors.ErrInvalidCharacter([], "false", )
	}
	 = append(, "false"...)
	 += 5
	return , , nil
}

func compactNull(,  []byte,  int64) ([]byte, int64, error) {
	if +3 >= int64(len()) {
		return nil, 0, errors.ErrUnexpectedEndOfJSON("null", )
	}
	if !bytes.Equal([:+4], []byte(`null`)) {
		return nil, 0, errors.ErrInvalidCharacter([], "null", )
	}
	 = append(, "null"...)
	 += 4
	return , , nil
}