Skip to content

Commit c38c53e

Browse files
committed
Merge branch 'main' of ssh://ssh.github.com:443/sourceryinstitute/sourcery into fixes-for-favpro
2 parents 65a30a6 + de6a957 commit c38c53e

5 files changed

+80
-88
lines changed

src/sourcery/sourcery_string_functions_m.f90

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/sourcery/sourcery_string_functions_s.f90

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/sourcery/sourcery_string_m.f90

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ module sourcery_string_m
1414
generic :: string => as_character
1515
procedure :: is_allocated
1616
procedure :: get_json_key
17+
procedure :: file_extension
18+
procedure :: base_name
1719
generic :: operator(//) => string_t_cat_string_t, string_t_cat_character, character_cat_string_t
1820
generic :: operator(/=) => string_t_ne_string_t, string_t_ne_character, character_ne_string_t
1921
generic :: operator(==) => string_t_eq_string_t, string_t_eq_character, character_eq_string_t
@@ -38,6 +40,12 @@ elemental module function construct(string) result(new_string)
3840
type(string_t) new_string
3941
end function
4042

43+
elemental module function from_default_integer(i) result(string)
44+
implicit none
45+
integer, intent(in) :: i
46+
type(string_t) string
47+
end function
48+
4149
end interface
4250

4351
interface
@@ -66,6 +74,18 @@ elemental module function get_json_key(self) result(unquoted_key)
6674
type(string_t) unquoted_key
6775
end function
6876

77+
elemental module function file_extension(self) result(extension)
78+
!! result contains all characters in file_name after the last dot (.)
79+
class(string_t), intent(in) :: self
80+
type(string_t) extension
81+
end function
82+
83+
pure module function base_name(self) result(base)
84+
!! result contains all characters in file_name before the last dot (.)
85+
class(string_t), intent(in) :: self
86+
type(string_t) base
87+
end function
88+
6989
elemental module function get_json_real(self, key, mold) result(value_)
7090
implicit none
7191
class(string_t), intent(in) :: self, key

src/sourcery/sourcery_string_s.f90

Lines changed: 35 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
@@ -51,6 +58,34 @@
5158

5259
end procedure
5360

61+
module procedure file_extension
62+
character(len=:), allocatable :: name_
63+
64+
name_ = trim(adjustl(self%string()))
65+
66+
associate( dot_location => index(name_, '.', back=.true.) )
67+
if (dot_location < len(name_)) then
68+
extension = trim(adjustl(name_(dot_location+1:)))
69+
else
70+
extension = ""
71+
end if
72+
end associate
73+
end procedure
74+
75+
module procedure base_name
76+
character(len=:), allocatable :: name_
77+
78+
name_ = self%string()
79+
80+
associate(dot_location => index(name_, '.', back=.true.) )
81+
if (dot_location < len(name_)) then
82+
base = trim(adjustl(name_(1:dot_location-1)))
83+
else
84+
base = ""
85+
end if
86+
end associate
87+
end procedure
88+
5489
module procedure get_json_real
5590
character(len=:), allocatable :: raw_line, string_value
5691

test/string_test.f90

Lines changed: 25 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,10 @@ 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()), &
38+
test_result_t('extracting file base name', extracts_file_base_name()), &
39+
test_result_t('extracting file name extension', extracts_file_name_extension()) &
3840
]
3941
end function
4042

@@ -151,4 +153,25 @@ function supports_concatenation_operator() result(passed)
151153
end associate
152154
end function
153155

156+
function constructs_from_default_integer() result(passed)
157+
logical passed
158+
associate(string => string_t(1234567890))
159+
passed = adjustl(trim(string%string())) == "1234567890"
160+
end associate
161+
end function
162+
163+
function extracts_file_base_name() result(passed)
164+
logical passed
165+
associate(string => string_t(" foo .bar.too "))
166+
passed = string%base_name() == "foo .bar"
167+
end associate
168+
end function
169+
170+
function extracts_file_name_extension() result(passed)
171+
logical passed
172+
associate(string => string_t(" foo .bar.too "))
173+
passed = string%file_extension() == "too"
174+
end associate
175+
end function
176+
154177
end module string_test_m

0 commit comments

Comments
 (0)