package terminfo

import (
	
	
	
	
	
	
)

// parametizer represents the a scan state for a parameterized string.
type parametizer struct {
	// z is the string to parameterize
	z []byte
	// pos is the current position in s.
	pos int
	// nest is the current nest level.
	nest int
	// s is the variable stack.
	s stack
	// skipElse keeps the state of skipping else.
	skipElse bool
	// buf is the result buffer.
	buf *bytes.Buffer
	// params are the parameters to interpolate.
	params [9]interface{}
	// vars are dynamic variables.
	vars [26]interface{}
}

// staticVars are the static, global variables.
var staticVars = struct {
	vars [26]interface{}
	sync.Mutex
}{}

var parametizerPool = sync.Pool{
	New: func() interface{} {
		 := new(parametizer)
		.buf = bytes.NewBuffer(make([]byte, 0, 45))
		return 
	},
}

// newParametizer returns a new initialized parametizer from the pool.
func ( []byte) *parametizer {
	 := parametizerPool.Get().(*parametizer)
	.z = 
	return 
}

// reset resets the parametizer.
func ( *parametizer) () {
	.pos, .nest = 0, 0
	.s.reset()
	.buf.Reset()
	.params, .vars = [9]interface{}{}, [26]interface{}{}
	parametizerPool.Put()
}

// stateFn represents the state of the scanner as a function that returns the
// next state.
type stateFn func() stateFn

// exec executes the parameterizer, interpolating the supplied parameters.
func ( *parametizer) () string {
	for  := .scanTextFn;  != nil; {
		 = ()
	}
	return .buf.String()
}

// peek returns the next byte.
func ( *parametizer) () (byte, error) {
	if .pos >= len(.z) {
		return 0, io.EOF
	}
	return .z[.pos], nil
}

// writeFrom writes the characters from ppos to pos to the buffer.
func ( *parametizer) ( int) {
	if .pos >  {
		// append remaining characters.
		.buf.Write(.z[:.pos])
	}
}

func ( *parametizer) () stateFn {
	 := .pos
	for {
		,  := .peek()
		if  != nil {
			.writeFrom()
			return nil
		}
		if  == '%' {
			.writeFrom()
			.pos++
			return .scanCodeFn
		}
		.pos++
	}
}

func ( *parametizer) () stateFn {
	,  := .peek()
	if  != nil {
		return nil
	}
	switch  {
	case '%':
		.buf.WriteByte('%')
	case ':':
		// this character is used to avoid interpreting "%-" and "%+" as operators.
		// the next character is where the format really begins.
		.pos++
		_,  = .peek()
		if  != nil {
			return nil
		}
		return .scanFormatFn
	case '#', ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.':
		return .scanFormatFn
	case 'o':
		.buf.WriteString(strconv.FormatInt(int64(.s.popInt()), 8))
	case 'd':
		.buf.WriteString(strconv.Itoa(.s.popInt()))
	case 'x':
		.buf.WriteString(strconv.FormatInt(int64(.s.popInt()), 16))
	case 'X':
		.buf.WriteString(strings.ToUpper(strconv.FormatInt(int64(.s.popInt()), 16)))
	case 's':
		.buf.WriteString(.s.popString())
	case 'c':
		.buf.WriteByte(.s.popByte())
	case 'p':
		.pos++
		return .pushParamFn
	case 'P':
		.pos++
		return .setDsVarFn
	case 'g':
		.pos++
		return .getDsVarFn
	case '\'':
		.pos++
		,  = .peek()
		if  != nil {
			return nil
		}
		.s.push()
		// skip the '\''
		.pos++
	case '{':
		.pos++
		return .pushIntfn
	case 'l':
		.s.push(len(.s.popString()))
	case '+':
		,  := .s.popInt(), .s.popInt()
		.s.push( + )
	case '-':
		,  := .s.popInt(), .s.popInt()
		.s.push( - )
	case '*':
		,  := .s.popInt(), .s.popInt()
		.s.push( * )
	case '/':
		,  := .s.popInt(), .s.popInt()
		if  != 0 {
			.s.push( / )
		} else {
			.s.push(0)
		}
	case 'm':
		,  := .s.popInt(), .s.popInt()
		if  != 0 {
			.s.push( % )
		} else {
			.s.push(0)
		}
	case '&':
		,  := .s.popInt(), .s.popInt()
		.s.push( & )
	case '|':
		,  := .s.popInt(), .s.popInt()
		.s.push( | )
	case '^':
		,  := .s.popInt(), .s.popInt()
		.s.push( ^ )
	case '=':
		,  := .s.popInt(), .s.popInt()
		.s.push( == )
	case '>':
		,  := .s.popInt(), .s.popInt()
		.s.push( > )
	case '<':
		,  := .s.popInt(), .s.popInt()
		.s.push( < )
	case 'A':
		,  := .s.popBool(), .s.popBool()
		.s.push( && )
	case 'O':
		,  := .s.popBool(), .s.popBool()
		.s.push( || )
	case '!':
		.s.push(!.s.popBool())
	case '~':
		.s.push(^.s.popInt())
	case 'i':
		for  := range .params[:2] {
			if ,  := .params[].(int);  {
				.params[] =  + 1
			}
		}
	case '?', ';':
	case 't':
		return .scanThenFn
	case 'e':
		.skipElse = true
		return .skipTextFn
	}
	.pos++
	return .scanTextFn
}

