fix line parsing function

This commit is contained in:
Jeremy Latt 2014-02-27 21:18:05 -08:00
parent 8c8d371ae2
commit 542744d52a
2 changed files with 28 additions and 14 deletions

View File

@ -80,7 +80,7 @@ func (command *BaseCommand) SetCode(code StringCode) {
} }
func ParseCommand(line string) (cmd editableCommand, err error) { func ParseCommand(line string) (cmd editableCommand, err error) {
code, args := parseLine(line) code, args := ParseLine(line)
constructor := parseCommandFuncs[code] constructor := parseCommandFuncs[code]
if constructor == nil { if constructor == nil {
cmd = NewUnknownCommand(args) cmd = NewUnknownCommand(args)
@ -97,19 +97,33 @@ var (
spacesExpr = regexp.MustCompile(` +`) spacesExpr = regexp.MustCompile(` +`)
) )
func parseLine(line string) (StringCode, []string) { func splitArg(line string) (arg string, rest string) {
var parts []string parts := spacesExpr.Split(line, 2)
if colonIndex := strings.IndexRune(line, ':'); colonIndex >= 0 { if len(parts) > 0 {
lastArg := norm.NFC.String(line[colonIndex+len(":"):]) arg = parts[0]
line = strings.TrimRight(line[:colonIndex], " ")
line = norm.NFKC.String(line)
parts = append(spacesExpr.Split(line, -1), lastArg)
} else {
line = strings.TrimRight(line, " ")
line = norm.NFKC.String(line)
parts = spacesExpr.Split(line, -1)
} }
return StringCode(strings.ToUpper(parts[0])), parts[1:] if len(parts) > 1 {
rest = parts[1]
}
return
}
func ParseLine(line string) (command StringCode, args []string) {
args = make([]string, 0)
if strings.HasPrefix(line, ":") {
_, line = splitArg(line)
}
arg, line := splitArg(line)
command = StringCode(strings.ToUpper(arg))
for len(line) > 0 {
if strings.HasPrefix(line, ":") {
args = append(args, norm.NFC.String(line[len(":"):]))
break
}
arg, line = splitArg(line)
args = append(args, norm.NFKC.String(arg))
}
return
} }
// <command> [args...] // <command> [args...]

View File

@ -23,7 +23,7 @@ var (
) )
const ( const (
SEM_VER = "ergonomadic-1.2.10" SEM_VER = "ergonomadic-1.2.11"
CRLF = "\r\n" CRLF = "\r\n"
MAX_REPLY_LEN = 512 - len(CRLF) MAX_REPLY_LEN = 512 - len(CRLF)