Skip to content

Commit fa70aa4

Browse files
authored
feat: implement std.splitLimitR (#745)
1 parent e544339 commit fa70aa4

22 files changed

+94
-0
lines changed

builtins.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,47 @@ func builtinSplitLimit(i *interpreter, strv, cv, maxSplitsV value) (value, error
13241324
return makeValueArray(res), nil
13251325
}
13261326

1327+
func builtinSplitLimitR(i *interpreter, strv, cv, maxSplitsV value) (value, error) {
1328+
str, err := i.getString(strv)
1329+
if err != nil {
1330+
return nil, err
1331+
}
1332+
c, err := i.getString(cv)
1333+
if err != nil {
1334+
return nil, err
1335+
}
1336+
maxSplits, err := i.getInt(maxSplitsV)
1337+
if err != nil {
1338+
return nil, err
1339+
}
1340+
if maxSplits < -1 {
1341+
return nil, i.Error(fmt.Sprintf("std.splitLimitR third parameter should be -1 or non-negative, got %v", maxSplits))
1342+
}
1343+
sStr := str.getGoString()
1344+
sC := c.getGoString()
1345+
if len(sC) < 1 {
1346+
return nil, i.Error(fmt.Sprintf("std.splitLimitR second parameter should have length 1 or greater, got %v", len(sC)))
1347+
}
1348+
1349+
count := strings.Count(sStr, sC)
1350+
if maxSplits > -1 && count > maxSplits {
1351+
count = maxSplits
1352+
}
1353+
strs := make([]string, count+1)
1354+
for i := count; i > 0; i-- {
1355+
index := strings.LastIndex(sStr, sC)
1356+
strs[i] = sStr[index+len(sC):]
1357+
sStr = sStr[:index]
1358+
}
1359+
strs[0] = sStr
1360+
res := make([]*cachedThunk, len(strs))
1361+
for i := range strs {
1362+
res[i] = readyThunk(makeValueString(strs[i]))
1363+
}
1364+
1365+
return makeValueArray(res), nil
1366+
}
1367+
13271368
func builtinStrReplace(i *interpreter, strv, fromv, tov value) (value, error) {
13281369
str, err := i.getString(strv)
13291370
if err != nil {
@@ -2511,6 +2552,7 @@ var funcBuiltins = buildBuiltinMap([]builtin{
25112552
&binaryBuiltin{name: "stripChars", function: builtinStripChars, params: ast.Identifiers{"str", "chars"}},
25122553
&ternaryBuiltin{name: "substr", function: builtinSubstr, params: ast.Identifiers{"str", "from", "len"}},
25132554
&ternaryBuiltin{name: "splitLimit", function: builtinSplitLimit, params: ast.Identifiers{"str", "c", "maxsplits"}},
2555+
&ternaryBuiltin{name: "splitLimitR", function: builtinSplitLimitR, params: ast.Identifiers{"str", "c", "maxsplits"}},
25142556
&ternaryBuiltin{name: "strReplace", function: builtinStrReplace, params: ast.Identifiers{"str", "from", "to"}},
25152557
&unaryBuiltin{name: "isEmpty", function: builtinIsEmpty, params: ast.Identifiers{"str"}},
25162558
&binaryBuiltin{name: "equalsIgnoreCase", function: builtinEqualsIgnoreCase, params: ast.Identifiers{"str1", "str2"}},

linter/internal/types/stdlib.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func prepareStdlib(g *typeGraph) {
9090
"rstripChars": g.newSimpleFuncType(stringType, "str", "chars"),
9191
"split": g.newSimpleFuncType(arrayOfString, "str", "c"),
9292
"splitLimit": g.newSimpleFuncType(arrayOfString, "str", "c", "maxsplits"),
93+
"splitLimitR": g.newSimpleFuncType(arrayOfString, "str", "c", "maxsplits"),
9394
"strReplace": g.newSimpleFuncType(stringType, "str", "from", "to"),
9495
"asciiUpper": g.newSimpleFuncType(stringType, "str"),
9596
"asciiLower": g.newSimpleFuncType(stringType, "str"),

testdata/builtinSplitLimitR.golden

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
"foo,bar",
3+
"baz",
4+
"qux"
5+
]

testdata/builtinSplitLimitR.jsonnet

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
std.splitLimitR('foo,bar,baz,qux', ',', 2)

testdata/builtinSplitLimitR.linter.golden

Whitespace-only changes.

testdata/builtinSplitLimitR2.golden

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
"foo,bar,baz,qux"
3+
]

testdata/builtinSplitLimitR2.jsonnet

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
std.splitLimitR('foo,bar,baz,qux', ',', 0)

testdata/builtinSplitLimitR2.linter.golden

Whitespace-only changes.

testdata/builtinSplitLimitR3.golden

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
"foo",
3+
"bar",
4+
"baz",
5+
"qux"
6+
]

testdata/builtinSplitLimitR3.jsonnet

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
std.splitLimitR('foo,bar,baz,qux', ',', -1)

0 commit comments

Comments
 (0)