1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-14 11:23:02 +02:00
ferret/pkg/stdlib/strings/split.go
Tim Voronov 1af8b37a0f
New type system (#232)
* New type system

* Fixed dot notation for HTML elements
2019-02-13 12:31:18 -05:00

50 lines
1.1 KiB
Go

package strings
import (
"context"
"strings"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
)
// Split splits the given string value into a list of strings, using the separator.
// @params text (String) - The string to split.
// @params separator (String) - The sperator.
// @params limit (Int) - Limit the number of split values in the result. If no limit is given, the number of splits returned is not bounded.
// @returns strings (Array<String>) - Array of strings.
func Split(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 2, 3)
if err != nil {
return values.None, err
}
text := args[0].String()
separator := args[1].String()
limit := -1
if len(args) > 2 {
if args[2].Type() == types.Int {
limit = int(args[2].(values.Int))
}
}
var strs []string
if limit < 0 {
strs = strings.Split(text, separator)
} else {
strs = strings.SplitN(text, separator, limit)
}
arr := values.NewArray(len(strs))
for _, str := range strs {
arr.Push(values.NewString(str))
}
return arr, nil
}