Skip to content

Proposal to fix the procedure move for string_type #736

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

Merged
merged 7 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/stdlib_string_type.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -680,9 +680,18 @@ contains
!> No output
elemental subroutine move_string_string(from, to)
type(string_type), intent(inout) :: from
type(string_type), intent(out) :: to
type(string_type), intent(inout) :: to

if(.not.allocated(from%raw))then
if(allocated(to%raw))deallocate(to%raw)
return
endif

call move_alloc(from%raw, to%raw)
if(from%raw .eq. to%raw)then
deallocate(from%raw)
else
call move_alloc(from%raw, to%raw)
endif

end subroutine move_string_string

Expand Down
15 changes: 14 additions & 1 deletion test/string/test_string_intrinsic.f90
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ subroutine test_move(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
type(string_type) :: from_string, to_string
type(string_type) :: from_string_not
type(string_type) :: from_strings(2), to_strings(2)
character(len=:), allocatable :: from_char, to_char

Expand Down Expand Up @@ -706,7 +707,7 @@ subroutine test_move(error)
call check(error, .not. allocated(from_char) .and. from_string == "new char", "move: test_case 6")
if (allocated(error)) return

! character (unallocated) --> string_type (allocated)
! character (not allocated) --> string_type (allocated)
call move(from_char, from_string)
call check(error, from_string == "", "move: test_case 7")
if (allocated(error)) return
Expand All @@ -720,6 +721,18 @@ subroutine test_move(error)
! elemental: string_type (allocated) --> string_type (not allocated)
call move(from_strings, to_strings)
call check(error, all(from_strings(:) == "") .and. all(to_strings(:) == "Move This String"), "move: test_case 9")

! string_type (not allocated) --> string_type (not allocated)
call move(from_string_not, to_string)
call check(error, from_string_not == "" .and. to_string == "", "move: test_case 10")
if (allocated(error)) return

! string_type (not allocated) --> string_type (not allocated)
to_string = "to be deallocated"
call move(from_string_not, to_string)
call check(error, from_string_not == "" .and. to_string == "", "move: test_case 11")
if (allocated(error)) return

end subroutine test_move

end module test_string_intrinsic
Expand Down