2018-09-18 22:42:38 +02:00
|
|
|
package values
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-10-05 21:17:22 +02:00
|
|
|
"hash/fnv"
|
2018-09-18 22:42:38 +02:00
|
|
|
"strings"
|
2018-10-12 17:58:08 +02:00
|
|
|
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
2019-02-13 19:31:18 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
2018-09-18 22:42:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type String string
|
|
|
|
|
2019-02-13 19:31:18 +02:00
|
|
|
const (
|
|
|
|
EmptyString = String("")
|
|
|
|
SpaceString = String(" ")
|
|
|
|
)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
func NewString(input string) String {
|
|
|
|
if input == "" {
|
|
|
|
return EmptyString
|
|
|
|
}
|
|
|
|
|
|
|
|
return String(input)
|
|
|
|
}
|
|
|
|
|
2018-09-22 02:36:33 +02:00
|
|
|
func NewStringFromRunes(input []rune) String {
|
|
|
|
if len(input) == 0 {
|
|
|
|
return EmptyString
|
|
|
|
}
|
|
|
|
|
|
|
|
return String(input)
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
func ParseString(input interface{}) (String, error) {
|
|
|
|
if core.IsNil(input) {
|
|
|
|
return EmptyString, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
str, ok := input.(string)
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
if str != "" {
|
|
|
|
return String(str), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return EmptyString, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
stringer, ok := input.(fmt.Stringer)
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
return String(stringer.String()), nil
|
|
|
|
}
|
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
return EmptyString, core.Error(core.ErrInvalidType, "expected 'string'")
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-13 01:01:49 +02:00
|
|
|
func MustParseString(input interface{}) String {
|
2018-09-18 22:42:38 +02:00
|
|
|
res, err := ParseString(input)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t String) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(string(t))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t String) Type() core.Type {
|
2019-02-13 19:31:18 +02:00
|
|
|
return types.String
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t String) String() string {
|
|
|
|
return string(t)
|
|
|
|
}
|
|
|
|
|
2019-02-13 19:31:18 +02:00
|
|
|
func (t String) Compare(other core.Value) int64 {
|
|
|
|
if other.Type() == types.String {
|
|
|
|
return int64(strings.Compare(string(t), other.Unwrap().(string)))
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
2019-02-13 19:31:18 +02:00
|
|
|
|
|
|
|
return types.Compare(types.String, other.Type())
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t String) Unwrap() interface{} {
|
|
|
|
return string(t)
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:17:22 +02:00
|
|
|
func (t String) Hash() uint64 {
|
|
|
|
h := fnv.New64a()
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-10-05 21:17:22 +02:00
|
|
|
h.Write([]byte(t.Type().String()))
|
|
|
|
h.Write([]byte(":"))
|
|
|
|
h.Write([]byte(t))
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-10-05 21:17:22 +02:00
|
|
|
return h.Sum64()
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:58:08 +02:00
|
|
|
func (t String) Copy() core.Value {
|
2018-09-27 17:53:26 +02:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
func (t String) Length() Int {
|
2019-02-24 20:21:34 +02:00
|
|
|
return Int(len([]rune(string(t))))
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t String) Contains(other String) Boolean {
|
|
|
|
return t.IndexOf(other) > -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t String) IndexOf(other String) Int {
|
|
|
|
return Int(strings.Index(string(t), string(other)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t String) Concat(other core.Value) String {
|
|
|
|
return String(string(t) + other.String())
|
|
|
|
}
|