Skip to content

Commit 2ca3ae0

Browse files
Reworked find_last() functions for consistency (#119)
* Reworked find_last() functions for consistency and symmetry to find_first() * Fixed incorrect compliance test. * Fixed typo in number literal * Update jep-014-string-functions.md Co-authored-by: Richard Gibson <[email protected]> * Documented find_first() and find_last() corner cases. * Added examples to illustrate handling of negative indices. * Update jep-014-string-functions.md Co-authored-by: Richard Gibson <[email protected]> * Update jep-014-string-functions.md Co-authored-by: Richard Gibson <[email protected]> * Update jep-014-string-functions.md Co-authored-by: Richard Gibson <[email protected]> * Documented parameters expected to be integers. Co-authored-by: Richard Gibson <[email protected]>
1 parent 6eba5e0 commit 2ca3ae0

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

jep-014-string-functions.md

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ _Functions can ~~either~~ have a specific arity, **a range of valid – minimum
2525
```
2626
int find_first(string $subject, string $sub[, int $start[, int $end]])
2727
```
28-
Given the `$subject` string, `find_first()` returns the zero-based index of the first occurence where the `$sub` substring appears in `$subject` or `null` if it does not appear.
28+
Given the `$subject` string, `find_first()` returns the zero-based index of the first occurence where the `$sub` substring appears in `$subject` or `null` if it does not appear. If either the `$subject` or the `$sub` argument is an empty string, `find_first()` returns `null`.
2929

30-
The `$start` and `$end` parameters are optional and allow restricting the range within `$subject` in which `$sub` must be found.
30+
The `$start` and `$end` parameters are optional and allow restricting to the slice `[$start:$end]` the range within `$subject` in which `$sub` must be found.
3131

3232
- If `$start` is omitted, it defaults to `0` (which is the start of the `$subject` string).
3333
- If `$end` is omitted, it defaults to `length(subject)` (which is past the end of the `$subject` string).
@@ -44,34 +44,43 @@ Contrary to similar functions found in most popular programming languages, the `
4444
| `"subject string"` | `` find_first(@, 'string', `0`) `` | `8`
4545
| `"subject string"` | `` find_first(@, 'string', `0`, `14`) `` | `8`
4646
| `"subject string"` | `` find_first(@, 'string', `-99`, `100`) `` | `8`
47+
| `"subject string"` | `` find_first(@, 'string', `-6`) `` | `8`
4748
| `"subject string"` | `` find_first(@, 'string', `0`, `13`) `` | `null`
4849
| `"subject string"` | `` find_first(@, 'string', `8`) `` | `8`
50+
| `"subject string"` | `` find_first(@, 'string', `8`, `11`) `` | `null`
4951
| `"subject string"` | `` find_first(@, 'string', `9`) `` | `null`
5052
| `"subject string"` | `` find_first(@, 's') `` | `0`
51-
| `"subject string"` | `` find_first(@, 's', 1) `` | `8`
53+
| `"subject string"` | `` find_first(@, 's', `1`) `` | `8`
5254
| `"subject string"` | `` find_first(@, '') `` | `null`
5355

5456
### find_last
5557

5658
```
57-
int find_last(string $subject, string $sub[, int $pos])
59+
int find_last(string $subject, string $sub[, int $start[, int $end]])
5860
```
59-
Given the `$subject` string, `find_last()` returns the zero-based index of the last occurence where the `$sub` substring appears in `$subject` or `null` if it does not appear.
61+
Given the `$subject` string, `find_last()` returns the zero-based index of the last occurence where the `$sub` substring appears in `$subject` or `null` if it does not appear. If either the `$subject` or the `$sub` argument is an empty string, `find_first()` returns `null`.
6062

61-
The `$pos` parameter is optional and allows restricting the maximum index within `$subject` at which to search for `$sub`.
62-
If this is parameter omitted, it defaults to `length(subject) - 1` (which is is the end of the `$subject` string).
63+
The `$start` and `$end` parameters are optional and allow restricting to the slice `[$start:$end]` the range within `$subject` in which `$sub` must be found.
6364

64-
Contrary to similar functions found in most popular programming languages, the `find_last()` function does not return `-1` if no occurrence of the substring can be found. Instead, it returns `null` for consistency reasons with how JMESPath behaves.
65+
- If `$start` is omitted, it defaults to `0` (which is the start of the `$subject` string).
66+
- If `$end` is omitted, it defaults to `length(subject)` (which is past the end of the `$subject` string).
67+
68+
If not omitted, the `$start` or `$end` arguments are expected to be integers. Otherwise, an error MUST be raised.
69+
70+
Contrary to similar functions found in most popular programming languages, the `find_first()` function does not return `-1` if no occurrence of the substring can be found. Instead, it returns `null` for consistency reasons with how JMESPath behaves.
6571

6672
### Examples
6773

6874
| Given | Expression | Result
6975
|---|---|---
7076
| `"subject string"` | `` find_last(@, 'string') `` | `8`
71-
| `"subject string"` | `` find_last(@, 'string', 8) `` | `8`
72-
| `"subject string"` | `` find_last(@, 'string', 7) `` | `null`
73-
| `"subject string"` | `` find_last(@, 's', 8) `` | `8`
74-
| `"subject string"` | `` find_last(@, 's', 7) `` | `0`
77+
| `"subject string"` | `` find_last(@, 'string', `8`) `` | `8`
78+
| `"subject string"` | `` find_last(@, 'string', `8`, `9`) `` | `null`
79+
| `"subject string"` | `` find_last(@, 'string', `9`) `` | `null`
80+
| `"subject string"` | `` find_last(@, 's') `` | `0`
81+
| `"subject string"` | `` find_last(@, 's', `1`) `` | `8`
82+
| `"subject string"` | `` find_last(@, 's', `0`, `7`) `` | `0`
83+
| `"subject string"` | `` find_last(@, '') `` | `null`
7584

7685
### lower
7786

0 commit comments

Comments
 (0)