ut/utio/string.go

38 lines
880 B
Go
Raw Normal View History

package utio
import "bytes"
// DecodeFromString d the bytes of s to fn.
func DecodeFromString(s string, d Decoder) error {
return ReadFromString(s, d.Decode)
}
// ReadFromString supplies the bytes of s to fn.
func ReadFromString(s string, fn ReadFunc) error {
return fn(bytes.NewBufferString(s))
}
// EncodeToString returns the encoded string value
func EncodeToString(e Encoder) (string, error) {
return WriteToString(e.Encode)
}
// MustEncodeToString returns the encoded string value and panics if an error has occurred
func MustEncodeToString(e Encoder) string {
s, err := WriteToString(e.Encode)
if err != nil {
panic(err)
}
return s
}
// WriteToString returns the string value of the data written to fn.
func WriteToString(fn WriteFunc) (string, error) {
var b = &bytes.Buffer{}
var err = fn(b)
if nil != err {
return "", err
}
return b.String(), nil
}