package terminfo
import (
)
type parametizer struct {
z []byte
pos int
nest int
s stack
skipElse bool
buf *bytes.Buffer
params [9]interface{}
vars [26]interface{}
}
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
},
}
func ( []byte) *parametizer {
:= parametizerPool.Get().(*parametizer)
.z =
return
}
func ( *parametizer) () {
.pos, .nest = 0, 0
.s.reset()
.buf.Reset()
.params, .vars = [9]interface{}{}, [26]interface{}{}
parametizerPool.Put()
}
type stateFn func() stateFn
func ( *parametizer) () string {
for := .scanTextFn; != nil; {
= ()
}
return .buf.String()
}
func ( *parametizer) () (byte, error) {
if .pos >= len(.z) {
return 0, io.EOF
}
return .z[.pos], nil
}
func ( *parametizer) ( int) {
if .pos > {
.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 ':':
.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()
.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 {
, := .peek()
:= []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)
}
.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
}
func ( []byte, ...interface{}) string {
:= newParametizer()
defer .reset()
for := 0; < len(.params) && < len(); ++ {
.params[] = []
}
return .exec()
}
func ( io.Writer, []byte, ...interface{}) {
.Write([]byte(Printf(, ...)))
}