Skip to content

Commit

Permalink
improved the implementation of last commit fa88905
Browse files Browse the repository at this point in the history
  • Loading branch information
aman-godara committed May 28, 2021
1 parent fa88905 commit 42a905d
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions src/stdlib_strings.f90
Original file line number Diff line number Diff line change
Expand Up @@ -349,27 +349,23 @@ pure function slice_char(string, first, last, stride) result(sliced_string)
if (present(last)) then
last_index = last
end if

strides_taken = floor( real(last_index - first_index) / real(stride_vector) )

if (strides_taken < 0 .or. &
((first_index < 1 .and. last_index < 1) .or. &
(first_index > length_string .and. last_index > length_string))) then

sliced_string = ""

if (stride_vector > 0) then
first_index = max(first_index, 1)
last_index = min(last_index, length_string)
else
first_index = clip(first_index, 1, length_string)
last_index = clip(last_index, 1, length_string)

strides_taken = (last_index - first_index) / stride_vector
allocate(character(len=strides_taken + 1) :: sliced_string)

j = 1
do i = first_index, last_index, stride_vector
sliced_string(j:j) = string(i:i)
j = j + 1
end do
first_index = min(first_index, length_string)
last_index = max(last_index, 1)
end if

strides_taken = floor( real(last_index - first_index)/real(stride_vector) )
allocate(character(len=max(0, strides_taken + 1)) :: sliced_string)

j = 1
do i = first_index, last_index, stride_vector
sliced_string(j:j) = string(i:i)
j = j + 1
end do
else
sliced_string = ""
end if
Expand Down

0 comments on commit 42a905d

Please sign in to comment.