Skip to content

Commit c023d9e

Browse files
committed
feat(string_t): encapsulate deferred-length string
1 parent c83fcca commit c023d9e

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/string_m.f90

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module string_m
2+
implicit none
3+
4+
private
5+
public :: string_t
6+
public :: array_of_strings
7+
8+
type string_t
9+
private
10+
character(len=:), allocatable :: string_
11+
contains
12+
procedure :: string
13+
end type
14+
15+
interface string_t
16+
17+
elemental module function construct(string) result(new_string)
18+
implicit none
19+
character(len=*), intent(in) :: string
20+
type(string_t) new_string
21+
end function
22+
23+
end interface
24+
25+
interface
26+
27+
pure module function string(self) result(raw_string)
28+
implicit none
29+
class(string_t), intent(in) :: self
30+
character(len=:), allocatable :: raw_string
31+
end function
32+
33+
pure module function array_of_strings(delimited_strings, delimiter) result(strings_array)
34+
implicit none
35+
character(len=*), intent(in) :: delimited_strings, delimiter
36+
type(string_t), allocatable :: strings_array(:)
37+
end function
38+
39+
end interface
40+
41+
end module string_m

src/string_s.f90

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
submodule(string_m) string_s
2+
implicit none
3+
4+
contains
5+
6+
module procedure construct
7+
new_string%string_ = string
8+
end procedure
9+
10+
module procedure string
11+
raw_string = self%string_
12+
end procedure
13+
14+
module procedure array_of_strings
15+
character(len=:), allocatable :: remainder, next_string
16+
integer next_delimiter, string_end
17+
18+
remainder = trim(adjustl(delimited_strings))
19+
allocate(strings_array(0))
20+
21+
do
22+
next_delimiter = index(remainder, delimiter)
23+
string_end = merge(len(remainder), next_delimiter-1, next_delimiter==0)
24+
next_string = trim(adjustl(remainder(:string_end)))
25+
if (len(next_string)==0) exit
26+
strings_array = [strings_array, string_t(next_string)]
27+
if (next_delimiter==0) then
28+
remainder = ""
29+
else
30+
remainder = trim(adjustl(remainder(next_delimiter+1:)))
31+
end if
32+
end do
33+
34+
end procedure
35+
36+
end submodule string_s

0 commit comments

Comments
 (0)