Skip to content

Commit 8d06ce8

Browse files
committed
Implemented find_last() function
1 parent c198ed6 commit 8d06ce8

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

jmespath/functions.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,19 @@ def _func_find_first(self, text, search, start = 0, end = -1):
321321
pos = text.find(search, start, end)
322322
return pos if pos != -1 else None
323323

324+
@signature(
325+
{'type': 'string'},
326+
{'type': 'string'},
327+
{'type': 'number', 'optional': True},
328+
{'type': 'number', 'optional': True})
329+
def _func_find_last(self, text, search, start = 0, end = -1):
330+
if len(search) == 0:
331+
return None
332+
if end == -1:
333+
end = len(text)
334+
pos = text[start:end].rfind(search)
335+
return start + pos if pos != -1 else None
336+
324337
@signature(
325338
{'type': 'string'},
326339
{'type': 'number'},

tests/compliance/string_functions.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@
3636

3737
{ "expression": "find_last(string, 'string')", "result": 8 },
3838
{ "expression": "find_last(string, 'string', `8`)", "result": 8 },
39-
{ "expression": "find_last(string, 'string', `7`)", "result": null },
40-
{ "expression": "find_last(string, 's', `8`)", "result": 8 },
41-
{ "expression": "find_last(string, 's', `7`)", "result": 0 },
42-
39+
{ "expression": "find_last(string, 'string', `8`, `9`)", "result": null },
40+
{ "expression": "find_last(string, 'string', `9`)", "result": null },
41+
{ "expression": "find_last(string, 's', `1`)", "result": 8 },
42+
{ "expression": "find_last(string, 's', `0`, `7`)", "result": 0 },
43+
4344
{ "expression": "lower('STRING')", "result": "string" },
4445
{ "expression": "upper('string')", "result": "STRING" },
4546

0 commit comments

Comments
 (0)