-
Notifications
You must be signed in to change notification settings - Fork 183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
intelligent slice functionality for strings #413
Comments
Other possibility could be to have flexible stride argument. Where instead of asking user to give exact value of |
There is subroutine In any case I would support this. It might be a good idea to overload this for the intrinsic character type, even if the intrinsic slice syntax exists. |
I prefer |
Can the slice function be ELEMENTAL? It appears to me that it can, since its arguments are scalars and it returns a scalar. |
@Beliavsky Consider this example: print *, slice("abcdef", 1, [1, 2, 3, 4, 5, 6], 1) The resulting scalar character values cannot be in an array due to the different length. |
Since this approach doesn't use Fortran indices but function arguments instead, we could also allow negative start/end values which will count from the end. There are two ways I can think of:
|
Here is the description of the 3.7.1 |
@Carltoffel , there some discussion of the different indexing systems in #311 (comment). Not sure if they are applicable here. |
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:
‘12345’
, stride=-1) should give'54321'
; start = 5, end = 1‘abcd’
, stride=-2, include_start=.false.) should give'ca'
; start = 4, end = 1‘abcde’
, 5, 2, 1) will give''
(empty string); user gave 1 as the stride argument‘abcde’
, 5, 2) will give'edcb'
; stride = -1‘abcde’
, end = 1, stride = -2) will give'eca'
; start = 5Prior Art
Python has it, but not exactly in the manner proposed above. In python one can do
which will return
'pdra'
.Please review the functionality and let me know your thoughts on this.
The text was updated successfully, but these errors were encountered: