gcc/libgfortran/intrinsics/string_intrinsics.c
Toon Moene 57dea9f62e re PR libfortran/19280 (Inconsistent licensing of libgfortran)
2005-01-12  Toon Moene  <toon@moene.indiv.nluug.nl>

	PR libfortran/19280
	c99_protos.h: License changed to GPL+exception.
	libgfortran.h: Ditto.
	intrinsics/abort.c: Ditto.
	intrinsics/args.c: Ditto.
	intrinsics/associated.c: Ditto.
	intrinsics/bessel.c: Ditto.
	intrinsics/c99_functions.c: Ditto.
	intrinsics/cpu_time.c: Ditto.
	intrinsics/cshift0.c: Ditto.
	intrinsics/date_and_time.c: Ditto.
	intrinsics/env.c: Ditto.
	intrinsics/eoshift0.c: Ditto.
	intrinsics/eoshift2.c: Ditto.
	intrinsics/erf.c: Ditto.
	intrinsics/etime.c: Ditto.
	intrinsics/exit.c: Ditto.
	intrinsics/flush.c: Ditto.
	intrinsics/fnum.c: Ditto.
	intrinsics/getXid.c: Ditto.
	intrinsics/getcwd.c: Ditto.
	intrinsics/ishftc.c: Ditto.
	intrinsics/mvbits.c: Ditto.
	intrinsics/pack_generic.c: Ditto.
	intrinsics/rand.c: Ditto.
	intrinsics/random.c: Ditto.
	intrinsics/reshape_generic.c: Ditto.
	intrinsics/reshape_packed.c: Ditto.
	intrinsics/size.c: Ditto.
	intrinsics/spread_generic.c: Ditto.
	intrinsics/stat.c: Ditto.
	intrinsics/string_intrinsics.c: Ditto.
	intrinsics/system.c: Ditto.
	intrinsics/system_clock.c: Ditto.
	intrinsics/transpose_generic.c: Ditto.
	intrinsics/umask.c: Ditto.
	intrinsics/unlink.c: Ditto.
	intrinsics/unpack_generic.c: Ditto.
	io/backspace.c: Ditto.
	io/close.c: Ditto.
	io/endfile.c: Ditto.
	io/format.c: Ditto.
	io/inquire.c: Ditto.
	io/io.h: Ditto.
	io/list_read.c: Ditto.
	io/lock.c: Ditto.
	io/open.c: Ditto.
	io/read.c: Ditto.
	io/rewind.c: Ditto.
	io/transfer.c: Ditto.
	io/unit.c: Ditto.
	io/unix.c: Ditto.
	io/write.c: Ditto.
	m4/all.m4: Ditto.
	m4/any.m4: Ditto.
	m4/cexp.m4: Ditto.
	m4/chyp.m4: Ditto.
	m4/count.m4: Ditto.
	m4/cshift1.m4: Ditto.
	m4/ctrig.m4: Ditto.
	m4/dotprod.m4: Ditto.
	m4/dotprodc.m4: Ditto.
	m4/dotprodl.m4: Ditto.
	m4/eoshift1.m4: Ditto.
	m4/eoshift3.m4: Ditto.
	m4/exponent.m4: Ditto.
	m4/fraction.m4: Ditto.
	m4/head.m4: Ditto.
	m4/iforeach.m4: Ditto.
	m4/ifunction.m4: Ditto.
	m4/in_pack.m4: Ditto.
	m4/in_unpack.m4: Ditto.
	m4/iparm.m4: Ditto.
	m4/matmul.m4: Ditto.
	m4/matmull.m4: Ditto.
	m4/maxloc0.m4: Ditto.
	m4/maxloc1.m4: Ditto.
	m4/maxval.m4: Ditto.
	m4/minloc0.m4: Ditto.
	m4/minloc1.m4: Ditto.
	m4/minval.m4: Ditto.
	m4/mtype.m4: Ditto.
	m4/nearest.m4: Ditto.
	m4/pow.m4: Ditto.
	m4/product.m4: Ditto.
	m4/reshape.m4: Ditto.
	m4/set_exponent.m4: Ditto.
	m4/shape.m4: Ditto.
	m4/specific.m4: Ditto.
	m4/specific2.m4: Ditto.
	m4/sum.m4: Ditto.
	m4/transpose.m4: Ditto.
	m4/types.m4: Ditto.
	runtime/environ.c: Ditto.
	runtime/error.c: Ditto.
	runtime/in_pack_generic.c: Ditto.
	runtime/in_unpack_generic.c: Ditto.
	runtime/main.c: Ditto.
	runtime/memory.c: Ditto.
	runtime/normalize.c: Ditto.
	runtime/pause.c: Ditto.
	runtime/select.c: Ditto.
	runtime/stop.c: Ditto.
	runtime/string.c: Ditto.
	generated/_abs_c4.f90: Regenerated.
	generated/_abs_c8.f90: Regenerated.
	generated/_abs_i4.f90: Regenerated.
	generated/_abs_i8.f90: Regenerated.
	generated/_abs_r4.f90: Regenerated.
	generated/_abs_r8.f90: Regenerated.
	generated/_acos_r4.f90: Regenerated.
	generated/_acos_r8.f90: Regenerated.
	generated/_aint_r4.f90: Regenerated.
	generated/_aint_r8.f90: Regenerated.
	generated/_anint_r4.f90: Regenerated.
	generated/_anint_r8.f90: Regenerated.
	generated/_asin_r4.f90: Regenerated.
	generated/_asin_r8.f90: Regenerated.
	generated/_atan2_r4.f90: Regenerated.
	generated/_atan2_r8.f90: Regenerated.
	generated/_atan_r4.f90: Regenerated.
	generated/_atan_r8.f90: Regenerated.
	generated/_conjg_c4.f90: Regenerated.
	generated/_conjg_c8.f90: Regenerated.
	generated/_cos_c4.f90: Regenerated.
	generated/_cos_c8.f90: Regenerated.
	generated/_cos_r4.f90: Regenerated.
	generated/_cos_r8.f90: Regenerated.
	generated/_cosh_r4.f90: Regenerated.
	generated/_cosh_r8.f90: Regenerated.
	generated/_dim_i4.f90: Regenerated.
	generated/_dim_i8.f90: Regenerated.
	generated/_dim_r4.f90: Regenerated.
	generated/_dim_r8.f90: Regenerated.
	generated/_exp_c4.f90: Regenerated.
	generated/_exp_c8.f90: Regenerated.
	generated/_exp_r4.f90: Regenerated.
	generated/_exp_r8.f90: Regenerated.
	generated/_log10_r4.f90: Regenerated.
	generated/_log10_r8.f90: Regenerated.
	generated/_log_c4.f90: Regenerated.
	generated/_log_c8.f90: Regenerated.
	generated/_log_r4.f90: Regenerated.
	generated/_log_r8.f90: Regenerated.
	generated/_mod_i4.f90: Regenerated.
	generated/_mod_i8.f90: Regenerated.
	generated/_mod_r4.f90: Regenerated.
	generated/_mod_r8.f90: Regenerated.
	generated/_sign_i4.f90: Regenerated.
	generated/_sign_i8.f90: Regenerated.
	generated/_sign_r4.f90: Regenerated.
	generated/_sign_r8.f90: Regenerated.
	generated/_sin_c4.f90: Regenerated.
	generated/_sin_c8.f90: Regenerated.
	generated/_sin_r4.f90: Regenerated.
	generated/_sin_r8.f90: Regenerated.
	generated/_sinh_r4.f90: Regenerated.
	generated/_sinh_r8.f90: Regenerated.
	generated/_sqrt_c4.f90: Regenerated.
	generated/_sqrt_c8.f90: Regenerated.
	generated/_sqrt_r4.f90: Regenerated.
	generated/_sqrt_r8.f90: Regenerated.
	generated/_tan_r4.f90: Regenerated.
	generated/_tan_r8.f90: Regenerated.
	generated/_tanh_r4.f90: Regenerated.
	generated/_tanh_r8.f90: Regenerated.
	generated/all_l4.c: Regenerated.
	generated/all_l8.c: Regenerated.
	generated/any_l4.c: Regenerated.
	generated/any_l8.c: Regenerated.
	generated/count_4_l4.c: Regenerated.
	generated/count_4_l8.c: Regenerated.
	generated/count_8_l4.c: Regenerated.
	generated/count_8_l8.c: Regenerated.
	generated/cshift1_4.c: Regenerated.
	generated/cshift1_8.c: Regenerated.
	generated/dotprod_c4.c: Regenerated.
	generated/dotprod_c8.c: Regenerated.
	generated/dotprod_i4.c: Regenerated.
	generated/dotprod_i8.c: Regenerated.
	generated/dotprod_l4.c: Regenerated.
	generated/dotprod_l8.c: Regenerated.
	generated/dotprod_r4.c: Regenerated.
	generated/dotprod_r8.c: Regenerated.
	generated/eoshift1_4.c: Regenerated.
	generated/eoshift1_8.c: Regenerated.
	generated/eoshift3_4.c: Regenerated.
	generated/eoshift3_8.c: Regenerated.
	generated/exp_c4.c: Regenerated.
	generated/exp_c8.c: Regenerated.
	generated/exponent_r4.c: Regenerated.
	generated/exponent_r8.c: Regenerated.
	generated/fraction_r4.c: Regenerated.
	generated/fraction_r8.c: Regenerated.
	generated/hyp_c4.c: Regenerated.
	generated/hyp_c8.c: Regenerated.
	generated/in_pack_i4.c: Regenerated.
	generated/in_pack_i8.c: Regenerated.
	generated/in_unpack_i4.c: Regenerated.
	generated/in_unpack_i8.c: Regenerated.
	generated/matmul_c4.c: Regenerated.
	generated/matmul_c8.c: Regenerated.
	generated/matmul_i4.c: Regenerated.
	generated/matmul_i8.c: Regenerated.
	generated/matmul_l4.c: Regenerated.
	generated/matmul_l8.c: Regenerated.
	generated/matmul_r4.c: Regenerated.
	generated/matmul_r8.c: Regenerated.
	generated/maxloc0_4_i4.c: Regenerated.
	generated/maxloc0_4_i8.c: Regenerated.
	generated/maxloc0_4_r4.c: Regenerated.
	generated/maxloc0_4_r8.c: Regenerated.
	generated/maxloc0_8_i4.c: Regenerated.
	generated/maxloc0_8_i8.c: Regenerated.
	generated/maxloc0_8_r4.c: Regenerated.
	generated/maxloc0_8_r8.c: Regenerated.
	generated/maxloc1_4_i4.c: Regenerated.
	generated/maxloc1_4_i8.c: Regenerated.
	generated/maxloc1_4_r4.c: Regenerated.
	generated/maxloc1_4_r8.c: Regenerated.
	generated/maxloc1_8_i4.c: Regenerated.
	generated/maxloc1_8_i8.c: Regenerated.
	generated/maxloc1_8_r4.c: Regenerated.
	generated/maxloc1_8_r8.c: Regenerated.
	generated/maxval_i4.c: Regenerated.
	generated/maxval_i8.c: Regenerated.
	generated/maxval_r4.c: Regenerated.
	generated/maxval_r8.c: Regenerated.
	generated/minloc0_4_i4.c: Regenerated.
	generated/minloc0_4_i8.c: Regenerated.
	generated/minloc0_4_r4.c: Regenerated.
	generated/minloc0_4_r8.c: Regenerated.
	generated/minloc0_8_i4.c: Regenerated.
	generated/minloc0_8_i8.c: Regenerated.
	generated/minloc0_8_r4.c: Regenerated.
	generated/minloc0_8_r8.c: Regenerated.
	generated/minloc1_4_i4.c: Regenerated.
	generated/minloc1_4_i8.c: Regenerated.
	generated/minloc1_4_r4.c: Regenerated.
	generated/minloc1_4_r8.c: Regenerated.
	generated/minloc1_8_i4.c: Regenerated.
	generated/minloc1_8_i8.c: Regenerated.
	generated/minloc1_8_r4.c: Regenerated.
	generated/minloc1_8_r8.c: Regenerated.
	generated/minval_i4.c: Regenerated.
	generated/minval_i8.c: Regenerated.
	generated/minval_r4.c: Regenerated.
	generated/minval_r8.c: Regenerated.
	generated/nearest_r4.c: Regenerated.
	generated/nearest_r8.c: Regenerated.
	generated/pow_c4_i4.c: Regenerated.
	generated/pow_c4_i8.c: Regenerated.
	generated/pow_c8_i4.c: Regenerated.
	generated/pow_c8_i8.c: Regenerated.
	generated/pow_i4_i4.c: Regenerated.
	generated/pow_i4_i8.c: Regenerated.
	generated/pow_i8_i4.c: Regenerated.
	generated/pow_i8_i8.c: Regenerated.
	generated/pow_r4_i4.c: Regenerated.
	generated/pow_r4_i8.c: Regenerated.
	generated/pow_r8_i4.c: Regenerated.
	generated/pow_r8_i8.c: Regenerated.
	generated/product_c4.c: Regenerated.
	generated/product_c8.c: Regenerated.
	generated/product_i4.c: Regenerated.
	generated/product_i8.c: Regenerated.
	generated/product_r4.c: Regenerated.
	generated/product_r8.c: Regenerated.
	generated/reshape_i4.c: Regenerated.
	generated/reshape_i8.c: Regenerated.
	generated/set_exponent_r4.c: Regenerated.
	generated/set_exponent_r8.c: Regenerated.
	generated/shape_i4.c: Regenerated.
	generated/shape_i8.c: Regenerated.
	generated/sum_c4.c: Regenerated.
	generated/sum_c8.c: Regenerated.
	generated/sum_i4.c: Regenerated.
	generated/sum_i8.c: Regenerated.
	generated/sum_r4.c: Regenerated.
	generated/sum_r8.c: Regenerated.
	generated/transpose_i4.c: Regenerated.
	generated/transpose_i8.c: Regenerated.
	generated/trig_c4.c: Regenerated.
	generated/trig_c8.c: Regenerated.

