2022-04-15 09:42:07 +02:00
|
|
|
package clean
|
2021-12-15 12:24:05 +01:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-01-17 16:07:31 +01:00
|
|
|
// SearchString replaces search operator with default symbols.
|
|
|
|
func SearchString(s string) string {
|
|
|
|
if s == "" || reject(s, MaxLength) {
|
|
|
|
return Empty
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normalize.
|
|
|
|
s = strings.ReplaceAll(s, "%%", "%")
|
|
|
|
s = strings.ReplaceAll(s, "%", "*")
|
|
|
|
s = strings.ReplaceAll(s, "**", "*")
|
|
|
|
|
|
|
|
// Trim.
|
2022-03-25 18:01:34 +01:00
|
|
|
return strings.Trim(s, "|\\<>\n\r\t")
|
2022-01-17 16:07:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SearchQuery replaces search operator with default symbols.
|
|
|
|
func SearchQuery(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)
|
2022-01-17 16:07:31 +01:00
|
|
|
s = strings.ReplaceAll(s, "%%", "%")
|
2021-12-15 12:24:05 +01:00
|
|
|
s = strings.ReplaceAll(s, "%", "*")
|
2022-01-17 16:07:31 +01:00
|
|
|
s = strings.ReplaceAll(s, "**", "*")
|
2021-12-15 12:24:05 +01:00
|
|
|
|
2021-12-16 15:26:54 +01:00
|
|
|
// Trim.
|
2022-03-25 18:01:34 +01:00
|
|
|
return strings.Trim(s, "|${}\\<>: \n\r\t")
|
2021-12-15 12:24:05 +01:00
|
|
|
}
|