1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-25 01:32:13 +02:00
ferret/pkg/stdlib/collections/reverse.go

52 lines
1.1 KiB
Go
Raw Normal View History

package collections
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
// Reverse returns the reverse of a given string or array value.
// @param text (String|Array) - The string or array to reverse.
// @returns (String|Array) - Returns a reversed version of a given value.
func Reverse(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.EmptyString, err
}
err = core.ValidateType(args[0], types.Array, types.String)
if err != nil {
return values.None, err
}
switch col := args[0].(type) {
case values.String:
runes := []rune(string(col))
size := len(runes)
// Reverse
for i := 0; i < size/2; i++ {
runes[i], runes[size-1-i] = runes[size-1-i], runes[i]
}
return values.NewString(string(runes)), nil
case *values.Array:
size := int(col.Length())
result := values.NewArray(size)
for i := size - 1; i >= 0; i-- {
result.Push(col.Get(values.NewInt(i)))
}
return result, nil
default:
return values.None, nil
}
}