mirror of
https://github.com/MontFerret/ferret.git
synced 2024-12-14 11:23:02 +02:00
125 lines
2.0 KiB
Go
125 lines
2.0 KiB
Go
package values
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
"github.com/pkg/errors"
|
|
"hash/fnv"
|
|
"strings"
|
|
)
|
|
|
|
type String string
|
|
|
|
var EmptyString = String("")
|
|
var SpaceString = String(" ")
|
|
|
|
func NewString(input string) String {
|
|
if input == "" {
|
|
return EmptyString
|
|
}
|
|
|
|
return String(input)
|
|
}
|
|
|
|
func NewStringFromRunes(input []rune) String {
|
|
if len(input) == 0 {
|
|
return EmptyString
|
|
}
|
|
|
|
return String(input)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
return EmptyString, errors.Wrap(core.ErrInvalidType, "expected 'string'")
|
|
}
|
|
|
|
func ParseStringP(input interface{}) String {
|
|
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 {
|
|
return core.StringType
|
|
}
|
|
|
|
func (t String) String() string {
|
|
return string(t)
|
|
}
|
|
|
|
func (t String) Compare(other core.Value) int {
|
|
switch other.Type() {
|
|
case core.StringType:
|
|
return strings.Compare(string(t), other.Unwrap().(string))
|
|
default:
|
|
if other.Type() > core.DateTimeType {
|
|
return -1
|
|
}
|
|
|
|
return 1
|
|
}
|
|
}
|
|
|
|
func (t String) Unwrap() interface{} {
|
|
return string(t)
|
|
}
|
|
|
|
func (t String) Hash() uint64 {
|
|
h := fnv.New64a()
|
|
|
|
h.Write([]byte(t.Type().String()))
|
|
h.Write([]byte(":"))
|
|
h.Write([]byte(t))
|
|
|
|
return h.Sum64()
|
|
}
|
|
|
|
func (t String) Clone() core.Value {
|
|
return t
|
|
}
|
|
|
|
func (t String) Length() Int {
|
|
return Int(len(t))
|
|
}
|
|
|
|
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())
|
|
}
|