Skip to content

Commit 5e69e27

Browse files
committed
refac(string_functions_{m,s}): mv to string_{m,s}
1 parent 71d1c15 commit 5e69e27

File tree

5 files changed

+59
-87
lines changed

5 files changed

+59
-87
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: 14 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
@@ -72,6 +74,18 @@ elemental module function get_json_key(self) result(unquoted_key)
7274
type(string_t) unquoted_key
7375
end function
7476

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+
7589
elemental module function get_json_real(self, key, mold) result(value_)
7690
implicit none
7791
class(string_t), intent(in) :: self, key

src/sourcery/sourcery_string_s.f90

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,34 @@
5858

5959
end procedure
6060

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+
6189
module procedure get_json_real
6290
character(len=:), allocatable :: raw_line, string_value
6391

test/string_test.f90

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ function results() result(test_results)
3434
test_result_t('assigning a string_t object to a character variable', assigns_string_t_to_character()), &
3535
test_result_t('assigning a character variable to a string_t object', assigns_character_to_string_t()), &
3636
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()) &
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

@@ -158,4 +160,18 @@ function constructs_from_default_integer() result(passed)
158160
end associate
159161
end function
160162

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+
161177
end module string_test_m

0 commit comments

Comments
 (0)