xref: /netbsd-src/external/gpl3/gcc/dist/libgfortran/runtime/string.c (revision b1e838363e3c6fc78a55519254d99869742dd33c)
1*b1e83836Smrg /* Copyright (C) 2002-2022 Free Software Foundation, Inc.
2181254a7Smrg    Contributed by Paul Brook
3181254a7Smrg 
4181254a7Smrg This file is part of the GNU Fortran runtime library (libgfortran).
5181254a7Smrg 
6181254a7Smrg Libgfortran is free software; you can redistribute it and/or modify
7181254a7Smrg it under the terms of the GNU General Public License as published by
8181254a7Smrg the Free Software Foundation; either version 3, or (at your option)
9181254a7Smrg any later version.
10181254a7Smrg 
11181254a7Smrg Libgfortran is distributed in the hope that it will be useful,
12181254a7Smrg but WITHOUT ANY WARRANTY; without even the implied warranty of
13181254a7Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14181254a7Smrg GNU General Public License for more details.
15181254a7Smrg 
16181254a7Smrg Under Section 7 of GPL version 3, you are granted additional
17181254a7Smrg permissions described in the GCC Runtime Library Exception, version
18181254a7Smrg 3.1, as published by the Free Software Foundation.
19181254a7Smrg 
20181254a7Smrg You should have received a copy of the GNU General Public License and
21181254a7Smrg a copy of the GCC Runtime Library Exception along with this program;
22181254a7Smrg see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23181254a7Smrg <http://www.gnu.org/licenses/>.  */
24181254a7Smrg 
25181254a7Smrg #include "libgfortran.h"
26*b1e83836Smrg #include <assert.h>
27181254a7Smrg #include <string.h>
28181254a7Smrg #include <strings.h>
29181254a7Smrg 
30181254a7Smrg 
31181254a7Smrg /* Given a fortran string, return its length exclusive of the trailing
32181254a7Smrg    spaces.  */
33181254a7Smrg 
34181254a7Smrg gfc_charlen_type
fstrlen(const char * string,gfc_charlen_type len)35181254a7Smrg fstrlen (const char *string, gfc_charlen_type len)
36181254a7Smrg {
37181254a7Smrg   for (; len > 0; len--)
38181254a7Smrg     if (string[len-1] != ' ')
39181254a7Smrg       break;
40181254a7Smrg 
41181254a7Smrg   return len;
42181254a7Smrg }
43181254a7Smrg 
44181254a7Smrg 
45181254a7Smrg /* Copy a Fortran string (not null-terminated, hence length arguments
46181254a7Smrg    for both source and destination strings. Returns the non-padded
47181254a7Smrg    length of the destination.  */
48181254a7Smrg 
49181254a7Smrg gfc_charlen_type
fstrcpy(char * dest,gfc_charlen_type destlen,const char * src,gfc_charlen_type srclen)50181254a7Smrg fstrcpy (char *dest, gfc_charlen_type destlen,
51181254a7Smrg 	 const char *src, gfc_charlen_type srclen)
52181254a7Smrg {
53181254a7Smrg   if (srclen >= destlen)
54181254a7Smrg     {
55181254a7Smrg       /* This will truncate if too long.  */
56181254a7Smrg       memcpy (dest, src, destlen);
57181254a7Smrg       return destlen;
58181254a7Smrg     }
59181254a7Smrg   else
60181254a7Smrg     {
61181254a7Smrg       memcpy (dest, src, srclen);
62181254a7Smrg       /* Pad with spaces.  */
63181254a7Smrg       memset (&dest[srclen], ' ', destlen - srclen);
64181254a7Smrg       return srclen;
65181254a7Smrg     }
66181254a7Smrg }
67181254a7Smrg 
68181254a7Smrg 
69181254a7Smrg /* Copy a null-terminated C string to a non-null-terminated Fortran
70181254a7Smrg    string. Returns the non-padded length of the destination string.  */
71181254a7Smrg 
72181254a7Smrg gfc_charlen_type
cf_strcpy(char * dest,gfc_charlen_type dest_len,const char * src)73181254a7Smrg cf_strcpy (char *dest, gfc_charlen_type dest_len, const char *src)
74181254a7Smrg {
75181254a7Smrg   size_t src_len;
76181254a7Smrg 
77181254a7Smrg   src_len = strlen (src);
78181254a7Smrg 
79181254a7Smrg   if (src_len >= (size_t) dest_len)
80181254a7Smrg     {
81181254a7Smrg       /* This will truncate if too long.  */
82181254a7Smrg       memcpy (dest, src, dest_len);
83181254a7Smrg       return dest_len;
84181254a7Smrg     }
85181254a7Smrg   else
86181254a7Smrg     {
87181254a7Smrg       memcpy (dest, src, src_len);
88181254a7Smrg       /* Pad with spaces.  */
89181254a7Smrg       memset (&dest[src_len], ' ', dest_len - src_len);
90181254a7Smrg       return src_len;
91181254a7Smrg     }
92181254a7Smrg }
93181254a7Smrg 
94181254a7Smrg 
95181254a7Smrg #ifndef HAVE_STRNLEN
96181254a7Smrg static size_t
strnlen(const char * s,size_t maxlen)97181254a7Smrg strnlen (const char *s, size_t maxlen)
98181254a7Smrg {
99181254a7Smrg   for (size_t ii = 0; ii < maxlen; ii++)
100181254a7Smrg     {
101181254a7Smrg       if (s[ii] == '\0')
102181254a7Smrg 	return ii;
103181254a7Smrg     }
104181254a7Smrg   return maxlen;
105181254a7Smrg }
106181254a7Smrg #endif
107181254a7Smrg 
108181254a7Smrg 
109181254a7Smrg #ifndef HAVE_STRNDUP
110181254a7Smrg static char *
strndup(const char * s,size_t n)111181254a7Smrg strndup (const char *s, size_t n)
112181254a7Smrg {
113181254a7Smrg   size_t len = strnlen (s, n);
114181254a7Smrg   char *p = malloc (len + 1);
115181254a7Smrg   if (!p)
116181254a7Smrg     return NULL;
117181254a7Smrg   memcpy (p, s, len);
118181254a7Smrg   p[len] = '\0';
119181254a7Smrg   return p;
120181254a7Smrg }
121181254a7Smrg #endif
122181254a7Smrg 
123181254a7Smrg 
124181254a7Smrg /* Duplicate a non-null-terminated Fortran string to a malloced
125181254a7Smrg    null-terminated C string.  */
126181254a7Smrg 
127181254a7Smrg char *
fc_strdup(const char * src,gfc_charlen_type src_len)128181254a7Smrg fc_strdup (const char *src, gfc_charlen_type src_len)
129181254a7Smrg {
130181254a7Smrg   gfc_charlen_type n = fstrlen (src, src_len);
131181254a7Smrg   char *p = strndup (src, n);
132181254a7Smrg   if (!p)
133181254a7Smrg     os_error ("Memory allocation failed in fc_strdup");
134181254a7Smrg   return p;
135181254a7Smrg }
136181254a7Smrg 
137181254a7Smrg 
138181254a7Smrg /* Duplicate a non-null-terminated Fortran string to a malloced
139181254a7Smrg    null-terminated C string, without getting rid of trailing
140181254a7Smrg    blanks.  */
141181254a7Smrg 
142181254a7Smrg char *
fc_strdup_notrim(const char * src,gfc_charlen_type src_len)143181254a7Smrg fc_strdup_notrim (const char *src, gfc_charlen_type src_len)
144181254a7Smrg {
145181254a7Smrg   char *p = strndup (src, src_len);
146181254a7Smrg   if (!p)
147181254a7Smrg     os_error ("Memory allocation failed in fc_strdup");
148181254a7Smrg   return p;
149181254a7Smrg }
150181254a7Smrg 
151181254a7Smrg 
152181254a7Smrg /* Given a fortran string and an array of st_option structures, search through
153181254a7Smrg    the array to find a match.  If the option is not found, we generate an error
154181254a7Smrg    if no default is provided.  */
155181254a7Smrg 
156181254a7Smrg int
find_option(st_parameter_common * cmp,const char * s1,gfc_charlen_type s1_len,const st_option * opts,const char * error_message)157181254a7Smrg find_option (st_parameter_common *cmp, const char *s1, gfc_charlen_type s1_len,
158181254a7Smrg 	     const st_option * opts, const char *error_message)
159181254a7Smrg {
160181254a7Smrg   /* Strip trailing blanks from the Fortran string.  */
161181254a7Smrg   size_t len = (size_t) fstrlen (s1, s1_len);
162181254a7Smrg 
163181254a7Smrg   for (; opts->name; opts++)
164181254a7Smrg     if (len == strlen(opts->name) && strncasecmp (s1, opts->name, len) == 0)
165181254a7Smrg       return opts->value;
166181254a7Smrg 
167181254a7Smrg   generate_error (cmp, LIBERROR_BAD_OPTION, error_message);
168181254a7Smrg 
169181254a7Smrg   return -1;
170181254a7Smrg }
171181254a7Smrg 
172181254a7Smrg 
173*b1e83836Smrg /* Fast helper function for a positive value that fits in uint64_t.  */
174*b1e83836Smrg 
175*b1e83836Smrg static inline char *
itoa64(uint64_t n,char * p)176*b1e83836Smrg itoa64 (uint64_t n, char *p)
177*b1e83836Smrg {
178*b1e83836Smrg   while (n != 0)
179*b1e83836Smrg     {
180*b1e83836Smrg       *--p = '0' + (n % 10);
181*b1e83836Smrg       n /= 10;
182*b1e83836Smrg     }
183*b1e83836Smrg   return p;
184*b1e83836Smrg }
185*b1e83836Smrg 
186*b1e83836Smrg 
187*b1e83836Smrg #if defined(HAVE_GFC_INTEGER_16)
188*b1e83836Smrg # define TEN19 ((GFC_UINTEGER_LARGEST) 1000000 * (GFC_UINTEGER_LARGEST) 1000000 * (GFC_UINTEGER_LARGEST) 10000000)
189*b1e83836Smrg 
190*b1e83836Smrg /* Same as itoa64(), with zero padding of 19 digits.  */
191*b1e83836Smrg 
192*b1e83836Smrg static inline char *
itoa64_pad19(uint64_t n,char * p)193*b1e83836Smrg itoa64_pad19 (uint64_t n, char *p)
194*b1e83836Smrg {
195*b1e83836Smrg   for (int k = 0; k < 19; k++)
196*b1e83836Smrg     {
197*b1e83836Smrg       *--p = '0' + (n % 10);
198*b1e83836Smrg       n /= 10;
199*b1e83836Smrg     }
200*b1e83836Smrg   return p;
201*b1e83836Smrg }
202*b1e83836Smrg #endif
203*b1e83836Smrg 
204*b1e83836Smrg 
205*b1e83836Smrg /* Integer to decimal conversion.
206*b1e83836Smrg 
207*b1e83836Smrg    This function is much more restricted than the widespread (but
208*b1e83836Smrg    non-standard) itoa() function.  This version has the following
209*b1e83836Smrg    characteristics:
210*b1e83836Smrg 
211*b1e83836Smrg      - it takes only non-negative arguments
212*b1e83836Smrg      - it is async-signal-safe (we use it runtime/backtrace.c)
213*b1e83836Smrg      - it works in base 10 (see xtoa, otoa, btoa functions
214*b1e83836Smrg        in io/write.c for other radices)
215*b1e83836Smrg  */
216181254a7Smrg 
217181254a7Smrg const char *
gfc_itoa(GFC_UINTEGER_LARGEST n,char * buffer,size_t len)218*b1e83836Smrg gfc_itoa (GFC_UINTEGER_LARGEST n, char *buffer, size_t len)
219181254a7Smrg {
220181254a7Smrg   char *p;
221181254a7Smrg 
222181254a7Smrg   if (len < GFC_ITOA_BUF_SIZE)
223181254a7Smrg     sys_abort ();
224181254a7Smrg 
225181254a7Smrg   if (n == 0)
226181254a7Smrg     return "0";
227181254a7Smrg 
228181254a7Smrg   p = buffer + GFC_ITOA_BUF_SIZE - 1;
229181254a7Smrg   *p = '\0';
230181254a7Smrg 
231*b1e83836Smrg #if defined(HAVE_GFC_INTEGER_16)
232*b1e83836Smrg   /* On targets that have a 128-bit integer type, division in that type
233*b1e83836Smrg      is slow, because it occurs through a function call. We avoid that.  */
234181254a7Smrg 
235*b1e83836Smrg   if (n <= UINT64_MAX)
236*b1e83836Smrg     /* If the value fits in uint64_t, use the fast function. */
237*b1e83836Smrg     return itoa64 (n, p);
238*b1e83836Smrg   else
239*b1e83836Smrg     {
240*b1e83836Smrg       /* Otherwise, break down into smaller bits by division. Two calls to
241*b1e83836Smrg 	 the uint64_t function are not sufficient for all 128-bit unsigned
242*b1e83836Smrg 	 integers (we would need three calls), but they do suffice for all
243*b1e83836Smrg 	 values up to 2^127, which is the largest that Fortran can produce
244*b1e83836Smrg 	 (-HUGE(0_16)-1) with its signed integer types.  */
245*b1e83836Smrg       _Static_assert (sizeof(GFC_UINTEGER_LARGEST) <= 2 * sizeof(uint64_t),
246*b1e83836Smrg 		      "integer too large");
247*b1e83836Smrg 
248*b1e83836Smrg       GFC_UINTEGER_LARGEST r;
249*b1e83836Smrg       r = n % TEN19;
250*b1e83836Smrg       n = n / TEN19;
251*b1e83836Smrg       assert (r <= UINT64_MAX);
252*b1e83836Smrg       p = itoa64_pad19 (r, p);
253*b1e83836Smrg 
254*b1e83836Smrg       assert(n <= UINT64_MAX);
255*b1e83836Smrg       return itoa64 (n, p);
256*b1e83836Smrg     }
257*b1e83836Smrg #else
258*b1e83836Smrg   /* On targets where the largest integer is 64-bit, just use that.  */
259*b1e83836Smrg   return itoa64 (n, p);
260*b1e83836Smrg #endif
261181254a7Smrg }
262