blob: 8380b9d2d4d9a856c7210b34623335d3f54a7882 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
! { dg-do compile }
!
! PR fortran/49110
! PR fortran/52843
!
! Based on a contributed code by jwmwalrus@gmail.com
!
! Before, character(len=:) result variable were rejected in PURE functions.
!
module mod1
use iso_c_binding
implicit none
contains
pure function c_strlen(str)
character(KIND = C_CHAR), intent(IN) :: str(*)
integer :: c_strlen,i
i = 1
do
if (i < 1) then
c_strlen = 0
return
end if
if (str(i) == c_null_char) exit
i = i + 1
end do
c_strlen = i - 1
end function c_strlen
pure function c2fstring(cbuffer) result(string)
character(:), allocatable :: string
character(KIND = C_CHAR), intent(IN) :: cbuffer(*)
integer :: i
continue
string = REPEAT(' ', c_strlen(cbuffer))
do i = 1, c_strlen(cbuffer)
if (cbuffer(i) == C_NULL_CHAR) exit
string(i:i) = cbuffer(i)
enddo
string = TRIM(string)
end function
end module mod1
use mod1
character(len=:), allocatable :: str
str = c2fstring("ABCDEF"//c_null_char//"GHI")
if (len(str) /= 6 .or. str /= "ABCDEF") call abort()
end
|