Skip to content

Commit 42a905d

Browse files
committed
improved the implementation of last commit fa88905
1 parent fa88905 commit 42a905d

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

src/stdlib_strings.f90

+15-19
Original file line numberDiff line numberDiff line change
@@ -349,27 +349,23 @@ pure function slice_char(string, first, last, stride) result(sliced_string)
349349
if (present(last)) then
350350
last_index = last
351351
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)
360356
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)
372359
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
373369
else
374370
sliced_string = ""
375371
end if

0 commit comments

Comments
 (0)