Skip to content

Commit d6c3536

Browse files
authored
Merge pull request #57 from sourceryinstitute/string-equivalence
Feature: support operator(==) and operator(/=) for string_t and character operands
2 parents 5ef0a88 + 426c6da commit d6c3536

File tree

3 files changed

+86
-10
lines changed

3 files changed

+86
-10
lines changed

src/sourcery/sourcery_string_m.f90

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ module sourcery_string_m
1414
generic :: string => as_character
1515
procedure :: is_allocated
1616
procedure :: get_json_key
17-
procedure, private :: &
18-
get_json_integer_array, get_json_logical, get_json_integer, get_json_string, get_json_real
17+
generic :: operator(/=) => string_t_ne_string_t, string_t_ne_character, character_ne_string_t
18+
generic :: operator(==) => string_t_eq_string_t, string_t_eq_character, character_eq_string_t
1919
generic :: get_json_value => &
2020
get_json_integer_array, get_json_logical, get_json_integer, get_json_string, get_json_real
21-
procedure, private :: equivalent
22-
generic :: operator(==) => equivalent
21+
procedure, private :: &
22+
get_json_integer_array, get_json_logical, get_json_integer, get_json_string, get_json_real
23+
procedure, private :: string_t_ne_string_t, string_t_ne_character
24+
procedure, private, pass(rhs) :: character_ne_string_t
25+
procedure, private :: string_t_eq_string_t, string_t_eq_character
26+
procedure, private, pass(rhs) :: character_eq_string_t
2327
end type
2428

2529
interface string_t
@@ -92,10 +96,44 @@ pure module function get_json_integer_array(self, key, mold) result(value_)
9296
integer, allocatable :: value_(:)
9397
end function
9498

95-
elemental module function equivalent(lhs, rhs) result(lhs_eqv_rhs)
99+
elemental module function string_t_eq_string_t(lhs, rhs) result(lhs_eq_rhs)
100+
implicit none
101+
class(string_t), intent(in) :: lhs, rhs
102+
logical lhs_eq_rhs
103+
end function
104+
105+
elemental module function string_t_eq_character(lhs, rhs) result(lhs_eq_rhs)
106+
implicit none
107+
class(string_t), intent(in) :: lhs
108+
character(len=*), intent(in) :: rhs
109+
logical lhs_eq_rhs
110+
end function
111+
112+
elemental module function character_eq_string_t(lhs, rhs) result(lhs_eq_rhs)
113+
implicit none
114+
class(string_t), intent(in) :: rhs
115+
character(len=*), intent(in) :: lhs
116+
logical lhs_eq_rhs
117+
end function
118+
119+
elemental module function string_t_ne_string_t(lhs, rhs) result(lhs_ne_rhs)
96120
implicit none
97121
class(string_t), intent(in) :: lhs, rhs
98-
logical lhs_eqv_rhs
122+
logical lhs_ne_rhs
123+
end function
124+
125+
elemental module function string_t_ne_character(lhs, rhs) result(lhs_ne_rhs)
126+
implicit none
127+
class(string_t), intent(in) :: lhs
128+
character(len=*), intent(in) :: rhs
129+
logical lhs_ne_rhs
130+
end function
131+
132+
elemental module function character_ne_string_t(lhs, rhs) result(lhs_ne_rhs)
133+
implicit none
134+
class(string_t), intent(in) :: rhs
135+
character(len=*), intent(in) :: lhs
136+
logical lhs_ne_rhs
99137
end function
100138

101139
end interface

src/sourcery/sourcery_string_s.f90

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,28 @@
155155

156156
end procedure
157157

158-
module procedure equivalent
159-
lhs_eqv_rhs = lhs%string() == rhs%string()
158+
module procedure string_t_eq_string_t
159+
lhs_eq_rhs = lhs%string() == rhs%string()
160+
end procedure
161+
162+
module procedure string_t_eq_character
163+
lhs_eq_rhs = lhs%string() == rhs
164+
end procedure
165+
166+
module procedure character_eq_string_t
167+
lhs_eq_rhs = lhs == rhs%string()
168+
end procedure
169+
170+
module procedure string_t_ne_string_t
171+
lhs_ne_rhs = lhs%string() /= rhs%string()
172+
end procedure
173+
174+
module procedure string_t_ne_character
175+
lhs_ne_rhs = lhs%string() /= rhs
176+
end procedure
177+
178+
module procedure character_ne_string_t
179+
lhs_ne_rhs = lhs /= rhs%string()
160180
end procedure
161181

162182
end submodule sourcery_string_s

test/string_test.f90

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ function results() result(test_results)
2929
test_result_t("extracting a string value from a colon-separated key/value pair", extracts_string_value()), &
3030
test_result_t("extracting a logical value from a colon-separated key/value pair", extracts_logical_value()), &
3131
test_result_t("extracting an integer array value from a colon-separated key/value pair", extracts_integer_array_value()), &
32-
test_result_t("extracting an integer value from a colon-separated key/value pair", extracts_integer_value()) &
32+
test_result_t("extracting an integer value from a colon-separated key/value pair", extracts_integer_value()), &
33+
test_result_t('supporting operator(==) for string_t and character operands', supports_equivalence_operator()), &
34+
test_result_t('supporting operator(/=) for string_t and character operands', supports_non_equivalence_operator()) &
3335
]
3436
end function
3537

@@ -55,7 +57,7 @@ function extracts_real_value() result(passed)
5557
logical passed
5658

5759
associate(line => string_t('"pi" : 3.14159'))
58-
passed = line%get_json_value(key=string_t("pi"), mold=2.71828) == 3.14159
60+
passed = line%get_json_value(key=string_t("pi"), mold=1.) == 3.14159
5961
end associate
6062
end function
6163

@@ -103,4 +105,20 @@ function extracts_integer_array_value() result(passed)
103105
end associate
104106
end function
105107

108+
function supports_equivalence_operator() result(passed)
109+
logical passed
110+
passed = &
111+
string_t("abcdefg") == string_t("abcdefg") .and. &
112+
string_t("xyz pdq") == "xyz pdq" .and. &
113+
"123.456" == string_t("123.456")
114+
end function
115+
116+
function supports_non_equivalence_operator() result(passed)
117+
logical passed
118+
passed = &
119+
string_t("abcdefg") /= string_t("xyz pdq") .and. &
120+
string_t("xyz pdq") /= "abcdefg" .and. &
121+
"123.456" /= string_t("456.123")
122+
end function
123+
106124
end module string_test_m

0 commit comments

Comments
 (0)