From-SVN: r93235
2005-01-12 21:27:33 +00:00

406 lines
8.5 KiB
C

/* String intrinsics helper functions.
Copyright 2002 Free Software Foundation, Inc.
Contributed by Paul Brook <paul@nowt.org>
This file is part of the GNU Fortran 95 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 2 of the License, or (at your option) any later version.
In addition to the permissions in the GNU General Public License, the
Free Software Foundation gives you unlimited permission to link the
compiled version of this file into combinations with other programs,
and to distribute those combinations without any restriction coming
from the use of this file. (The General Public License restrictions
do apply in other respects; for example, they cover modification of
the file, and distribution when not linked into a combine
executable.)
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.
You should have received a copy of the GNU General Public
License along with libgfortran; see the file COPYING. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
/* Unlike what the name of this file suggests, we don't actually
implement the Fortran intrinsics here. At least, not with the
names they have in the standard. The functions here provide all
the support we need for the standard string intrinsics, and the
compiler translates the actual intrinsics calls to calls to
functions in this file. */
#include <stdlib.h>
#include <string.h>
#include "libgfortran.h"
/* String functions. */
extern void copy_string (GFC_INTEGER_4, char *, GFC_INTEGER_4, const char *);
export_proto(copy_string);
extern void concat_string (GFC_INTEGER_4, char *,
GFC_INTEGER_4, const char *,
GFC_INTEGER_4, const char *);
export_proto(concat_string);
extern GFC_INTEGER_4 string_len_trim (GFC_INTEGER_4, const char *);
export_proto(string_len_trim);
extern void adjustl (char *, GFC_INTEGER_4, const char *);
export_proto(adjustl);
extern void adjustr (char *, GFC_INTEGER_4, const char *);
export_proto(adjustr);
extern GFC_INTEGER_4 string_index (GFC_INTEGER_4, const char *, GFC_INTEGER_4,
const char *, GFC_LOGICAL_4);
export_proto(string_index);
extern GFC_INTEGER_4 string_scan (GFC_INTEGER_4, const char *, GFC_INTEGER_4,
const char *, GFC_LOGICAL_4);
export_proto(string_scan);
extern GFC_INTEGER_4 string_verify (GFC_INTEGER_4, const char *, GFC_INTEGER_4,
const char *, GFC_LOGICAL_4);
export_proto(string_verify);
extern void string_trim (GFC_INTEGER_4 *, void **, GFC_INTEGER_4, const char *);
export_proto(string_trim);
extern void string_repeat (char *, GFC_INTEGER_4, const char *, GFC_INTEGER_4);
export_proto(string_repeat);
/* The two areas may overlap so we use memmove. */
void
copy_string (GFC_INTEGER_4 destlen, char * dest,
GFC_INTEGER_4 srclen, const char * src)
{
if (srclen >= destlen)
{
/* This will truncate if too long. */
memmove (dest, src, destlen);
/*memcpy (dest, src, destlen);*/
}
else
{
memmove (dest, src, srclen);
/*memcpy (dest, src, srclen);*/
/* Pad with spaces. */
memset (&dest[srclen], ' ', destlen - srclen);
}
}
/* Strings of unequal length are extended with pad characters. */
GFC_INTEGER_4
compare_string (GFC_INTEGER_4 len1, const char * s1,
GFC_INTEGER_4 len2, const char * s2)
{
int res;
const char *s;
int len;
res = strncmp (s1, s2, (len1 < len2) ? len1 : len2);
if (res != 0)
return res;
if (len1 == len2)
return 0;
if (len1 < len2)
{
len = len2 - len1;
s = &s2[len1];
res = -1;
}
else
{
len = len1 - len2;
s = &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_INTEGER_4 destlen, char * dest,
GFC_INTEGER_4 len1, const char * s1,
GFC_INTEGER_4 len2, const char * s2)
{
if (len1 >= destlen)
{
memcpy (dest, s1, destlen);
return;
}
memcpy (dest, s1, len1);
dest += len1;
destlen -= len1;
if (len2 >= destlen)
{
memcpy (dest, s2, destlen);
return;
}
memcpy (dest, s2, len2);
memset (&dest[len2], ' ', destlen - len2);
}
/* Return string with all trailing blanks removed. */
void
string_trim (GFC_INTEGER_4 * len, void ** dest, GFC_INTEGER_4 slen,
const char * src)
{
int i;
/* Determine length of result string. */
for (i = slen - 1; i >= 0; i--)
{
if (src[i] != ' ')
break;
}
*len = i + 1;
if (*len > 0)
{
/* Allocate space for result string. */
*dest = internal_malloc_size (*len);
/* copy string if necessary. */
memmove (*dest, src, *len);
}
}
/* The length of a string not including trailing blanks. */
GFC_INTEGER_4
string_len_trim (GFC_INTEGER_4 len, const char * s)
{
int i;
for (i = len - 1; i >= 0; i--)
{
if (s[i] != ' ')
break;
}
return i + 1;
}
/* Find a substring within a string. */
GFC_INTEGER_4
string_index (GFC_INTEGER_4 slen, const char * str, GFC_INTEGER_4 sslen,
const char * sstr, GFC_LOGICAL_4 back)
{
int start;
int last;
int i;
int delta;
if (sslen == 0)
return 1;
if (sslen > slen)
return 0;
if (!back)
{
last = slen + 1 - sslen;
start = 0;
delta = 1;
}
else
{
last = -1;
start = slen - sslen;
delta = -1;
}
i = 0;
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 (char *dest, GFC_INTEGER_4 len, const char *src)
{
int i;
i = 0;
while (i<len && src[i] == ' ')
i++;
if (i < len)
memcpy (dest, &src[i], len - i);
if (i > 0)
memset (&dest[len - i], ' ', i);
}
/* Remove trailing blanks from a string. */
void
adjustr (char *dest, GFC_INTEGER_4 len, const char *src)
{
int i;
i = len;
while (i > 0 && src[i - 1] == ' ')
i--;
if (i < len)
memset (dest, ' ', len - i);
memcpy (dest + (len - i), src, i );
}
/* Scan a string for any one of the characters in a set of characters. */
GFC_INTEGER_4
string_scan (GFC_INTEGER_4 slen, const char * str, GFC_INTEGER_4 setlen,
const char * set, GFC_LOGICAL_4 back)
{
int start;
int last;
int i;
int delta;
if (slen == 0 || setlen == 0)
return 0;
if (back)
{
last = 0;
start = slen - 1;
delta = -1;
}
else
{
last = slen - 1;
start = 0;
delta = 1;
}
i = 0;
for (; start != last; start += delta)
{
for (i = 0; i < setlen; i++)
{
if (str[start] == set[i])
return (start + 1);
}
}
return 0;
}
/* Verify that a set of characters contains all the characters in a
string by indentifying the position of the first character in a
characters that dose not appear in a given set of characters. */
GFC_INTEGER_4
string_verify (GFC_INTEGER_4 slen, const char * str, GFC_INTEGER_4 setlen,
const char * set, GFC_LOGICAL_4 back)
{
int start;
int last;
int i;
int delta;
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;
}
/* Concatenate several copies of a string. */
void
string_repeat (char * dest, GFC_INTEGER_4 slen,
const char * src, GFC_INTEGER_4 ncopies)
{
int i;
/* See if ncopies is valid. */
if (ncopies < 0)
{
/* The error is already reported. */
runtime_error ("Augument NCOPIES is negative.");
}
/* Copy characters. */
for (i = 0; i < ncopies; i++)
{
memmove (dest + (i * slen), src, slen);
}
}