Closed springcomp closed 1 year ago
Given a
start
,stop
, andstep
value, the sub elements in an array or characters in a string are extracted as follows
What is the definition of "character"? Grammar and following sections imply that a string is a sequence of code points, a definition which is affirmed by the description of length
/reverse
/sort
/etc. (even though the in-page jmespath.js implementation of those functions incorrectly treats each supplementary plane code point [i.e., U+10000 through U+10FFFF] as if it were a sequence of two surrogate code points [i.e., a code point from U+D800 through U+DBFF followed by a code point from U+DC00 through U+DFFF]—https://github.com/jmespath-community/jmespath.test/issues/2 ).
@gibson042 see my answer.
String Slices
Abstract
The original JEP 5 introduced
slice-expression
in the grammar to slice specific portions of an array. While the syntax was specifically designed to operate on strings, there is no actual limitation in the grammar to extend this behaviour to string.This JEP introduces changes to allow
slice-expression
to operate on string types and act like a more powerfulsubstring()
function.Motivation
String manipulation functions are a frequently requested feature to be added to JMESPath. While introducing a whole host of string manipulation functions will certainly be proposed at some point, slicing strings is an easy extension to JMESPath that does not require any grammar change and is fully backwards compatible.
Slices
_This section outline word changes to the Slices documentation of the grammar in bold._
A slice expression allows you to select a subset of an array or string. A slice has a
start
,stop
, andstep
value. The general form of a slice is[start:stop:step]
, but each component is optional and can be omitted.Given a
start
,stop
, andstep
value, the sub elements in an array or characters in a string are extracted as follows:start
.end - 1
.step
value determines how many indices to skip after each element is selected from the array or each character is selected from the string. The defaultstep
value of1
will not skip any indices and will return a contiguous subset of the original array or a substring of the original string. Astep
value greater than1
will skip indices while extracting elements from an array or characters from a string. For instance, astep
value of2
will skip every other element or character. Negativestep
values start from the end of the array or string and extract elements or characters in reverse order.Slice expressions adhere to the following rules:
start
position is given, it is calculated as the total length of the array or string plus the given start position.start
position is given, it is assumed to be0
if the givenstep
is greater than0
or the end of the array or string if the givenstep
is less than0
.stop
position is given, it is calculated as the total length of the array or string plus the givenstop
position.stop
position is given, it is assumed to be the length of the array or string if the givenstep
is greater than0
or0
if the givenstep
is less than0
.step
is omitted, it it assumed to be1
.step
is0
, an error MUST be raised.null
.Examples
Slicing operates on a strings exactly as if a string were thought of as an array of characters.
search( foo[0:4], {"foo": "hello, world!" } -> "hell"
search( [::], 'raw-string') -> "raw-string"
search( [::2], 'raw-string') -> "rwsrn"
search( [::-1], 'raw-string') -> "gnirts-war"
Compliance tests
A new
string_slices.json
file will be added to the compliance test suite.