2018-09-21 20:36:33 -04:00
|
|
|
package strings
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-10-14 20:06:27 +03:00
|
|
|
"strings"
|
|
|
|
|
2018-09-21 20:36:33 -04:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
2019-02-13 12:31:18 -05:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
2018-09-21 20:36:33 -04:00
|
|
|
)
|
|
|
|
|
2020-08-07 21:49:29 -04:00
|
|
|
// FIND_FIRST returns the position of the first occurrence of the string search inside the string text. Positions start at 0.
|
|
|
|
// @param {String} str - The source string.
|
|
|
|
// @param {String} search - The string to seek.
|
|
|
|
// @param {Int} [start] - Limit the search to a subset of the text, beginning at start.
|
|
|
|
// @param {Int} [end] - Limit the search to a subset of the text, ending at end
|
|
|
|
// @return {Int} - The character position of the match. If search is not contained in text, -1 is returned. If search is empty, start is returned.
|
2018-09-21 20:36:33 -04:00
|
|
|
func FindFirst(_ context.Context, args ...core.Value) (core.Value, error) {
|
|
|
|
err := core.ValidateArgs(args, 2, 4)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.NewInt(-1), err
|
|
|
|
}
|
|
|
|
|
|
|
|
argsCount := len(args)
|
|
|
|
|
|
|
|
text := args[0].String()
|
|
|
|
runes := []rune(text)
|
|
|
|
search := args[1].String()
|
|
|
|
start := values.NewInt(0)
|
2019-03-29 17:48:51 +03:00
|
|
|
end := values.NewInt(len(text))
|
2018-09-21 20:36:33 -04:00
|
|
|
|
|
|
|
if argsCount == 3 {
|
|
|
|
arg3 := args[2]
|
|
|
|
|
2019-02-13 12:31:18 -05:00
|
|
|
if arg3.Type() == types.Int {
|
2018-09-21 20:36:33 -04:00
|
|
|
start = arg3.(values.Int)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if argsCount == 4 {
|
|
|
|
arg4 := args[3]
|
|
|
|
|
2019-02-13 12:31:18 -05:00
|
|
|
if arg4.Type() == types.Int {
|
2018-09-21 20:36:33 -04:00
|
|
|
end = arg4.(values.Int)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
found := strings.Index(string(runes[start:end]), search)
|
|
|
|
|
|
|
|
if found > -1 {
|
|
|
|
return values.NewInt(found + int(start)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.NewInt(found), nil
|
|
|
|
}
|
|
|
|
|
2020-08-07 21:49:29 -04:00
|
|
|
// FIND_LAST returns the position of the last occurrence of the string search inside the string text. Positions start at 0.
|
|
|
|
// @param {String} src - The source string.
|
|
|
|
// @param {String} search - The string to seek.
|
|
|
|
// @param {Int} [start] - Limit the search to a subset of the text, beginning at start.
|
|
|
|
// @param {Int} [end] - Limit the search to a subset of the text, ending at end
|
|
|
|
// @return {Int} - The character position of the match. If search is not contained in text, -1 is returned. If search is empty, start is returned.
|
2018-09-21 20:36:33 -04:00
|
|
|
func FindLast(_ context.Context, args ...core.Value) (core.Value, error) {
|
|
|
|
err := core.ValidateArgs(args, 2, 4)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.NewInt(-1), err
|
|
|
|
}
|
|
|
|
|
|
|
|
argsCount := len(args)
|
|
|
|
|
|
|
|
text := args[0].String()
|
|
|
|
runes := []rune(text)
|
|
|
|
search := args[1].String()
|
|
|
|
start := values.NewInt(0)
|
2019-03-29 17:48:51 +03:00
|
|
|
end := values.NewInt(len(text))
|
2018-09-21 20:36:33 -04:00
|
|
|
|
|
|
|
if argsCount == 3 {
|
|
|
|
arg3 := args[2]
|
|
|
|
|
2019-02-13 12:31:18 -05:00
|
|
|
if arg3.Type() == types.Int {
|
2018-09-21 20:36:33 -04:00
|
|
|
start = arg3.(values.Int)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if argsCount == 4 {
|
|
|
|
arg4 := args[3]
|
|
|
|
|
2019-02-13 12:31:18 -05:00
|
|
|
if arg4.Type() == types.Int {
|
2018-09-21 20:36:33 -04:00
|
|
|
end = arg4.(values.Int)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
found := strings.LastIndex(string(runes[start:end]), search)
|
|
|
|
|
|
|
|
if found > -1 {
|
|
|
|
return values.NewInt(found + int(start)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.NewInt(found), nil
|
|
|
|
}
|