Skip to content

fix: index handling in chomp #1012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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