Skip to content

Commit

Permalink
interpret as an empty string when "" is passed
Browse files Browse the repository at this point in the history
  • Loading branch information
ktr0731 committed Jan 18, 2020
1 parent d2f5a50 commit b893a8f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 8 additions & 4 deletions shellstring.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ func Parse(in string) ([]string, error) {
return nil, errors.New("open single quote missing")
}
stack = stack[:len(stack)-1]
needCloseSingleQuote = false

// if next char is space, split word
if len(in)-1 > i+1 && in[i+1] == ' ' {
out = append(out, string(s))
s = []rune{}
s = nil
} else if len(stack) == 0 && len(s) == 0 && (len(in)-1 > i+1 && in[i+1] == ' ' || len(in)-1 == i) {
out = append(out, "")
}
needCloseSingleQuote = false
} else {
if len(stack) == 0 {
stack = append(stack, '\'')
Expand All @@ -46,7 +48,9 @@ func Parse(in string) ([]string, error) {
// if next char is space, split word
if len(in)-1 > i+1 && in[i+1] == ' ' {
out = append(out, string(s))
s = []rune{}
s = nil
} else if len(stack) == 0 && len(s) == 0 && (len(in)-1 > i+1 && in[i+1] == ' ' || len(in)-1 == i) {
out = append(out, "")
}
} else {
if len(stack) == 0 {
Expand All @@ -61,7 +65,7 @@ func Parse(in string) ([]string, error) {
if len(stack) == 0 {
if len(s) != 0 {
out = append(out, string(s))
s = []rune{}
s = nil
}
} else {
s = append(s, r)
Expand Down
5 changes: 5 additions & 0 deletions shellstring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func Test_main(t *testing.T) {
{"foo", []string{"foo"}, false},
{"foo bar", []string{"foo", "bar"}, false},
{`"foo"`, []string{"foo"}, false},
{`""`, []string{""}, false},
{`''`, []string{""}, false},
{`'' ""`, []string{"", ""}, false},
{`"" ''`, []string{"", ""}, false},
{`"foo bar"`, []string{"foo bar"}, false},
{`"foo" "bar"`, []string{"foo", "bar"}, false},
{`"foo""bar"`, []string{"foobar"}, false},
Expand All @@ -27,6 +31,7 @@ func Test_main(t *testing.T) {
{`'"foo bar"'`, []string{`"foo bar"`}, false},
{`'"'foo bar'"'`, []string{`"foo`, `bar"`}, false},
{`''foo''`, []string{`foo`}, false},
{`""foo""`, []string{`foo`}, false},
}

for _, c := range cases {
Expand Down

0 comments on commit b893a8f

Please sign in to comment.