1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-02-07 13:31:56 +02:00

68 lines
1.5 KiB
Go
Raw Normal View History

package strings
import (
"context"
"regexp"
"strings"
"github.com/gobwas/glob"
2018-10-07 15:22:04 +03:00
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
var (
deprecatedLikeSyntax = regexp.MustCompile("[%_]")
)
// LIKE checks whether the pattern search is contained in the string text, using wildcard matching.
// @param {String} str - The string to search in.
// @param {String} search - A search pattern that can contain the wildcard characters.
// @param {Boolean} caseInsensitive - If set to true, the matching will be case-insensitive. The default is false.
// @return {Boolean} - Returns true if the pattern is contained in text, and false otherwise.
func Like(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 2, 3)
if err != nil {
return values.False, err
}
str := args[0].String()
pattern := args[1].String()
2018-10-07 15:22:04 +03:00
if len(pattern) == 0 {
return values.NewBoolean(len(str) == 0), nil
}
// TODO: Remove me in next releases
replaced := deprecatedLikeSyntax.ReplaceAllFunc([]byte(pattern), func(b []byte) []byte {
str := string(b)
2018-10-07 15:22:04 +03:00
switch str {
case "%":
return []byte("*")
case "_":
return []byte("?")
default:
return b
}
})
2018-10-07 15:22:04 +03:00
pattern = string(replaced)
2018-10-07 15:22:04 +03:00
if len(args) > 2 {
if values.ToBoolean(args[2]) {
str = strings.ToLower(str)
pattern = strings.ToLower(pattern)
2018-10-07 15:22:04 +03:00
}
}
g, err := glob.Compile(pattern)
2018-10-07 15:22:04 +03:00
if err != nil {
return nil, err
}
2018-10-07 15:22:04 +03:00
return values.NewBoolean(g.Match(str)), nil
}