diff options
Diffstat (limited to 'libgfortran/intrinsics/string_intrinsics_inc.c')
-rw-r--r-- | libgfortran/intrinsics/string_intrinsics_inc.c | 29 |
1 files changed, 16 insertions, 13 deletions
diff --git a/libgfortran/intrinsics/string_intrinsics_inc.c b/libgfortran/intrinsics/string_intrinsics_inc.c index 350642d3354..bfec683f528 100644 --- a/libgfortran/intrinsics/string_intrinsics_inc.c +++ b/libgfortran/intrinsics/string_intrinsics_inc.c @@ -177,23 +177,25 @@ string_trim (gfc_charlen_type *len, CHARTYPE **dest, gfc_charlen_type slen, gfc_charlen_type string_len_trim (gfc_charlen_type len, const CHARTYPE *s) { - const gfc_charlen_type long_len = (gfc_charlen_type) sizeof (unsigned long); - gfc_charlen_type i; + if (len <= 0) + return 0; - i = len - 1; + const size_t long_len = sizeof (unsigned long); + + size_t i = len - 1; /* If we've got the standard (KIND=1) character type, we scan the string in long word chunks to speed it up (until a long word is hit that does not consist of ' 's). */ if (sizeof (CHARTYPE) == 1 && i >= long_len) { - int starting; + size_t starting; unsigned long blank_longword; /* Handle the first characters until we're aligned on a long word boundary. Actually, s + i + 1 must be properly aligned, because s + i will be the last byte of a long word read. */ - starting = ((unsigned long) + starting = ( #ifdef __INTPTR_TYPE__ (__INTPTR_TYPE__) #endif @@ -224,14 +226,15 @@ string_len_trim (gfc_charlen_type len, const CHARTYPE *s) break; } } - - /* Now continue for the last characters with naive approach below. */ - assert (i >= 0); } /* Simply look for the first non-blank character. */ - while (i >= 0 && s[i] == ' ') - --i; + while (s[i] == ' ') + { + if (i == 0) + return 0; + --i; + } return i + 1; } @@ -327,12 +330,12 @@ string_scan (gfc_charlen_type slen, const CHARTYPE *str, if (back) { - for (i = slen - 1; i >= 0; i--) + for (i = slen; i != 0; i--) { for (j = 0; j < setlen; j++) { - if (str[i] == set[j]) - return (i + 1); + if (str[i - 1] == set[j]) + return i; } } } |