From 05941b03211ff619643cd0fdc38c25f10c91186d Mon Sep 17 00:00:00 2001 From: Jeremie Vandenplas Date: Sun, 3 Sep 2023 11:37:18 +0200 Subject: [PATCH 1/7] add additional tests for string_type move --- test/string/test_string_intrinsic.f90 | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/test/string/test_string_intrinsic.f90 b/test/string/test_string_intrinsic.f90 index 582541d63..43b577dda 100644 --- a/test/string/test_string_intrinsic.f90 +++ b/test/string/test_string_intrinsic.f90 @@ -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 @@ -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 @@ -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 From ae4a38f89b984de07ade91794e0769aefa7e5c21 Mon Sep 17 00:00:00 2001 From: Jeremie Vandenplas Date: Sun, 3 Sep 2023 11:38:24 +0200 Subject: [PATCH 2/7] Fix bug related to string_type move #731 #735 --- src/stdlib_string_type.fypp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/stdlib_string_type.fypp b/src/stdlib_string_type.fypp index 0bb5ff8a2..b7a634246 100644 --- a/src/stdlib_string_type.fypp +++ b/src/stdlib_string_type.fypp @@ -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 From 8f088b3674801205641bb3c0e2417c4b51237636 Mon Sep 17 00:00:00 2001 From: Jeremie Vandenplas Date: Mon, 4 Sep 2023 07:43:51 -0400 Subject: [PATCH 3/7] Update src/stdlib_string_type.fypp Co-authored-by: Sebastian Ehlert <28669218+awvwgk@users.noreply.github.com> --- src/stdlib_string_type.fypp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/stdlib_string_type.fypp b/src/stdlib_string_type.fypp index b7a634246..436f28e55 100644 --- a/src/stdlib_string_type.fypp +++ b/src/stdlib_string_type.fypp @@ -681,11 +681,10 @@ contains elemental subroutine move_string_string(from, to) type(string_type), intent(inout) :: from type(string_type), intent(inout) :: to + character(:), allocatable :: tmp - if(.not.allocated(from%raw))then - if(allocated(to%raw))deallocate(to%raw) - return - endif + call move_alloc(from%raw, tmp) + call move_alloc(tmp, to%raw) if(from%raw .eq. to%raw)then deallocate(from%raw) From d38d61552e37b2b4892183f68de3df79fb1df7af Mon Sep 17 00:00:00 2001 From: Jeremie Vandenplas Date: Mon, 4 Sep 2023 07:44:00 -0400 Subject: [PATCH 4/7] Update src/stdlib_string_type.fypp Co-authored-by: Sebastian Ehlert <28669218+awvwgk@users.noreply.github.com> --- src/stdlib_string_type.fypp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/stdlib_string_type.fypp b/src/stdlib_string_type.fypp index 436f28e55..6ca4e1363 100644 --- a/src/stdlib_string_type.fypp +++ b/src/stdlib_string_type.fypp @@ -686,12 +686,6 @@ contains call move_alloc(from%raw, tmp) call move_alloc(tmp, 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 !> Moves the allocated character scalar from 'from' to 'to' From 3066e3949bdb4e5c14d10eee986a2a43450bdbe0 Mon Sep 17 00:00:00 2001 From: Jeremie Vandenplas Date: Mon, 4 Sep 2023 13:49:06 +0200 Subject: [PATCH 5/7] update specs --- doc/specs/stdlib_string_type.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/specs/stdlib_string_type.md b/doc/specs/stdlib_string_type.md index a0444b442..93f1df619 100644 --- a/doc/specs/stdlib_string_type.md +++ b/doc/specs/stdlib_string_type.md @@ -1537,7 +1537,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 From 790ef59a13c358e647317dce5346b1961bb8baae Mon Sep 17 00:00:00 2001 From: Jeremie Vandenplas Date: Mon, 4 Sep 2023 13:56:17 +0200 Subject: [PATCH 6/7] fix test_string_intrinsic.f90 --- test/string/test_string_intrinsic.f90 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/string/test_string_intrinsic.f90 b/test/string/test_string_intrinsic.f90 index 43b577dda..c84fbbd48 100644 --- a/test/string/test_string_intrinsic.f90 +++ b/test/string/test_string_intrinsic.f90 @@ -715,7 +715,7 @@ subroutine test_move(error) 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) From b82b39a688110116935581eab74759d112e8deb9 Mon Sep 17 00:00:00 2001 From: Jeremie Vandenplas Date: Mon, 4 Sep 2023 16:12:48 +0200 Subject: [PATCH 7/7] add new behaviour in specs --- doc/specs/stdlib_string_type.md | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/specs/stdlib_string_type.md b/doc/specs/stdlib_string_type.md index 93f1df619..b1d34684e 100644 --- a/doc/specs/stdlib_string_type.md +++ b/doc/specs/stdlib_string_type.md @@ -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