@@ -349,27 +349,23 @@ pure function slice_char(string, first, last, stride) result(sliced_string)
349
349
if (present (last)) then
350
350
last_index = last
351
351
end if
352
-
353
- strides_taken = floor ( real (last_index - first_index) / real (stride_vector) )
354
-
355
- if (strides_taken < 0 .or. &
356
- ((first_index < 1 .and. last_index < 1 ) .or. &
357
- (first_index > length_string .and. last_index > length_string))) then
358
-
359
- sliced_string = " "
352
+
353
+ if (stride_vector > 0 ) then
354
+ first_index = max (first_index, 1 )
355
+ last_index = min (last_index, length_string)
360
356
else
361
- first_index = clip(first_index, 1 , length_string)
362
- last_index = clip(last_index, 1 , length_string)
363
-
364
- strides_taken = (last_index - first_index) / stride_vector
365
- allocate (character (len= strides_taken + 1 ) :: sliced_string)
366
-
367
- j = 1
368
- do i = first_index, last_index, stride_vector
369
- sliced_string(j:j) = string (i:i)
370
- j = j + 1
371
- end do
357
+ first_index = min (first_index, length_string)
358
+ last_index = max (last_index, 1 )
372
359
end if
360
+
361
+ strides_taken = floor ( real (last_index - first_index)/ real (stride_vector) )
362
+ allocate (character (len= max (0 , strides_taken + 1 )) :: sliced_string)
363
+
364
+ j = 1
365
+ do i = first_index, last_index, stride_vector
366
+ sliced_string(j:j) = string (i:i)
367
+ j = j + 1
368
+ end do
373
369
else
374
370
sliced_string = " "
375
371
end if
0 commit comments