Skip to content

Commit cbc4c38

Browse files
committed
Uppdated string functions spec from JEP-14.
1 parent 2ca3ae0 commit cbc4c38

File tree

7 files changed

+32
-13
lines changed

7 files changed

+32
-13
lines changed

functions/find_first.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ returns:
2020
desc: ''
2121
desc: |
2222
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.
23+
If either the `$subject` or the `$sub` argument is an empty string, `find_first()` return `null`.
2324
2425
The `start` and `end` parameters are optional and allow restricting the range within `subject` in which `sub` must be found.
2526
2627
* If `start` is omitted, it defaults to `0` (which is the start of the `subject` string).
2728
* If `end` is omitted, it defaults to `length(subject) - 1` (which is is the end of the `subject` string).
2829
30+
If not omitted, the `$start` and `$end` arguments are expected to be integers. Othewise, an error MUST be raised.
31+
2932
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.
3033
examples:
3134
find_first:

functions/find_last.yml

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,48 @@ args:
99
type: [string]
1010
desc: 'Substring'
1111
optional:
12-
- name: pos
12+
- name: start
1313
type: [number]
1414
desc: 'Position in the subject string where searching should start.'
15+
- name: end
16+
type: [number]
17+
desc: 'Position in the subject string where searching should end.'
1518
returns:
1619
type: number
1720
desc: ''
1821
desc: |
19-
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.
22+
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_last()` returns `null`.
23+
24+
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.
25+
26+
- If `$start` is omitted, it defaults to `0` (which is the start of the `$subject` string).
27+
- If `$end` is omitted, it defaults to `length(subject)` (which is past the end of the `$subject` string).
2028
21-
The `pos` parameter is optional and allows restricting the maximum index within `subject` at which to search for `sub`.
22-
If this is parameter omitted, it defaults to `length(subject) - 1` (which is is the end of the `subject` string).
29+
If not omitted, the `$start` or `$end` arguments are expected to be integers. Otherwise, an error MUST be raised.
2330
2431
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.
2532
examples:
2633
find_last:
2734
context: &subject subject string
2835
args: ['@', 'string']
2936
returns: 8.0
30-
find_last_pos:
37+
find_last_start:
3138
context: *subject
3239
args: ['@', 'string', '`8`']
3340
returns: 8.0
34-
find_last_pos_ends_prematurely:
41+
find_last_start_ends_prematurely:
42+
context: *subject
43+
args: ['@', 'string', '`8`', '`9`']
44+
returns: null
45+
find_last_start_after_occurrence:
3546
context: *subject
36-
args: ['@', 'string', '`7`']
47+
args: ['@', 'string', '`9`']
3748
returns: null
3849
find_last_second_occurrence:
3950
context: *subject
40-
args: ['@', 's', '`8`']
51+
args: ['@', 's', '`1`']
4152
returns: 8.0
4253
find_last_first_occurrence:
4354
context: *subject
44-
args: ['@', 's', '`7`']
55+
args: ['@', 's', '`0`', '`7`']
4556
returns: 0.0

functions/pad_left.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ desc: |
2323
If present, it MUST have length 1, otherwise an error MUST be raised.
2424
2525
If the `subject` string has length greater than or equal to `width`, it is returned unmodified.
26+
27+
If `$width` is not an integer, an error MUST be raised.
2628
examples:
2729
pad_left_less_than_length:
2830
context: &subject string

functions/pad_right.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ desc: |
2323
If present, it MUST have length 1, otherwise an error MUST be raised.
2424
2525
If the `subject` string has length greater than or equal to `width`, it is returned unmodified.
26+
27+
If `$width` is not an integer, an error MUST be raised.
2628
examples:
2729
pad_right_less_than_length:
2830
context: &subject string

functions/replace.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ desc: |
2222
Given the `subject` string, `replace()` replaces ocurrences of the `old` substring with the `new` substring.
2323
2424
The `count` optional parameter specifies how many occurrences of the `old` substring in `subject` are to be replaced.
25-
If this parameter is omitted, all occurrences are replaced. If `count` is negative, an error MUST be raised.
25+
If this parameter is omitted, all occurrences are replaced. If `count` is not an integer or is negative,
2626
2727
The `replace()` function has no effect if `count` is `0`.
2828
examples:

functions/split.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ desc: |
2323
string will be returned.
2424
2525
The `count` optional parameter specifies the maximum number of split points within the `search` string.
26-
If this parameter is omitted, all occurrences are split. If `count` is negative, an error MUST be raised.
26+
If this parameter is omitted, all occurrences are split. If `count` is not an integer or is negative,
27+
an error MUST be raised.
2728
2829
If `count` is equal to `0`, `split()` returns an array containing a single element, the `subject` string.
2930

jep-014-string-functions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Contrary to similar functions found in most popular programming languages, the `
5858
```
5959
int find_last(string $subject, string $sub[, int $start[, int $end]])
6060
```
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`.
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_last()` returns `null`.
6262

6363
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.
6464

@@ -67,7 +67,7 @@ The `$start` and `$end` parameters are optional and allow restricting to the sli
6767

6868
If not omitted, the `$start` or `$end` arguments are expected to be integers. Otherwise, an error MUST be raised.
6969

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.
70+
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.
7171

7272
### Examples
7373

0 commit comments

Comments
 (0)