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:
parent
10549e49e1
commit
4c835a467b
|
@ -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))
|
||||
}
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue