Skip to content

Commit 661f72f

Browse files
authored
Merge pull request #9 from jmespath-community/jep/string-slices
JEP-15 String Slices
2 parents bbe7300 + a89da7f commit 661f72f

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

jmespath/visitor.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ def visit_index_expression(self, node, value):
216216
return result
217217

218218
def visit_slice(self, node, value):
219+
if isinstance(value, string_type):
220+
start = node['children'][0]
221+
end = node['children'][1]
222+
step = node['children'][2]
223+
return value[start:end:step]
224+
219225
if not isinstance(value, list):
220226
return None
221227
s = slice(*node['children'])
@@ -271,6 +277,17 @@ def visit_pipe(self, node, value):
271277

272278
def visit_projection(self, node, value):
273279
base = self.visit(node['children'][0], value)
280+
281+
allow_string = False
282+
first_child = node['children'][0]
283+
if first_child['type'] == 'index_expression':
284+
nested_children = first_child['children']
285+
if len(nested_children) > 1 and nested_children[1]['type'] == 'slice':
286+
allow_string = True
287+
288+
if isinstance(base, string_type) and allow_string:
289+
return base
290+
274291
if not isinstance(base, list):
275292
return None
276293
collected = []

tests/compliance/slice.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,12 @@
184184
"result": []
185185
}
186186
]
187+
}, {
188+
"given": null,
189+
"cases": [
190+
{
191+
"expression": "'e\u0301le\u0301ment'[::-1]",
192+
"result": "tnem\u0301el\u0301e"
193+
}
194+
]
187195
}]

0 commit comments

Comments
 (0)