Skip to content

Commit

Permalink
Merge pull request #64 from sourceryinstitute/string_t-concatenation
Browse files Browse the repository at this point in the history
feat(string_t): support concatenation operator(//)
  • Loading branch information
rouson authored Dec 3, 2023
2 parents 00d4fee + 46c17ef commit d335111
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/sourcery/sourcery_string_m.f90
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module sourcery_string_m
generic :: string => as_character
procedure :: is_allocated
procedure :: get_json_key
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
generic :: assignment(= ) => assign_string_t_to_character, assign_character_to_string_t
Expand All @@ -22,6 +23,8 @@ module sourcery_string_m
procedure, private :: string_t_ne_string_t, string_t_ne_character
procedure, private :: string_t_eq_string_t, string_t_eq_character
procedure, private :: assign_character_to_string_t
procedure, private :: string_t_cat_string_t, string_t_cat_character
procedure, private, pass(rhs) :: character_cat_string_t
procedure, private, pass(rhs) :: character_ne_string_t
procedure, private, pass(rhs) :: character_eq_string_t
procedure, private, pass(rhs) :: assign_string_t_to_character
Expand Down Expand Up @@ -137,6 +140,26 @@ elemental module function character_ne_string_t(lhs, rhs) result(lhs_ne_rhs)
logical lhs_ne_rhs
end function

pure module function string_t_cat_string_t(lhs, rhs) result(lhs_cat_rhs)
implicit none
class(string_t), intent(in) :: lhs, rhs
type(string_t) lhs_cat_rhs
end function

pure module function string_t_cat_character(lhs, rhs) result(lhs_cat_rhs)
implicit none
class(string_t), intent(in) :: lhs
character(len=*), intent(in) :: rhs
type(string_t) lhs_cat_rhs
end function

pure module function character_cat_string_t(lhs, rhs) result(lhs_cat_rhs)
implicit none
character(len=*), intent(in) :: lhs
class(string_t), intent(in) :: rhs
type(string_t) lhs_cat_rhs
end function

pure module subroutine assign_character_to_string_t(lhs, rhs)
implicit none
class(string_t), intent(inout) :: lhs
Expand Down
12 changes: 12 additions & 0 deletions src/sourcery/sourcery_string_s.f90
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,17 @@
module procedure assign_character_to_string_t
lhs%string_ = rhs
end procedure

module procedure string_t_cat_string_t
lhs_cat_rhs = string_t(lhs%string_ // rhs%string_)
end procedure

module procedure string_t_cat_character
lhs_cat_rhs = string_t(lhs%string_ // rhs)
end procedure

module procedure character_cat_string_t
lhs_cat_rhs = string_t(lhs // rhs%string_)
end procedure

end submodule sourcery_string_s
12 changes: 11 additions & 1 deletion test/string_test.f90
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ function results() result(test_results)
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()), &
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('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()) &
]
end function

Expand Down Expand Up @@ -141,4 +142,13 @@ function assigns_character_to_string_t() result(passed)
passed = lhs == rhs
end function

function supports_concatenation_operator() result(passed)
logical passed
character(len=*), parameter :: prefix = "foo", postfix="bar"

associate(infix => string_t(" yada yada "))
passed = prefix // infix // postfix == prefix // infix%string() // postfix
end associate
end function

end module string_test_m

0 comments on commit d335111

Please sign in to comment.