Skip to content

Commit fce0722

Browse files
authored
Merge pull request #115 from gibson042/2022-10-string-functions-editorial
[string functions] Editorial improvements
2 parents 3f784e3 + 6e5c199 commit fce0722

File tree

7 files changed

+48
-43
lines changed

7 files changed

+48
-43
lines changed

functions/find_first.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ returns:
1919
type: number
2020
desc: ''
2121
desc: |
22-
Given the `subject` string, `find_first()` returns the index of the first occurence where the `sub` substring appears in `subject` or `null`.
22+
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.
2323
24-
The `start` and `end` parameters are optional and allow to select where `find_first()` must perform its search within `subject`.
24+
The `start` and `end` parameters are optional and allow restricting the range within `subject` in which `sub` must be found.
2525
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) - 1` which is is the end of the `subject` string.
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) - 1` (which is is the end of the `subject` string).
28+
29+
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.
2830
examples:
2931
find_first:
3032
context: &subject subject string
@@ -61,4 +63,4 @@ examples:
6163
find_first_second_occurrence:
6264
context: *subject
6365
args: ['@', 's', '`1`']
64-
returns: 8.0
66+
returns: 8.0

functions/find_last.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ returns:
1616
type: number
1717
desc: ''
1818
desc: |
19-
Given the `subject` string, `find_last()` returns the index of the last occurence where the `sub` substring appears in `subject` or `null`.
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.
2020
21-
The `pos` parameter is optional and allow to select where `find_first()` must perform its search within `subject`.
22-
If this is parameter omitted, it defaults to `length(subject) - 1` which is is the end of the `subject` string.
23-
`find_last()` then searches backwards in the `subject` string for the first occurrence of the `sub` substring.
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).
23+
24+
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.
2425
examples:
2526
find_last:
2627
context: &subject subject string
@@ -41,4 +42,4 @@ examples:
4142
find_last_first_occurrence:
4243
context: *subject
4344
args: ['@', 's', '`7`']
44-
returns: 0.0
45+
returns: 0.0

functions/pad_left.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ returns:
1616
type: string
1717
desc: ''
1818
desc: |
19-
Given the `subject` string, `pad_left()` adds `width - length(subject)` characters to the beginning of the string
20-
and returns a string of length `width`.
19+
Given the `subject` string, `pad_left()` adds characters to the beginning and returns a string of length at least `width`.
2120
22-
The `pad` optional string parameter specifies the padding character. If this parameter is omitted, `pad_left()`
23-
ad one or more ASCII space characters to the beginning of the string. If present, the `pad` string must have a
24-
single character, otherwise an error MUST be raised.
21+
The `pad` optional string parameter specifies the padding character.
22+
If omitted, it defaults to an ASCII space (U+0020).
23+
If present, it MUST have length 1, otherwise an error MUST be raised.
2524
26-
The `pad_left()` function has no effect if `width` is less than, or equal to the length of the `subject` string.
25+
If the `subject` string has length greater than or equal to `width`, it is returned unmodified.
2726
examples:
2827
pad_left_less_than_length:
2928
context: &subject string

functions/pad_right.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ returns:
1616
type: string
1717
desc: ''
1818
desc: |
19-
Given the `subject` string, `pad_right()` adds `width - length(subject)` characters to the end of the string
20-
and returns a string of length `width`.
19+
Given the `subject` string, `pad_right()` adds characters to the end and returns a string of length at least `width`.
2120
22-
The `pad` optional string parameter specifies the padding character. If this parameter is omitted, `pad_right()`
23-
ad one or more ASCII space characters to the end of the string. If present, the `pad` string must have a
24-
single character, otherwise an error MUST be raised.
21+
The `pad` optional string parameter specifies the padding character.
22+
If omitted, it defaults to an ASCII space (U+0020).
23+
If present, it MUST have length 1, otherwise an error MUST be raised.
2524
26-
The `pad_right()` function has no effect if `width` is less than, or equal to the length of the `subject` string.
25+
If the `subject` string has length greater than or equal to `width`, it is returned unmodified.
2726
examples:
2827
pad_right_less_than_length:
2928
context: &subject string

functions/replace.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ returns:
1919
type: string
2020
desc: ''
2121
desc: |
22-
Given the `subject` string, `replace()` replaces all ocurrences of the `old` substring with the `new` substring.
22+
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.
2525
If this parameter is omitted, all occurrences are replaced. If `count` is negative, an error MUST be raised.

functions/split.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ desc: |
2222
If `subject` contains no occurrences of the `search` text, an array containing just the original `subject`
2323
string will be returned.
2424
25-
The `count` optional parameter specifies how many occurrences of the `search` string are split.
25+
The `count` optional parameter specifies the maximum number of split points within the `search` string.
2626
If this parameter is omitted, all occurrences are split. If `count` is negative, an error MUST be raised.
2727
2828
If `count` is equal to `0`, `split()` returns an array containing a single element, the `subject` string.

jep-014-string-functions.md

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ This JEP introduces a core set of useful string manipulation functions. Those fu
1919
```
2020
int find_first(string $subject, string $sub[, int $start[, int $end]])
2121
```
22-
Given the `$subject` string, `find_first()` returns the index of the first occurence where the `$sub` substring appears in `$subject` or `null`.
22+
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.
2323

