https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691
--- Comment #3 from kargl at gcc dot gnu.org ---
There is no need to special case the substring length of 1 case with my
suggested patch. Here's an update to eliminate the special case.
diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc
index 233cc42137f..fa9938d6a1e 100644
--- a/gcc/fortran/simplify.cc
+++ b/gcc/fortran/simplify.cc
@@ -3589,49 +3589,31 @@ gfc_simplify_index (gfc_expr *x, gfc_expr *y, gfc_expr
*b, gfc_expr *kind)
mpz_set_si (result->value.integer, len + 1);
return result;
}
- else if (lensub == 1)
+ else
{
- for (i = 0; i < len; i++)
+ /* Start at the tail of the string, offset by the length of the
+ substring, and search for a match moving towards the head of
+ string. */
+ for (i = len - lensub; i >= 0; i--)
{
- for (j = 0; j < lensub; j++)
+ /* If the first character does not match, then the rest of the
+ string cannot match. */
+ if (x->value.character.string[i]
+ == y->value.character.string[0])
{
- if (y->value.character.string[j]
- == x->value.character.string[len - i])
+
+ /* Compare substring to starting location in string. */
+ for (j = 0, count = 0; j < lensub; j++, count++)
{
- index = len - i + 1;
- goto done;
+ if (x->value.character.string[i + j]
+ != y->value.character.string[j])
+ break;
}
- }
- }
- }
- else
- {
- for (i = 0; i < len; i++)
- {
- for (j = 0; j < lensub; j++)
- {
- if (y->value.character.string[j]
- == x->value.character.string[len - i])
+
+ if (count == lensub)
{
- start = len - i;
- if (start <= len - lensub)
- {
- count = 0;
- for (k = 0; k < lensub; k++)
- if (y->value.character.string[k]
- == x->value.character.string[k + start])
- count++;
-
- if (count == lensub)
- {
- index = start + 1;
- goto done;
- }
- }
- else
- {
- continue;
- }
+ index = i + 1;
+ goto done;
}
}
}