From 8d59c934aba73a472824a5f2e50b8c5a710bc97a Mon Sep 17 00:00:00 2001 From: Damian Rouson Date: Sun, 29 Oct 2023 13:41:58 -0700 Subject: [PATCH 1/2] feat(string_t):operator(==) for string_t/character --- src/sourcery/sourcery_string_m.f90 | 23 +++++++++++++++++++---- src/sourcery/sourcery_string_s.f90 | 12 ++++++++++-- test/string_test.f90 | 13 +++++++++++-- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/sourcery/sourcery_string_m.f90 b/src/sourcery/sourcery_string_m.f90 index 0c3b0b3f..05d4a532 100644 --- a/src/sourcery/sourcery_string_m.f90 +++ b/src/sourcery/sourcery_string_m.f90 @@ -18,8 +18,9 @@ module sourcery_string_m get_json_integer_array, get_json_logical, get_json_integer, get_json_string, get_json_real generic :: get_json_value => & get_json_integer_array, get_json_logical, get_json_integer, get_json_string, get_json_real - procedure, private :: equivalent - generic :: operator(==) => equivalent + procedure, private :: string_t_eq_string_t, string_t_eq_character + procedure, private, pass(rhs) :: character_eq_string_t + generic :: operator(==) => string_t_eq_string_t, string_t_eq_character, character_eq_string_t end type interface string_t @@ -92,10 +93,24 @@ pure module function get_json_integer_array(self, key, mold) result(value_) integer, allocatable :: value_(:) end function - elemental module function equivalent(lhs, rhs) result(lhs_eqv_rhs) + elemental module function string_t_eq_string_t(lhs, rhs) result(lhs_eq_rhs) implicit none class(string_t), intent(in) :: lhs, rhs - logical lhs_eqv_rhs + logical lhs_eq_rhs + end function + + elemental module function string_t_eq_character(lhs, rhs) result(lhs_eq_rhs) + implicit none + class(string_t), intent(in) :: lhs + character(len=*), intent(in) :: rhs + logical lhs_eq_rhs + end function + + elemental module function character_eq_string_t(lhs, rhs) result(lhs_eq_rhs) + implicit none + class(string_t), intent(in) :: rhs + character(len=*), intent(in) :: lhs + logical lhs_eq_rhs end function end interface diff --git a/src/sourcery/sourcery_string_s.f90 b/src/sourcery/sourcery_string_s.f90 index c72f263c..3094cbfc 100644 --- a/src/sourcery/sourcery_string_s.f90 +++ b/src/sourcery/sourcery_string_s.f90 @@ -155,8 +155,16 @@ end procedure - module procedure equivalent - lhs_eqv_rhs = lhs%string() == rhs%string() + module procedure string_t_eq_string_t + lhs_eq_rhs = lhs%string() == rhs%string() + end procedure + + module procedure string_t_eq_character + lhs_eq_rhs = lhs%string() == rhs + end procedure + + module procedure character_eq_string_t + lhs_eq_rhs = lhs == rhs%string() end procedure end submodule sourcery_string_s diff --git a/test/string_test.f90 b/test/string_test.f90 index 763e7127..10bc5b47 100644 --- a/test/string_test.f90 +++ b/test/string_test.f90 @@ -29,7 +29,8 @@ function results() result(test_results) test_result_t("extracting a string value from a colon-separated key/value pair", extracts_string_value()), & test_result_t("extracting a logical value from a colon-separated key/value pair", extracts_logical_value()), & test_result_t("extracting an integer array value from a colon-separated key/value pair", extracts_integer_array_value()), & - test_result_t("extracting an integer value from a colon-separated key/value pair", extracts_integer_value()) & + test_result_t("extracting an integer value from a colon-separated key/value pair", extracts_integer_value()), & + test_result_t('supporting operator(==) for string_t and character operands', supports_equivalence_operator()) & ] end function @@ -55,7 +56,7 @@ function extracts_real_value() result(passed) logical passed associate(line => string_t('"pi" : 3.14159')) - passed = line%get_json_value(key=string_t("pi"), mold=2.71828) == 3.14159 + passed = line%get_json_value(key=string_t("pi"), mold=1.) == 3.14159 end associate end function @@ -103,4 +104,12 @@ function extracts_integer_array_value() result(passed) end associate end function + function supports_equivalence_operator() result(passed) + logical passed + passed = & + string_t("abcdefg") == string_t("abcdefg") .and. & + string_t("xyz pdq") == "xyz pdq" .and. & + "123.456" == string_t("123.456") + end function + end module string_test_m From 426c6dacb7c68245983db926ba46195417e6a6d3 Mon Sep 17 00:00:00 2001 From: Damian Rouson Date: Sun, 29 Oct 2023 14:00:34 -0700 Subject: [PATCH 2/2] feat(string_t):operator(/=) for string_t/character --- src/sourcery/sourcery_string_m.f90 | 29 ++++++++++++++++++++++++++--- src/sourcery/sourcery_string_s.f90 | 12 ++++++++++++ test/string_test.f90 | 11 ++++++++++- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/sourcery/sourcery_string_m.f90 b/src/sourcery/sourcery_string_m.f90 index 05d4a532..d90faf12 100644 --- a/src/sourcery/sourcery_string_m.f90 +++ b/src/sourcery/sourcery_string_m.f90 @@ -14,13 +14,16 @@ module sourcery_string_m generic :: string => as_character procedure :: is_allocated procedure :: get_json_key - procedure, private :: & - get_json_integer_array, get_json_logical, get_json_integer, get_json_string, get_json_real + 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 generic :: get_json_value => & get_json_integer_array, get_json_logical, get_json_integer, get_json_string, get_json_real + procedure, private :: & + get_json_integer_array, get_json_logical, get_json_integer, get_json_string, get_json_real + procedure, private :: string_t_ne_string_t, string_t_ne_character + procedure, private, pass(rhs) :: character_ne_string_t procedure, private :: string_t_eq_string_t, string_t_eq_character procedure, private, pass(rhs) :: character_eq_string_t - generic :: operator(==) => string_t_eq_string_t, string_t_eq_character, character_eq_string_t end type interface string_t @@ -113,6 +116,26 @@ elemental module function character_eq_string_t(lhs, rhs) result(lhs_eq_rhs) logical lhs_eq_rhs end function + elemental module function string_t_ne_string_t(lhs, rhs) result(lhs_ne_rhs) + implicit none + class(string_t), intent(in) :: lhs, rhs + logical lhs_ne_rhs + end function + + elemental module function string_t_ne_character(lhs, rhs) result(lhs_ne_rhs) + implicit none + class(string_t), intent(in) :: lhs + character(len=*), intent(in) :: rhs + logical lhs_ne_rhs + end function + + elemental module function character_ne_string_t(lhs, rhs) result(lhs_ne_rhs) + implicit none + class(string_t), intent(in) :: rhs + character(len=*), intent(in) :: lhs + logical lhs_ne_rhs + end function + end interface end module sourcery_string_m diff --git a/src/sourcery/sourcery_string_s.f90 b/src/sourcery/sourcery_string_s.f90 index 3094cbfc..a71a2b4a 100644 --- a/src/sourcery/sourcery_string_s.f90 +++ b/src/sourcery/sourcery_string_s.f90 @@ -167,4 +167,16 @@ lhs_eq_rhs = lhs == rhs%string() end procedure + module procedure string_t_ne_string_t + lhs_ne_rhs = lhs%string() /= rhs%string() + end procedure + + module procedure string_t_ne_character + lhs_ne_rhs = lhs%string() /= rhs + end procedure + + module procedure character_ne_string_t + lhs_ne_rhs = lhs /= rhs%string() + end procedure + end submodule sourcery_string_s diff --git a/test/string_test.f90 b/test/string_test.f90 index 10bc5b47..0bbbef80 100644 --- a/test/string_test.f90 +++ b/test/string_test.f90 @@ -30,7 +30,8 @@ function results() result(test_results) test_result_t("extracting a logical value from a colon-separated key/value pair", extracts_logical_value()), & test_result_t("extracting an integer array value from a colon-separated key/value pair", extracts_integer_array_value()), & test_result_t("extracting an integer value from a colon-separated key/value pair", extracts_integer_value()), & - test_result_t('supporting operator(==) for string_t and character operands', supports_equivalence_operator()) & + test_result_t('supporting operator(==) for string_t and character operands', supports_equivalence_operator()), & + test_result_t('supporting operator(/=) for string_t and character operands', supports_non_equivalence_operator()) & ] end function @@ -112,4 +113,12 @@ function supports_equivalence_operator() result(passed) "123.456" == string_t("123.456") end function + function supports_non_equivalence_operator() result(passed) + logical passed + passed = & + string_t("abcdefg") /= string_t("xyz pdq") .and. & + string_t("xyz pdq") /= "abcdefg" .and. & + "123.456" /= string_t("456.123") + end function + end module string_test_m