Skip to content

feat: allow negative index/end on std.slice #1093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 2, 2024
Merged
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
4 changes: 4 additions & 0 deletions doc/_stdlib_gen/stdlib-content.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,10 @@ local html = import 'html.libsonnet';
input: 'std.slice("jsonnet", 0, 4, 1)',
output: std.slice('jsonnet', 0, 4, 1),
},
{
input: 'std.slice("jsonnet", -3, null, null)',
output: std.slice('jsonnet', -3, null, null),
},
],
},
{
Expand Down
21 changes: 15 additions & 6 deletions stdlib/std.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,27 @@ limitations under the License.
{
indexable: indexable,
index:
if index == null then 0
else index,
if index == null
then 0
else
if index < 0
then std.max(0, std.length(indexable) + index)
else index,
end:
if end == null then std.length(indexable)
else end,
if end == null
then std.length(indexable)
else
if end < 0
then std.length(indexable) + end
else end,
step:
if step == null then 1
if step == null
then 1
else step,
length: std.length(indexable),
type: std.type(indexable),
};
assert invar.index >= 0 && invar.end >= 0 && invar.step >= 0 : 'got [%s:%s:%s] but negative index, end, and steps are not supported' % [invar.index, invar.end, invar.step];
assert invar.step >= 0 : 'got [%s:%s:%s] but negative steps are not supported' % [invar.index, invar.end, invar.step];
assert step != 0 : 'got %s but step must be greater than 0' % step;
assert std.isString(indexable) || std.isArray(indexable) : 'std.slice accepts a string or an array, but got: %s' % std.type(indexable);
local build(slice, cur) =
Expand Down
57 changes: 56 additions & 1 deletion test_suite/slice.sugar.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,34 @@ local arrCases = [
input: (arr)[2:1000],
output: [2, 3, 4, 5],
},

{
input: arr[-2:],
output: [4, 5],
},
{
input: arr[:-3],
output: [0, 1, 2],
},
{
input: arr[-3:-1],
output: [3, 4],
},
{
input: arr[-1:-1],
output: [],
},
{
input: arr[-6:3],
output: [0, 1, 2],
},
{
input: arr[-100:3],
output: [0, 1, 2],
},
{
input: arr[-100:-90],
output: [],
},
];

local strCases = [
Expand Down Expand Up @@ -168,6 +195,34 @@ local strCases = [
input: (str)[2:1000],
output: '2345',
},
{
input: str[-2:],
output: '45',
},
{
input: str[:-3],
output: '012',
},
{
input: str[-3:-1],
output: '34',
},
{
input: str[-1:-1],
output: '',
},
{
input: str[-6:3],
output: '012',
},
{
input: str[-100:3],
output: '012',
},
{
input: str[-100:-90],
output: '',
},
];

std.foldl(
Expand Down
57 changes: 56 additions & 1 deletion test_suite/slice.sugar.jsonnet.fmt.golden
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,34 @@ local arrCases = [
input: (arr)[2:1000],
output: [2, 3, 4, 5],
},

{
input: arr[-2:],
output: [4, 5],
},
{
input: arr[:-3],
output: [0, 1, 2],
},
{
input: arr[-3:-1],
output: [3, 4],
},
{
input: arr[-1:-1],
output: [],
},
{
input: arr[-6:3],
output: [0, 1, 2],
},
{
input: arr[-100:3],
output: [0, 1, 2],
},
{
input: arr[-100:-90],
output: [],
},
];

local strCases = [
Expand Down Expand Up @@ -168,6 +195,34 @@ local strCases = [
input: (str)[2:1000],
output: '2345',
},
{
input: str[-2:],
output: '45',
},
{
input: str[:-3],
output: '012',
},
{
input: str[-3:-1],
output: '34',
},
{
input: str[-1:-1],
output: '',
},
{
input: str[-6:3],
output: '012',
},
{
input: str[-100:3],
output: '012',
},
{
input: str[-100:-90],
output: '',
},
];

std.foldl(
Expand Down