From c023d9ea5e86fbcad0edbd29d81a272594b7fcc0 Mon Sep 17 00:00:00 2001 From: Damian Rouson Date: Fri, 21 Oct 2022 23:31:11 -0700 Subject: [PATCH] feat(string_t): encapsulate deferred-length string --- src/string_m.f90 | 41 +++++++++++++++++++++++++++++++++++++++++ src/string_s.f90 | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src/string_m.f90 create mode 100644 src/string_s.f90 diff --git a/src/string_m.f90 b/src/string_m.f90 new file mode 100644 index 00000000..1ae63255 --- /dev/null +++ b/src/string_m.f90 @@ -0,0 +1,41 @@ +module string_m + implicit none + + private + public :: string_t + public :: array_of_strings + + type string_t + private + character(len=:), allocatable :: string_ + contains + procedure :: string + end type + + interface string_t + + elemental module function construct(string) result(new_string) + implicit none + character(len=*), intent(in) :: string + type(string_t) new_string + end function + + end interface + + interface + + pure module function string(self) result(raw_string) + implicit none + class(string_t), intent(in) :: self + character(len=:), allocatable :: raw_string + end function + + pure module function array_of_strings(delimited_strings, delimiter) result(strings_array) + implicit none + character(len=*), intent(in) :: delimited_strings, delimiter + type(string_t), allocatable :: strings_array(:) + end function + + end interface + +end module string_m diff --git a/src/string_s.f90 b/src/string_s.f90 new file mode 100644 index 00000000..401a91fd --- /dev/null +++ b/src/string_s.f90 @@ -0,0 +1,36 @@ +submodule(string_m) string_s + implicit none + +contains + + module procedure construct + new_string%string_ = string + end procedure + + module procedure string + raw_string = self%string_ + end procedure + + module procedure array_of_strings + character(len=:), allocatable :: remainder, next_string + integer next_delimiter, string_end + + remainder = trim(adjustl(delimited_strings)) + allocate(strings_array(0)) + + do + next_delimiter = index(remainder, delimiter) + string_end = merge(len(remainder), next_delimiter-1, next_delimiter==0) + next_string = trim(adjustl(remainder(:string_end))) + if (len(next_string)==0) exit + strings_array = [strings_array, string_t(next_string)] + if (next_delimiter==0) then + remainder = "" + else + remainder = trim(adjustl(remainder(next_delimiter+1:))) + end if + end do + + end procedure + +end submodule string_s