Skip to content

Commit 81c8242

Browse files
sneivandtsparkprime
authored andcommitted
add std.find and std.findSubstr
1 parent edfcffc commit 81c8242

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

doc/ref/stdlib.html

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,28 @@ <h4 id="substr">std.substr(s, from, len)</h4>
446446
</div>
447447

448448

449+
<div class="hgroup">
450+
<div class="hgroup-inline">
451+
<div class="panel">
452+
<h4 id="substr">std.findSubstr(pat, str)</h4>
453+
</div>
454+
<div style="clear: both"></div>
455+
</div>
456+
</div>
457+
458+
<div class="hgroup">
459+
<div class="hgroup-inline">
460+
<div class="panel">
461+
<p>
462+
Returns an array that contains the indexes of all occurances of <code>pat</code> in
463+
<code>str</code>.
464+
</p>
465+
</div>
466+
<div style="clear: both"></div>
467+
</div>
468+
</div>
469+
470+
449471
<div class="hgroup">
450472
<div class="hgroup-inline">
451473
<div class="panel">
@@ -1221,6 +1243,28 @@ <h4 id="count">std.count(arr, x)</h4>
12211243
</div>
12221244

12231245

1246+
<div class="hgroup">
1247+
<div class="hgroup-inline">
1248+
<div class="panel">
1249+
<h4 id="count">std.find(value, arr)</h4>
1250+
</div>
1251+
<div style="clear: both"></div>
1252+
</div>
1253+
</div>
1254+
1255+
<div class="hgroup">
1256+
<div class="hgroup-inline">
1257+
<div class="panel">
1258+
<p>
1259+
Returns an array that contains the indexes of all occurances of <code>value</code> in
1260+
<code>arr</code>.
1261+
</p>
1262+
</div>
1263+
<div style="clear: both"></div>
1264+
</div>
1265+
</div>
1266+
1267+
12241268
<div class="hgroup">
12251269
<div class="hgroup-inline">
12261270
<div class="panel">
@@ -1277,7 +1321,7 @@ <h4 id="filterMap">std.filterMap(filter_func, map_func, arr)</h4>
12771321
<div class="panel">
12781322
<p>
12791323
This is primarily used to desugar array comprehension syntax. It first filters, then maps
1280-
thte given array, using the two functions provided.
1324+
the given array, using the two functions provided.
12811325
</p>
12821326
</div>
12831327
<div style="clear: both"></div>

stdlib/std.jsonnet

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,4 +1245,23 @@ limitations under the License.
12451245
if isContent(std.prune(a[x]))
12461246
} else
12471247
a,
1248+
1249+
findSubstr(pat, str)::
1250+
if std.type(pat) != 'string' then
1251+
error 'findSubstr first parameter should be a string, got ' + std.type(pat)
1252+
else if std.type(str) != 'string' then
1253+
error 'findSubstr second parameter should be a string, got ' + std.type(str)
1254+
else
1255+
local pat_len = std.length(pat);
1256+
local str_len = std.length(str);
1257+
if pat_len == 0 || str_len == 0 || pat_len > str_len then
1258+
[]
1259+
else
1260+
std.filter(function(i) str[i:i+pat_len] == pat, std.range(0, str_len - pat_len)),
1261+
1262+
find(value, arr)::
1263+
if std.type(arr) != 'array' then
1264+
error 'find second parameter should be an array, got ' + std.type(arr)
1265+
else
1266+
std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),
12481267
}

test_suite/stdlib.jsonnet

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,4 +590,20 @@ std.assertEqual(std.trace('', []), []) &&
590590
std.assertEqual(std.trace('', [{ a: 'b' }, { a: 'b' }]), [{ a: 'b' }, { a: 'b' }]) &&
591591
std.assertEqual(std.trace('Some Trace Message', { a: {} }), { a: {} }) &&
592592

593+
std.assertEqual(std.findSubstr('', 'a'), []) &&
594+
std.assertEqual(std.findSubstr('aa', ''), []) &&
595+
std.assertEqual(std.findSubstr('aa', 'bb'), []) &&
596+
std.assertEqual(std.findSubstr('aa', 'a'), []) &&
597+
std.assertEqual(std.findSubstr('aa', 'aa'), [0]) &&
598+
std.assertEqual(std.findSubstr('aa', 'bbaabaaa'), [2, 5, 6]) &&
599+
600+
std.assertEqual(std.find(null, [null]), [0]) &&
601+
std.assertEqual(std.find([], [[]]), [0]) &&
602+
std.assertEqual(std.find({}, [{}]), [0]) &&
603+
std.assertEqual(std.find('a', []), []) &&
604+
std.assertEqual(std.find('a', ['b']), []) &&
605+
std.assertEqual(std.find('a', ['a']), [0]) &&
606+
std.assertEqual(std.find('a', ['a', ['a'], 'b', 'a']), [0, 3]) &&
607+
std.assertEqual(std.find(['a'], [['a']]), [0]) &&
608+
593609
true

0 commit comments

Comments
 (0)