2018-09-21 20:36:33 -04:00
|
|
|
package strings
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-10-14 20:06:27 +03:00
|
|
|
|
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
|
|
|
// SUBSTRING returns a substring of value.
|
|
|
|
// @param {String} str - The source string.
|
|
|
|
// @param {Int} offset - Start at offset, offsets start at position 0.
|
|
|
|
// @param {Int} [length] - At most length characters, omit to get the substring from offset to the end of the string.
|
|
|
|
// @return {String} - A substring of value.
|
2018-09-21 20:36:33 -04:00
|
|
|
func Substring(_ context.Context, args ...core.Value) (core.Value, error) {
|
|
|
|
err := core.ValidateArgs(args, 2, 3)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString, err
|
|
|
|
}
|
|
|
|
|
2019-02-13 12:31:18 -05:00
|
|
|
err = core.ValidateType(args[1], types.Int)
|
2018-09-21 20:36:33 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString, err
|
|
|
|
}
|
|
|
|
|
|
|
|
text := args[0].String()
|
|
|
|
runes := []rune(text)
|
|
|
|
size := len(runes)
|
|
|
|
offset := int(args[1].(values.Int))
|
|
|
|
length := size
|
|
|
|
|
|
|
|
if len(args) > 2 {
|
2019-02-13 12:31:18 -05:00
|
|
|
if args[2].Type() == types.Int {
|
2018-09-21 20:36:33 -04:00
|
|
|
length = int(args[2].(values.Int))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var substr []rune
|
|
|
|
|
|
|
|
if length == size {
|
|
|
|
substr = runes[offset:]
|
|
|
|
} else {
|
|
|
|
end := offset + length
|
|
|
|
|
|
|
|
if size > end {
|
|
|
|
substr = runes[offset:end]
|
|
|
|
} else {
|
|
|
|
substr = runes[offset:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.NewStringFromRunes(substr), nil
|
|
|
|
}
|
|
|
|
|
2020-08-07 21:49:29 -04:00
|
|
|
// LEFT returns the leftmost characters of the string value by index.
|
|
|
|
// @param {String} str - The source string.
|
|
|
|
// @param {Int} length - The amount of characters to return.
|
|
|
|
// @return {String} - The leftmost characters of the string value by index.
|
2018-09-21 20:36:33 -04:00
|
|
|
func Left(_ context.Context, args ...core.Value) (core.Value, error) {
|
|
|
|
err := core.ValidateArgs(args, 2, 2)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString, err
|
|
|
|
}
|
|
|
|
|
|
|
|
text := args[0].String()
|
|
|
|
runes := []rune(text)
|
|
|
|
|
|
|
|
var pos int
|
|
|
|
|
2019-02-13 12:31:18 -05:00
|
|
|
if args[1].Type() == types.Int {
|
2018-09-21 20:36:33 -04:00
|
|
|
pos = int(args[1].(values.Int))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(text) < pos {
|
|
|
|
return values.NewString(text), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.NewStringFromRunes(runes[0:pos]), nil
|
|
|
|
}
|
|
|
|
|
2020-08-07 21:49:29 -04:00
|
|
|
// RIGHT returns the rightmost characters of the string value.
|
|
|
|
// @param {String} str - The source string.
|
|
|
|
// @param {Int} length - The amount of characters to return.
|
|
|
|
// @return {String} - The rightmost characters of the string value.
|
2018-09-21 20:36:33 -04:00
|
|
|
func Right(_ context.Context, args ...core.Value) (core.Value, error) {
|
|
|
|
err := core.ValidateArgs(args, 2, 2)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString, err
|
|
|
|
}
|
|
|
|
|
|
|
|
text := args[0].String()
|
|
|
|
runes := []rune(text)
|
|
|
|
size := len(runes)
|
|
|
|
pos := size
|
|
|
|
|
2019-02-13 12:31:18 -05:00
|
|
|
if args[1].Type() == types.Int {
|
2018-09-21 20:36:33 -04:00
|
|
|
pos = int(args[1].(values.Int))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(text) < pos {
|
2019-03-29 17:48:51 +03:00
|
|
|
return values.NewString(text), nil
|
2018-09-21 20:36:33 -04:00
|
|
|
}
|
|
|
|
|
2018-11-07 23:04:18 +08:00
|
|
|
return values.NewStringFromRunes(runes[size-pos : size]), nil
|
2018-09-21 20:36:33 -04:00
|
|
|
}
|