5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-27 00:51:36 +00:00
matterbridge/vendor/github.com/d5/tengo/v2/stdlib/text_regexp.go

252 lines
5.3 KiB
Go
Raw Normal View History

package stdlib
import (
"regexp"
2020-01-09 20:52:19 +00:00
"github.com/d5/tengo/v2"
)
2020-01-09 20:52:19 +00:00
func makeTextRegexp(re *regexp.Regexp) *tengo.ImmutableMap {
return &tengo.ImmutableMap{
Value: map[string]tengo.Object{
// match(text) => bool
2020-01-09 20:52:19 +00:00
"match": &tengo.UserFunction{
Value: func(args ...tengo.Object) (
ret tengo.Object,
err error,
) {
if len(args) != 1 {
2020-01-09 20:52:19 +00:00
err = tengo.ErrWrongNumArguments
return
}
2020-01-09 20:52:19 +00:00
s1, ok := tengo.ToString(args[0])
if !ok {
2020-01-09 20:52:19 +00:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "string(compatible)",
Found: args[0].TypeName(),
}
return
}
if re.MatchString(s1) {
2020-01-09 20:52:19 +00:00
ret = tengo.TrueValue
} else {
2020-01-09 20:52:19 +00:00
ret = tengo.FalseValue
}
return
},
},
// find(text) => array(array({text:,begin:,end:}))/undefined
// find(text, maxCount) => array(array({text:,begin:,end:}))/undefined
2020-01-09 20:52:19 +00:00
"find": &tengo.UserFunction{
Value: func(args ...tengo.Object) (
ret tengo.Object,
err error,
) {
numArgs := len(args)
if numArgs != 1 && numArgs != 2 {
2020-01-09 20:52:19 +00:00
err = tengo.ErrWrongNumArguments
return
}
2020-01-09 20:52:19 +00:00
s1, ok := tengo.ToString(args[0])
if !ok {
2020-01-09 20:52:19 +00:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "string(compatible)",
Found: args[0].TypeName(),
}
return
}
if numArgs == 1 {
m := re.FindStringSubmatchIndex(s1)
if m == nil {
2020-01-09 20:52:19 +00:00
ret = tengo.UndefinedValue
return
}
2020-01-09 20:52:19 +00:00
arr := &tengo.Array{}
for i := 0; i < len(m); i += 2 {
2020-01-09 20:52:19 +00:00
arr.Value = append(arr.Value,
&tengo.ImmutableMap{
Value: map[string]tengo.Object{
"text": &tengo.String{
Value: s1[m[i]:m[i+1]],
},
"begin": &tengo.Int{
Value: int64(m[i]),
},
"end": &tengo.Int{
Value: int64(m[i+1]),
},
}})
}
2020-01-09 20:52:19 +00:00
ret = &tengo.Array{Value: []tengo.Object{arr}}
return
}
2020-01-09 20:52:19 +00:00
i2, ok := tengo.ToInt(args[1])
if !ok {
2020-01-09 20:52:19 +00:00
err = tengo.ErrInvalidArgumentType{
Name: "second",
Expected: "int(compatible)",
Found: args[1].TypeName(),
}
return
}
m := re.FindAllStringSubmatchIndex(s1, i2)
if m == nil {
2020-01-09 20:52:19 +00:00
ret = tengo.UndefinedValue
return
}
2020-01-09 20:52:19 +00:00
arr := &tengo.Array{}
for _, m := range m {
2020-01-09 20:52:19 +00:00
subMatch := &tengo.Array{}
for i := 0; i < len(m); i += 2 {
2020-01-09 20:52:19 +00:00
subMatch.Value = append(subMatch.Value,
&tengo.ImmutableMap{
Value: map[string]tengo.Object{
"text": &tengo.String{
Value: s1[m[i]:m[i+1]],
},
"begin": &tengo.Int{
Value: int64(m[i]),
},
"end": &tengo.Int{
Value: int64(m[i+1]),
},
}})
}
arr.Value = append(arr.Value, subMatch)
}
ret = arr
return
},
},
// replace(src, repl) => string
2020-01-09 20:52:19 +00:00
"replace": &tengo.UserFunction{
Value: func(args ...tengo.Object) (
ret tengo.Object,
err error,
) {
if len(args) != 2 {
2020-01-09 20:52:19 +00:00
err = tengo.ErrWrongNumArguments
return
}
2020-01-09 20:52:19 +00:00
s1, ok := tengo.ToString(args[0])
if !ok {
2020-01-09 20:52:19 +00:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "string(compatible)",
Found: args[0].TypeName(),
}
return
}
2020-01-09 20:52:19 +00:00
s2, ok := tengo.ToString(args[1])
if !ok {
2020-01-09 20:52:19 +00:00
err = tengo.ErrInvalidArgumentType{
Name: "second",
Expected: "string(compatible)",
Found: args[1].TypeName(),
}
return
}
s, ok := doTextRegexpReplace(re, s1, s2)
if !ok {
2020-01-09 20:52:19 +00:00
return nil, tengo.ErrStringLimit
}
2020-01-09 20:52:19 +00:00
ret = &tengo.String{Value: s}
return
},
},
// split(text) => array(string)
// split(text, maxCount) => array(string)
2020-01-09 20:52:19 +00:00
"split": &tengo.UserFunction{
Value: func(args ...tengo.Object) (
ret tengo.Object,
err error,
) {
numArgs := len(args)
if numArgs != 1 && numArgs != 2 {
2020-01-09 20:52:19 +00:00
err = tengo.ErrWrongNumArguments
return
}
2020-01-09 20:52:19 +00:00
s1, ok := tengo.ToString(args[0])
if !ok {
2020-01-09 20:52:19 +00:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "string(compatible)",
Found: args[0].TypeName(),
}
return
}
var i2 = -1
if numArgs > 1 {
2020-01-09 20:52:19 +00:00
i2, ok = tengo.ToInt(args[1])
if !ok {
2020-01-09 20:52:19 +00:00
err = tengo.ErrInvalidArgumentType{
Name: "second",
Expected: "int(compatible)",
Found: args[1].TypeName(),
}
return
}
}
2020-01-09 20:52:19 +00:00
arr := &tengo.Array{}
for _, s := range re.Split(s1, i2) {
2020-01-09 20:52:19 +00:00
arr.Value = append(arr.Value,
&tengo.String{Value: s})
}
ret = arr
return
},
},
},
}
}
// Size-limit checking implementation of regexp.ReplaceAllString.
func doTextRegexpReplace(re *regexp.Regexp, src, repl string) (string, bool) {
idx := 0
out := ""
for _, m := range re.FindAllStringSubmatchIndex(src, -1) {
var exp []byte
exp = re.ExpandString(exp, repl, src, m)
if len(out)+m[0]-idx+len(exp) > tengo.MaxStringLen {
return "", false
}
out += src[idx:m[0]] + string(exp)
idx = m[1]
}
if idx < len(src) {
if len(out)+len(src)-idx > tengo.MaxStringLen {
return "", false
}
out += src[idx:]
}
2020-01-09 20:52:19 +00:00
return out, true
}