1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-14 11:23:02 +02:00
ferret/pkg/stdlib/strings/reverse.go
Tim Voronov e6d692010c stdlib.strings
Added string functions to standard library
2018-09-21 20:37:09 -04:00

32 lines
690 B
Go

package strings
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
/*
* Returns the reverse of the string value.
* @param text (String) - The string to revers
* @returns (String) - Returns a reversed version of the string.
*/
func Reverse(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.EmptyString, err
}
text := args[0].String()
runes := []rune(text)
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
}