24-
The `$start` and `$end` parameters are optional and allow to select where `find_first()` must perform its search within `$subject`.
24+
The `$start` and `$end` parameters are optional and allow restricting the range within `$subject` in which `$sub` must be found.
2525

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) -1` which is the end of the string.
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) - 1` (which is is the end of the `$subject` string).
2828

29-
Contrary to similar functions found in most popular programming languages, the `find_first()` function does not return `-1` if the occurrence of the `$sub` substring cannot be found. Instead, it returns `null` for consistency reasons with how JMESPath behaves.
29+
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.
3030

3131
### Examples
3232

@@ -47,13 +47,12 @@ Contrary to similar functions found in most popular programming languages, the `
4747
```
4848
int find_last(string $subject, string $sub[, int $pos])
4949
```
50-
Given the `$subject` string, `find_last()` returns the index of the last occurence where the `$sub` substring appears in `$subject` or `null`.
50+
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.
5151

52-
The `$pos` optional parameter allow to select where `find_last()` must perform its search within `$subject`.
52+
The `$pos` parameter is optional and allows restricting the maximum index within `$subject` at which to search for `$sub`.
53+
If this is parameter omitted, it defaults to `length(subject) - 1` (which is is the end of the `$subject` string).
5354

54-
If this parameter is omitted, It defaults to `length($subject) - 1` which is the end of the `$subject` string. `find_last()` then searches backwards in the `$subject` string for the first occurrence of `$sub`.
55-
56-
Likewise, the `find_last()` function does not return `-1` if the occurrence of the `$sub` substring cannot be found. Instead, it returns `null` for consistency reasons with how JMESPath behaves.
55+
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.
5756

5857
| Given | Expression | Result
5958
|---|---|---
@@ -82,11 +81,13 @@ Returns the lowercase `$subject` string.
8281
string pad_left(string $subject, number $width[, string $pad])
8382
```
8483

85-
Given the `$subject` string, `pad_left()` adds `$width - length($subject)` characters to the beginning of the string and returns a string of length `$width`.
84+
Given the `$subject` string, `pad_left()` adds characters to the beginning and returns a string of length at least `$width`.
8685

87-
The `$pad` optional string parameter specifies the padding character to use. If this parameter is omitted, `pad_left()` adds one of more ASCII space characters to the beginning of the string. If present, the `$pad` string must have a single character, otherwise, an error MUST be raised.
86+
The `$pad` optional string parameter specifies the padding character.
87+
If omitted, it defaults to an ASCII space (U+0020).
88+
If present, it MUST have length 1, otherwise an error MUST be raised.
8889

89-
The `pad_left()` function has no effect if `$width` is less than, or equal to the length of the ` $subject` string.
90+
If the `$subject` string has length greater than or equal to `$width`, it is returned unmodified.
9091

9192
### Examples
9293

@@ -103,11 +104,13 @@ The `pad_left()` function has no effect if `$width` is less than, or equal to th
103104
string pad_right(string $subject, number $width[, string $pad])
104105
```
105106

106-
Given the `$subject` string, `pad_right()` adds `$width - length($subject)` characters to the end of the string and returns a string of length `$width`.
107+
Given the `$subject` string, `pad_right()` adds characters to the end and returns a string of length at least `$width`.
107108

108-
The `$pad` optional string parameter specifies the padding character to use. If this parameter is omitted, `pad_right()` adds ASCII space characters to the end of the string. If present, the `$pad` string must have a single character, otherwise, an error MUST be raised.
109+
The `$pad` optional string parameter specifies the padding character.
110+
If omitted, it defaults to an ASCII space (U+0020).
111+
If present, it MUST have length 1, otherwise an error MUST be raised.
109112

110-
The `pad_right()` function has no effect if `$width` is less than, or equal to the length of the ` $subject` string.
113+
If the `$subject` string has length greater than or equal to `$width`, it is returned unmodified.
111114

112115
### Examples
113116

@@ -123,7 +126,7 @@ The `pad_right()` function has no effect if `$width` is less than, or equal to t
123126
```
124127
string replace(string $subject, string $old, string $new[, number $count])
125128
```
126-
Given the `$subject` string, `replace()` replaces all occurrences of the `$old` substring with the `$new` substring.
129+
Given the `$subject` string, `replace()` replaces occurrences of the `$old` substring with the `$new` substring.
127130

128131
The `$count` optional parameter specifies how many occurrences of the `$old` substring in `$subject` are replaced. If this parameter is omitted, all occurrences are replaced. If `$count` is negative, an error MUST be raised.
129132

@@ -145,11 +148,12 @@ The `replace()` function has no effect if `$count` is `0`.
145148
array[string] split(string $subject, string $search[, number $count])
146149
```
147150

148-
Given string `$subject`, `split()` breaks on occurrences of the string `$search` and returns an array.
151+
Given the `$subject` string, `split()` breaks on ocurrences of the string `$search` and returns an array.
149152

150153
The `split()` function returns an array containing each partial string between occurrences of `$search`. If `$subject` contains no occurrences of the `$search` string, an array containing just the original `$subject` string will be returned.
151154

152-
The `$count` optional parameter specifies how many occurrences of the `$search` string are split. If this parameter is omitted, all occurrences are split. If `$count` is negative, and error MUST be raised.
155+
The `$count` optional parameter specifies the maximum number of split points within the `$search` string.
156+
If this parameter is omitted, all occurrences are split. If `$count` is negative, an error MUST be raised.
153157

154158
If `$count` is equal to `0`, `split()` returns an array containing a single element, the `$subject` string.
155159

0 commit comments

Comments
 (0)