/* String intrinsics helper functions.
- Copyright 2002, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
+ Copyright (C) 2002-2020 Free Software Foundation, Inc.
This file is part of the GNU Fortran runtime library (libgfortran).
gfc_charlen_type, const CHARTYPE *);
export_proto(concat_string);
-extern gfc_charlen_type string_len_trim (gfc_charlen_type, const CHARTYPE *);
-export_proto(string_len_trim);
-
extern void adjustl (CHARTYPE *, gfc_charlen_type, const CHARTYPE *);
export_proto(adjustl);
gfc_charlen_type len;
int res;
- res = memcmp (s1, s2, ((len1 < len2) ? len1 : len2) * sizeof (CHARTYPE));
+ /* Placate the sanitizer. */
+ if (!s1 && !s2)
+ return 0;
+ if (!s1)
+ return -1;
+ if (!s2)
+ return 1;
+
+ res = MEMCMP (s1, s2, ((len1 < len2) ? len1 : len2));
if (res != 0)
return res;
else
{
/* Allocate space for result string. */
- *dest = internal_malloc_size (*len * sizeof (CHARTYPE));
+ *dest = xmallocarray (*len, sizeof (CHARTYPE));
/* Copy string if necessary. */
memcpy (*dest, src, *len * sizeof (CHARTYPE));
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;
+
+ const size_t long_len = sizeof (unsigned long);
- i = len - 1;
+ 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
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;
}
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;
}
}
}
*dest = &zero_length_string;
else
{
- CHARTYPE *tmp = internal_malloc_size (*rlen * sizeof (CHARTYPE));
+ CHARTYPE *tmp = xmallocarray (*rlen, sizeof (CHARTYPE));
memcpy (tmp, res, reslen * sizeof (CHARTYPE));
MEMSET (&tmp[reslen], ' ', *rlen - reslen);
*dest = tmp;