Skip to content
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

Proposal to fix the procedure move for string_type #736

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion doc/specs/stdlib_string_type.md
Expand Up @@ -1523,6 +1523,7 @@ Experimental
Moves the allocation from `from` to `to`, consequently deallocating `from` in this process.
If `from` is not allocated before execution, `to` gets deallocated by the process.
An unallocated `string_type` instance is equivalent to an empty string.
If `from` and `to` are the same variable, then `from` remains unchanged.

#### Syntax

Expand All @@ -1537,7 +1538,8 @@ Pure subroutine (Elemental subroutine, only when both `from` and `to` are `type(
- `from`: Character scalar or [[stdlib_string_type(module):string_type(type)]].
This argument is `intent(inout)`.
- `to`: Character scalar or [[stdlib_string_type(module):string_type(type)]].
This argument is `intent(out)`.
This argument is `intent(inout)` when both `from` and `to` are `type(string_type)`,
otherwise `intent(out)`.

#### Example

Expand Down
6 changes: 4 additions & 2 deletions src/stdlib_string_type.fypp
Expand Up @@ -680,9 +680,11 @@ 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
character(:), allocatable :: tmp

call move_alloc(from%raw, to%raw)
call move_alloc(from%raw, tmp)
call move_alloc(tmp, to%raw)

end subroutine move_string_string

Expand Down
17 changes: 15 additions & 2 deletions test/string/test_string_intrinsic.f90
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,20 +707,32 @@ 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

from_string = "moving to self"
! string_type (allocated) --> string_type (allocated)
call move(from_string, from_string)
call check(error, from_string == "", "move: test_case 8")
call check(error, from_string == "moving to self", "move: test_case 8")
if (allocated(error)) return

! 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