From 71d1c15e621a893421da25c919b702232088dc9a Mon Sep 17 00:00:00 2001 From: Damian Rouson Date: Sun, 28 Jan 2024 15:16:08 -0800 Subject: [PATCH 1/2] feat(string_t): constructor with default int arg --- src/sourcery/sourcery_string_m.f90 | 6 ++++++ src/sourcery/sourcery_string_s.f90 | 7 +++++++ test/string_test.f90 | 11 +++++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/sourcery/sourcery_string_m.f90 b/src/sourcery/sourcery_string_m.f90 index 90f133b2..b8e753ff 100644 --- a/src/sourcery/sourcery_string_m.f90 +++ b/src/sourcery/sourcery_string_m.f90 @@ -38,6 +38,12 @@ elemental module function construct(string) result(new_string) type(string_t) new_string end function + elemental module function from_default_integer(i) result(string) + implicit none + integer, intent(in) :: i + type(string_t) string + end function + end interface interface diff --git a/src/sourcery/sourcery_string_s.f90 b/src/sourcery/sourcery_string_s.f90 index db15b6f5..588cbeec 100644 --- a/src/sourcery/sourcery_string_s.f90 +++ b/src/sourcery/sourcery_string_s.f90 @@ -17,6 +17,13 @@ string_allocated = allocated(self%string_) end procedure + module procedure from_default_integer + integer, parameter :: sign_width = 1, digits_width = range(i) + 1 + character(len = digits_width + sign_width) characters + write(characters, '(i0)') i + string = string_t(characters) + end procedure + module procedure array_of_strings character(len=:), allocatable :: remainder, next_string integer next_delimiter, string_end diff --git a/test/string_test.f90 b/test/string_test.f90 index c482b8f9..9febc673 100644 --- a/test/string_test.f90 +++ b/test/string_test.f90 @@ -2,7 +2,6 @@ module string_test_m use sourcery_m, only : test_t, test_result_t, string_t implicit none - private public :: string_test_t @@ -34,7 +33,8 @@ function results() result(test_results) test_result_t('supporting operator(/=) for string_t and character operands', supports_non_equivalence_operator()), & test_result_t('assigning a string_t object to a character variable', assigns_string_t_to_character()), & test_result_t('assigning a character variable to a string_t object', assigns_character_to_string_t()), & - test_result_t('supporting operator(//) for string_t and character operands', supports_concatenation_operator()) & + test_result_t('supporting operator(//) for string_t and character operands', supports_concatenation_operator()), & + test_result_t('constructing from a default integer', constructs_from_default_integer()) & ] end function @@ -151,4 +151,11 @@ function supports_concatenation_operator() result(passed) end associate end function + function constructs_from_default_integer() result(passed) + logical passed + associate(string => string_t(1234567890)) + passed = adjustl(trim(string%string())) == "1234567890" + end associate + end function + end module string_test_m From 5e69e27277074b4caeedab24e93714cb0b021447 Mon Sep 17 00:00:00 2001 From: Damian Rouson Date: Sun, 28 Jan 2024 15:43:26 -0800 Subject: [PATCH 2/2] refac(string_functions_{m,s}): mv to string_{m,s} --- src/sourcery/sourcery_string_functions_m.f90 | 41 ------------------ src/sourcery/sourcery_string_functions_s.f90 | 45 -------------------- src/sourcery/sourcery_string_m.f90 | 14 ++++++ src/sourcery/sourcery_string_s.f90 | 28 ++++++++++++ test/string_test.f90 | 18 +++++++- 5 files changed, 59 insertions(+), 87 deletions(-) delete mode 100644 src/sourcery/sourcery_string_functions_m.f90 delete mode 100644 src/sourcery/sourcery_string_functions_s.f90 diff --git a/src/sourcery/sourcery_string_functions_m.f90 b/src/sourcery/sourcery_string_functions_m.f90 deleted file mode 100644 index 3b82e993..00000000 --- a/src/sourcery/sourcery_string_functions_m.f90 +++ /dev/null @@ -1,41 +0,0 @@ -! -! (c) 2019-2020 Guide Star Engineering, LLC -! This Software was developed for the US Nuclear Regulatory Commission (US NRC) under contract -! "Multi-Dimensional Physics Implementation into Fuel Analysis under Steady-state and Transients (FAST)", -! contract # NRC-HQ-60-17-C-0007 -! -module sourcery_string_functions_m - !! author: Damian Rouson - !! date: August 23, 2019 - !! summary: utilities for manipulating or producing character variables - implicit none - - private - public :: file_extension, base_name, string - - interface string - module procedure integer_to_string - end interface - - interface - - pure module function file_extension(file_name) result(extension) - !! result contains all characters in file_name after the first dot (.) - character(len=*), intent(in) :: file_name - character(len=:), allocatable :: extension - end function - - pure module function base_name(file_name) result(base) - !! result contains all characters in file_name before the first dot (.) - character(len=*), intent(in) :: file_name - character(len=:), allocatable :: base - end function - - pure module function integer_to_string(integer_value) result(characters) - integer, intent(in) :: integer_value - character(len=:), allocatable :: characters - end function - - end interface - -end module diff --git a/src/sourcery/sourcery_string_functions_s.f90 b/src/sourcery/sourcery_string_functions_s.f90 deleted file mode 100644 index 1e85a9cb..00000000 --- a/src/sourcery/sourcery_string_functions_s.f90 +++ /dev/null @@ -1,45 +0,0 @@ -! -! (c) 2019-2020 Guide Star Engineering, LLC -! This Software was developed for the US Nuclear Regulatory Commission (US NRC) under contract -! "Multi-Dimensional Physics Implementation into Fuel Analysis under Steady-state and Transients (FAST)", -! contract # NRC-HQ-60-17-C-0007 -! -submodule(sourcery_string_functions_m) sourcery_string_functions_s - implicit none - -contains - - module procedure file_extension - character(len=:), allocatable :: name_ - - name_ = trim(file_name) - associate( dot_location => index(name_, '.', back=.true.) ) - if (dot_location < len(name_)) then - extension = name_(dot_location+1:) - else - extension = "" - end if - end associate - end procedure - - module procedure base_name - character(len=:), allocatable :: name_ - - name_ = trim(file_name) - associate( dot_location => index(name_, '.', back=.true.) ) - if (dot_location < len(name_)) then - base = name_(1:dot_location-1) - else - base = "" - end if - end associate - end procedure - - module procedure integer_to_string - integer, parameter :: max_characters=16 - character(len=max_characters) string - write(string,*) integer_value - characters = trim(adjustl(string)) - end procedure - -end submodule diff --git a/src/sourcery/sourcery_string_m.f90 b/src/sourcery/sourcery_string_m.f90 index b8e753ff..c13a0bff 100644 --- a/src/sourcery/sourcery_string_m.f90 +++ b/src/sourcery/sourcery_string_m.f90 @@ -14,6 +14,8 @@ module sourcery_string_m generic :: string => as_character procedure :: is_allocated procedure :: get_json_key + procedure :: file_extension + procedure :: base_name generic :: operator(//) => string_t_cat_string_t, string_t_cat_character, character_cat_string_t generic :: operator(/=) => string_t_ne_string_t, string_t_ne_character, character_ne_string_t generic :: operator(==) => string_t_eq_string_t, string_t_eq_character, character_eq_string_t @@ -72,6 +74,18 @@ elemental module function get_json_key(self) result(unquoted_key) type(string_t) unquoted_key end function + elemental module function file_extension(self) result(extension) + !! result contains all characters in file_name after the last dot (.) + class(string_t), intent(in) :: self + type(string_t) extension + end function + + pure module function base_name(self) result(base) + !! result contains all characters in file_name before the last dot (.) + class(string_t), intent(in) :: self + type(string_t) base + end function + elemental module function get_json_real(self, key, mold) result(value_) implicit none class(string_t), intent(in) :: self, key diff --git a/src/sourcery/sourcery_string_s.f90 b/src/sourcery/sourcery_string_s.f90 index 588cbeec..a91211d7 100644 --- a/src/sourcery/sourcery_string_s.f90 +++ b/src/sourcery/sourcery_string_s.f90 @@ -58,6 +58,34 @@ end procedure + module procedure file_extension + character(len=:), allocatable :: name_ + + name_ = trim(adjustl(self%string())) + + associate( dot_location => index(name_, '.', back=.true.) ) + if (dot_location < len(name_)) then + extension = trim(adjustl(name_(dot_location+1:))) + else + extension = "" + end if + end associate + end procedure + + module procedure base_name + character(len=:), allocatable :: name_ + + name_ = self%string() + + associate(dot_location => index(name_, '.', back=.true.) ) + if (dot_location < len(name_)) then + base = trim(adjustl(name_(1:dot_location-1))) + else + base = "" + end if + end associate + end procedure + module procedure get_json_real character(len=:), allocatable :: raw_line, string_value diff --git a/test/string_test.f90 b/test/string_test.f90 index 9febc673..0b979c4e 100644 --- a/test/string_test.f90 +++ b/test/string_test.f90 @@ -34,7 +34,9 @@ function results() result(test_results) test_result_t('assigning a string_t object to a character variable', assigns_string_t_to_character()), & test_result_t('assigning a character variable to a string_t object', assigns_character_to_string_t()), & test_result_t('supporting operator(//) for string_t and character operands', supports_concatenation_operator()), & - test_result_t('constructing from a default integer', constructs_from_default_integer()) & + test_result_t('constructing from a default integer', constructs_from_default_integer()), & + test_result_t('extracting file base name', extracts_file_base_name()), & + test_result_t('extracting file name extension', extracts_file_name_extension()) & ] end function @@ -158,4 +160,18 @@ function constructs_from_default_integer() result(passed) end associate end function + function extracts_file_base_name() result(passed) + logical passed + associate(string => string_t(" foo .bar.too ")) + passed = string%base_name() == "foo .bar" + end associate + end function + + function extracts_file_name_extension() result(passed) + logical passed + associate(string => string_t(" foo .bar.too ")) + passed = string%file_extension() == "too" + end associate + end function + end module string_test_m