Add an experimental -match builtin.

This commit is contained in:
Qi Xiao 2017-03-30 00:10:50 +02:00
parent 344fddfb83
commit 8556834802

View File

@ -86,6 +86,9 @@ func init() {
&BuiltinFn{"has-prefix", WrapFn(hasPrefix)},
&BuiltinFn{"has-suffix", WrapFn(hasSuffix)},
// Regular expression
&BuiltinFn{"-match", WrapFn(match)},
// String comparison
&BuiltinFn{"<s",
wrapStrCompare(func(a, b string) bool { return a < b })},
@ -576,6 +579,12 @@ func hasSuffix(ec *EvalCtx, s, suffix String) {
ec.OutputChan() <- Bool(strings.HasSuffix(string(s), string(suffix)))
}
func match(ec *EvalCtx, p, s String) {
matched, err := regexp.MatchString(string(p), string(s))
maybeThrow(err)
ec.OutputChan() <- Bool(matched)
}
// toJSON converts a stream of Value's to JSON data.
func toJSON(ec *EvalCtx, iterate func(func(Value))) {
out := ec.ports[1].File