Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,56 @@ func builtinStrReplace(i *interpreter, strv, fromv, tov value) (value, error) {
return makeValueString(strings.Replace(sStr, sFrom, sTo, -1)), nil
}

func builtinRegexMatch(i *interpreter, regexv, strv value) (value, error) {
regex, err := i.getString(regexv)
if err != nil {
return nil, err
}

str, err := i.getString(strv)
if err != nil {
return nil, err
}

sRegex := regex.getGoString()
sStr := str.getGoString()

match, err := regexp.MatchString(sRegex, sStr)
if err != nil {
return nil, err
}

return makeValueBoolean(match), nil
}

func builtinRegexSubst(i *interpreter, regexv, srcv, replv value) (value, error) {
regex, err := i.getString(regexv)
if err != nil {
return nil, err
}

src, err := i.getString(srcv)
if err != nil {
return nil, err
}

repl, err := i.getString(replv)
if err != nil {
return nil, err
}

sRegex := regex.getGoString()
sSrc := src.getGoString()
sRepl := repl.getGoString()

r, err := regexp.Compile(sRegex)
if err != nil {
return nil, err
}

return makeValueString(r.ReplaceAllString(sSrc, sRepl)), nil
}

func builtinIsEmpty(i *interpreter, strv value) (value, error) {
str, err := i.getString(strv)
if err != nil {
Expand Down Expand Up @@ -2865,6 +2915,8 @@ var funcBuiltins = buildBuiltinMap([]builtin{
&ternaryBuiltin{name: "splitLimit", function: builtinSplitLimit, params: ast.Identifiers{"str", "c", "maxsplits"}},
&ternaryBuiltin{name: "splitLimitR", function: builtinSplitLimitR, params: ast.Identifiers{"str", "c", "maxsplits"}},
&ternaryBuiltin{name: "strReplace", function: builtinStrReplace, params: ast.Identifiers{"str", "from", "to"}},
&binaryBuiltin{name: "regexMatch", function: builtinRegexMatch, params: ast.Identifiers{"regex", "str"}},
&ternaryBuiltin{name: "regexSubst", function: builtinRegexSubst, params: ast.Identifiers{"regex", "src", "repl"}},
&unaryBuiltin{name: "isEmpty", function: builtinIsEmpty, params: ast.Identifiers{"str"}},
&binaryBuiltin{name: "equalsIgnoreCase", function: builtinEqualsIgnoreCase, params: ast.Identifiers{"str1", "str2"}},
&unaryBuiltin{name: "trim", function: builtinTrim, params: ast.Identifiers{"str"}},
Expand Down
2 changes: 2 additions & 0 deletions linter/internal/types/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ func prepareStdlib(g *typeGraph) {
"splitLimit": g.newSimpleFuncType(arrayOfString, "str", "c", "maxsplits"),
"splitLimitR": g.newSimpleFuncType(arrayOfString, "str", "c", "maxsplits"),
"strReplace": g.newSimpleFuncType(stringType, "str", "from", "to"),
"regexMatch": g.newSimpleFuncType(boolType, "regex", "str"),
"regexSubst": g.newSimpleFuncType(stringType, "regex", "src", "repl"),
"asciiUpper": g.newSimpleFuncType(stringType, "str"),
"asciiLower": g.newSimpleFuncType(stringType, "str"),
"stringChars": g.newSimpleFuncType(stringType, "str"),
Expand Down
1 change: 1 addition & 0 deletions testdata/builtinRegexMatch.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
1 change: 1 addition & 0 deletions testdata/builtinRegexMatch.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.regexMatch('\\d+', 'abc123def')
Empty file.
1 change: 1 addition & 0 deletions testdata/builtinRegexMatch2.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
false
1 change: 1 addition & 0 deletions testdata/builtinRegexMatch2.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.regexMatch('\\d+', 'abcdef')
Empty file.
1 change: 1 addition & 0 deletions testdata/builtinRegexSubst.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"abc[123]def[456]ghi"
1 change: 1 addition & 0 deletions testdata/builtinRegexSubst.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.regexSubst('([0-9]+)', 'abc123def456ghi', '[$1]')
Empty file.
1 change: 1 addition & 0 deletions testdata/builtinRegexSubst2.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"hello world"
1 change: 1 addition & 0 deletions testdata/builtinRegexSubst2.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.regexSubst('Hello', 'hello world', 'Hi')
Empty file.
2 changes: 2 additions & 0 deletions testdata/stdlib_smoke_test.golden
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@
4,
5
],
"regexMatch": true,
"regexSubst": "abc[123]def[456]ghi",
"repeat": "foofoofoo",
"reverse": [
"a",
Expand Down
2 changes: 2 additions & 0 deletions testdata/stdlib_smoke_test.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
splitLimit: std.splitLimit(str="a,b,c", c=",", maxsplits=1),
splitLimitR: std.splitLimitR(str="a,b,c", c=",", maxsplits=1),
strReplace: std.strReplace(str="aaa", from="aa", to="bb"),
regexMatch: std.regexMatch(regex="\\d+", str="abc123def"),
regexSubst: std.regexSubst(regex="([0-9]+)", src="abc123def456ghi", repl="[$1]"),
asciiUpper: std.asciiUpper(str="Blah"),
asciiLower: std.asciiLower(str="Blah"),
stringChars: std.stringChars(str="blah"),
Expand Down