MODFLOW 6  version 6.6.0.dev0
USGS Modular Hydrologic Model
CharString.f90
Go to the documentation of this file.
2 
3  implicit none
4  private
5  public :: characterstringtype
6 
7  !> This class is used to store a single deferred-length
8  !! character string. It was designed to work in an
9  !! array implementation so that a jagged character array
10  !! could be used in MODFLOW and stored in the memory
11  !! manager.
12  !!
13  !! The overloaded methods allow instances to behave like
14  !! a regular string and work with intrinsic Fortran
15  !! character strings. Ideas for the implementation were
16  !! inspired by:
17  !! https://gitlab.com/everythingfunctional/iso_varying_string
18  !
19  !! Can be improved as necessary to overload other string
20  !! functions, such as write_formatted, trim, len, ...
21  !!
22  !<
24  private
25  character(len=:), allocatable :: charstring
26  contains
27  procedure, pass(lhs) :: assign_to_charstring
28  procedure, pass(rhs) :: assign_from_charstring
29  procedure, pass(rhs) :: character_eq_charstring
30  procedure, pass(lhs) :: charstring_eq_character
32  procedure :: write_unformatted
33  procedure :: strlen
34  generic :: assignment(=) => assign_to_charstring, assign_from_charstring
35  generic :: operator(==) => character_eq_charstring, &
38  ! not supported by gfortran 5 and 6
39  ! disable for now
40  ! generic :: write (unformatted) => write_unformatted
41  end type characterstringtype
42 
43 contains
44 
45  recursive subroutine assign_to_charstring(lhs, rhs)
46  class(characterstringtype), intent(out) :: lhs
47  character(len=*), intent(in) :: rhs
48  logical :: allocate_charstring
49  allocate_charstring = .false.
50  if (allocated(lhs%charstring)) then
51  if (len(lhs%charstring) <= len(rhs)) then
52  lhs%charstring(:) = rhs
53  else
54  allocate_charstring = .true.
55  end if
56  else
57  allocate_charstring = .true.
58  end if
59  if (allocate_charstring) then
60  lhs%charstring = rhs
61  end if
62  end subroutine assign_to_charstring
63 
64  subroutine assign_from_charstring(lhs, rhs)
65  character(len=*), intent(out) :: lhs
66  class(characterstringtype), intent(in) :: rhs
67  if (allocated(rhs%charstring)) then
68  lhs = rhs%charstring
69  else
70  lhs = ''
71  end if
72  end subroutine assign_from_charstring
73 
74  elemental function character_eq_charstring(lhs, rhs) result(equals)
75  character(len=*), intent(in) :: lhs
76  class(characterstringtype), intent(in) :: rhs
77  logical :: equals
78  if (allocated(rhs%charstring)) then
79  equals = lhs == rhs%charstring
80  else
81  equals = lhs == ''
82  end if
83  end function character_eq_charstring
84 
85  elemental function charstring_eq_character(lhs, rhs) result(equals)
86  class(characterstringtype), intent(in) :: lhs
87  character(len=*), intent(in) :: rhs
88  logical :: equals
89  if (allocated(lhs%charstring)) then
90  equals = lhs%charstring == rhs
91  else
92  equals = rhs == ''
93  end if
94  end function charstring_eq_character
95 
96  elemental function charstring_eq_charstring(this, rhs) result(equals)
97  class(characterstringtype), intent(in) :: this
98  class(characterstringtype), intent(in) :: rhs
99  logical :: equals
100 
101  equals = .false.
102  if (allocated(this%charstring)) then
103  equals = (rhs == this%charstring)
104  end if
105 
106  end function charstring_eq_charstring
107 
108  subroutine write_unformatted(this, unit, iostat, iomsg)
109  class(characterstringtype), intent(in) :: this
110  integer, intent(in) :: unit
111  integer, intent(out) :: iostat
112  character(len=*), intent(inout) :: iomsg
113  iostat = 0
114  if (allocated(this%charstring)) then
115  write (unit, iostat=iostat) this%charstring
116  end if
117  end subroutine write_unformatted
118 
119  function strlen(this) result(length)
120  class(characterstringtype), intent(in) :: this
121  integer :: length
122 
123  if (allocated(this%charstring)) then
124  length = len(this%charstring)
125  else
126  length = 0
127  end if
128  end function strlen
129 
130 end module characterstringmodule
elemental logical function charstring_eq_character(lhs, rhs)
Definition: CharString.f90:86
recursive subroutine assign_to_charstring(lhs, rhs)
Definition: CharString.f90:46
subroutine write_unformatted(this, unit, iostat, iomsg)
Definition: CharString.f90:109
elemental logical function character_eq_charstring(lhs, rhs)
Definition: CharString.f90:75
subroutine assign_from_charstring(lhs, rhs)
Definition: CharString.f90:65
elemental logical function charstring_eq_charstring(this, rhs)
Definition: CharString.f90:97
integer function strlen(this)
Definition: CharString.f90:120
This class is used to store a single deferred-length character string. It was designed to work in an ...
Definition: CharString.f90:23