func ( *parametizer) () stateFn {
	// the character was already read, so no need to check the error.
	,  := .peek()
	// 6 should be the maximum length of a format string, for example "%:-9.9d".
	 := []byte{'%', , 0, 0, 0, 0}
	var  error
	for {
		.pos++
		,  = .peek()
		if  != nil {
			return nil
		}
		 = append(, )
		switch  {
		case 'o', 'd', 'x', 'X':
			fmt.Fprintf(.buf, string(), .s.popInt())
			break
		case 's':
			fmt.Fprintf(.buf, string(), .s.popString())
			break
		case 'c':
			fmt.Fprintf(.buf, string(), .s.popByte())
			break
		}
	}
	.pos++
	return .scanTextFn
}

func ( *parametizer) () stateFn {
	,  := .peek()
	if  != nil {
		return nil
	}
	if  := int( - '1');  >= 0 &&  < len(.params) {
		.s.push(.params[])
	} else {
		.s.push(0)
	}
	// skip the '}'
	.pos++
	return .scanTextFn
}

func ( *parametizer) () stateFn {
	,  := .peek()
	if  != nil {
		return nil
	}
	if  >= 'A' &&  <= 'Z' {
		staticVars.Lock()
		staticVars.vars[int(-'A')] = .s.pop()
		staticVars.Unlock()
	} else if  >= 'a' &&  <= 'z' {
		.vars[int(-'a')] = .s.pop()
	}
	.pos++
	return .scanTextFn
}

func ( *parametizer) () stateFn {
	,  := .peek()
	if  != nil {
		return nil
	}
	var  byte
	if  >= 'A' &&  <= 'Z' {
		 = 'A'
	} else if  >= 'a' &&  <= 'z' {
		 = 'a'
	}
	staticVars.Lock()
	.s.push(staticVars.vars[int(-)])
	staticVars.Unlock()
	.pos++
	return .scanTextFn
}

func ( *parametizer) () stateFn {
	var  int
	for {
		,  := .peek()
		if  != nil {
			return nil
		}
		.pos++
		if  < '0' ||  > '9' {
			.s.push()
			return .scanTextFn
		}
		 = ( * 10) + int(-'0')
	}
}

func ( *parametizer) () stateFn {
	.pos++
	if .s.popBool() {
		return .scanTextFn
	}
	.skipElse = false
	return .skipTextFn
}

func ( *parametizer) () stateFn {
	for {
		,  := .peek()
		if  != nil {
			return nil
		}
		.pos++
		if  == '%' {
			break
		}
	}
	if .skipElse {
		return .skipElseFn
	}
	return .skipThenFn
}

func ( *parametizer) () stateFn {
	,  := .peek()
	if  != nil {
		return nil
	}
	.pos++
	switch  {
	case ';':
		if .nest == 0 {
			return .scanTextFn
		}
		.nest--
	case '?':
		.nest++
	case 'e':
		if .nest == 0 {
			return .scanTextFn
		}
	}
	return .skipTextFn
}

func ( *parametizer) () stateFn {
	,  := .peek()
	if  != nil {
		return nil
	}
	.pos++
	switch  {
	case ';':
		if .nest == 0 {
			return .scanTextFn
		}
		.nest--
	case '?':
		.nest++
	}
	return .skipTextFn
}

// Printf evaluates a parameterized terminfo value z, interpolating params.
func ( []byte,  ...interface{}) string {
	 := newParametizer()
	defer .reset()
	// make sure we always have 9 parameters -- makes it easier
	// later to skip checks and its faster
	for  := 0;  < len(.params) &&  < len(); ++ {
		.params[] = []
	}
	return .exec()
}

// Fprintf evaluates a parameterized terminfo value z, interpolating params and
// writing to w.
func ( io.Writer,  []byte,  ...interface{}) {
	.Write([]byte(Printf(, ...)))
}