2018-10-14 17:45:06 +02:00
|
|
|
package strings
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
2019-06-25 12:51:51 -04:00
|
|
|
"net/url"
|
2020-05-08 05:34:21 +03:00
|
|
|
"strconv"
|
2018-10-14 17:45:06 +02:00
|
|
|
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
|
|
|
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
)
|
|
|
|
|
2020-08-07 21:49:29 -04:00
|
|
|
// FROM_BASE64 returns the value of a base64 representation.
|
|
|
|
// @param {String} str - The string to decode.
|
|
|
|
// @return {String} - The decoded string.
|
2018-10-14 17:45:06 +02:00
|
|
|
func FromBase64(_ context.Context, args ...core.Value) (core.Value, error) {
|
|
|
|
err := core.ValidateArgs(args, 1, 1)
|
2019-06-25 12:51:51 -04:00
|
|
|
|
2018-10-14 17:45:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString, err
|
|
|
|
}
|
|
|
|
|
|
|
|
value := args[0].String()
|
|
|
|
|
|
|
|
out, err := base64.StdEncoding.DecodeString(value)
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.NewString(string(out)), nil
|
|
|
|
}
|
2019-06-25 12:51:51 -04:00
|
|
|
|
2020-08-07 21:49:29 -04:00
|
|
|
// DECODE_URI_COMPONENT returns the decoded String of uri.
|
|
|
|
// @param {String} uri - Uri to decode.
|
|
|
|
// @return {String} - Decoded string.
|
2019-06-25 12:51:51 -04:00
|
|
|
func DecodeURIComponent(_ context.Context, args ...core.Value) (core.Value, error) {
|
|
|
|
err := core.ValidateArgs(args, 1, 1)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString, err
|
|
|
|
}
|
|
|
|
|
|
|
|
str, err := url.QueryUnescape(args[0].String())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2020-05-08 05:34:21 +03:00
|
|
|
// hack for decoding unicode symbols.
|
|
|
|
// eg. convert "\u0026" -> "&""
|
|
|
|
str, err = strconv.Unquote("\"" + str + "\"")
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2019-06-25 12:51:51 -04:00
|
|
|
return values.NewString(str), nil
|
|
|
|
}
|