@@ -7,8 +7,8 @@ module stdlib_strings
7
7
use stdlib_ascii, only: whitespace
8
8
use stdlib_string_type, only: string_type, char, verify, repeat, len, len_trim, move
9
9
use stdlib_optval, only: optval
10
- use stdlib_kinds, only: sp, dp, xdp, qp, int8, int16, int32, int64, lk, c_bool
11
- use iso_c_binding, only: c_char, c_null_char
10
+ use stdlib_kinds, only: sp, dp, xdp, qp, int8, int16, int32, int64, lk, c_bool, c_char
11
+ use iso_c_binding, only: c_null_char
12
12
implicit none
13
13
private
14
14
@@ -45,6 +45,15 @@ module stdlib_strings
45
45
#:endfor
46
46
end interface to_string
47
47
48
+ !> Version: experimental
49
+ !>
50
+ !> Format or transfer other types as a string.
51
+ !> ([Specification](../page/specs/stdlib_strings.html#to_c_string))
52
+ interface to_c_string
53
+ module procedure to_c_string_from_char
54
+ module procedure to_c_string_from_string
55
+ end interface to_c_string
56
+
48
57
!> Remove leading and trailing whitespace characters.
49
58
!>
50
59
!> Version: experimental
@@ -959,15 +968,30 @@ contains
959
968
!> Convert a Fortran character string to a C character array
960
969
!>
961
970
!> Version: experimental
962
- pure function to_c_string (value) result(cstr)
971
+ pure function to_c_string_from_char (value) result(cstr)
963
972
character(len=*), intent(in) :: value
964
973
character(kind=c_char) :: cstr(len(value)+1)
965
- integer :: i
966
- do concurrent (i=1:len(value))
974
+ integer :: i,lv
975
+ lv = len(value)
976
+ do concurrent (i=1:lv)
967
977
cstr(i) = value(i:i)
968
978
end do
969
- cstr(len(value)+1) = c_null_char
970
- end function to_c_string
979
+ cstr(lv+1) = c_null_char
980
+ end function to_c_string_from_char
981
+
982
+ !> Convert a Fortran string type to a C character array
983
+ !>
984
+ !> Version: experimental
985
+ pure function to_c_string_from_string(value) result(cstr)
986
+ type(string_type), intent(in) :: value
987
+ character(kind=c_char) :: cstr(len(value)+1)
988
+ integer :: i,lv
989
+ lv = len(value)
990
+ do concurrent (i=1:lv)
991
+ cstr(i) = char(value,pos=i)
992
+ end do
993
+ cstr(lv+1) = c_null_char
994
+ end function to_c_string_from_string
971
995
972
996
!> Joins a list of strings with a separator (default: space).
973
997
!> Returns a new string
0 commit comments