Description
Description
Name of functionality: Slice
Signature: slice(string, start(optional)= 1 or len(string), end(optional)=len(string) or 1, stride(optional)= 1, include_start(optional)= .true.)
Output: a new string_type object
Traverses the input string from start index to end index taking a stride of stride indexes to return a new string. start can be greater than end as well giving function an added functionality of reversing input string. start index will always be included in the output substring unless include_start is set to .false. where the end index will be included. So either start index or end index will be included in the output string (or both).
This function is an intelligent one, if the user doesn’t provide any one or more of the 3 optional arguments (start, end or stride) it figures them out automatically using optional arguments which are given (see examples).
But if the user provides arguments he/she is expected to be responsible for that (see example 3).
Examples:
- slice(
‘12345’
, stride=-1) should give'54321'
; start = 5, end = 1 - slice(
‘abcd’
, stride=-2, include_start=.false.) should give'ca'
; start = 4, end = 1 - slice(
‘abcde’
, 5, 2, 1) will give''
(empty string); user gave 1 as the stride argument - slice(
‘abcde’
, 5, 2) will give'edcb'
; stride = -1 - slice(
‘abcde’
, end = 1, stride = -2) will give'eca'
; start = 5
Prior Art
Python has it, but not exactly in the manner proposed above. In python one can do
str = 'spiderman'
print(str[1:9:2])
which will return 'pdra'
.
Please review the functionality and let me know your thoughts on this.