Added function to sanitize strings by escaping quotes (#2360)

This commit is contained in:
Ankit Chawla
2022-02-21 12:44:27 +05:30
committed by GitHub
parent f45d85f30a
commit fe5e5f592b
7 changed files with 61 additions and 1 deletions
+6
View File
@@ -205,3 +205,9 @@ func DownloadUrl(ctx context.Context, httpClient *http.Client, url string, local
return nil
}
func EscapeQuotes(str string) string {
replacer := strings.NewReplacer("\n", "", "\r", "", "\t", "", `"`, `\"`)
str = replacer.Replace(str)
return str
}
+37
View File
@@ -81,3 +81,40 @@ func TestGetChecksum(t *testing.T) {
})
}
}
func TestEscapeQuotes(t *testing.T) {
tests := []struct {
name string
src string
want string
}{
{
name: "Testing tab escape sequence",
src: "This\tis\ta\ttest\t string.",
want: "Thisisatest string.",
},
{
name: "Testing carriage return",
src: "This is a \rtest string. \r This is the second test string\r.",
want: "This is a test string. This is the second test string.",
},
{
name: "Testing next line escape sequence",
src: "This is a \ntest string. \n This is the second test string\n.",
want: "This is a test string. This is the second test string.",
},
{
name: "Testing quotes",
src: `This is a "test string"". This is" the second test string "."`,
want: `This is a \"test string\"\". This is\" the second test string \".\"`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := EscapeQuotes(tt.src)
if got != tt.want {
t.Errorf("EscapeQuotes() got = %v, want = %v", got, tt.want)
}
})
}
}