Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/stdlib_strings.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,21 @@ contains

last = len(string)
nsub = len(substring)
if (nsub > 0) then
do while(string(last-nsub+1:last) == substring)
last = last - nsub
if (nsub > 0 .and. nsub <= last) then
do while(last >= nsub)
if (string(last-nsub+1:last) == substring) then
last = last - nsub
else
exit
end if
end do
end if
chomped_string = string(1:last)

if (last <= 0) then
chomped_string = ''
else
chomped_string = string(1:last)
end if

end function chomp_substring_char_char

Expand Down
2 changes: 2 additions & 0 deletions test/string/test_string_strip_chomp.f90
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ subroutine test_chomp_substring_char(error)
call check(error, chomp("hellooooo", "oo") == "hello")
if (allocated(error)) return
call check(error, chomp("hellooooo", substring="oo") == "hello")
if (allocated(error)) return
call check(error, chomp("helhel", substring="hel") == "")
end subroutine test_chomp_substring_char

subroutine test_chomp_substring_string(error)
Expand Down
Loading