1
0
Fork 0
mirror of https://github.com/diamondburned/cchat.git synced 2025-07-12 23:16:10 +00:00

Fixed several ArgsIndexed bugs

This commit fixes several ArgsIndexed bugs that would cause the function
to return no words with -1. Additional test cases have been added.
This commit is contained in:
diamondburned 2020-10-14 23:24:26 -07:00
parent 10549e49e1
commit 4c835a467b
2 changed files with 48 additions and 4 deletions

View file

@ -1,6 +1,8 @@
package split
import "bytes"
import (
"bytes"
)
// The original shellwords implementation belongs to mattn. This version alters
// some code along with some trivial optimizations.
@ -32,8 +34,20 @@ func ArgsIndexed(text string, offset int64) (args []string, argIndex int64) {
switch {
case escaped:
buf.WriteByte(r)
got = true
escaped = false
if doubleQuoted {
switch r {
case 'n':
buf.WriteByte('\n')
continue
case 't':
buf.WriteByte('\t')
continue
}
}
buf.WriteByte(r)
continue
case isSpace(r):
@ -81,8 +95,8 @@ func ArgsIndexed(text string, offset int64) (args []string, argIndex int64) {
buf.WriteByte(r)
}
if got {
if argIndex == -1 {
if got || escaped || singleQuoted || doubleQuoted {
if argIndex < 0 {
argIndex = int64(len(args))
}

View file

@ -48,6 +48,36 @@ var argsSplitTests = []testEntry{
output: []string{"echo", `this "quote" is a regular test`},
index: 0,
},
{
input: `echo "`,
offset: 6,
output: []string{"echo", ""},
index: 1,
},
{
input: `info \n`,
offset: 7,
output: []string{"info", "n"},
index: 1,
},
{
input: `info "\n"`,
offset: 7,
output: []string{"info", "\n"},
index: 1,
},
{
input: `info '\nnot a new line!'`,
offset: 15,
output: []string{"info", `\nnot a new line!`},
index: 1,
},
{
input: `info \\n`,
offset: 7,
output: []string{"info", "\\n"},
index: 1,
},
}
func TestArgsIndexed(t *testing.T) {