mirror of
https://gcc.gnu.org/git/gcc.git
synced 2025-01-04 10:03:47 +08:00
1a0fd3d33a
2012-03-26 Janne Blomqvist <jb@gcc.gnu.org> * runtime/memory.c (get_mem): Rename to xmalloc, check for zero size. (internal_malloc_size): Remove. * libgfortran.h (get_mem): Rename to xmalloc. (internal_malloc_size): Remove prototype. * Makefile.am (i_iall_c): Fix dependency. (i_iany_c): Likewise. (i_iparity_c): Likewise. (i_norm2_c): Likewise. (i_parity_c): Likewise. * Makefile.in: Regenerated. * m4/parity.m4: Fix typo in comment. * generated/all_l1.c: Regenerated. * generated/all_l16.c: Regenerated. * generated/all_l2.c: Regenerated. * generated/all_l4.c: Regenerated. * generated/all_l8.c: Regenerated. * generated/any_l1.c: Regenerated. * generated/any_l16.c: Regenerated. * generated/any_l2.c: Regenerated. * generated/any_l4.c: Regenerated. * generated/any_l8.c: Regenerated. * generated/bessel_r10.c: Regenerated. * generated/bessel_r16.c: Regenerated. * generated/bessel_r4.c: Regenerated. * generated/bessel_r8.c: Regenerated. * generated/count_16_l.c: Regenerated. * generated/count_1_l.c: Regenerated. * generated/count_2_l.c: Regenerated. * generated/count_4_l.c: Regenerated. * generated/count_8_l.c: Regenerated. * generated/cshift1_16.c: Regenerated. * generated/cshift1_4.c: Regenerated. * generated/cshift1_8.c: Regenerated. * generated/eoshift1_16.c: Regenerated. * generated/eoshift1_4.c: Regenerated. * generated/eoshift1_8.c: Regenerated. * generated/eoshift3_16.c: Regenerated. * generated/eoshift3_4.c: Regenerated. * generated/eoshift3_8.c: Regenerated. * generated/iall_i1.c: Regenerated. * generated/iall_i16.c: Regenerated. * generated/iall_i2.c: Regenerated. * generated/iall_i4.c: Regenerated. * generated/iall_i8.c: Regenerated. * generated/iany_i1.c: Regenerated. * generated/iany_i16.c: Regenerated. * generated/iany_i2.c: Regenerated. * generated/iany_i4.c: Regenerated. * generated/iany_i8.c: Regenerated. * generated/in_pack_c10.c: Regenerated. * generated/in_pack_c16.c: Regenerated. * generated/in_pack_c4.c: Regenerated. * generated/in_pack_c8.c: Regenerated. * generated/in_pack_i1.c: Regenerated. * generated/in_pack_i16.c: Regenerated. * generated/in_pack_i2.c: Regenerated. * generated/in_pack_i4.c: Regenerated. * generated/in_pack_i8.c: Regenerated. * generated/in_pack_r10.c: Regenerated. * generated/in_pack_r16.c: Regenerated. * generated/in_pack_r4.c: Regenerated. * generated/in_pack_r8.c: Regenerated. * generated/iparity_i1.c: Regenerated. * generated/iparity_i16.c: Regenerated. * generated/iparity_i2.c: Regenerated. * generated/iparity_i4.c: Regenerated. * generated/iparity_i8.c: Regenerated. * generated/matmul_c10.c: Regenerated. * generated/matmul_c16.c: Regenerated. * generated/matmul_c4.c: Regenerated. * generated/matmul_c8.c: Regenerated. * generated/matmul_i1.c: Regenerated. * generated/matmul_i16.c: Regenerated. * generated/matmul_i2.c: Regenerated. * generated/matmul_i4.c: Regenerated. * generated/matmul_i8.c: Regenerated. * generated/matmul_l16.c: Regenerated. * generated/matmul_l4.c: Regenerated. * generated/matmul_l8.c: Regenerated. * generated/matmul_r10.c: Regenerated. * generated/matmul_r16.c: Regenerated. * generated/matmul_r4.c: Regenerated. * generated/matmul_r8.c: Regenerated. * generated/maxloc0_16_i1.c: Regenerated. * generated/maxloc0_16_i16.c: Regenerated. * generated/maxloc0_16_i2.c: Regenerated. * generated/maxloc0_16_i4.c: Regenerated. * generated/maxloc0_16_i8.c: Regenerated. * generated/maxloc0_16_r10.c: Regenerated. * generated/maxloc0_16_r16.c: Regenerated. * generated/maxloc0_16_r4.c: Regenerated. * generated/maxloc0_16_r8.c: Regenerated. * generated/maxloc0_4_i1.c: Regenerated. * generated/maxloc0_4_i16.c: Regenerated. * generated/maxloc0_4_i2.c: Regenerated. * generated/maxloc0_4_i4.c: Regenerated. * generated/maxloc0_4_i8.c: Regenerated. * generated/maxloc0_4_r10.c: Regenerated. * generated/maxloc0_4_r16.c: Regenerated. * generated/maxloc0_4_r4.c: Regenerated. * generated/maxloc0_4_r8.c: Regenerated. * generated/maxloc0_8_i1.c: Regenerated. * generated/maxloc0_8_i16.c: Regenerated. * generated/maxloc0_8_i2.c: Regenerated. * generated/maxloc0_8_i4.c: Regenerated. * generated/maxloc0_8_i8.c: Regenerated. * generated/maxloc0_8_r10.c: Regenerated. * generated/maxloc0_8_r16.c: Regenerated. * generated/maxloc0_8_r4.c: Regenerated. * generated/maxloc0_8_r8.c: Regenerated. * generated/maxloc1_16_i1.c: Regenerated. * generated/maxloc1_16_i16.c: Regenerated. * generated/maxloc1_16_i2.c: Regenerated. * generated/maxloc1_16_i4.c: Regenerated. * generated/maxloc1_16_i8.c: Regenerated. * generated/maxloc1_16_r10.c: Regenerated. * generated/maxloc1_16_r16.c: Regenerated. * generated/maxloc1_16_r4.c: Regenerated. * generated/maxloc1_16_r8.c: Regenerated. * generated/maxloc1_4_i1.c: Regenerated. * generated/maxloc1_4_i16.c: Regenerated. * generated/maxloc1_4_i2.c: Regenerated. * generated/maxloc1_4_i4.c: Regenerated. * generated/maxloc1_4_i8.c: Regenerated. * generated/maxloc1_4_r10.c: Regenerated. * generated/maxloc1_4_r16.c: Regenerated. * generated/maxloc1_4_r4.c: Regenerated. * generated/maxloc1_4_r8.c: Regenerated. * generated/maxloc1_8_i1.c: Regenerated. * generated/maxloc1_8_i16.c: Regenerated. * generated/maxloc1_8_i2.c: Regenerated. * generated/maxloc1_8_i4.c: Regenerated. * generated/maxloc1_8_i8.c: Regenerated. * generated/maxloc1_8_r10.c: Regenerated. * generated/maxloc1_8_r16.c: Regenerated. * generated/maxloc1_8_r4.c: Regenerated. * generated/maxloc1_8_r8.c: Regenerated. * generated/maxval_i1.c: Regenerated. * generated/maxval_i16.c: Regenerated. * generated/maxval_i2.c: Regenerated. * generated/maxval_i4.c: Regenerated. * generated/maxval_i8.c: Regenerated. * generated/maxval_r10.c: Regenerated. * generated/maxval_r16.c: Regenerated. * generated/maxval_r4.c: Regenerated. * generated/maxval_r8.c: Regenerated. * generated/minloc0_16_i1.c: Regenerated. * generated/minloc0_16_i16.c: Regenerated. * generated/minloc0_16_i2.c: Regenerated. * generated/minloc0_16_i4.c: Regenerated. * generated/minloc0_16_i8.c: Regenerated. * generated/minloc0_16_r10.c: Regenerated. * generated/minloc0_16_r16.c: Regenerated. * generated/minloc0_16_r4.c: Regenerated. * generated/minloc0_16_r8.c: Regenerated. * generated/minloc0_4_i1.c: Regenerated. * generated/minloc0_4_i16.c: Regenerated. * generated/minloc0_4_i2.c: Regenerated. * generated/minloc0_4_i4.c: Regenerated. * generated/minloc0_4_i8.c: Regenerated. * generated/minloc0_4_r10.c: Regenerated. * generated/minloc0_4_r16.c: Regenerated. * generated/minloc0_4_r4.c: Regenerated. * generated/minloc0_4_r8.c: Regenerated. * generated/minloc0_8_i1.c: Regenerated. * generated/minloc0_8_i16.c: Regenerated. * generated/minloc0_8_i2.c: Regenerated. * generated/minloc0_8_i4.c: Regenerated. * generated/minloc0_8_i8.c: Regenerated. * generated/minloc0_8_r10.c: Regenerated. * generated/minloc0_8_r16.c: Regenerated. * generated/minloc0_8_r4.c: Regenerated. * generated/minloc0_8_r8.c: Regenerated. * generated/minloc1_16_i1.c: Regenerated. * generated/minloc1_16_i16.c: Regenerated. * generated/minloc1_16_i2.c: Regenerated. * generated/minloc1_16_i4.c: Regenerated. * generated/minloc1_16_i8.c: Regenerated. * generated/minloc1_16_r10.c: Regenerated. * generated/minloc1_16_r16.c: Regenerated. * generated/minloc1_16_r4.c: Regenerated. * generated/minloc1_16_r8.c: Regenerated. * generated/minloc1_4_i1.c: Regenerated. * generated/minloc1_4_i16.c: Regenerated. * generated/minloc1_4_i2.c: Regenerated. * generated/minloc1_4_i4.c: Regenerated. * generated/minloc1_4_i8.c: Regenerated. * generated/minloc1_4_r10.c: Regenerated. * generated/minloc1_4_r16.c: Regenerated. * generated/minloc1_4_r4.c: Regenerated. * generated/minloc1_4_r8.c: Regenerated. * generated/minloc1_8_i1.c: Regenerated. * generated/minloc1_8_i16.c: Regenerated. * generated/minloc1_8_i2.c: Regenerated. * generated/minloc1_8_i4.c: Regenerated. * generated/minloc1_8_i8.c: Regenerated. * generated/minloc1_8_r10.c: Regenerated. * generated/minloc1_8_r16.c: Regenerated. * generated/minloc1_8_r4.c: Regenerated. * generated/minloc1_8_r8.c: Regenerated. * generated/minval_i1.c: Regenerated. * generated/minval_i16.c: Regenerated. * generated/minval_i2.c: Regenerated. * generated/minval_i4.c: Regenerated. * generated/minval_i8.c: Regenerated. * generated/minval_r10.c: Regenerated. * generated/minval_r16.c: Regenerated. * generated/minval_r4.c: Regenerated. * generated/minval_r8.c: Regenerated. * generated/norm2_r10.c: Regenerated. * generated/norm2_r16.c: Regenerated. * generated/norm2_r4.c: Regenerated. * generated/norm2_r8.c: Regenerated. * generated/pack_c10.c: Regenerated. * generated/pack_c16.c: Regenerated. * generated/pack_c4.c: Regenerated. * generated/pack_c8.c: Regenerated. * generated/pack_i1.c: Regenerated. * generated/pack_i16.c: Regenerated. * generated/pack_i2.c: Regenerated. * generated/pack_i4.c: Regenerated. * generated/pack_i8.c: Regenerated. * generated/pack_r10.c: Regenerated. * generated/pack_r16.c: Regenerated. * generated/pack_r4.c: Regenerated. * generated/pack_r8.c: Regenerated. * generated/parity_l1.c: Regenerated. * generated/parity_l16.c: Regenerated. * generated/parity_l2.c: Regenerated. * generated/parity_l4.c: Regenerated. * generated/parity_l8.c: Regenerated. * generated/product_c10.c: Regenerated. * generated/product_c16.c: Regenerated. * generated/product_c4.c: Regenerated. * generated/product_c8.c: Regenerated. * generated/product_i1.c: Regenerated. * generated/product_i16.c: Regenerated. * generated/product_i2.c: Regenerated. * generated/product_i4.c: Regenerated. * generated/product_i8.c: Regenerated. * generated/product_r10.c: Regenerated. * generated/product_r16.c: Regenerated. * generated/product_r4.c: Regenerated. * generated/product_r8.c: Regenerated. * generated/reshape_c10.c: Regenerated. * generated/reshape_c16.c: Regenerated. * generated/reshape_c4.c: Regenerated. * generated/reshape_c8.c: Regenerated. * generated/reshape_i16.c: Regenerated. * generated/reshape_i4.c: Regenerated. * generated/reshape_i8.c: Regenerated. * generated/reshape_r10.c: Regenerated. * generated/reshape_r16.c: Regenerated. * generated/reshape_r4.c: Regenerated. * generated/reshape_r8.c: Regenerated. * generated/shape_i16.c: Regenerated. * generated/shape_i4.c: Regenerated. * generated/shape_i8.c: Regenerated. * generated/spread_c10.c: Regenerated. * generated/spread_c16.c: Regenerated. * generated/spread_c4.c: Regenerated. * generated/spread_c8.c: Regenerated. * generated/spread_i1.c: Regenerated. * generated/spread_i16.c: Regenerated. * generated/spread_i2.c: Regenerated. * generated/spread_i4.c: Regenerated. * generated/spread_i8.c: Regenerated. * generated/spread_r10.c: Regenerated. * generated/spread_r16.c: Regenerated. * generated/spread_r4.c: Regenerated. * generated/spread_r8.c: Regenerated. * generated/sum_c10.c: Regenerated. * generated/sum_c16.c: Regenerated. * generated/sum_c4.c: Regenerated. * generated/sum_c8.c: Regenerated. * generated/sum_i1.c: Regenerated. * generated/sum_i16.c: Regenerated. * generated/sum_i2.c: Regenerated. * generated/sum_i4.c: Regenerated. * generated/sum_i8.c: Regenerated. * generated/sum_r10.c: Regenerated. * generated/sum_r16.c: Regenerated. * generated/sum_r4.c: Regenerated. * generated/sum_r8.c: Regenerated. * generated/transpose_c10.c: Regenerated. * generated/transpose_c16.c: Regenerated. * generated/transpose_c4.c: Regenerated. * generated/transpose_c8.c: Regenerated. * generated/transpose_i16.c: Regenerated. * generated/transpose_i4.c: Regenerated. * generated/transpose_i8.c: Regenerated. * generated/transpose_r10.c: Regenerated. * generated/transpose_r16.c: Regenerated. * generated/transpose_r4.c: Regenerated. * generated/transpose_r8.c: Regenerated. * generated/unpack_c10.c: Regenerated. * generated/unpack_c16.c: Regenerated. * generated/unpack_c4.c: Regenerated. * generated/unpack_c8.c: Regenerated. * generated/unpack_i1.c: Regenerated. * generated/unpack_i16.c: Regenerated. * generated/unpack_i2.c: Regenerated. * generated/unpack_i4.c: Regenerated. * generated/unpack_i8.c: Regenerated. * generated/unpack_r10.c: Regenerated. * generated/unpack_r16.c: Regenerated. * generated/unpack_r4.c: Regenerated. * generated/unpack_r8.c: Regenerated. * intrinsics/cshift0.c: Use xmalloc. * intrinsics/ctime.c: Likewise. * intrinsics/date_and_time.c: Likewise. * intrinsics/eoshift0.c: Likewise. * intrinsics/eoshift2.c: Likewise. * intrinsics/getlog.c: Likewise. * intrinsics/pack_generic.c: Likewise. * intrinsics/reshape_generic.c: Likewise. * intrinsics/spread_generic.c: Likewise. * intrinsics/string_intrinsics_inc.c: Likewise. * intrinsics/transpose_generic.c: Likewise. * intrinsics/unpack_generic.c: Likewise. * io/fbuf.c: Likewise. * io/format.c: Likewise. * io/intrinsics.c: Likewise. * io/list_read.c: Likewise. * io/open.c: Likewise. * io/transfer.c: Likewise. * io/unit.c: Likewise. * io/unix.c: Likewise. * io/write.c: Likewise. * m4/bessel.m4: Likewise. * m4/cshift1.m4: Likewise. * m4/eoshift1.m4: Likewise. * m4/eoshift3.m4: Likewise. * m4/iforeach.m4: Likewise. * m4/ifunction.m4: Likewise. * m4/ifunction_logical.m4: Likewise. * m4/in_pack.m4: Likewise. * m4/matmul.m4: Likewise. * m4/matmull.m4: Likewise. * m4/pack.m4: Likewise. * m4/reshape.m4: Likewise. * m4/shape.m4: Likewise. * m4/spread.m4: Likewise. * m4/transpose.m4: Likewise. * m4/unpack.m4: Likewise. * runtime/convert_char.c: Likewise. * runtime/environ.c: Likewise. * runtime/in_pack_generic.c: Likewise. From-SVN: r185806
454 lines
10 KiB
C
454 lines
10 KiB
C
/* String intrinsics helper functions.
|
|
Copyright 2002, 2005, 2007, 2008, 2009, 2012 Free Software Foundation, Inc.
|
|
|
|
This file is part of the GNU Fortran runtime library (libgfortran).
|
|
|
|
Libgfortran is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU General Public
|
|
License as published by the Free Software Foundation; either
|
|
version 3 of the License, or (at your option) any later version.
|
|
|
|
Libgfortran is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
Under Section 7 of GPL version 3, you are granted additional
|
|
permissions described in the GCC Runtime Library Exception, version
|
|
3.1, as published by the Free Software Foundation.
|
|
|
|
You should have received a copy of the GNU General Public License and
|
|
a copy of the GCC Runtime Library Exception along with this program;
|
|
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
/* Rename the functions. */
|
|
#define concat_string SUFFIX(concat_string)
|
|
#define string_len_trim SUFFIX(string_len_trim)
|
|
#define adjustl SUFFIX(adjustl)
|
|
#define adjustr SUFFIX(adjustr)
|
|
#define string_index SUFFIX(string_index)
|
|
#define string_scan SUFFIX(string_scan)
|
|
#define string_verify SUFFIX(string_verify)
|
|
#define string_trim SUFFIX(string_trim)
|
|
#define string_minmax SUFFIX(string_minmax)
|
|
#define zero_length_string SUFFIX(zero_length_string)
|
|
#define compare_string SUFFIX(compare_string)
|
|
|
|
|
|
/* The prototypes. */
|
|
|
|
extern void concat_string (gfc_charlen_type, CHARTYPE *,
|
|
gfc_charlen_type, const CHARTYPE *,
|
|
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);
|
|
|
|
extern void adjustr (CHARTYPE *, gfc_charlen_type, const CHARTYPE *);
|
|
export_proto(adjustr);
|
|
|
|
extern gfc_charlen_type string_index (gfc_charlen_type, const CHARTYPE *,
|
|
gfc_charlen_type, const CHARTYPE *,
|
|
GFC_LOGICAL_4);
|
|
export_proto(string_index);
|
|
|
|
extern gfc_charlen_type string_scan (gfc_charlen_type, const CHARTYPE *,
|
|
gfc_charlen_type, const CHARTYPE *,
|
|
GFC_LOGICAL_4);
|
|
export_proto(string_scan);
|
|
|
|
extern gfc_charlen_type string_verify (gfc_charlen_type, const CHARTYPE *,
|
|
gfc_charlen_type, const CHARTYPE *,
|
|
GFC_LOGICAL_4);
|
|
export_proto(string_verify);
|
|
|
|
extern void string_trim (gfc_charlen_type *, CHARTYPE **, gfc_charlen_type,
|
|
const CHARTYPE *);
|
|
export_proto(string_trim);
|
|
|
|
extern void string_minmax (gfc_charlen_type *, CHARTYPE **, int, int, ...);
|
|
export_proto(string_minmax);
|
|
|
|
|
|
/* Use for functions which can return a zero-length string. */
|
|
static CHARTYPE zero_length_string = 0;
|
|
|
|
|
|
/* Strings of unequal length are extended with pad characters. */
|
|
|
|
int
|
|
compare_string (gfc_charlen_type len1, const CHARTYPE *s1,
|
|
gfc_charlen_type len2, const CHARTYPE *s2)
|
|
{
|
|
const UCHARTYPE *s;
|
|
gfc_charlen_type len;
|
|
int res;
|
|
|
|
res = MEMCMP (s1, s2, ((len1 < len2) ? len1 : len2));
|
|
if (res != 0)
|
|
return res;
|
|
|
|
if (len1 == len2)
|
|
return 0;
|
|
|
|
if (len1 < len2)
|
|
{
|
|
len = len2 - len1;
|
|
s = (UCHARTYPE *) &s2[len1];
|
|
res = -1;
|
|
}
|
|
else
|
|
{
|
|
len = len1 - len2;
|
|
s = (UCHARTYPE *) &s1[len2];
|
|
res = 1;
|
|
}
|
|
|
|
while (len--)
|
|
{
|
|
if (*s != ' ')
|
|
{
|
|
if (*s > ' ')
|
|
return res;
|
|
else
|
|
return -res;
|
|
}
|
|
s++;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
iexport(compare_string);
|
|
|
|
|
|
/* The destination and source should not overlap. */
|
|
|
|
void
|
|
concat_string (gfc_charlen_type destlen, CHARTYPE * dest,
|
|
gfc_charlen_type len1, const CHARTYPE * s1,
|
|
gfc_charlen_type len2, const CHARTYPE * s2)
|
|
{
|
|
if (len1 >= destlen)
|
|
{
|
|
memcpy (dest, s1, destlen * sizeof (CHARTYPE));
|
|
return;
|
|
}
|
|
memcpy (dest, s1, len1 * sizeof (CHARTYPE));
|
|
dest += len1;
|
|
destlen -= len1;
|
|
|
|
if (len2 >= destlen)
|
|
{
|
|
memcpy (dest, s2, destlen * sizeof (CHARTYPE));
|
|
return;
|
|
}
|
|
|
|
memcpy (dest, s2, len2 * sizeof (CHARTYPE));
|
|
MEMSET (&dest[len2], ' ', destlen - len2);
|
|
}
|
|
|
|
|
|
/* Return string with all trailing blanks removed. */
|
|
|
|
void
|
|
string_trim (gfc_charlen_type *len, CHARTYPE **dest, gfc_charlen_type slen,
|
|
const CHARTYPE *src)
|
|
{
|
|
*len = string_len_trim (slen, src);
|
|
|
|
if (*len == 0)
|
|
*dest = &zero_length_string;
|
|
else
|
|
{
|
|
/* Allocate space for result string. */
|
|
*dest = xmalloc (*len * sizeof (CHARTYPE));
|
|
|
|
/* Copy string if necessary. */
|
|
memcpy (*dest, src, *len * sizeof (CHARTYPE));
|
|
}
|
|
}
|
|
|
|
|
|
/* The length of a string not including trailing blanks. */
|
|
|
|
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;
|
|
|
|
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;
|
|
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)
|
|
#ifdef __INTPTR_TYPE__
|
|
(__INTPTR_TYPE__)
|
|
#endif
|
|
(s + i + 1)) % long_len;
|
|
i -= starting;
|
|
for (; starting > 0; --starting)
|
|
if (s[i + starting] != ' ')
|
|
return i + starting + 1;
|
|
|
|
/* Handle the others in a batch until first non-blank long word is
|
|
found. Here again, s + i is the last byte of the current chunk,
|
|
to it starts at s + i - sizeof (long) + 1. */
|
|
|
|
#if __SIZEOF_LONG__ == 4
|
|
blank_longword = 0x20202020L;
|
|
#elif __SIZEOF_LONG__ == 8
|
|
blank_longword = 0x2020202020202020L;
|
|
#else
|
|
#error Invalid size of long!
|
|
#endif
|
|
|
|
while (i >= long_len)
|
|
{
|
|
i -= long_len;
|
|
if (*((unsigned long*) (s + i + 1)) != blank_longword)
|
|
{
|
|
i += long_len;
|
|
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;
|
|
return i + 1;
|
|
}
|
|
|
|
|
|
/* Find a substring within a string. */
|
|
|
|
gfc_charlen_type
|
|
string_index (gfc_charlen_type slen, const CHARTYPE *str,
|
|
gfc_charlen_type sslen, const CHARTYPE *sstr,
|
|
GFC_LOGICAL_4 back)
|
|
{
|
|
gfc_charlen_type start, last, delta, i;
|
|
|
|
if (sslen == 0)
|
|
return back ? (slen + 1) : 1;
|
|
|
|
if (sslen > slen)
|
|
return 0;
|
|
|
|
if (!back)
|
|
{
|
|
last = slen + 1 - sslen;
|
|
start = 0;
|
|
delta = 1;
|
|
}
|
|
else
|
|
{
|
|
last = -1;
|
|
start = slen - sslen;
|
|
delta = -1;
|
|
}
|
|
|
|
for (; start != last; start+= delta)
|
|
{
|
|
for (i = 0; i < sslen; i++)
|
|
{
|
|
if (str[start + i] != sstr[i])
|
|
break;
|
|
}
|
|
if (i == sslen)
|
|
return (start + 1);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
/* Remove leading blanks from a string, padding at end. The src and dest
|
|
should not overlap. */
|
|
|
|
void
|
|
adjustl (CHARTYPE *dest, gfc_charlen_type len, const CHARTYPE *src)
|
|
{
|
|
gfc_charlen_type i;
|
|
|
|
i = 0;
|
|
while (i < len && src[i] == ' ')
|
|
i++;
|
|
|
|
if (i < len)
|
|
memcpy (dest, &src[i], (len - i) * sizeof (CHARTYPE));
|
|
if (i > 0)
|
|
MEMSET (&dest[len - i], ' ', i);
|
|
}
|
|
|
|
|
|
/* Remove trailing blanks from a string. */
|
|
|
|
void
|
|
adjustr (CHARTYPE *dest, gfc_charlen_type len, const CHARTYPE *src)
|
|
{
|
|
gfc_charlen_type i;
|
|
|
|
i = len;
|
|
while (i > 0 && src[i - 1] == ' ')
|
|
i--;
|
|
|
|
if (i < len)
|
|
MEMSET (dest, ' ', len - i);
|
|
memcpy (&dest[len - i], src, i * sizeof (CHARTYPE));
|
|
}
|
|
|
|
|
|
/* Scan a string for any one of the characters in a set of characters. */
|
|
|
|
gfc_charlen_type
|
|
string_scan (gfc_charlen_type slen, const CHARTYPE *str,
|
|
gfc_charlen_type setlen, const CHARTYPE *set, GFC_LOGICAL_4 back)
|
|
{
|
|
gfc_charlen_type i, j;
|
|
|
|
if (slen == 0 || setlen == 0)
|
|
return 0;
|
|
|
|
if (back)
|
|
{
|
|
for (i = slen - 1; i >= 0; i--)
|
|
{
|
|
for (j = 0; j < setlen; j++)
|
|
{
|
|
if (str[i] == set[j])
|
|
return (i + 1);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (i = 0; i < slen; i++)
|
|
{
|
|
for (j = 0; j < setlen; j++)
|
|
{
|
|
if (str[i] == set[j])
|
|
return (i + 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
/* Verify that a set of characters contains all the characters in a
|
|
string by identifying the position of the first character in a
|
|
characters that does not appear in a given set of characters. */
|
|
|
|
gfc_charlen_type
|
|
string_verify (gfc_charlen_type slen, const CHARTYPE *str,
|
|
gfc_charlen_type setlen, const CHARTYPE *set,
|
|
GFC_LOGICAL_4 back)
|
|
{
|
|
gfc_charlen_type start, last, delta, i;
|
|
|
|
if (slen == 0)
|
|
return 0;
|
|
|
|
if (back)
|
|
{
|
|
last = -1;
|
|
start = slen - 1;
|
|
delta = -1;
|
|
}
|
|
else
|
|
{
|
|
last = slen;
|
|
start = 0;
|
|
delta = 1;
|
|
}
|
|
for (; start != last; start += delta)
|
|
{
|
|
for (i = 0; i < setlen; i++)
|
|
{
|
|
if (str[start] == set[i])
|
|
break;
|
|
}
|
|
if (i == setlen)
|
|
return (start + 1);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
/* MIN and MAX intrinsics for strings. The front-end makes sure that
|
|
nargs is at least 2. */
|
|
|
|
void
|
|
string_minmax (gfc_charlen_type *rlen, CHARTYPE **dest, int op, int nargs, ...)
|
|
{
|
|
va_list ap;
|
|
int i;
|
|
CHARTYPE *next, *res;
|
|
gfc_charlen_type nextlen, reslen;
|
|
|
|
va_start (ap, nargs);
|
|
reslen = va_arg (ap, gfc_charlen_type);
|
|
res = va_arg (ap, CHARTYPE *);
|
|
*rlen = reslen;
|
|
|
|
if (res == NULL)
|
|
runtime_error ("First argument of '%s' intrinsic should be present",
|
|
op > 0 ? "MAX" : "MIN");
|
|
|
|
for (i = 1; i < nargs; i++)
|
|
{
|
|
nextlen = va_arg (ap, gfc_charlen_type);
|
|
next = va_arg (ap, CHARTYPE *);
|
|
|
|
if (next == NULL)
|
|
{
|
|
if (i == 1)
|
|
runtime_error ("Second argument of '%s' intrinsic should be "
|
|
"present", op > 0 ? "MAX" : "MIN");
|
|
else
|
|
continue;
|
|
}
|
|
|
|
if (nextlen > *rlen)
|
|
*rlen = nextlen;
|
|
|
|
if (op * compare_string (reslen, res, nextlen, next) < 0)
|
|
{
|
|
reslen = nextlen;
|
|
res = next;
|
|
}
|
|
}
|
|
va_end (ap);
|
|
|
|
if (*rlen == 0)
|
|
*dest = &zero_length_string;
|
|
else
|
|
{
|
|
CHARTYPE *tmp = xmalloc (*rlen * sizeof (CHARTYPE));
|
|
memcpy (tmp, res, reslen * sizeof (CHARTYPE));
|
|
MEMSET (&tmp[reslen], ' ', *rlen - reslen);
|
|
*dest = tmp;
|
|
}
|
|
}
|