-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathstring.f90
91 lines (81 loc) · 2.2 KB
/
string.f90
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
module string
implicit none
type,public :: string_splitter
private
character(len=:),allocatable :: buffer
character :: delimeter
integer :: current_pos
integer :: size
contains
procedure :: splite => string_splitt
procedure :: next => next_substring
procedure :: count => count_substring
final :: destroy_splitter
end type string_splitter
private :: string_splitt,next_substring
contains
subroutine string_splitt(this,source,delimeter)
implicit none
class(string_splitter) :: this
character(len=*),intent(in) :: source
character,intent(in) :: delimeter
integer :: buffer_size
buffer_size = len(source)
allocate(character(len=buffer_size) :: this%buffer)
this%buffer = source
this%delimeter = delimeter
this%current_pos = 0
this%size = len(trim(source))
write(*,*) source
write(*,*) this%buffer
write(*,*) this%size
end subroutine string_splitt
! next out_str must be at least 50 chars long
subroutine next_substring(this,out_str)
implicit none
class(string_splitter) :: this
character(len=*),intent(out) :: out_str
! character :: temp(50)
integer :: sub_pos
if(this%current_pos>this%size)then
! out_str(1:1) = char(0)
return
endif
sub_pos = 1
this%current_pos = this%current_pos + 1
do while(this%buffer(this%current_pos : this%current_pos) &
.ne. this%delimeter)
out_str(sub_pos:sub_pos) = this%buffer(this%current_pos : this%current_pos)
sub_pos = sub_pos + 1
this%current_pos = this%current_pos + 1
if(this%current_pos > this%size)then
! out_str(sub_pos:sub_pos) = char(0)
return
endif
enddo
! out_str(sub_pos:sub_pos) = char(0)
end subroutine next_substring
subroutine count_substring(this,num)
implicit none
class(string_splitter) :: this
integer, intent(out) :: num
integer :: i
num = 0
if(this%size .eq. 0 ) then
return
endif
do i = 1,this%size
if(this%buffer(i:i).eq.this%delimeter)then
num = num + 1
endif
enddo
num = num + 1
end subroutine count_substring
subroutine destroy_splitter(this)
implicit none
type(string_splitter) :: this
if(allocated(this%buffer))then
deallocate(this%buffer)
endif
end subroutine destroy_splitter
end module string