Add builtins has-{pre,suf}fix.

This commit is contained in:
Qi Xiao 2016-04-06 07:57:11 +02:00
parent 8b960a0315
commit 99f1c061f2

View File

@ -63,6 +63,8 @@ func init() {
&BuiltinFn{"++", WrapFn(join)},
&BuiltinFn{"//", WrapFn(split)},
&BuiltinFn{"has-prefix", WrapFn(hasPrefix)},
&BuiltinFn{"has-suffix", WrapFn(hasSuffix)},
&BuiltinFn{"to-json", WrapFn(toJSON)},
&BuiltinFn{"from-json", WrapFn(fromJSON)},
@ -390,6 +392,18 @@ func split(ec *EvalCtx, s, sep String) {
}
}
func hasPrefix(ec *EvalCtx, s, prefix String) {
if !strings.HasPrefix(string(s), string(prefix)) {
throw(ErrFalse)
}
}
func hasSuffix(ec *EvalCtx, s, suffix String) {
if !strings.HasSuffix(string(s), string(suffix)) {
throw(ErrFalse)
}
}
// toJSON converts a stream of Value's to JSON data.
func toJSON(ec *EvalCtx, iterate func(func(Value))) {
out := ec.ports[1].File