Skip to content

Commit 41a1cfc

Browse files
wassup05perazz
andauthored
fix: index handling in chomp (#1012)
* index handling * Update src/stdlib_strings.fypp * add a simple test case * remove short-circuiting logic --------- Co-authored-by: Federico Perini <federico.perini@gmail.com>
1 parent 7b572c7 commit 41a1cfc

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/stdlib_strings.fypp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,21 @@ contains
302302

303303
last = len(string)
304304
nsub = len(substring)
305-
if (nsub > 0) then
306-
do while(string(last-nsub+1:last) == substring)
307-
last = last - nsub
305+
if (nsub > 0 .and. nsub <= last) then
306+
do while(last >= nsub)
307+
if (string(last-nsub+1:last) == substring) then
308+
last = last - nsub
309+
else
310+
exit
311+
end if
308312
end do
309313
end if
310-
chomped_string = string(1:last)
314+
315+
if (last <= 0) then
316+
chomped_string = ''
317+
else
318+
chomped_string = string(1:last)
319+
end if
311320

312321
end function chomp_substring_char_char
313322

test/string/test_string_strip_chomp.f90

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ subroutine test_chomp_substring_char(error)
151151
call check(error, chomp("hellooooo", "oo") == "hello")
152152
if (allocated(error)) return
153153
call check(error, chomp("hellooooo", substring="oo") == "hello")
154+
if (allocated(error)) return
155+
call check(error, chomp("helhel", substring="hel") == "")
154156
end subroutine test_chomp_substring_char
155157

156158
subroutine test_chomp_substring_string(error)

0 commit comments

Comments
 (0)