2021-12-15 12:24:05 +01:00
|
|
|
package sanitize
|
|
|
|
|
2021-12-16 15:26:54 +01:00
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
2021-12-15 12:24:05 +01:00
|
|
|
|
|
|
|
// spaced returns the string padded with a space left and right.
|
|
|
|
func spaced(s string) string {
|
|
|
|
return Space + s + Space
|
|
|
|
}
|
|
|
|
|
2021-12-16 15:26:54 +01:00
|
|
|
// replace performs a case-insensitive string replacement.
|
|
|
|
func replace(subject string, search string, replace string) string {
|
|
|
|
return regexp.MustCompile("(?i)"+search).ReplaceAllString(subject, replace)
|
|
|
|
}
|
|
|
|
|
2021-12-15 12:24:05 +01:00
|
|
|
// Query replaces search operator with default symbols.
|
|
|
|
func Query(s string) string {
|
2021-12-16 15:26:54 +01:00
|
|
|
if s == "" || reject(s, MaxLength) {
|
2021-12-15 12:24:05 +01:00
|
|
|
return Empty
|
|
|
|
}
|
|
|
|
|
2021-12-16 15:26:54 +01:00
|
|
|
// Normalize.
|
|
|
|
s = replace(s, spaced(EnOr), Or)
|
|
|
|
s = replace(s, spaced(EnOr), Or)
|
|
|
|
s = replace(s, spaced(EnAnd), And)
|
|
|
|
s = replace(s, spaced(EnWith), And)
|
|
|
|
s = replace(s, spaced(EnIn), And)
|
|
|
|
s = replace(s, spaced(EnAt), And)
|
2021-12-15 12:24:05 +01:00
|
|
|
s = strings.ReplaceAll(s, SpacedPlus, And)
|
|
|
|
s = strings.ReplaceAll(s, "%", "*")
|
|
|
|
|
2021-12-16 15:26:54 +01:00
|
|
|
// Trim.
|
|
|
|
return strings.Trim(s, "+&|-=$^(){}\\<>,;: \n\r\t")
|
2021-12-15 12:24:05 +01:00
|
|
|
}
|