github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/strings/contains.go (about) 1 package strings 2 3 import ( 4 "context" 5 6 "github.com/MontFerret/ferret/pkg/runtime/core" 7 "github.com/MontFerret/ferret/pkg/runtime/values" 8 "github.com/MontFerret/ferret/pkg/runtime/values/types" 9 ) 10 11 // CONTAINS returns a value indicating whether a specified substring occurs within a string. 12 // @param {String} str - The source string. 13 // @param {String} search - The string to seek. 14 // @param {Boolean} [returnIndex=False] - Values which indicates whether to return the character position of the match is returned instead of a boolean. 15 // @return {Boolean | Int} - A value indicating whether a specified substring occurs within a string. 16 func Contains(_ context.Context, args ...core.Value) (core.Value, error) { 17 err := core.ValidateArgs(args, 2, 3) 18 19 if err != nil { 20 return values.False, err 21 } 22 23 var text values.String 24 var search values.String 25 returnIndex := values.False 26 27 arg1 := args[0] 28 arg2 := args[1] 29 30 if arg1.Type() == types.String { 31 text = arg1.(values.String) 32 } else { 33 text = values.NewString(arg1.String()) 34 } 35 36 if arg2.Type() == types.String { 37 search = arg2.(values.String) 38 } else { 39 search = values.NewString(arg2.String()) 40 } 41 42 if len(args) > 2 { 43 arg3 := args[2] 44 45 if arg3.Type() == types.Boolean { 46 returnIndex = arg3.(values.Boolean) 47 } 48 } 49 50 if returnIndex == values.True { 51 return text.IndexOf(search), nil 52 } 53 54 return text.Contains(search), nil 55 }