Skip to content

Commit 422af98

Browse files
Clamp negative slice start range to prevent out-of-range, and update fmt test golden
Slice is documented to behave similarly to Python. Negative indexes in Python slices are clamped so slice indexes never produce out-of-range runtime errors. Update the 'slice' sugar to do the same. Note that only the starting index needs to be clamped in this way; the ending index can be out-of-range doesn't cause an error, as the loop exit condition compares the current index against the ending index.
1 parent e2e995a commit 422af98

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

stdlib/std.jsonnet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ limitations under the License.
212212
then 0
213213
else
214214
if index < 0
215-
then std.length(indexable) + index
215+
then std.max(0, std.length(indexable) + index)
216216
else index,
217217
end:
218218
if end == null

test_suite/slice.sugar.jsonnet

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ local arrCases = [
108108
input: arr[-1:-1],
109109
output: [],
110110
},
111+
{
112+
input: arr[-6:3],
113+
output: [0, 1, 2],
114+
},
115+
{
116+
input: arr[-100:3],
117+
output: [0, 1, 2],
118+
},
119+
{
120+
input: arr[-100:-90],
121+
output: [],
122+
},
111123
];
112124

113125
local strCases = [
@@ -199,6 +211,18 @@ local strCases = [
199211
input: str[-1:-1],
200212
output: '',
201213
},
214+
{
215+
input: str[-6:3],
216+
output: '012',
217+
},
218+
{
219+
input: str[-100:3],
220+
output: '012',
221+
},
222+
{
223+
input: str[-100:-90],
224+
output: '',
225+
},
202226
];
203227

204228
std.foldl(

test_suite/slice.sugar.jsonnet.fmt.golden

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,34 @@ local arrCases = [
9292
input: (arr)[2:1000],
9393
output: [2, 3, 4, 5],
9494
},
95-
95+
{
96+
input: arr[-2:],
97+
output: [4, 5],
98+
},
99+
{
100+
input: arr[:-3],
101+
output: [0, 1, 2],
102+
},
103+
{
104+
input: arr[-3:-1],
105+
output: [3, 4],
106+
},
107+
{
108+
input: arr[-1:-1],
109+
output: [],
110+
},
111+
{
112+
input: arr[-6:3],
113+
output: [0, 1, 2],
114+
},
115+
{
116+
input: arr[-100:3],
117+
output: [0, 1, 2],
118+
},
119+
{
120+
input: arr[-100:-90],
121+
output: [],
122+
},
96123
];
97124

98125
local strCases = [
@@ -168,6 +195,34 @@ local strCases = [
168195
input: (str)[2:1000],
169196
output: '2345',
170197
},
198+
{
199+
input: str[-2:],
200+
output: '45',
201+
},
202+
{
203+
input: str[:-3],
204+
output: '012',
205+
},
206+
{
207+
input: str[-3:-1],
208+
output: '34',
209+
},
210+
{
211+
input: str[-1:-1],
212+
output: '',
213+
},
214+
{
215+
input: str[-6:3],
216+
output: '012',
217+
},
218+
{
219+
input: str[-100:3],
220+
output: '012',
221+
},
222+
{
223+
input: str[-100:-90],
224+
output: '',
225+
},
171226
];
172227

173228
std.foldl(

0 commit comments

Comments
 (0)