-
Notifications
You must be signed in to change notification settings - Fork 1
/
string_class.f90
84 lines (53 loc) · 2.19 KB
/
string_class.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
!=====================================================================!
! Contains a derived type 'string' and implemented procedures
!
! Author: Komahan Boopathy ([email protected])
!=====================================================================!
module string_class
use object_class, only : object
implicit none
private
public :: string
!-------------------------------------------------------------------!
! Derived type for string
!-------------------------------------------------------------------!
type, extends(object) :: string
character(:), allocatable :: str ! character array
type(integer) :: count ! length
contains
! Override
procedure :: print
! Destructor
final :: destroy
end type string
!-------------------------------------------------------------------!
! Interface to construct a string
!-------------------------------------------------------------------!
interface string
module procedure create
end interface string
contains
!===================================================================!
! Construct a string object from the supplied literal, find its
! length, initialize its hashcode as zero.
!===================================================================!
pure type(string) function create(str) result (this)
type(character(*)), intent(in) :: str
allocate(this % str, source=str) ! source copies, mold does not
this % count = len(str)
end function create
!===================================================================!
! Destructor for string object
!===================================================================!
pure subroutine destroy(this)
type(string), intent(inout) :: this
if(allocated(this % str)) deallocate(this % str)
end subroutine destroy
!===================================================================!
! Returns the string representation of the object
!===================================================================!
subroutine print(this)
class(string), intent(in) :: this
print *, "string : ", this % str
end subroutine print
end module string_class