Skip to content

Commit

Permalink
Better handling of deep-quoting argument strings
Browse files Browse the repository at this point in the history
  • Loading branch information
zix99 committed Nov 30, 2019
1 parent 187c2b1 commit 8d3a74b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
13 changes: 10 additions & 3 deletions pkg/expressions/argSplitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ func splitTokenizedArguments(s string) []string {
sb.WriteRune(runes[i])
} else if r == '"' && !quoted {
quoted = true
if tokenDepth > 0 {
sb.WriteRune('"')
}
} else if r == '"' && quoted {
quoted = false
// Always append, even if empty
args = append(args, sb.String())
sb.Reset()
if tokenDepth > 0 {
sb.WriteRune('"')
} else {
// Always append, even if empty
args = append(args, sb.String())
sb.Reset()
}
} else if r == '{' && !quoted {
tokenDepth++
sb.WriteRune(r)
Expand Down
12 changes: 10 additions & 2 deletions pkg/expressions/argSplitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,19 @@ func TestEscaping(t *testing.T) {
}

func TestEmptyString(t *testing.T) {
tokens := splitTokenizedArguments(`a "bc" ""`)
assert.Equal(t, []string{"a", "bc", ""}, tokens)
assert.Equal(t, []string{"a", "bc", ""}, splitTokenizedArguments(`a "bc" ""`))
assert.Equal(t, []string{"a", "bc", "", "def"}, splitTokenizedArguments(`a bc "" def`))
}

func TestQuotedBraces(t *testing.T) {
tokens := splitTokenizedArguments(`a "{bc\"" def`)
assert.Equal(t, []string{"a", "{bc\"", "def"}, tokens)
}

func TestDeepQuoting(t *testing.T) {
tokens := splitTokenizedArguments(`if {eq {0} "abc def"} abc`)
assert.Equal(t, []string{"if", `{eq {0} "abc def"}`, "abc"}, tokens)

tokens2 := splitTokenizedArguments(`eq {0} "abc def"`)
assert.Equal(t, []string{"eq", "{0}", "abc def"}, tokens2)
}
1 change: 1 addition & 0 deletions pkg/expressions/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func TestIfStatement(t *testing.T) {
testExpression(t, mockContext("abc", "q"),
`{if {0} {1} efg} {if {0} abc} {if {not {0}} a b} {if "" a} {if "" a b}`,
"q abc b b")
testExpression(t, mockContext("abc efg"), `{if {eq {0} "abc efg"} beq}`, "beq")
}

func TestComparisonEquality(t *testing.T) {
Expand Down

0 comments on commit 8d3a74b

Please sign in to comment.