Skip to content

Commit 71d1c15

Browse files
committed
feat(string_t): constructor with default int arg
1 parent 2ee49f3 commit 71d1c15

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/sourcery/sourcery_string_m.f90

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ elemental module function construct(string) result(new_string)
3838
type(string_t) new_string
3939
end function
4040

41+
elemental module function from_default_integer(i) result(string)
42+
implicit none
43+
integer, intent(in) :: i
44+
type(string_t) string
45+
end function
46+
4147
end interface
4248

4349
interface

src/sourcery/sourcery_string_s.f90

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
string_allocated = allocated(self%string_)
1818
end procedure
1919

20+
module procedure from_default_integer
21+
integer, parameter :: sign_width = 1, digits_width = range(i) + 1
22+
character(len = digits_width + sign_width) characters
23+
write(characters, '(i0)') i
24+
string = string_t(characters)
25+
end procedure
26+
2027
module procedure array_of_strings
2128
character(len=:), allocatable :: remainder, next_string
2229
integer next_delimiter, string_end

test/string_test.f90

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module string_test_m
22
use sourcery_m, only : test_t, test_result_t, string_t
33
implicit none
44

5-
65
private
76
public :: string_test_t
87

@@ -34,7 +33,8 @@ function results() result(test_results)
3433
test_result_t('supporting operator(/=) for string_t and character operands', supports_non_equivalence_operator()), &
3534
test_result_t('assigning a string_t object to a character variable', assigns_string_t_to_character()), &
3635
test_result_t('assigning a character variable to a string_t object', assigns_character_to_string_t()), &
37-
test_result_t('supporting operator(//) for string_t and character operands', supports_concatenation_operator()) &
36+
test_result_t('supporting operator(//) for string_t and character operands', supports_concatenation_operator()), &
37+
test_result_t('constructing from a default integer', constructs_from_default_integer()) &
3838
]
3939
end function
4040

@@ -151,4 +151,11 @@ function supports_concatenation_operator() result(passed)
151151
end associate
152152
end function
153153

154+
function constructs_from_default_integer() result(passed)
155+
logical passed
156+
associate(string => string_t(1234567890))
157+
passed = adjustl(trim(string%string())) == "1234567890"
158+
end associate
159+
end function
160+
154161
end module string_test_m

0 commit comments

Comments
 (0)