From 5085ac5d75573c137851008e7deee25bb217a1eb Mon Sep 17 00:00:00 2001 From: "supritsj@Arch" Date: Wed, 2 Jul 2025 23:13:59 +0530 Subject: [PATCH 1/4] index handling --- src/stdlib_strings.fypp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/stdlib_strings.fypp b/src/stdlib_strings.fypp index 0d3904fbf..30e32f209 100644 --- a/src/stdlib_strings.fypp +++ b/src/stdlib_strings.fypp @@ -302,11 +302,17 @@ contains last = len(string) nsub = len(substring) - if (nsub > 0) then - do while(string(last-nsub+1:last) == substring) + if (nsub > 0 .and. nsub <= last) then + do while(last >= nsub .and. string(last-nsub+1:last) == substring) last = last - nsub end do end if + + if (last <= 0) then + chomped_string = '' + return + end if + chomped_string = string(1:last) end function chomp_substring_char_char From 0ad37564e3d481e9a4e0e65919023db160c8725a Mon Sep 17 00:00:00 2001 From: Federico Perini Date: Thu, 3 Jul 2025 15:53:34 +0200 Subject: [PATCH 2/4] Update src/stdlib_strings.fypp --- src/stdlib_strings.fypp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/stdlib_strings.fypp b/src/stdlib_strings.fypp index 30e32f209..f0b2c9eee 100644 --- a/src/stdlib_strings.fypp +++ b/src/stdlib_strings.fypp @@ -310,10 +310,9 @@ contains if (last <= 0) then chomped_string = '' - return - end if - - chomped_string = string(1:last) + else + chomped_string = string(1:last) + end if end function chomp_substring_char_char From c81f267ba65a2143e0478988e204ac6f8c194ddc Mon Sep 17 00:00:00 2001 From: "supritsj@Arch" Date: Thu, 3 Jul 2025 19:30:02 +0530 Subject: [PATCH 3/4] add a simple test case --- test/string/test_string_strip_chomp.f90 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/string/test_string_strip_chomp.f90 b/test/string/test_string_strip_chomp.f90 index 095a53c3b..b7a9b9c93 100644 --- a/test/string/test_string_strip_chomp.f90 +++ b/test/string/test_string_strip_chomp.f90 @@ -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) From 177e09dd9392e7cdae36764ceeb0a6924d7ce864 Mon Sep 17 00:00:00 2001 From: "supritsj@Arch" Date: Thu, 3 Jul 2025 19:55:17 +0530 Subject: [PATCH 4/4] remove short-circuiting logic --- src/stdlib_strings.fypp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/stdlib_strings.fypp b/src/stdlib_strings.fypp index f0b2c9eee..9cacb1f72 100644 --- a/src/stdlib_strings.fypp +++ b/src/stdlib_strings.fypp @@ -303,8 +303,12 @@ contains last = len(string) nsub = len(substring) if (nsub > 0 .and. nsub <= last) then - do while(last >= nsub .and. string(last-nsub+1:last) == substring) - last = last - nsub + do while(last >= nsub) + if (string(last-nsub+1:last) == substring) then + last = last - nsub + else + exit + end if end do end if