Commit 35b59d8e authored by Quentin Smith's avatar Quentin Smith

analysis/app: escape filters containing spaces

Change-Id: Iedf55dbc792d9cc12fc5d2a0a674628eed8ad726
Reviewed-on: https://go-review.googlesource.com/35945Reviewed-by: default avatarRuss Cox <rsc@golang.org>
parent 961f6a46
......@@ -113,6 +113,11 @@ func (vs valueSet) TopN(n int) []valueCount {
// addToQuery returns a new query string with add applied as a filter.
func addToQuery(query, add string) string {
if strings.ContainsAny(add, " \t\\\"") {
add = strings.Replace(add, `\`, `\\`, -1)
add = strings.Replace(add, `"`, `\"`, -1)
add = `"` + add + `"`
}
if strings.Contains(query, "|") {
return add + " " + query
}
......
......@@ -111,3 +111,23 @@ func TestCompareQuery(t *testing.T) {
})
}
}
func TestAddToQuery(t *testing.T) {
tests := []struct {
query, add string
want string
}{
{"one", "two", "two | one"},
{"pre | one vs two", "three", "three pre | one vs two"},
{"four", "five six", `"five six" | four`},
{"seven", `extra "fun"\problem`, `"extra \"fun\"\\problem" | seven`},
{"eight", `ni\"ne`, `"ni\\\"ne" | eight`},
}
for i, test := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
if got := addToQuery(test.query, test.add); got != test.want {
t.Errorf("addToQuery(%q, %q) = %q, want %q", test.query, test.add, got, test.want)
}
})
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment