@@ -1324,6 +1324,47 @@ func builtinSplitLimit(i *interpreter, strv, cv, maxSplitsV value) (value, error
1324
1324
return makeValueArray (res ), nil
1325
1325
}
1326
1326
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
+
1327
1368
func builtinStrReplace (i * interpreter , strv , fromv , tov value ) (value , error ) {
1328
1369
str , err := i .getString (strv )
1329
1370
if err != nil {
@@ -2511,6 +2552,7 @@ var funcBuiltins = buildBuiltinMap([]builtin{
2511
2552
& binaryBuiltin {name : "stripChars" , function : builtinStripChars , params : ast.Identifiers {"str" , "chars" }},
2512
2553
& ternaryBuiltin {name : "substr" , function : builtinSubstr , params : ast.Identifiers {"str" , "from" , "len" }},
2513
2554
& ternaryBuiltin {name : "splitLimit" , function : builtinSplitLimit , params : ast.Identifiers {"str" , "c" , "maxsplits" }},
2555
+ & ternaryBuiltin {name : "splitLimitR" , function : builtinSplitLimitR , params : ast.Identifiers {"str" , "c" , "maxsplits" }},
2514
2556
& ternaryBuiltin {name : "strReplace" , function : builtinStrReplace , params : ast.Identifiers {"str" , "from" , "to" }},
2515
2557
& unaryBuiltin {name : "isEmpty" , function : builtinIsEmpty , params : ast.Identifiers {"str" }},
2516
2558
& binaryBuiltin {name : "equalsIgnoreCase" , function : builtinEqualsIgnoreCase , params : ast.Identifiers {"str1" , "str2" }},
0 commit comments