1 /* String intrinsics helper functions.
2 Copyright 2002, 2005, 2007 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING. If not,
28 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
32 /* Unlike what the name of this file suggests, we don't actually
33 implement the Fortran intrinsics here. At least, not with the
34 names they have in the standard. The functions here provide all
35 the support we need for the standard string intrinsics, and the
36 compiler translates the actual intrinsics calls to calls to
37 functions in this file. */
43 #include "libgfortran.h"
46 /* String functions. */
48 extern void concat_string (GFC_INTEGER_4, char *,
49 GFC_INTEGER_4, const char *,
50 GFC_INTEGER_4, const char *);
51 export_proto(concat_string);
53 extern GFC_INTEGER_4 string_len_trim (GFC_INTEGER_4, const char *);
54 export_proto(string_len_trim);
56 extern void adjustl (char *, GFC_INTEGER_4, const char *);
57 export_proto(adjustl);
59 extern void adjustr (char *, GFC_INTEGER_4, const char *);
60 export_proto(adjustr);
62 extern GFC_INTEGER_4 string_index (GFC_INTEGER_4, const char *, GFC_INTEGER_4,
63 const char *, GFC_LOGICAL_4);
64 export_proto(string_index);
66 extern GFC_INTEGER_4 string_scan (GFC_INTEGER_4, const char *, GFC_INTEGER_4,
67 const char *, GFC_LOGICAL_4);
68 export_proto(string_scan);
70 extern GFC_INTEGER_4 string_verify (GFC_INTEGER_4, const char *, GFC_INTEGER_4,
71 const char *, GFC_LOGICAL_4);
72 export_proto(string_verify);
74 extern void string_trim (GFC_INTEGER_4 *, void **, GFC_INTEGER_4, const char *);
75 export_proto(string_trim);
77 extern void string_minmax (GFC_INTEGER_4 *, void **, int, int, ...);
78 export_proto(string_minmax);
81 /* Use for functions which can return a zero-length string. */
82 static char zero_length_string = '\0';
85 /* Strings of unequal length are extended with pad characters. */
88 compare_string (GFC_INTEGER_4 len1, const char * s1,
89 GFC_INTEGER_4 len2, const char * s2)
92 const unsigned char *s;
95 res = memcmp (s1, s2, (len1 < len2) ? len1 : len2);
105 s = (unsigned char *) &s2[len1];
111 s = (unsigned char *) &s1[len2];
129 iexport(compare_string);
132 /* The destination and source should not overlap. */
135 concat_string (GFC_INTEGER_4 destlen, char * dest,
136 GFC_INTEGER_4 len1, const char * s1,
137 GFC_INTEGER_4 len2, const char * s2)
141 memcpy (dest, s1, destlen);
144 memcpy (dest, s1, len1);
150 memcpy (dest, s2, destlen);
154 memcpy (dest, s2, len2);
155 memset (&dest[len2], ' ', destlen - len2);
159 /* Return string with all trailing blanks removed. */
162 string_trim (GFC_INTEGER_4 * len, void ** dest, GFC_INTEGER_4 slen,
167 /* Determine length of result string. */
168 for (i = slen - 1; i >= 0; i--)
176 *dest = &zero_length_string;
179 /* Allocate space for result string. */
180 *dest = internal_malloc_size (*len);
182 /* Copy string if necessary. */
183 memmove (*dest, src, *len);
188 /* The length of a string not including trailing blanks. */
191 string_len_trim (GFC_INTEGER_4 len, const char * s)
195 for (i = len - 1; i >= 0; i--)
204 /* Find a substring within a string. */
207 string_index (GFC_INTEGER_4 slen, const char * str, GFC_INTEGER_4 sslen,
208 const char * sstr, GFC_LOGICAL_4 back)
223 last = slen + 1 - sslen;
230 start = slen - sslen;
234 for (; start != last; start+= delta)
236 for (i = 0; i < sslen; i++)
238 if (str[start + i] != sstr[i])
248 /* Remove leading blanks from a string, padding at end. The src and dest
249 should not overlap. */
252 adjustl (char *dest, GFC_INTEGER_4 len, const char *src)
257 while (i<len && src[i] == ' ')
261 memcpy (dest, &src[i], len - i);
263 memset (&dest[len - i], ' ', i);
267 /* Remove trailing blanks from a string. */
270 adjustr (char *dest, GFC_INTEGER_4 len, const char *src)
275 while (i > 0 && src[i - 1] == ' ')
279 memset (dest, ' ', len - i);
280 memcpy (dest + (len - i), src, i );
284 /* Scan a string for any one of the characters in a set of characters. */
287 string_scan (GFC_INTEGER_4 slen, const char * str, GFC_INTEGER_4 setlen,
288 const char * set, GFC_LOGICAL_4 back)
292 if (slen == 0 || setlen == 0)
297 for (i = slen - 1; i >= 0; i--)
299 for (j = 0; j < setlen; j++)
301 if (str[i] == set[j])
308 for (i = 0; i < slen; i++)
310 for (j = 0; j < setlen; j++)
312 if (str[i] == set[j])
322 /* Verify that a set of characters contains all the characters in a
323 string by identifying the position of the first character in a
324 characters that does not appear in a given set of characters. */
327 string_verify (GFC_INTEGER_4 slen, const char * str, GFC_INTEGER_4 setlen,
328 const char * set, GFC_LOGICAL_4 back)
350 for (; start != last; start += delta)
352 for (i = 0; i < setlen; i++)
354 if (str[start] == set[i])
365 /* MIN and MAX intrinsics for strings. The front-end makes sure that
366 nargs is at least 2. */
369 string_minmax (GFC_INTEGER_4 *rlen, void **dest, int op, int nargs, ...)
374 GFC_INTEGER_4 nextlen, reslen;
376 va_start (ap, nargs);
377 reslen = va_arg (ap, GFC_INTEGER_4);
378 res = va_arg (ap, char *);
382 runtime_error ("First argument of '%s' intrinsic should be present",
383 op > 0 ? "MAX" : "MIN");
385 for (i = 1; i < nargs; i++)
387 nextlen = va_arg (ap, GFC_INTEGER_4);
388 next = va_arg (ap, char *);
394 runtime_error ("Second argument of '%s' intrinsic should be "
395 "present", op > 0 ? "MAX" : "MIN");
403 if (op * compare_string (reslen, res, nextlen, next) < 0)
412 *dest = &zero_length_string;
415 char * tmp = internal_malloc_size (*rlen);
416 memcpy (tmp, res, reslen);
417 memset (&tmp[reslen], ' ', *rlen - reslen);