Skip to content

Commit 8d59c93

Browse files
committed
feat(string_t):operator(==) for string_t/character
1 parent 5ef0a88 commit 8d59c93

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

src/sourcery/sourcery_string_m.f90

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ module sourcery_string_m
1818
get_json_integer_array, get_json_logical, get_json_integer, get_json_string, get_json_real
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 :: string_t_eq_string_t, string_t_eq_character
22+
procedure, private, pass(rhs) :: character_eq_string_t
23+
generic :: operator(==) => string_t_eq_string_t, string_t_eq_character, character_eq_string_t
2324
end type
2425

2526
interface string_t
@@ -92,10 +93,24 @@ pure module function get_json_integer_array(self, key, mold) result(value_)
9293
integer, allocatable :: value_(:)
9394
end function
9495

95-
elemental module function equivalent(lhs, rhs) result(lhs_eqv_rhs)
96+
elemental module function string_t_eq_string_t(lhs, rhs) result(lhs_eq_rhs)
9697
implicit none
9798
class(string_t), intent(in) :: lhs, rhs
98-
logical lhs_eqv_rhs
99+
logical lhs_eq_rhs
100+
end function
101+
102+
elemental module function string_t_eq_character(lhs, rhs) result(lhs_eq_rhs)
103+
implicit none
104+
class(string_t), intent(in) :: lhs
105+
character(len=*), intent(in) :: rhs
106+
logical lhs_eq_rhs
107+
end function
108+
109+
elemental module function character_eq_string_t(lhs, rhs) result(lhs_eq_rhs)
110+
implicit none
111+
class(string_t), intent(in) :: rhs
112+
character(len=*), intent(in) :: lhs
113+
logical lhs_eq_rhs
99114
end function
100115

101116
end interface

src/sourcery/sourcery_string_s.f90

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,16 @@
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()
160168
end procedure
161169

162170
end submodule sourcery_string_s

test/string_test.f90

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ 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()) &
3334
]
3435
end function
3536

@@ -55,7 +56,7 @@ function extracts_real_value() result(passed)
5556
logical passed
5657

5758
associate(line => string_t('"pi" : 3.14159'))
58-
passed = line%get_json_value(key=string_t("pi"), mold=2.71828) == 3.14159
59+
passed = line%get_json_value(key=string_t("pi"), mold=1.) == 3.14159
5960
end associate
6061
end function
6162

@@ -103,4 +104,12 @@ function extracts_integer_array_value() result(passed)
103104
end associate
104105
end function
105106

107+
function supports_equivalence_operator() result(passed)
108+
logical passed
109+
passed = &
110+
string_t("abcdefg") == string_t("abcdefg") .and. &
111+
string_t("xyz pdq") == "xyz pdq" .and. &
112+
"123.456" == string_t("123.456")
113+
end function
114+
106115
end module string_test_m

0 commit comments

Comments
 (0)