mirror of
https://github.com/veops/oneterm.git
synced 2025-10-28 09:51:34 +08:00
59 lines
1.1 KiB
Go
59 lines
1.1 KiB
Go
package guacd
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
InternalDataOpcode = ""
|
|
Delimiter = ';'
|
|
)
|
|
|
|
var (
|
|
InternalOpcodeIns = []byte(fmt.Sprint(len(InternalDataOpcode), ".", InternalDataOpcode))
|
|
)
|
|
|
|
type Instruction struct {
|
|
Opcode string
|
|
Args []string
|
|
cache string
|
|
}
|
|
|
|
func NewInstruction(opcode string, args ...string) *Instruction {
|
|
return &Instruction{
|
|
Opcode: opcode,
|
|
Args: args,
|
|
}
|
|
}
|
|
|
|
func (i *Instruction) String() string {
|
|
if len(i.cache) > 0 {
|
|
return i.cache
|
|
}
|
|
|
|
i.cache = fmt.Sprintf("%d.%s", len(i.Opcode), i.Opcode)
|
|
for _, value := range i.Args {
|
|
i.cache += fmt.Sprintf(",%d.%s", len(value), value)
|
|
}
|
|
i.cache += string(Delimiter)
|
|
return i.cache
|
|
}
|
|
|
|
func (i *Instruction) Bytes() []byte {
|
|
return []byte(i.String())
|
|
}
|
|
|
|
func (i *Instruction) Parse(content string) *Instruction {
|
|
if strings.LastIndex(content, ";") > 0 {
|
|
content = strings.TrimRight(content, ";")
|
|
}
|
|
elements := strings.Split(content, ",")
|
|
|
|
var args = make([]string, len(elements))
|
|
for i, e := range elements {
|
|
args[i] = strings.Split(e, ".")[1]
|
|
}
|
|
return NewInstruction(args[0], args[1:]...)
|
|
}
|